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 | }, |
| 461 | ['P' - 'A'] = { |
| 462 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 463 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 464 | }, |
| 465 | ['T' - 'A'] = { |
| 466 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 467 | }, |
| 468 | /* rest is empty like this : |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 469 | * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 470 | */ |
| 471 | }; |
| 472 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 473 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 474 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 475 | [HTTP_METH_GET] = { "GET", 3 }, |
| 476 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 477 | [HTTP_METH_POST] = { "POST", 4 }, |
| 478 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 479 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 480 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 481 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 482 | }; |
| 483 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 484 | /* 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] | 485 | * 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] | 486 | * RFC2616 for those chars. |
| 487 | */ |
| 488 | |
| 489 | const char http_is_spht[256] = { |
| 490 | [' '] = 1, ['\t'] = 1, |
| 491 | }; |
| 492 | |
| 493 | const char http_is_crlf[256] = { |
| 494 | ['\r'] = 1, ['\n'] = 1, |
| 495 | }; |
| 496 | |
| 497 | const char http_is_lws[256] = { |
| 498 | [' '] = 1, ['\t'] = 1, |
| 499 | ['\r'] = 1, ['\n'] = 1, |
| 500 | }; |
| 501 | |
| 502 | const char http_is_sep[256] = { |
| 503 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 504 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 505 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 506 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 507 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 508 | }; |
| 509 | |
| 510 | const char http_is_ctl[256] = { |
| 511 | [0 ... 31] = 1, |
| 512 | [127] = 1, |
| 513 | }; |
| 514 | |
| 515 | /* |
| 516 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 517 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 518 | * them correctly. Instead, define every non-CTL char's status. |
| 519 | */ |
| 520 | const char http_is_token[256] = { |
| 521 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 522 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 523 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 524 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 525 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 526 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 527 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 528 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 529 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 530 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 531 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 532 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 533 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 534 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 535 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 536 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 537 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 538 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 539 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 540 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 541 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 542 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 543 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 544 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 545 | }; |
| 546 | |
| 547 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 548 | /* |
| 549 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 550 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 551 | */ |
| 552 | const char http_is_ver_token[256] = { |
| 553 | ['.'] = 1, ['/'] = 1, |
| 554 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 555 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
Thierry FOURNIER | 63d692c | 2015-02-28 19:03:56 +0100 | [diff] [blame] | 556 | ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1, |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 557 | }; |
| 558 | |
| 559 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 560 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 561 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 562 | * 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] | 563 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 564 | * of headers is automatically adjusted. The number of bytes added is returned |
| 565 | * on success, otherwise <0 is returned indicating an error. |
| 566 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 567 | 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] | 568 | { |
| 569 | int bytes, len; |
| 570 | |
| 571 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 572 | 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] | 573 | if (!bytes) |
| 574 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 575 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 576 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 577 | } |
| 578 | |
| 579 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 580 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 581 | * 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] | 582 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 583 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 584 | * of headers is automatically adjusted. The number of bytes added is returned |
| 585 | * on success, otherwise <0 is returned indicating an error. |
| 586 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 587 | int http_header_add_tail2(struct http_msg *msg, |
| 588 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 589 | { |
| 590 | int bytes; |
| 591 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 592 | 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] | 593 | if (!bytes) |
| 594 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 595 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 596 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 597 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 598 | |
| 599 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 600 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 601 | * If so, returns the position of the first non-space character relative to |
| 602 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 603 | * to return a pointer to the place after the first space. Returns 0 if the |
| 604 | * header name does not match. Checks are case-insensitive. |
| 605 | */ |
| 606 | int http_header_match2(const char *hdr, const char *end, |
| 607 | const char *name, int len) |
| 608 | { |
| 609 | const char *val; |
| 610 | |
| 611 | if (hdr + len >= end) |
| 612 | return 0; |
| 613 | if (hdr[len] != ':') |
| 614 | return 0; |
| 615 | if (strncasecmp(hdr, name, len) != 0) |
| 616 | return 0; |
| 617 | val = hdr + len + 1; |
| 618 | while (val < end && HTTP_IS_SPHT(*val)) |
| 619 | val++; |
| 620 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 621 | return len + 2; /* we may replace starting from second space */ |
| 622 | return val - hdr; |
| 623 | } |
| 624 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 625 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 626 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 627 | * structure holds everything necessary to use the header and find next |
| 628 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 629 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 630 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 631 | * http_find_header2() except that it is designed to work with full-line headers |
| 632 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 633 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 634 | * header is rescanned. This allows rescanning after a header deletion. |
| 635 | */ |
| 636 | int http_find_full_header2(const char *name, int len, |
| 637 | char *sol, struct hdr_idx *idx, |
| 638 | struct hdr_ctx *ctx) |
| 639 | { |
| 640 | char *eol, *sov; |
| 641 | int cur_idx, old_idx; |
| 642 | |
| 643 | cur_idx = ctx->idx; |
| 644 | if (cur_idx) { |
| 645 | /* We have previously returned a header, let's search another one */ |
| 646 | sol = ctx->line; |
| 647 | eol = sol + idx->v[cur_idx].len; |
| 648 | goto next_hdr; |
| 649 | } |
| 650 | |
| 651 | /* first request for this header */ |
| 652 | sol += hdr_idx_first_pos(idx); |
| 653 | old_idx = 0; |
| 654 | cur_idx = hdr_idx_first_idx(idx); |
| 655 | while (cur_idx) { |
| 656 | eol = sol + idx->v[cur_idx].len; |
| 657 | |
| 658 | if (len == 0) { |
| 659 | /* No argument was passed, we want any header. |
| 660 | * To achieve this, we simply build a fake request. */ |
| 661 | while (sol + len < eol && sol[len] != ':') |
| 662 | len++; |
| 663 | name = sol; |
| 664 | } |
| 665 | |
| 666 | if ((len < eol - sol) && |
| 667 | (sol[len] == ':') && |
| 668 | (strncasecmp(sol, name, len) == 0)) { |
| 669 | ctx->del = len; |
| 670 | sov = sol + len + 1; |
| 671 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 672 | sov++; |
| 673 | |
| 674 | ctx->line = sol; |
| 675 | ctx->prev = old_idx; |
| 676 | ctx->idx = cur_idx; |
| 677 | ctx->val = sov - sol; |
| 678 | ctx->tws = 0; |
| 679 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 680 | eol--; |
| 681 | ctx->tws++; |
| 682 | } |
| 683 | ctx->vlen = eol - sov; |
| 684 | return 1; |
| 685 | } |
| 686 | next_hdr: |
| 687 | sol = eol + idx->v[cur_idx].cr + 1; |
| 688 | old_idx = cur_idx; |
| 689 | cur_idx = idx->v[cur_idx].next; |
| 690 | } |
| 691 | return 0; |
| 692 | } |
| 693 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 694 | /* Find the first or next header field in message buffer <sol> using headers |
| 695 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 696 | * everything necessary to use the header and find next occurrence. If its |
| 697 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 698 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 699 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 700 | * no header name. |
| 701 | */ |
| 702 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 703 | { |
| 704 | char *eol, *sov; |
| 705 | int cur_idx, old_idx; |
| 706 | int len; |
| 707 | |
| 708 | cur_idx = ctx->idx; |
| 709 | if (cur_idx) { |
| 710 | /* We have previously returned a header, let's search another one */ |
| 711 | sol = ctx->line; |
| 712 | eol = sol + idx->v[cur_idx].len; |
| 713 | goto next_hdr; |
| 714 | } |
| 715 | |
| 716 | /* first request for this header */ |
| 717 | sol += hdr_idx_first_pos(idx); |
| 718 | old_idx = 0; |
| 719 | cur_idx = hdr_idx_first_idx(idx); |
| 720 | while (cur_idx) { |
| 721 | eol = sol + idx->v[cur_idx].len; |
| 722 | |
| 723 | len = 0; |
| 724 | while (1) { |
| 725 | if (len >= eol - sol) |
| 726 | goto next_hdr; |
| 727 | if (sol[len] == ':') |
| 728 | break; |
| 729 | len++; |
| 730 | } |
| 731 | |
| 732 | ctx->del = len; |
| 733 | sov = sol + len + 1; |
| 734 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 735 | sov++; |
| 736 | |
| 737 | ctx->line = sol; |
| 738 | ctx->prev = old_idx; |
| 739 | ctx->idx = cur_idx; |
| 740 | ctx->val = sov - sol; |
| 741 | ctx->tws = 0; |
| 742 | |
| 743 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 744 | eol--; |
| 745 | ctx->tws++; |
| 746 | } |
| 747 | ctx->vlen = eol - sov; |
| 748 | return 1; |
| 749 | |
| 750 | next_hdr: |
| 751 | sol = eol + idx->v[cur_idx].cr + 1; |
| 752 | old_idx = cur_idx; |
| 753 | cur_idx = idx->v[cur_idx].next; |
| 754 | } |
| 755 | return 0; |
| 756 | } |
| 757 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 758 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 759 | * par 2.2 for more information. Note that it requires a valid header to return |
| 760 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 761 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 762 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 763 | { |
| 764 | int quoted, qdpair; |
| 765 | |
| 766 | quoted = qdpair = 0; |
| 767 | for (; s < e; s++) { |
| 768 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 769 | else if (quoted) { |
| 770 | if (*s == '\\') qdpair = 1; |
| 771 | else if (*s == '"') quoted = 0; |
| 772 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 773 | else if (*s == '"') quoted = 1; |
| 774 | else if (*s == ',') return s; |
| 775 | } |
| 776 | return s; |
| 777 | } |
| 778 | |
| 779 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 780 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 781 | * structure holds everything necessary to use the header and find next |
| 782 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 783 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 784 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 785 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 786 | * is NULL when searching for a new values of a header, the current header is |
| 787 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 788 | */ |
| 789 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 790 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 791 | struct hdr_ctx *ctx) |
| 792 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 793 | char *eol, *sov; |
| 794 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 795 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 796 | cur_idx = ctx->idx; |
| 797 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 798 | /* We have previously returned a value, let's search |
| 799 | * another one on the same line. |
| 800 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 801 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 802 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 803 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 804 | eol = sol + idx->v[cur_idx].len; |
| 805 | |
| 806 | if (sov >= eol) |
| 807 | /* no more values in this header */ |
| 808 | goto next_hdr; |
| 809 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 810 | /* values remaining for this header, skip the comma but save it |
| 811 | * for later use (eg: for header deletion). |
| 812 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 813 | sov++; |
| 814 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 815 | sov++; |
| 816 | |
| 817 | goto return_hdr; |
| 818 | } |
| 819 | |
| 820 | /* first request for this header */ |
| 821 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 822 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 823 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 824 | while (cur_idx) { |
| 825 | eol = sol + idx->v[cur_idx].len; |
| 826 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 827 | if (len == 0) { |
| 828 | /* No argument was passed, we want any header. |
| 829 | * To achieve this, we simply build a fake request. */ |
| 830 | while (sol + len < eol && sol[len] != ':') |
| 831 | len++; |
| 832 | name = sol; |
| 833 | } |
| 834 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 835 | if ((len < eol - sol) && |
| 836 | (sol[len] == ':') && |
| 837 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 838 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 839 | sov = sol + len + 1; |
| 840 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 841 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 842 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 843 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 844 | ctx->prev = old_idx; |
| 845 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 846 | ctx->idx = cur_idx; |
| 847 | ctx->val = sov - sol; |
| 848 | |
| 849 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 850 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 851 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 852 | eol--; |
| 853 | ctx->tws++; |
| 854 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 855 | ctx->vlen = eol - sov; |
| 856 | return 1; |
| 857 | } |
| 858 | next_hdr: |
| 859 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 860 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 861 | cur_idx = idx->v[cur_idx].next; |
| 862 | } |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 867 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 868 | struct hdr_ctx *ctx) |
| 869 | { |
| 870 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 871 | } |
| 872 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 873 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 874 | * the http_find_header functions. The value is removed, as well as surrounding |
| 875 | * 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] | 876 | * 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] | 877 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 878 | * no more header, so any processing may stop. The ctx is always left in a form |
| 879 | * that can be handled by http_find_header2() to find next occurrence. |
| 880 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 881 | 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] | 882 | { |
| 883 | int cur_idx = ctx->idx; |
| 884 | char *sol = ctx->line; |
| 885 | struct hdr_idx_elem *hdr; |
| 886 | int delta, skip_comma; |
| 887 | |
| 888 | if (!cur_idx) |
| 889 | return 0; |
| 890 | |
| 891 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 892 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 893 | /* 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] | 894 | 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] | 895 | http_msg_move_end(msg, delta); |
| 896 | idx->used--; |
| 897 | hdr->len = 0; /* unused entry */ |
| 898 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 899 | if (idx->tail == ctx->idx) |
| 900 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 901 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 902 | 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] | 903 | 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] | 904 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 905 | return ctx->idx; |
| 906 | } |
| 907 | |
| 908 | /* 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] | 909 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 910 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 911 | */ |
| 912 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 913 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 914 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 915 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 916 | NULL, 0); |
| 917 | hdr->len += delta; |
| 918 | http_msg_move_end(msg, delta); |
| 919 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 920 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 921 | return ctx->idx; |
| 922 | } |
| 923 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 924 | /* This function handles a server error at the stream interface level. The |
| 925 | * stream interface is assumed to be already in a closed state. An optional |
| 926 | * message is copied into the input buffer, and an HTTP status code stored. |
| 927 | * 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] | 928 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 929 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 930 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 931 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 932 | { |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 933 | channel_auto_read(si_oc(si)); |
| 934 | channel_abort(si_oc(si)); |
| 935 | channel_auto_close(si_oc(si)); |
| 936 | channel_erase(si_oc(si)); |
| 937 | channel_auto_close(si_ic(si)); |
| 938 | channel_auto_read(si_ic(si)); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 939 | if (status > 0 && msg) { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 940 | s->txn->status = status; |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 941 | bo_inject(si_ic(si), msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 942 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 943 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 944 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 945 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 946 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 947 | } |
| 948 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 949 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 950 | * and message. |
| 951 | */ |
| 952 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 953 | struct chunk *http_error_message(struct stream *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 954 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 955 | if (s->be->errmsg[msgnum].str) |
| 956 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 957 | else if (strm_fe(s)->errmsg[msgnum].str) |
| 958 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 959 | else |
| 960 | return &http_err_chunks[msgnum]; |
| 961 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 962 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 963 | /* |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 964 | * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown |
| 965 | * ones. |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 966 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 967 | 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] | 968 | { |
| 969 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 970 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 971 | |
| 972 | m = ((unsigned)*str - 'A'); |
| 973 | |
| 974 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 975 | for (h = http_methods[m]; h->len > 0; h++) { |
| 976 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 977 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 978 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 979 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 980 | }; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 981 | } |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 982 | return HTTP_METH_OTHER; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 983 | } |
| 984 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 985 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 986 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 987 | * It is returned otherwise. |
| 988 | */ |
Thierry FOURNIER | 3c33178 | 2015-09-17 19:33:35 +0200 | [diff] [blame] | 989 | char *http_get_path(struct http_txn *txn) |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 990 | { |
| 991 | char *ptr, *end; |
| 992 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 993 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 994 | end = ptr + txn->req.sl.rq.u_l; |
| 995 | |
| 996 | if (ptr >= end) |
| 997 | return NULL; |
| 998 | |
| 999 | /* RFC2616, par. 5.1.2 : |
| 1000 | * Request-URI = "*" | absuri | abspath | authority |
| 1001 | */ |
| 1002 | |
| 1003 | if (*ptr == '*') |
| 1004 | return NULL; |
| 1005 | |
| 1006 | if (isalpha((unsigned char)*ptr)) { |
| 1007 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1008 | ptr++; |
| 1009 | while (ptr < end && |
| 1010 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 1011 | ptr++; |
| 1012 | /* skip '://' */ |
| 1013 | if (ptr == end || *ptr++ != ':') |
| 1014 | return NULL; |
| 1015 | if (ptr == end || *ptr++ != '/') |
| 1016 | return NULL; |
| 1017 | if (ptr == end || *ptr++ != '/') |
| 1018 | return NULL; |
| 1019 | } |
| 1020 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1021 | |
| 1022 | while (ptr < end && *ptr != '/') |
| 1023 | ptr++; |
| 1024 | |
| 1025 | if (ptr == end) |
| 1026 | return NULL; |
| 1027 | |
| 1028 | /* OK, we got the '/' ! */ |
| 1029 | return ptr; |
| 1030 | } |
| 1031 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 1032 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 1033 | * If not found, return NULL. It is returned otherwise. |
| 1034 | */ |
| 1035 | static char * |
| 1036 | http_get_path_from_string(char *str) |
| 1037 | { |
| 1038 | char *ptr = str; |
| 1039 | |
| 1040 | /* RFC2616, par. 5.1.2 : |
| 1041 | * Request-URI = "*" | absuri | abspath | authority |
| 1042 | */ |
| 1043 | |
| 1044 | if (*ptr == '*') |
| 1045 | return NULL; |
| 1046 | |
| 1047 | if (isalpha((unsigned char)*ptr)) { |
| 1048 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1049 | ptr++; |
| 1050 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 1051 | ptr++; |
| 1052 | /* skip '://' */ |
| 1053 | if (*ptr == '\0' || *ptr++ != ':') |
| 1054 | return NULL; |
| 1055 | if (*ptr == '\0' || *ptr++ != '/') |
| 1056 | return NULL; |
| 1057 | if (*ptr == '\0' || *ptr++ != '/') |
| 1058 | return NULL; |
| 1059 | } |
| 1060 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1061 | |
| 1062 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 1063 | ptr++; |
| 1064 | |
| 1065 | if (*ptr == '\0' || *ptr == ' ') |
| 1066 | return NULL; |
| 1067 | |
| 1068 | /* OK, we got the '/' ! */ |
| 1069 | return ptr; |
| 1070 | } |
| 1071 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 1072 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 1073 | * in redirect mode. This may only be called just after the stream interface |
| 1074 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 1075 | * follow normal proxy processing. NOTE: this function is designed to support |
| 1076 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1077 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1078 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1079 | { |
| 1080 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1081 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1082 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1083 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1084 | |
| 1085 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1086 | trash.len = strlen(HTTP_302); |
| 1087 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1088 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1089 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1090 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1091 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1092 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1093 | return; |
| 1094 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1095 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1096 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1097 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 1098 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1099 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1100 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1101 | /* 3: add the request URI. Since it was already forwarded, we need |
| 1102 | * to temporarily rewind the buffer. |
| 1103 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1104 | txn = s->txn; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1105 | b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1106 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1107 | path = http_get_path(txn); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1108 | 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] | 1109 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1110 | b_adv(s->req.buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1111 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1112 | if (!path) |
| 1113 | return; |
| 1114 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1115 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1116 | return; |
| 1117 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1118 | memcpy(trash.str + trash.len, path, len); |
| 1119 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1120 | |
| 1121 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1122 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 1123 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1124 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1125 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 1126 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1127 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1128 | |
| 1129 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1130 | si_shutr(si); |
| 1131 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1132 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1133 | si->state = SI_ST_CLO; |
| 1134 | |
| 1135 | /* send the message */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1136 | 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] | 1137 | |
| 1138 | /* 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] | 1139 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 1140 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1141 | } |
| 1142 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1143 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1144 | * that the server side is closed. Note that err_type is actually a |
| 1145 | * bitmask, where almost only aborts may be cumulated with other |
| 1146 | * values. We consider that aborted operations are more important |
| 1147 | * than timeouts or errors due to the fact that nobody else in the |
| 1148 | * logs might explain incomplete retries. All others should avoid |
| 1149 | * being cumulated. It should normally not be possible to have multiple |
| 1150 | * 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] | 1151 | * Note that connection errors appearing on the second request of a keep-alive |
| 1152 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1153 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1154 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1155 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1156 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1157 | |
| 1158 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1159 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1160 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1161 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1162 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1163 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1164 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1165 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1166 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1167 | 503, 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_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1169 | http_server_error(s, si, SF_ERR_SRVCL, 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_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1172 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1173 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1174 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1175 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1176 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
| 1177 | 503, (s->flags & SF_SRV_REUSED) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1178 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1179 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1180 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1181 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1182 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1183 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1184 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1185 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1186 | } |
| 1187 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1188 | extern const char sess_term_cond[8]; |
| 1189 | extern const char sess_fin_state[8]; |
| 1190 | extern const char *monthname[12]; |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 1191 | struct pool_head *pool2_http_txn; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1192 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1193 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1194 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1195 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1196 | /* |
| 1197 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1198 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1199 | */ |
| 1200 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1201 | char **cap, struct cap_hdr *cap_hdr) |
| 1202 | { |
| 1203 | char *eol, *sol, *col, *sov; |
| 1204 | int cur_idx; |
| 1205 | struct cap_hdr *h; |
| 1206 | int len; |
| 1207 | |
| 1208 | sol = som + hdr_idx_first_pos(idx); |
| 1209 | cur_idx = hdr_idx_first_idx(idx); |
| 1210 | |
| 1211 | while (cur_idx) { |
| 1212 | eol = sol + idx->v[cur_idx].len; |
| 1213 | |
| 1214 | col = sol; |
| 1215 | while (col < eol && *col != ':') |
| 1216 | col++; |
| 1217 | |
| 1218 | sov = col + 1; |
| 1219 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1220 | sov++; |
| 1221 | |
| 1222 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1223 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1224 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1225 | if (cap[h->index] == NULL) |
| 1226 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1227 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1228 | |
| 1229 | if (cap[h->index] == NULL) { |
| 1230 | Alert("HTTP capture : out of memory.\n"); |
| 1231 | continue; |
| 1232 | } |
| 1233 | |
| 1234 | len = eol - sov; |
| 1235 | if (len > h->len) |
| 1236 | len = h->len; |
| 1237 | |
| 1238 | memcpy(cap[h->index], sov, len); |
| 1239 | cap[h->index][len]=0; |
| 1240 | } |
| 1241 | } |
| 1242 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1243 | cur_idx = idx->v[cur_idx].next; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1248 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1249 | */ |
| 1250 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1251 | |
| 1252 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1253 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1254 | */ |
| 1255 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1256 | ptr++; \ |
| 1257 | if (likely(ptr < end)) \ |
| 1258 | goto good; \ |
| 1259 | else { \ |
| 1260 | state = (st); \ |
| 1261 | goto http_msg_ood; \ |
| 1262 | } \ |
| 1263 | } while (0) |
| 1264 | |
| 1265 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1266 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1267 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1268 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1269 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1270 | * will give undefined results. |
| 1271 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1272 | * and that msg->sol points to the beginning of the response. |
| 1273 | * If a complete line is found (which implies that at least one CR or LF is |
| 1274 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1275 | * returned indicating an incomplete line (which does not mean that parts have |
| 1276 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1277 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1278 | * upon next call. |
| 1279 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1280 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1281 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1282 | * 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] | 1283 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1284 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1285 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1286 | enum ht_state state, const char *ptr, const char *end, |
| 1287 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1288 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1289 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1290 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1291 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1292 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1293 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1294 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1295 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1296 | |
| 1297 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1298 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1299 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1300 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1301 | state = HTTP_MSG_ERROR; |
| 1302 | break; |
| 1303 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1304 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1305 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1306 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1307 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1308 | goto http_msg_rpcode; |
| 1309 | } |
| 1310 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1311 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1312 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1313 | state = HTTP_MSG_ERROR; |
| 1314 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1315 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1316 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1317 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1318 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1319 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1320 | |
| 1321 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1322 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1323 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1324 | } |
| 1325 | |
| 1326 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1327 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1328 | http_msg_rsp_reason: |
| 1329 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1330 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1331 | msg->sl.st.r_l = 0; |
| 1332 | goto http_msg_rpline_eol; |
| 1333 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1334 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1335 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1336 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1337 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1338 | goto http_msg_rpreason; |
| 1339 | } |
| 1340 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1341 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1342 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1343 | goto http_msg_rsp_reason; |
| 1344 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1345 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1346 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1347 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1348 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1349 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1350 | http_msg_rpline_eol: |
| 1351 | /* We have seen the end of line. Note that we do not |
| 1352 | * necessarily have the \n yet, but at least we know that we |
| 1353 | * have EITHER \r OR \n, otherwise the response would not be |
| 1354 | * complete. We can then record the response length and return |
| 1355 | * to the caller which will be able to register it. |
| 1356 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1357 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1358 | return ptr; |
| 1359 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1360 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1361 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1362 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1363 | exit(1); |
| 1364 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1365 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1369 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1370 | if (ret_state) |
| 1371 | *ret_state = state; |
| 1372 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1373 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1374 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1375 | } |
| 1376 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1377 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1378 | * This function parses a request line between <ptr> and <end>, starting with |
| 1379 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1380 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1381 | * will give undefined results. |
| 1382 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1383 | * and that msg->sol points to the beginning of the request. |
| 1384 | * If a complete line is found (which implies that at least one CR or LF is |
| 1385 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1386 | * returned indicating an incomplete line (which does not mean that parts have |
| 1387 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1388 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1389 | * upon next call. |
| 1390 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1391 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1392 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1393 | * 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] | 1394 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1395 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1396 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1397 | enum ht_state state, const char *ptr, const char *end, |
| 1398 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1399 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1400 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1401 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1402 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1403 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1404 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1405 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1406 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1407 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1408 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1409 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1410 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1411 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1412 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1413 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1414 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1415 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1416 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1417 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1418 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1419 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1420 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1421 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1422 | msg->sl.rq.v_l = 0; |
| 1423 | goto http_msg_rqline_eol; |
| 1424 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1425 | state = HTTP_MSG_ERROR; |
| 1426 | break; |
| 1427 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1428 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1429 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1430 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1431 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1432 | goto http_msg_rquri; |
| 1433 | } |
| 1434 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1435 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1436 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1437 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1438 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1439 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1440 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1441 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1442 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1443 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1444 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1445 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1446 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1447 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1448 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1449 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1450 | /* non-ASCII chars are forbidden unless option |
| 1451 | * accept-invalid-http-request is enabled in the frontend. |
| 1452 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1453 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1454 | if (msg->err_pos < -1) |
| 1455 | goto invalid_char; |
| 1456 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1457 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1458 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1459 | } |
| 1460 | |
| 1461 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1462 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1463 | goto http_msg_req09_uri_e; |
| 1464 | } |
| 1465 | |
| 1466 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1467 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1468 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1469 | state = HTTP_MSG_ERROR; |
| 1470 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1471 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1472 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1473 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1474 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1475 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1476 | goto http_msg_rqver; |
| 1477 | } |
| 1478 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1479 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1480 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1481 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1482 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1483 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1484 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1485 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1486 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1487 | |
| 1488 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1489 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1490 | http_msg_rqline_eol: |
| 1491 | /* We have seen the end of line. Note that we do not |
| 1492 | * necessarily have the \n yet, but at least we know that we |
| 1493 | * have EITHER \r OR \n, otherwise the request would not be |
| 1494 | * complete. We can then record the request length and return |
| 1495 | * to the caller which will be able to register it. |
| 1496 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1497 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1498 | return ptr; |
| 1499 | } |
| 1500 | |
| 1501 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1502 | state = HTTP_MSG_ERROR; |
| 1503 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1504 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1505 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1506 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1507 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1508 | exit(1); |
| 1509 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1510 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1511 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1512 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1513 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1514 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1515 | if (ret_state) |
| 1516 | *ret_state = state; |
| 1517 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1518 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1519 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1520 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1521 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1522 | /* |
| 1523 | * Returns the data from Authorization header. Function may be called more |
| 1524 | * than once so data is stored in txn->auth_data. When no header is found |
| 1525 | * 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] | 1526 | * searching again for something we are unable to find anyway. However, if |
| 1527 | * 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] | 1528 | * have the credentials overwritten by another stream in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1529 | */ |
| 1530 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 1531 | /* This bufffer is initialized in the file 'src/haproxy.c'. This length is |
| 1532 | * set according to global.tune.bufsize. |
| 1533 | */ |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1534 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1535 | |
| 1536 | int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1537 | get_http_auth(struct stream *s) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1538 | { |
| 1539 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1540 | struct http_txn *txn = s->txn; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1541 | struct chunk auth_method; |
| 1542 | struct hdr_ctx ctx; |
| 1543 | char *h, *p; |
| 1544 | int len; |
| 1545 | |
| 1546 | #ifdef DEBUG_AUTH |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1547 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1548 | #endif |
| 1549 | |
| 1550 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1551 | return 0; |
| 1552 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1553 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1554 | |
| 1555 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1556 | |
| 1557 | if (txn->flags & TX_USE_PX_CONN) { |
| 1558 | h = "Proxy-Authorization"; |
| 1559 | len = strlen(h); |
| 1560 | } else { |
| 1561 | h = "Authorization"; |
| 1562 | len = strlen(h); |
| 1563 | } |
| 1564 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1565 | 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] | 1566 | return 0; |
| 1567 | |
| 1568 | h = ctx.line + ctx.val; |
| 1569 | |
| 1570 | p = memchr(h, ' ', ctx.vlen); |
| 1571 | if (!p || p == h) |
| 1572 | return 0; |
| 1573 | |
| 1574 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1575 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1576 | |
| 1577 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1578 | |
| 1579 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1580 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1581 | |
| 1582 | if (len < 0) |
| 1583 | return 0; |
| 1584 | |
| 1585 | |
| 1586 | get_http_auth_buff[len] = '\0'; |
| 1587 | |
| 1588 | p = strchr(get_http_auth_buff, ':'); |
| 1589 | |
| 1590 | if (!p) |
| 1591 | return 0; |
| 1592 | |
| 1593 | txn->auth.user = get_http_auth_buff; |
| 1594 | *p = '\0'; |
| 1595 | txn->auth.pass = p+1; |
| 1596 | |
| 1597 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1598 | return 1; |
| 1599 | } |
| 1600 | |
| 1601 | return 0; |
| 1602 | } |
| 1603 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1604 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1605 | /* |
| 1606 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1607 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1608 | * ensuring that the message does not wrap. The function can be preempted |
| 1609 | * everywhere when data are missing and recalled at the exact same location |
| 1610 | * with no information loss. The message may even be realigned between two |
| 1611 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1612 | * 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] | 1613 | * fields. Note that msg->sol will be initialized after completing the first |
| 1614 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1615 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1616 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1617 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1618 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1619 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1620 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1621 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1622 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1623 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1624 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1625 | ptr = buf->p + msg->next; |
| 1626 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1627 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1628 | if (unlikely(ptr >= end)) |
| 1629 | goto http_msg_ood; |
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 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1632 | /* |
| 1633 | * First, states that are specific to the response only. |
| 1634 | * We check them first so that request and headers are |
| 1635 | * closer to each other (accessed more often). |
| 1636 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1637 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1638 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1639 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1640 | /* we have a start of message, but we have to check |
| 1641 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1642 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1643 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1644 | if (unlikely(ptr != buf->p)) { |
| 1645 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1646 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1647 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1648 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1649 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1650 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1651 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1652 | hdr_idx_init(idx); |
| 1653 | state = HTTP_MSG_RPVER; |
| 1654 | goto http_msg_rpver; |
| 1655 | } |
| 1656 | |
| 1657 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1658 | goto http_msg_invalid; |
| 1659 | |
| 1660 | if (unlikely(*ptr == '\n')) |
| 1661 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1662 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1663 | /* stop here */ |
| 1664 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1665 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1666 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1667 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1668 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1669 | /* stop here */ |
| 1670 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1671 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1672 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1673 | case HTTP_MSG_RPVER_SP: |
| 1674 | case HTTP_MSG_RPCODE: |
| 1675 | case HTTP_MSG_RPCODE_SP: |
| 1676 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1677 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1678 | state, ptr, end, |
| 1679 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1680 | if (unlikely(!ptr)) |
| 1681 | return; |
| 1682 | |
| 1683 | /* we have a full response and we know that we have either a CR |
| 1684 | * or an LF at <ptr>. |
| 1685 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1686 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1687 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1688 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1689 | if (likely(*ptr == '\r')) |
| 1690 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1691 | goto http_msg_rpline_end; |
| 1692 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1693 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1694 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1695 | /* msg->sol must point to the first of CR or LF. */ |
| 1696 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1697 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1698 | /* stop here */ |
| 1699 | |
| 1700 | /* |
| 1701 | * Second, states that are specific to the request only |
| 1702 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1703 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1704 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1705 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1706 | /* we have a start of message, but we have to check |
| 1707 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1708 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1709 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1710 | if (likely(ptr != buf->p)) { |
| 1711 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1712 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1713 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1714 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1715 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1716 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1717 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1718 | state = HTTP_MSG_RQMETH; |
| 1719 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1720 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1721 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1722 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1723 | goto http_msg_invalid; |
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(*ptr == '\n')) |
| 1726 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1727 | 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] | 1728 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1729 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1730 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1731 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1732 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1733 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1734 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1735 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1736 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1737 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1738 | case HTTP_MSG_RQMETH_SP: |
| 1739 | case HTTP_MSG_RQURI: |
| 1740 | case HTTP_MSG_RQURI_SP: |
| 1741 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1742 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1743 | state, ptr, end, |
| 1744 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1745 | if (unlikely(!ptr)) |
| 1746 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1747 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1748 | /* we have a full request and we know that we have either a CR |
| 1749 | * or an LF at <ptr>. |
| 1750 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1751 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1752 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1753 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1754 | if (likely(*ptr == '\r')) |
| 1755 | 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] | 1756 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1757 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1758 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1759 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1760 | /* check for HTTP/0.9 request : no version information available. |
| 1761 | * msg->sol must point to the first of CR or LF. |
| 1762 | */ |
| 1763 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1764 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1765 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1766 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1767 | 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] | 1768 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1769 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1770 | /* |
| 1771 | * Common states below |
| 1772 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1773 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1774 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1775 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1776 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1777 | goto http_msg_hdr_name; |
| 1778 | } |
| 1779 | |
| 1780 | if (likely(*ptr == '\r')) |
| 1781 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1782 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1783 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1784 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1785 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1786 | /* assumes msg->sol points to the first char */ |
| 1787 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1788 | 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] | 1789 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1790 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1791 | 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] | 1792 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1793 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1794 | goto http_msg_invalid; |
| 1795 | |
| 1796 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1797 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1798 | |
| 1799 | /* and we still accept this non-token character */ |
| 1800 | 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] | 1801 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1802 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1803 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1804 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1805 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1806 | 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] | 1807 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1808 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1809 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1810 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1811 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1812 | goto http_msg_hdr_val; |
| 1813 | } |
| 1814 | |
| 1815 | if (likely(*ptr == '\r')) |
| 1816 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1817 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1818 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1819 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1820 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1821 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1822 | 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] | 1823 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1824 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1825 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1826 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1827 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1828 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1829 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1830 | goto http_msg_hdr_l1_sp; |
| 1831 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1832 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1833 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1834 | goto http_msg_complete_header; |
| 1835 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1836 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1837 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1838 | /* assumes msg->sol points to the first char, and msg->sov |
| 1839 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1840 | */ |
| 1841 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1842 | 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] | 1843 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1844 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1845 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1846 | * real header end in case it ends with lots of LWS, but is this |
| 1847 | * really needed ? |
| 1848 | */ |
| 1849 | if (likely(*ptr == '\r')) |
| 1850 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1851 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1852 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1853 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1854 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1855 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1856 | 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] | 1857 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1858 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1859 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1860 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1861 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1862 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1863 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1864 | goto http_msg_hdr_val; |
| 1865 | } |
| 1866 | http_msg_complete_header: |
| 1867 | /* |
| 1868 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1869 | * Assumes msg->sol points to the first char, msg->sov points |
| 1870 | * to the first character of the value and msg->eol to the |
| 1871 | * first CR or LF so we know how the line ends. We insert last |
| 1872 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1873 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1874 | 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] | 1875 | idx, idx->tail) < 0)) |
| 1876 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1877 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1878 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1879 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1880 | goto http_msg_hdr_name; |
| 1881 | } |
| 1882 | |
| 1883 | if (likely(*ptr == '\r')) |
| 1884 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1885 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1886 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1887 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1888 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1889 | /* Assumes msg->sol points to the first of either CR or LF. |
| 1890 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 1891 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 1892 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1893 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1894 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1895 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1896 | msg->eoh = msg->sol; |
| 1897 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1898 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1899 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1900 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1901 | |
| 1902 | case HTTP_MSG_ERROR: |
| 1903 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1904 | break; |
| 1905 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1906 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1907 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1908 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1909 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1910 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1911 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1912 | } |
| 1913 | http_msg_ood: |
| 1914 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1915 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1916 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1917 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1918 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1919 | http_msg_invalid: |
| 1920 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1921 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1922 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1923 | return; |
| 1924 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1925 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1926 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1927 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1928 | * nothing is done and 1 is returned. |
| 1929 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1930 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1931 | { |
| 1932 | int delta; |
| 1933 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1934 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1935 | |
| 1936 | if (msg->sl.rq.v_l != 0) |
| 1937 | return 1; |
| 1938 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1939 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1940 | if (txn->meth != HTTP_METH_GET) |
| 1941 | return 0; |
| 1942 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1943 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1944 | delta = 0; |
| 1945 | |
| 1946 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1947 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1948 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1949 | } |
| 1950 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1951 | 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] | 1952 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1953 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1954 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1955 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1956 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1957 | NULL, NULL); |
| 1958 | if (unlikely(!cur_end)) |
| 1959 | return 0; |
| 1960 | |
| 1961 | /* we have a full HTTP/1.0 request now and we know that |
| 1962 | * we have either a CR or an LF at <ptr>. |
| 1963 | */ |
| 1964 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1965 | return 1; |
| 1966 | } |
| 1967 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1968 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1969 | * and "keep-alive" values. If we already know that some headers may safely |
| 1970 | * 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] | 1971 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1972 | * - 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] | 1973 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1974 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1975 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1976 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1977 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1978 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1979 | 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] | 1980 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1981 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1982 | const char *hdr_val = "Connection"; |
| 1983 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1984 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1985 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1986 | return; |
| 1987 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1988 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1989 | hdr_val = "Proxy-Connection"; |
| 1990 | hdr_len = 16; |
| 1991 | } |
| 1992 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1993 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1994 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1995 | 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] | 1996 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1997 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1998 | if (to_del & 2) |
| 1999 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2000 | else |
| 2001 | txn->flags |= TX_CON_KAL_SET; |
| 2002 | } |
| 2003 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2004 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2005 | if (to_del & 1) |
| 2006 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2007 | else |
| 2008 | txn->flags |= TX_CON_CLO_SET; |
| 2009 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2010 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 2011 | txn->flags |= TX_HDR_CONN_UPG; |
| 2012 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2013 | } |
| 2014 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2015 | txn->flags |= TX_HDR_CONN_PRS; |
| 2016 | return; |
| 2017 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2018 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2019 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 2020 | * added depending on the <wanted> flags, which are exclusively composed of |
| 2021 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 2022 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 2023 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2024 | 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] | 2025 | { |
| 2026 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2027 | const char *hdr_val = "Connection"; |
| 2028 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2029 | |
| 2030 | ctx.idx = 0; |
| 2031 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2032 | |
| 2033 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2034 | hdr_val = "Proxy-Connection"; |
| 2035 | hdr_len = 16; |
| 2036 | } |
| 2037 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2038 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2039 | 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] | 2040 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2041 | if (wanted & TX_CON_KAL_SET) |
| 2042 | txn->flags |= TX_CON_KAL_SET; |
| 2043 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2044 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2045 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2046 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2047 | if (wanted & TX_CON_CLO_SET) |
| 2048 | txn->flags |= TX_CON_CLO_SET; |
| 2049 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2050 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2051 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2052 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2053 | |
| 2054 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 2055 | return; |
| 2056 | |
| 2057 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 2058 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2059 | hdr_val = "Connection: close"; |
| 2060 | hdr_len = 17; |
| 2061 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2062 | hdr_val = "Proxy-Connection: close"; |
| 2063 | hdr_len = 23; |
| 2064 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2065 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 2069 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2070 | hdr_val = "Connection: keep-alive"; |
| 2071 | hdr_len = 22; |
| 2072 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2073 | hdr_val = "Proxy-Connection: keep-alive"; |
| 2074 | hdr_len = 28; |
| 2075 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2076 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2077 | } |
| 2078 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2079 | } |
| 2080 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2081 | /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to |
| 2082 | * the first byte of data after the chunk size, so that we know we can forward |
| 2083 | * exactly msg->next bytes. msg->sol contains the exact number of bytes forming |
| 2084 | * the chunk size. That way it is always possible to differentiate between the |
| 2085 | * start of the body and the start of the data. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2086 | * 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] | 2087 | * 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] | 2088 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2089 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2090 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2091 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2092 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2093 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2094 | const char *end = buf->data + buf->size; |
| 2095 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2096 | unsigned int chunk = 0; |
| 2097 | |
| 2098 | /* The chunk size is in the following form, though we are only |
| 2099 | * interested in the size and CRLF : |
| 2100 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2101 | */ |
| 2102 | while (1) { |
| 2103 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2104 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2105 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2106 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2107 | if (c < 0) /* not a hex digit anymore */ |
| 2108 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2109 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2110 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 2111 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2112 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2113 | chunk = (chunk << 4) + c; |
| 2114 | } |
| 2115 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2116 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2117 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2118 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2119 | |
| 2120 | while (http_is_spht[(unsigned char)*ptr]) { |
| 2121 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2122 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2123 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2124 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2125 | } |
| 2126 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2127 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2128 | * for the end of chunk size. |
| 2129 | */ |
| 2130 | while (1) { |
| 2131 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2132 | /* we now have a CR or an LF at ptr */ |
| 2133 | if (likely(*ptr == '\r')) { |
| 2134 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2135 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2136 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2137 | return 0; |
| 2138 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2139 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2140 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2141 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2142 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2143 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2144 | /* done */ |
| 2145 | break; |
| 2146 | } |
| 2147 | else if (*ptr == ';') { |
| 2148 | /* chunk extension, ends at next CRLF */ |
| 2149 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2150 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2151 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2152 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2153 | |
| 2154 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2155 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2156 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2157 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2158 | return 0; |
| 2159 | } |
| 2160 | /* we have a CRLF now, loop above */ |
| 2161 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2162 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2163 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2164 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2165 | } |
| 2166 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2167 | /* 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] | 2168 | * which may or may not be present. We save that into ->next, |
| 2169 | * and the number of bytes parsed into msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2170 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2171 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2172 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2173 | msg->sol += buf->size; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2174 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2175 | msg->chunk_len = chunk; |
| 2176 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2177 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2178 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2179 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2180 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2181 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2182 | } |
| 2183 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2184 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2185 | * 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] | 2186 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2187 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2188 | * If not enough data are available, the function does not change anything |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2189 | * except maybe msg->next if it could parse some lines, and returns zero. |
| 2190 | * If a parse error is encountered, the function returns < 0 and does not |
| 2191 | * change anything except maybe msg->next. Note that the message must |
| 2192 | * already be in HTTP_MSG_TRAILERS state before calling this function, |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2193 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2194 | * forwarding, and that msg->next exactly matches the length of trailers |
| 2195 | * already parsed and not forwarded. It is also important to note that this |
| 2196 | * 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] | 2197 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2198 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2199 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2200 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2201 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2202 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2203 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2204 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2205 | const char *ptr = b_ptr(buf, msg->next); |
| 2206 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2207 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2208 | |
| 2209 | /* scan current line and stop at LF or CRLF */ |
| 2210 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2211 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2212 | return 0; |
| 2213 | |
| 2214 | if (*ptr == '\n') { |
| 2215 | if (!p1) |
| 2216 | p1 = ptr; |
| 2217 | p2 = ptr; |
| 2218 | break; |
| 2219 | } |
| 2220 | |
| 2221 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2222 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2223 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2224 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2225 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2226 | p1 = ptr; |
| 2227 | } |
| 2228 | |
| 2229 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2230 | if (ptr >= buf->data + buf->size) |
| 2231 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | /* after LF; point to beginning of next line */ |
| 2235 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2236 | if (p2 >= buf->data + buf->size) |
| 2237 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2238 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2239 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2240 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2241 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2242 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2243 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2244 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2245 | * Everything was scheduled for forwarding, there's nothing |
| 2246 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2247 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2248 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2249 | msg->msg_state = HTTP_MSG_DONE; |
| 2250 | return 1; |
| 2251 | } |
| 2252 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2253 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2254 | } |
| 2255 | } |
| 2256 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2257 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF |
| 2258 | * or a possible LF alone at the end of a chunk. It automatically adjusts |
| 2259 | * 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] | 2260 | * 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] | 2261 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2262 | * not enough data are available, the function does not change anything and |
| 2263 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2264 | * does not change anything. Note: this function is designed to parse wrapped |
| 2265 | * CRLF at the end of the buffer. |
| 2266 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2267 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2268 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2269 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2270 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2271 | int bytes; |
| 2272 | |
| 2273 | /* NB: we'll check data availabilty at the end. It's not a |
| 2274 | * problem because whatever we match first will be checked |
| 2275 | * against the correct length. |
| 2276 | */ |
| 2277 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2278 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2279 | if (*ptr == '\r') { |
| 2280 | bytes++; |
| 2281 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2282 | if (ptr >= buf->data + buf->size) |
| 2283 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2284 | } |
| 2285 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2286 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2287 | return 0; |
| 2288 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2289 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2290 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2291 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2292 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2293 | |
| 2294 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2295 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2296 | ptr = buf->data; |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2297 | /* Advance ->next to allow the CRLF to be forwarded */ |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2298 | msg->next += bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2299 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2300 | return 1; |
| 2301 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2302 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2303 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2304 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2305 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2306 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2307 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2308 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2309 | { |
| 2310 | int q = 1000; |
| 2311 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2312 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2313 | goto out; |
| 2314 | q = (*qvalue++ - '0') * 1000; |
| 2315 | |
| 2316 | if (*qvalue++ != '.') |
| 2317 | goto out; |
| 2318 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2319 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2320 | goto out; |
| 2321 | q += (*qvalue++ - '0') * 100; |
| 2322 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2323 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2324 | goto out; |
| 2325 | q += (*qvalue++ - '0') * 10; |
| 2326 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2327 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2328 | goto out; |
| 2329 | q += (*qvalue++ - '0') * 1; |
| 2330 | out: |
| 2331 | if (q > 1000) |
| 2332 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2333 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2334 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2335 | return q; |
| 2336 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2337 | |
| 2338 | /* |
| 2339 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2340 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2341 | int select_compression_request_header(struct stream *s, struct buffer *req) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2342 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2343 | struct http_txn *txn = s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2344 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2345 | struct hdr_ctx ctx; |
| 2346 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2347 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2348 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2349 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 2350 | * 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] | 2351 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 2352 | */ |
| 2353 | ctx.idx = 0; |
| 2354 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2355 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2356 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2357 | (ctx.vlen < 31 || |
| 2358 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2359 | ctx.line[ctx.val + 30] < '6' || |
| 2360 | (ctx.line[ctx.val + 30] == '6' && |
| 2361 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2362 | s->comp_algo = NULL; |
| 2363 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2364 | } |
| 2365 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2366 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2367 | 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] | 2368 | int best_q = 0; |
| 2369 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2370 | ctx.idx = 0; |
| 2371 | 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] | 2372 | const char *qval; |
| 2373 | int q; |
| 2374 | int toklen; |
| 2375 | |
| 2376 | /* try to isolate the token from the optional q-value */ |
| 2377 | toklen = 0; |
| 2378 | while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)]) |
| 2379 | toklen++; |
| 2380 | |
| 2381 | qval = ctx.line + ctx.val + toklen; |
| 2382 | while (1) { |
| 2383 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2384 | qval++; |
| 2385 | |
| 2386 | if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') { |
| 2387 | qval = NULL; |
| 2388 | break; |
| 2389 | } |
| 2390 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2391 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2392 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 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 | if (qval >= ctx.line + ctx.val + ctx.vlen) { |
| 2396 | qval = NULL; |
| 2397 | break; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2398 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2399 | if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0) |
| 2400 | break; |
| 2401 | |
| 2402 | while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';') |
| 2403 | qval++; |
| 2404 | } |
| 2405 | |
| 2406 | /* 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] | 2407 | q = qval ? parse_qvalue(qval + 2, NULL) : 1000; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2408 | |
| 2409 | if (q <= best_q) |
| 2410 | continue; |
| 2411 | |
| 2412 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 2413 | if (*(ctx.line + ctx.val) == '*' || |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2414 | 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] | 2415 | s->comp_algo = comp_algo; |
| 2416 | best_q = q; |
| 2417 | break; |
| 2418 | } |
| 2419 | } |
| 2420 | } |
| 2421 | } |
| 2422 | |
| 2423 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2424 | if (s->comp_algo) { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2425 | 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] | 2426 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2427 | ctx.idx = 0; |
| 2428 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2429 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2430 | } |
| 2431 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2432 | return 1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | /* identity is implicit does not require headers */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2436 | 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] | 2437 | 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] | 2438 | 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] | 2439 | s->comp_algo = comp_algo; |
| 2440 | return 1; |
| 2441 | } |
| 2442 | } |
| 2443 | } |
| 2444 | |
| 2445 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2446 | return 0; |
| 2447 | } |
| 2448 | |
| 2449 | /* |
| 2450 | * Selects a comression algorithm depending of the server response. |
| 2451 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2452 | int select_compression_response_header(struct stream *s, struct buffer *res) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2453 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2454 | struct http_txn *txn = s->txn; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2455 | struct http_msg *msg = &txn->rsp; |
| 2456 | struct hdr_ctx ctx; |
| 2457 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2458 | |
| 2459 | /* no common compression algorithm was found in request header */ |
| 2460 | if (s->comp_algo == NULL) |
| 2461 | goto fail; |
| 2462 | |
| 2463 | /* HTTP < 1.1 should not be compressed */ |
Willy Tarreau | 7257550 | 2013-12-24 14:41:35 +0100 | [diff] [blame] | 2464 | 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] | 2465 | goto fail; |
| 2466 | |
Jesse Hathaway | 2468d4e | 2015-03-06 20:16:15 +0000 | [diff] [blame] | 2467 | /* compress 200,201,202,203 responses only */ |
| 2468 | if ((txn->status != 200) && |
| 2469 | (txn->status != 201) && |
| 2470 | (txn->status != 202) && |
| 2471 | (txn->status != 203)) |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2472 | goto fail; |
| 2473 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2474 | /* Content-Length is null */ |
| 2475 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2476 | goto fail; |
| 2477 | |
| 2478 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2479 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2480 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2481 | goto fail; |
| 2482 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2483 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2484 | ctx.idx = 0; |
| 2485 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2486 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2487 | goto fail; |
| 2488 | } |
| 2489 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2490 | comp_type = NULL; |
| 2491 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2492 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2493 | * not listed in the "compression type" directive if any. If no content-type was |
| 2494 | * found but configuration requires one, we don't compress either. Backend has |
| 2495 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2496 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2497 | ctx.idx = 0; |
| 2498 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2499 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2500 | goto fail; |
| 2501 | |
| 2502 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2503 | (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2504 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2505 | if (ctx.vlen >= comp_type->name_len && |
| 2506 | 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] | 2507 | /* this Content-Type should be compressed */ |
| 2508 | break; |
| 2509 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2510 | /* this Content-Type should not be compressed */ |
| 2511 | if (comp_type == NULL) |
| 2512 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2513 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2514 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2515 | else { /* no content-type header */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2516 | 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] | 2517 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2518 | } |
| 2519 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2520 | /* limit compression rate */ |
| 2521 | if (global.comp_rate_lim > 0) |
| 2522 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2523 | goto fail; |
| 2524 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2525 | /* limit cpu usage */ |
| 2526 | if (idle_pct < compress_min_idle) |
| 2527 | goto fail; |
| 2528 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2529 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2530 | 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] | 2531 | goto fail; |
| 2532 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2533 | s->flags |= SF_COMP_READY; |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2534 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2535 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2536 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2537 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2538 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2539 | |
| 2540 | /* add Transfer-Encoding header */ |
| 2541 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2542 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2543 | |
| 2544 | /* |
| 2545 | * Add Content-Encoding header when it's not identity encoding. |
| 2546 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2547 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2548 | * header. |
| 2549 | */ |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2550 | 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] | 2551 | trash.len = 18; |
| 2552 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2553 | memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len); |
| 2554 | trash.len += s->comp_algo->ua_name_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2555 | trash.str[trash.len] = '\0'; |
| 2556 | 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] | 2557 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2558 | return 1; |
| 2559 | |
| 2560 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2561 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2562 | return 0; |
| 2563 | } |
| 2564 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2565 | 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] | 2566 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2567 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2568 | int tmp = TX_CON_WANT_KAL; |
| 2569 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2570 | if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 2571 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2572 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2573 | tmp = TX_CON_WANT_TUN; |
| 2574 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2575 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2576 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2577 | tmp = TX_CON_WANT_TUN; |
| 2578 | } |
| 2579 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2580 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2581 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 2582 | /* option httpclose + server_close => forceclose */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2583 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2584 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2585 | tmp = TX_CON_WANT_CLO; |
| 2586 | else |
| 2587 | tmp = TX_CON_WANT_SCL; |
| 2588 | } |
| 2589 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2590 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2591 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 2592 | tmp = TX_CON_WANT_CLO; |
| 2593 | |
| 2594 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2595 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2596 | |
| 2597 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 2598 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2599 | /* parse the Connection header and possibly clean it */ |
| 2600 | int to_del = 0; |
| 2601 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 2602 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2603 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2604 | to_del |= 2; /* remove "keep-alive" */ |
| 2605 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2606 | to_del |= 1; /* remove "close" */ |
| 2607 | http_parse_connection_header(txn, msg, to_del); |
| 2608 | } |
| 2609 | |
| 2610 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2611 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2612 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2613 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2614 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 2615 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2616 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2617 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2618 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2619 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2620 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2621 | * processing can continue on next analysers, or zero if it either needs more |
| 2622 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2623 | * 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] | 2624 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2625 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2626 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2627 | 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] | 2628 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2629 | /* |
| 2630 | * We will parse the partial (or complete) lines. |
| 2631 | * We will check the request syntax, and also join multi-line |
| 2632 | * headers. An index of all the lines will be elaborated while |
| 2633 | * parsing. |
| 2634 | * |
| 2635 | * For the parsing, we use a 28 states FSM. |
| 2636 | * |
| 2637 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2638 | * req->buf->p = beginning of request |
| 2639 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2640 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2641 | * msg->eol = end of current header or line (LF or CRLF) |
| 2642 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2643 | * |
| 2644 | * 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] | 2645 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2646 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2647 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2648 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2649 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2650 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2651 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2652 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2653 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2654 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2655 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2656 | 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] | 2657 | now_ms, __FUNCTION__, |
| 2658 | s, |
| 2659 | req, |
| 2660 | req->rex, req->wex, |
| 2661 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2662 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2663 | req->analysers); |
| 2664 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2665 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2666 | s->srv_error = http_return_srv_error; |
| 2667 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2668 | /* There's a protected area at the end of the buffer for rewriting |
| 2669 | * purposes. We don't want to start to parse the request if the |
| 2670 | * protected area is affected, because we may have to move processed |
| 2671 | * data later, which is much more complicated. |
| 2672 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2673 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2674 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 2675 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2676 | 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] | 2677 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2678 | /* 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] | 2679 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2680 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2681 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2682 | return 0; |
| 2683 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2684 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2685 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2686 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2687 | } |
| 2688 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2689 | /* Note that we have the same problem with the response ; we |
| 2690 | * may want to send a redirect, error or anything which requires |
| 2691 | * some spare space. So we'll ensure that we have at least |
| 2692 | * maxrewrite bytes available in the response buffer before |
| 2693 | * processing that one. This will only affect pipelined |
| 2694 | * keep-alive requests. |
| 2695 | */ |
| 2696 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2697 | unlikely(!channel_is_rewritable(&s->res) || |
| 2698 | bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) || |
| 2699 | bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) { |
| 2700 | if (s->res.buf->o) { |
| 2701 | 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] | 2702 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2703 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2704 | channel_dont_connect(req); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2705 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 2706 | s->res.flags |= CF_WAKE_WRITE; |
| 2707 | s->res.analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2708 | return 0; |
| 2709 | } |
| 2710 | } |
| 2711 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2712 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2713 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2714 | } |
| 2715 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2716 | /* 1: we might have to print this header in debug mode */ |
| 2717 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2718 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2719 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2720 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2721 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2722 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2723 | /* this is a bit complex : in case of error on the request line, |
| 2724 | * we know that rq.l is still zero, so we display only the part |
| 2725 | * up to the end of the line (truncated by debug_hdr). |
| 2726 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2727 | 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] | 2728 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2729 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2730 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2731 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2732 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2733 | while (cur_idx) { |
| 2734 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2735 | debug_hdr("clihdr", s, sol, eol); |
| 2736 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2737 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2738 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2739 | } |
| 2740 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2741 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2742 | /* |
| 2743 | * Now we quickly check if we have found a full valid request. |
| 2744 | * If not so, we check the FD and buffer states before leaving. |
| 2745 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2746 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2747 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2748 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 2749 | * 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] | 2750 | * 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] | 2751 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2752 | * Last, we may increase some tracked counters' http request errors on |
| 2753 | * the cases that are deliberately the client's fault. For instance, |
| 2754 | * a timeout or connection reset is not counted as an error. However |
| 2755 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2756 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2757 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2758 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2759 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2760 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2761 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2762 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2763 | stream_inc_http_req_ctr(s); |
| 2764 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2765 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2766 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2767 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2768 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2769 | /* 1: Since we are in header mode, if there's no space |
| 2770 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2771 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2772 | * must terminate it now. |
| 2773 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2774 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2775 | /* FIXME: check if URI is set and return Status |
| 2776 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2777 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2778 | stream_inc_http_req_ctr(s); |
| 2779 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2780 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2781 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2782 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2783 | goto return_bad_req; |
| 2784 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2785 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2786 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2787 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2788 | if (!(s->flags & SF_ERR_MASK)) |
| 2789 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2790 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2791 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2792 | goto failed_keep_alive; |
| 2793 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2794 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2795 | goto failed_keep_alive; |
| 2796 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2797 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2798 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2799 | 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] | 2800 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2801 | } |
| 2802 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2803 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2804 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2805 | msg->msg_state = HTTP_MSG_ERROR; |
| 2806 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2807 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2808 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2809 | proxy_inc_fe_req_ctr(sess->fe); |
| 2810 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2811 | if (sess->listener->counters) |
| 2812 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2813 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2814 | if (!(s->flags & SF_FINST_MASK)) |
| 2815 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2816 | return 0; |
| 2817 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2818 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2819 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2820 | 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] | 2821 | if (!(s->flags & SF_ERR_MASK)) |
| 2822 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2823 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2824 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2825 | goto failed_keep_alive; |
| 2826 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2827 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2828 | goto failed_keep_alive; |
| 2829 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2830 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2831 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2832 | 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] | 2833 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2834 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2835 | txn->status = 408; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2836 | 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] | 2837 | msg->msg_state = HTTP_MSG_ERROR; |
| 2838 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2839 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2840 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2841 | proxy_inc_fe_req_ctr(sess->fe); |
| 2842 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2843 | if (sess->listener->counters) |
| 2844 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2845 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2846 | if (!(s->flags & SF_FINST_MASK)) |
| 2847 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2848 | return 0; |
| 2849 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2850 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2851 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2852 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2853 | if (!(s->flags & SF_ERR_MASK)) |
| 2854 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2855 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2856 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2857 | goto failed_keep_alive; |
| 2858 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2859 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2860 | goto failed_keep_alive; |
| 2861 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2862 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2863 | 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] | 2864 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2865 | 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] | 2866 | msg->msg_state = HTTP_MSG_ERROR; |
| 2867 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2868 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2869 | stream_inc_http_err_ctr(s); |
| 2870 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2871 | proxy_inc_fe_req_ctr(sess->fe); |
| 2872 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2873 | if (sess->listener->counters) |
| 2874 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2875 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2876 | if (!(s->flags & SF_FINST_MASK)) |
| 2877 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2878 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2879 | } |
| 2880 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2881 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2882 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2883 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2884 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2885 | if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i && |
| 2886 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2887 | /* We need more data, we have to re-enable quick-ack in case we |
| 2888 | * previously disabled it, otherwise we might cause the client |
| 2889 | * to delay next data. |
| 2890 | */ |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2891 | 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] | 2892 | } |
| 2893 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2894 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2895 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2896 | /* If the client starts to talk, let's fall back to |
| 2897 | * request timeout processing. |
| 2898 | */ |
| 2899 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2900 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2901 | } |
| 2902 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2903 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2904 | if (!tick_isset(req->analyse_exp)) { |
| 2905 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2906 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2907 | tick_isset(s->be->timeout.httpka)) |
| 2908 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2909 | else |
| 2910 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2911 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2912 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2913 | /* we're not ready yet */ |
| 2914 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2915 | |
| 2916 | failed_keep_alive: |
| 2917 | /* Here we process low-level errors for keep-alive requests. In |
| 2918 | * short, if the request is not the first one and it experiences |
| 2919 | * a timeout, read error or shutdown, we just silently close so |
| 2920 | * that the client can try again. |
| 2921 | */ |
| 2922 | txn->status = 0; |
| 2923 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2924 | req->analysers = 0; |
| 2925 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2926 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2927 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2928 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2929 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2930 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2931 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2932 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2933 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2934 | * 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] | 2935 | * 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] | 2936 | * 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] | 2937 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2938 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2939 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2940 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2941 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2942 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2943 | 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] | 2944 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2945 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2946 | /* kill the pending keep-alive timeout */ |
| 2947 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2948 | req->analyse_exp = TICK_ETERNITY; |
| 2949 | } |
| 2950 | |
| 2951 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2952 | /* Maybe we found in invalid header name while we were configured not |
| 2953 | * to block on that, so we have to capture it now. |
| 2954 | */ |
| 2955 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2956 | 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] | 2957 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2958 | /* |
| 2959 | * 1: identify the method |
| 2960 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2961 | 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] | 2962 | |
| 2963 | /* we can make use of server redirect on GET and HEAD */ |
| 2964 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2965 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2966 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2967 | /* |
| 2968 | * 2: check if the URI matches the monitor_uri. |
| 2969 | * We have to do this for every request which gets in, because |
| 2970 | * the monitor-uri is defined by the frontend. |
| 2971 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2972 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 2973 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2974 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2975 | sess->fe->monitor_uri, |
| 2976 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2977 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2978 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2979 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2980 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2981 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2982 | s->flags |= SF_MONITOR; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2983 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2984 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2985 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2986 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2987 | 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] | 2988 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2989 | ret = acl_pass(ret); |
| 2990 | if (cond->pol == ACL_COND_UNLESS) |
| 2991 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2992 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2993 | if (ret) { |
| 2994 | /* we fail this request, let's return 503 service unavail */ |
| 2995 | txn->status = 503; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2996 | 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] | 2997 | if (!(s->flags & SF_ERR_MASK)) |
| 2998 | 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] | 2999 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 3000 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3001 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 3002 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3003 | /* nothing to fail, let's reply normaly */ |
| 3004 | txn->status = 200; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3005 | 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] | 3006 | if (!(s->flags & SF_ERR_MASK)) |
| 3007 | 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] | 3008 | goto return_prx_cond; |
| 3009 | } |
| 3010 | |
| 3011 | /* |
| 3012 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 3013 | * Note: we cannot log anymore if the request has been |
| 3014 | * classified as invalid. |
| 3015 | */ |
| 3016 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 3017 | /* we have a complete HTTP request that we must log */ |
| 3018 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 3019 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3020 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3021 | if (urilen >= REQURI_LEN) |
| 3022 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3023 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3024 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3025 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 3026 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3027 | s->do_log(s); |
| 3028 | } else { |
| 3029 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3030 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3031 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3032 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 3033 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 3034 | * exactly one digit "." one digit. This check may be disabled using |
| 3035 | * option accept-invalid-http-request. |
| 3036 | */ |
| 3037 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 3038 | if (msg->sl.rq.v_l != 8) { |
| 3039 | msg->err_pos = msg->sl.rq.v; |
| 3040 | goto return_bad_req; |
| 3041 | } |
| 3042 | |
| 3043 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 3044 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 3045 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 3046 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 3047 | msg->err_pos = msg->sl.rq.v + 4; |
| 3048 | goto return_bad_req; |
| 3049 | } |
| 3050 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 3051 | else { |
| 3052 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 3053 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 3054 | goto return_bad_req; |
| 3055 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 3056 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3057 | /* ... and check if the request is HTTP/1.1 or above */ |
| 3058 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3059 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 3060 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 3061 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3062 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3063 | |
| 3064 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3065 | 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] | 3066 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3067 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 3068 | * we have what looks like a proxied connection instead of a connection, |
| 3069 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 3070 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 3071 | * happen to do that despite being non-standard :-( |
| 3072 | * We consider that a request not beginning with either '/' or '*' is |
| 3073 | * a proxied connection, which covers both "scheme://location" and |
| 3074 | * CONNECT ip:port. |
| 3075 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3076 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3077 | 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] | 3078 | txn->flags |= TX_USE_PX_CONN; |
| 3079 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3080 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3081 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3082 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3083 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 3084 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3085 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 3086 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3087 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3088 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 3089 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3090 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3091 | * The length of a message body is determined by one of the following |
| 3092 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3093 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3094 | * 1. Any response to a HEAD request and any response with a 1xx |
| 3095 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 3096 | * code is always terminated by the first empty line after the |
| 3097 | * header fields, regardless of the header fields present in the |
| 3098 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3099 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3100 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 3101 | * the connection will become a tunnel immediately after the empty |
| 3102 | * line that concludes the header fields. A client MUST ignore any |
| 3103 | * Content-Length or Transfer-Encoding header fields received in |
| 3104 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3105 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3106 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 3107 | * transfer coding (Section 4.1) is the final encoding, the message |
| 3108 | * body length is determined by reading and decoding the chunked |
| 3109 | * data until the transfer coding indicates the data is complete. |
| 3110 | * |
| 3111 | * If a Transfer-Encoding header field is present in a response and |
| 3112 | * the chunked transfer coding is not the final encoding, the |
| 3113 | * message body length is determined by reading the connection until |
| 3114 | * it is closed by the server. If a Transfer-Encoding header field |
| 3115 | * is present in a request and the chunked transfer coding is not |
| 3116 | * the final encoding, the message body length cannot be determined |
| 3117 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 3118 | * status code and then close the connection. |
| 3119 | * |
| 3120 | * If a message is received with both a Transfer-Encoding and a |
| 3121 | * Content-Length header field, the Transfer-Encoding overrides the |
| 3122 | * Content-Length. Such a message might indicate an attempt to |
| 3123 | * perform request smuggling (Section 9.5) or response splitting |
| 3124 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 3125 | * remove the received Content-Length field prior to forwarding such |
| 3126 | * a message downstream. |
| 3127 | * |
| 3128 | * 4. If a message is received without Transfer-Encoding and with |
| 3129 | * either multiple Content-Length header fields having differing |
| 3130 | * field-values or a single Content-Length header field having an |
| 3131 | * invalid value, then the message framing is invalid and the |
| 3132 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 3133 | * request message, the server MUST respond with a 400 (Bad Request) |
| 3134 | * status code and then close the connection. If this is a response |
| 3135 | * message received by a proxy, the proxy MUST close the connection |
| 3136 | * to the server, discard the received response, and send a 502 (Bad |
| 3137 | * Gateway) response to the client. If this is a response message |
| 3138 | * received by a user agent, the user agent MUST close the |
| 3139 | * connection to the server and discard the received response. |
| 3140 | * |
| 3141 | * 5. If a valid Content-Length header field is present without |
| 3142 | * Transfer-Encoding, its decimal value defines the expected message |
| 3143 | * body length in octets. If the sender closes the connection or |
| 3144 | * the recipient times out before the indicated number of octets are |
| 3145 | * received, the recipient MUST consider the message to be |
| 3146 | * incomplete and close the connection. |
| 3147 | * |
| 3148 | * 6. If this is a request message and none of the above are true, then |
| 3149 | * the message body length is zero (no message body is present). |
| 3150 | * |
| 3151 | * 7. Otherwise, this is a response message without a declared message |
| 3152 | * body length, so the message body length is determined by the |
| 3153 | * number of octets received prior to the server closing the |
| 3154 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3155 | */ |
| 3156 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3157 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3158 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 3159 | 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] | 3160 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3161 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 3162 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3163 | /* chunked not last, return badreq */ |
| 3164 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3165 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3166 | } |
| 3167 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3168 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3169 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3170 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 3171 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 3172 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 3173 | } |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3174 | 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] | 3175 | signed long long cl; |
| 3176 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3177 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3178 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3179 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3180 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3181 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3182 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3183 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3184 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3185 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3186 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3187 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3188 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3189 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3190 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3191 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3192 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3193 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3194 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3195 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3196 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3197 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3198 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3199 | } |
| 3200 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3201 | /* even bodyless requests have a known length */ |
| 3202 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3203 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3204 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 3205 | * only change if both the request and the config reference something else. |
| 3206 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3207 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3208 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3209 | * if FE and BE have the same settings (common). The method consists in |
| 3210 | * checking if options changed between the two calls (implying that either |
| 3211 | * one is non-null, or one of them is non-null and we are there for the first |
| 3212 | * time. |
| 3213 | */ |
| 3214 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3215 | ((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] | 3216 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3217 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 3218 | /* we may have to wait for the request's body */ |
| 3219 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 3220 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 3221 | req->analysers |= AN_REQ_HTTP_BODY; |
| 3222 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3223 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3224 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3225 | req->analyse_exp = TICK_ETERNITY; |
| 3226 | return 1; |
| 3227 | |
| 3228 | return_bad_req: |
| 3229 | /* We centralize bad requests processing here */ |
| 3230 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3231 | /* we detected a parsing error. We want to archive this request |
| 3232 | * in the dedicated proxy area for later troubleshooting. |
| 3233 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3234 | 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] | 3235 | } |
| 3236 | |
| 3237 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3238 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3239 | 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] | 3240 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3241 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3242 | if (sess->listener->counters) |
| 3243 | sess->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3244 | |
| 3245 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3246 | if (!(s->flags & SF_ERR_MASK)) |
| 3247 | s->flags |= SF_ERR_PRXCOND; |
| 3248 | if (!(s->flags & SF_FINST_MASK)) |
| 3249 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3250 | |
| 3251 | req->analysers = 0; |
| 3252 | req->analyse_exp = TICK_ETERNITY; |
| 3253 | return 0; |
| 3254 | } |
| 3255 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3256 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3257 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3258 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3259 | * and program a response message if something was unexpected. It cannot fail |
| 3260 | * 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] | 3261 | * 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] | 3262 | * 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] | 3263 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3264 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3265 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3266 | { |
| 3267 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3268 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3269 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3270 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3271 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3272 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3273 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3274 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3275 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3276 | appctx = si_appctx(si); |
| 3277 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3278 | appctx->st1 = appctx->st2 = 0; |
| 3279 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3280 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3281 | 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] | 3282 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3283 | |
| 3284 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3285 | lookup = uri + uri_auth->uri_len; |
| 3286 | |
| 3287 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3288 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3289 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3290 | break; |
| 3291 | } |
| 3292 | } |
| 3293 | |
| 3294 | if (uri_auth->refresh) { |
| 3295 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3296 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3297 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3298 | break; |
| 3299 | } |
| 3300 | } |
| 3301 | } |
| 3302 | |
| 3303 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3304 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3305 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3306 | break; |
| 3307 | } |
| 3308 | } |
| 3309 | |
| 3310 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3311 | if (memcmp(h, ";st=", 4) == 0) { |
| 3312 | int i; |
| 3313 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3314 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3315 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3316 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3317 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3318 | break; |
| 3319 | } |
| 3320 | } |
| 3321 | break; |
| 3322 | } |
| 3323 | } |
| 3324 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3325 | appctx->ctx.stats.scope_str = 0; |
| 3326 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3327 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3328 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3329 | int itx = 0; |
| 3330 | const char *h2; |
| 3331 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3332 | const char *err; |
| 3333 | |
| 3334 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3335 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3336 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3337 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3338 | itx++; |
| 3339 | h++; |
| 3340 | } |
| 3341 | |
| 3342 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3343 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3344 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3345 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3346 | /* 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] | 3347 | memcpy(scope_txt, h2, itx); |
| 3348 | scope_txt[itx] = '\0'; |
| 3349 | err = invalid_char(scope_txt); |
| 3350 | if (err) { |
| 3351 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3352 | appctx->ctx.stats.scope_str = 0; |
| 3353 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3354 | } |
| 3355 | break; |
| 3356 | } |
| 3357 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3358 | |
| 3359 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3360 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3361 | int ret = 1; |
| 3362 | |
| 3363 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3364 | 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] | 3365 | ret = acl_pass(ret); |
| 3366 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3367 | ret = !ret; |
| 3368 | } |
| 3369 | |
| 3370 | if (ret) { |
| 3371 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3372 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3373 | break; |
| 3374 | } |
| 3375 | } |
| 3376 | |
| 3377 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3378 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3379 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3380 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3381 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 3382 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3383 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3384 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3385 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3386 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3387 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3388 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3389 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3390 | else { |
| 3391 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3392 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3393 | } |
| 3394 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3395 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3396 | return 1; |
| 3397 | } |
| 3398 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3399 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3400 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3401 | */ |
Thierry FOURNIER | 7fe75e0 | 2015-03-16 12:03:44 +0100 | [diff] [blame] | 3402 | void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3403 | { |
| 3404 | #ifdef IP_TOS |
| 3405 | if (from.ss_family == AF_INET) |
| 3406 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3407 | #endif |
| 3408 | #ifdef IPV6_TCLASS |
| 3409 | if (from.ss_family == AF_INET6) { |
| 3410 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3411 | /* v4-mapped addresses need IP_TOS */ |
| 3412 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3413 | else |
| 3414 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3415 | } |
| 3416 | #endif |
| 3417 | } |
| 3418 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3419 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3420 | const char* name, unsigned int name_len, |
| 3421 | const char *str, struct my_regex *re, |
| 3422 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3423 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3424 | struct hdr_ctx ctx; |
| 3425 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3426 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3427 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 3428 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3429 | struct chunk *output = get_trash_chunk(); |
| 3430 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3431 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3432 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3433 | /* Choose the header browsing function. */ |
| 3434 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3435 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3436 | http_find_hdr_func = http_find_header2; |
| 3437 | break; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3438 | case ACT_HTTP_REPLACE_HDR: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3439 | http_find_hdr_func = http_find_full_header2; |
| 3440 | break; |
| 3441 | default: /* impossible */ |
| 3442 | return -1; |
| 3443 | } |
| 3444 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3445 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 3446 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3447 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3448 | char *val = ctx.line + ctx.val; |
| 3449 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3450 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3451 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 3452 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3453 | |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3454 | output->len = exp_replace(output->str, output->size, val, str, pmatch); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3455 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3456 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3457 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3458 | 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] | 3459 | |
| 3460 | hdr->len += delta; |
| 3461 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3462 | |
| 3463 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3464 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3465 | } |
| 3466 | |
| 3467 | return 0; |
| 3468 | } |
| 3469 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3470 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3471 | const char* name, unsigned int name_len, |
| 3472 | struct list *fmt, struct my_regex *re, |
| 3473 | int action) |
| 3474 | { |
| 3475 | struct chunk *replace = get_trash_chunk(); |
| 3476 | |
| 3477 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 3478 | if (replace->len >= replace->size - 1) |
| 3479 | return -1; |
| 3480 | |
| 3481 | return http_transform_header_str(s, msg, name, name_len, replace->str, re, action); |
| 3482 | } |
| 3483 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3484 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3485 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3486 | * further processing of the request (auth, deny, ...), and defaults to |
| 3487 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3488 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 3489 | * on txn->flags if it encounters a tarpit rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3490 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3491 | enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3492 | 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] | 3493 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3494 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3495 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3496 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3497 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3498 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3499 | const char *auth_realm; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3500 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3501 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3502 | * resume condition. If a resume is needed it is always in the action |
| 3503 | * and never in the ACL or converters. In this case, we initialise the |
| 3504 | * current rule, and go to the action execution point. |
| 3505 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3506 | if (s->current_rule) { |
| 3507 | rule = s->current_rule; |
| 3508 | s->current_rule = NULL; |
| 3509 | if (s->current_rule_list == rules) |
| 3510 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3511 | } |
| 3512 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3513 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3514 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3515 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3516 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3517 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3518 | int ret; |
| 3519 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3520 | 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] | 3521 | ret = acl_pass(ret); |
| 3522 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3523 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3524 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3525 | |
| 3526 | if (!ret) /* condition not matched */ |
| 3527 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3528 | } |
| 3529 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3530 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3531 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3532 | case ACT_ACTION_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3533 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3534 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3535 | case ACT_ACTION_DENY: |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 3536 | txn->rule_deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3537 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3538 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3539 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3540 | txn->flags |= TX_CLTARPIT; |
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 | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3543 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3544 | case ACT_HTTP_REQ_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3545 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3546 | auth_realm = rule->arg.auth.realm; |
| 3547 | if (!auth_realm) { |
| 3548 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3549 | auth_realm = STATS_DEFAULT_REALM; |
| 3550 | else |
| 3551 | auth_realm = px->id; |
| 3552 | } |
| 3553 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3554 | * count one error, because normal browsing won't significantly |
| 3555 | * increase the counter but brute force attempts will. |
| 3556 | */ |
| 3557 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3558 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
| 3559 | stream_int_retnclose(&s->si[0], &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3560 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3561 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3562 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3563 | case ACT_HTTP_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3564 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3565 | return HTTP_RULE_RES_BADREQ; |
| 3566 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3567 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3568 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3569 | s->task->nice = rule->arg.nice; |
| 3570 | break; |
| 3571 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3572 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3573 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3574 | 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] | 3575 | break; |
| 3576 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3577 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3578 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3579 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3580 | 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] | 3581 | #endif |
| 3582 | break; |
| 3583 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3584 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3585 | s->logs.level = rule->arg.loglevel; |
| 3586 | break; |
| 3587 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3588 | case ACT_HTTP_REPLACE_HDR: |
| 3589 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3590 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 3591 | rule->arg.hdr_add.name_len, |
| 3592 | &rule->arg.hdr_add.fmt, |
| 3593 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3594 | return HTTP_RULE_RES_BADREQ; |
| 3595 | break; |
| 3596 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3597 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3598 | ctx.idx = 0; |
| 3599 | /* remove all occurrences of the header */ |
| 3600 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3601 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3602 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3603 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3604 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3605 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3606 | case ACT_HTTP_SET_HDR: |
| 3607 | case ACT_HTTP_ADD_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3608 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3609 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3610 | trash.len = rule->arg.hdr_add.name_len; |
| 3611 | trash.str[trash.len++] = ':'; |
| 3612 | trash.str[trash.len++] = ' '; |
| 3613 | 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] | 3614 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3615 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3616 | /* remove all occurrences of the header */ |
| 3617 | ctx.idx = 0; |
| 3618 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3619 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3620 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3621 | } |
| 3622 | } |
| 3623 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3624 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3625 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3626 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3627 | case ACT_HTTP_DEL_ACL: |
| 3628 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3629 | struct pat_ref *ref; |
| 3630 | char *key; |
| 3631 | int len; |
| 3632 | |
| 3633 | /* collect reference */ |
| 3634 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3635 | if (!ref) |
| 3636 | continue; |
| 3637 | |
| 3638 | /* collect key */ |
| 3639 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3640 | key = trash.str; |
| 3641 | key[len] = '\0'; |
| 3642 | |
| 3643 | /* perform update */ |
| 3644 | /* returned code: 1=ok, 0=ko */ |
| 3645 | pat_ref_delete(ref, key); |
| 3646 | |
| 3647 | break; |
| 3648 | } |
| 3649 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3650 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3651 | struct pat_ref *ref; |
| 3652 | char *key; |
| 3653 | struct chunk *trash_key; |
| 3654 | int len; |
| 3655 | |
| 3656 | trash_key = get_trash_chunk(); |
| 3657 | |
| 3658 | /* collect reference */ |
| 3659 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3660 | if (!ref) |
| 3661 | continue; |
| 3662 | |
| 3663 | /* collect key */ |
| 3664 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3665 | key = trash_key->str; |
| 3666 | key[len] = '\0'; |
| 3667 | |
| 3668 | /* perform update */ |
| 3669 | /* add entry only if it does not already exist */ |
| 3670 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3671 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3672 | |
| 3673 | break; |
| 3674 | } |
| 3675 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3676 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3677 | struct pat_ref *ref; |
| 3678 | char *key, *value; |
| 3679 | struct chunk *trash_key, *trash_value; |
| 3680 | int len; |
| 3681 | |
| 3682 | trash_key = get_trash_chunk(); |
| 3683 | trash_value = get_trash_chunk(); |
| 3684 | |
| 3685 | /* collect reference */ |
| 3686 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3687 | if (!ref) |
| 3688 | continue; |
| 3689 | |
| 3690 | /* collect key */ |
| 3691 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3692 | key = trash_key->str; |
| 3693 | key[len] = '\0'; |
| 3694 | |
| 3695 | /* collect value */ |
| 3696 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3697 | value = trash_value->str; |
| 3698 | value[len] = '\0'; |
| 3699 | |
| 3700 | /* perform update */ |
| 3701 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3702 | /* update entry if it exists */ |
| 3703 | pat_ref_set(ref, key, value, NULL); |
| 3704 | else |
| 3705 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3706 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3707 | |
| 3708 | break; |
| 3709 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3710 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3711 | case ACT_CUSTOM: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3712 | switch (rule->action_ptr(rule, px, s->sess, s)) { |
| 3713 | case ACT_RET_ERR: |
| 3714 | case ACT_RET_CONT: |
| 3715 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3716 | case ACT_RET_STOP: |
| 3717 | return HTTP_RULE_RES_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3718 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3719 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3720 | return HTTP_RULE_RES_YIELD; |
| 3721 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3722 | break; |
| 3723 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3724 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3725 | /* Note: only the first valid tracking parameter of each |
| 3726 | * applies. |
| 3727 | */ |
| 3728 | |
| 3729 | if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) { |
| 3730 | struct stktable *t; |
| 3731 | struct stksess *ts; |
| 3732 | struct stktable_key *key; |
| 3733 | void *ptr; |
| 3734 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 3735 | t = rule->arg.trk_ctr.table.t; |
| 3736 | 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] | 3737 | |
| 3738 | if (key && (ts = stktable_get_entry(t, key))) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3739 | 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] | 3740 | |
| 3741 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3742 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3743 | if (ptr) |
| 3744 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3745 | |
| 3746 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3747 | if (ptr) |
| 3748 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3749 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3750 | |
| 3751 | 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] | 3752 | if (sess->fe != s->be) |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3753 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3754 | } |
| 3755 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3756 | break; |
| 3757 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3758 | case ACT_HTTP_REQ_SET_SRC: |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3759 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) { |
| 3760 | struct sample *smp; |
| 3761 | |
Thierry FOURNIER | a002dc9 | 2015-07-31 08:50:51 +0200 | [diff] [blame] | 3762 | 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] | 3763 | |
| 3764 | if (smp) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3765 | if (smp->data.type == SMP_T_IPV4) { |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3766 | ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3767 | ((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] | 3768 | ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3769 | } else if (smp->data.type == SMP_T_IPV6) { |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3770 | ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3771 | 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] | 3772 | ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0; |
| 3773 | } |
| 3774 | } |
| 3775 | } |
| 3776 | break; |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3777 | |
| 3778 | /* other flags exists, but normaly, they never be matched. */ |
| 3779 | default: |
| 3780 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3781 | } |
| 3782 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3783 | |
| 3784 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3785 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3786 | } |
| 3787 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3788 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3789 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 3790 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 3791 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 3792 | * is returned, the process can continue the evaluation of next rule list. If |
| 3793 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 3794 | * is returned, it means the operation could not be processed and a server error |
| 3795 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 3796 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 3797 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3798 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3799 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3800 | 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] | 3801 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3802 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3803 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3804 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3805 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3806 | struct hdr_ctx ctx; |
| 3807 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3808 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3809 | * resume condition. If a resume is needed it is always in the action |
| 3810 | * and never in the ACL or converters. In this case, we initialise the |
| 3811 | * current rule, and go to the action execution point. |
| 3812 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3813 | if (s->current_rule) { |
| 3814 | rule = s->current_rule; |
| 3815 | s->current_rule = NULL; |
| 3816 | if (s->current_rule_list == rules) |
| 3817 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3818 | } |
| 3819 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3820 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3821 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3822 | |
| 3823 | /* check optional condition */ |
| 3824 | if (rule->cond) { |
| 3825 | int ret; |
| 3826 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3827 | 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] | 3828 | ret = acl_pass(ret); |
| 3829 | |
| 3830 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3831 | ret = !ret; |
| 3832 | |
| 3833 | if (!ret) /* condition not matched */ |
| 3834 | continue; |
| 3835 | } |
| 3836 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3837 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3838 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3839 | case ACT_ACTION_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3840 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3841 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3842 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3843 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3844 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3845 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3846 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3847 | s->task->nice = rule->arg.nice; |
| 3848 | break; |
| 3849 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3850 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3851 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3852 | 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] | 3853 | break; |
| 3854 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3855 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3856 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3857 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3858 | 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] | 3859 | #endif |
| 3860 | break; |
| 3861 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3862 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3863 | s->logs.level = rule->arg.loglevel; |
| 3864 | break; |
| 3865 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3866 | case ACT_HTTP_REPLACE_HDR: |
| 3867 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3868 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 3869 | rule->arg.hdr_add.name_len, |
| 3870 | &rule->arg.hdr_add.fmt, |
| 3871 | &rule->arg.hdr_add.re, rule->action)) |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3872 | return HTTP_RULE_RES_STOP; /* note: we should report an error here */ |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3873 | break; |
| 3874 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3875 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3876 | ctx.idx = 0; |
| 3877 | /* remove all occurrences of the header */ |
| 3878 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3879 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3880 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3881 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3882 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3883 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3884 | case ACT_HTTP_SET_HDR: |
| 3885 | case ACT_HTTP_ADD_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3886 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3887 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3888 | trash.len = rule->arg.hdr_add.name_len; |
| 3889 | trash.str[trash.len++] = ':'; |
| 3890 | trash.str[trash.len++] = ' '; |
| 3891 | 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] | 3892 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3893 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3894 | /* remove all occurrences of the header */ |
| 3895 | ctx.idx = 0; |
| 3896 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3897 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3898 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3899 | } |
| 3900 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3901 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3902 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3903 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3904 | case ACT_HTTP_DEL_ACL: |
| 3905 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3906 | struct pat_ref *ref; |
| 3907 | char *key; |
| 3908 | int len; |
| 3909 | |
| 3910 | /* collect reference */ |
| 3911 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3912 | if (!ref) |
| 3913 | continue; |
| 3914 | |
| 3915 | /* collect key */ |
| 3916 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3917 | key = trash.str; |
| 3918 | key[len] = '\0'; |
| 3919 | |
| 3920 | /* perform update */ |
| 3921 | /* returned code: 1=ok, 0=ko */ |
| 3922 | pat_ref_delete(ref, key); |
| 3923 | |
| 3924 | break; |
| 3925 | } |
| 3926 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3927 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3928 | struct pat_ref *ref; |
| 3929 | char *key; |
| 3930 | struct chunk *trash_key; |
| 3931 | int len; |
| 3932 | |
| 3933 | trash_key = get_trash_chunk(); |
| 3934 | |
| 3935 | /* collect reference */ |
| 3936 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3937 | if (!ref) |
| 3938 | continue; |
| 3939 | |
| 3940 | /* collect key */ |
| 3941 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3942 | key = trash_key->str; |
| 3943 | key[len] = '\0'; |
| 3944 | |
| 3945 | /* perform update */ |
| 3946 | /* check if the entry already exists */ |
| 3947 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3948 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3949 | |
| 3950 | break; |
| 3951 | } |
| 3952 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3953 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3954 | struct pat_ref *ref; |
| 3955 | char *key, *value; |
| 3956 | struct chunk *trash_key, *trash_value; |
| 3957 | int len; |
| 3958 | |
| 3959 | trash_key = get_trash_chunk(); |
| 3960 | trash_value = get_trash_chunk(); |
| 3961 | |
| 3962 | /* collect reference */ |
| 3963 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3964 | if (!ref) |
| 3965 | continue; |
| 3966 | |
| 3967 | /* collect key */ |
| 3968 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3969 | key = trash_key->str; |
| 3970 | key[len] = '\0'; |
| 3971 | |
| 3972 | /* collect value */ |
| 3973 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3974 | value = trash_value->str; |
| 3975 | value[len] = '\0'; |
| 3976 | |
| 3977 | /* perform update */ |
| 3978 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3979 | /* update entry if it exists */ |
| 3980 | pat_ref_set(ref, key, value, NULL); |
| 3981 | else |
| 3982 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3983 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3984 | |
| 3985 | break; |
| 3986 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3987 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3988 | case ACT_HTTP_REDIR: |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3989 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3990 | return HTTP_RULE_RES_BADREQ; |
| 3991 | return HTTP_RULE_RES_DONE; |
| 3992 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3993 | case ACT_CUSTOM: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3994 | switch (rule->action_ptr(rule, px, s->sess, s)) { |
| 3995 | case ACT_RET_ERR: |
| 3996 | case ACT_RET_CONT: |
| 3997 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3998 | case ACT_RET_STOP: |
| 3999 | return HTTP_RULE_RES_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 4000 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 4001 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4002 | return HTTP_RULE_RES_YIELD; |
| 4003 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 4004 | break; |
| 4005 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 4006 | /* other flags exists, but normaly, they never be matched. */ |
| 4007 | default: |
| 4008 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 4009 | } |
| 4010 | } |
| 4011 | |
| 4012 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 4013 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 4014 | } |
| 4015 | |
| 4016 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4017 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 4018 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 4019 | * as too large a request to build a valid response. |
| 4020 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4021 | 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] | 4022 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4023 | struct http_msg *req = &txn->req; |
| 4024 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4025 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4026 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4027 | |
| 4028 | /* build redirect message */ |
| 4029 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 4030 | case 308: |
| 4031 | msg_fmt = HTTP_308; |
| 4032 | break; |
| 4033 | case 307: |
| 4034 | msg_fmt = HTTP_307; |
| 4035 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4036 | case 303: |
| 4037 | msg_fmt = HTTP_303; |
| 4038 | break; |
| 4039 | case 301: |
| 4040 | msg_fmt = HTTP_301; |
| 4041 | break; |
| 4042 | case 302: |
| 4043 | default: |
| 4044 | msg_fmt = HTTP_302; |
| 4045 | break; |
| 4046 | } |
| 4047 | |
| 4048 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 4049 | return 0; |
| 4050 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4051 | location = trash.str + trash.len; |
| 4052 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4053 | switch(rule->type) { |
| 4054 | case REDIRECT_TYPE_SCHEME: { |
| 4055 | const char *path; |
| 4056 | const char *host; |
| 4057 | struct hdr_ctx ctx; |
| 4058 | int pathlen; |
| 4059 | int hostlen; |
| 4060 | |
| 4061 | host = ""; |
| 4062 | hostlen = 0; |
| 4063 | ctx.idx = 0; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4064 | 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] | 4065 | host = ctx.line + ctx.val; |
| 4066 | hostlen = ctx.vlen; |
| 4067 | } |
| 4068 | |
| 4069 | path = http_get_path(txn); |
| 4070 | /* build message using path */ |
| 4071 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4072 | 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] | 4073 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 4074 | int qs = 0; |
| 4075 | while (qs < pathlen) { |
| 4076 | if (path[qs] == '?') { |
| 4077 | pathlen = qs; |
| 4078 | break; |
| 4079 | } |
| 4080 | qs++; |
| 4081 | } |
| 4082 | } |
| 4083 | } else { |
| 4084 | path = "/"; |
| 4085 | pathlen = 1; |
| 4086 | } |
| 4087 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4088 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4089 | /* check if we can add scheme + "://" + host + path */ |
| 4090 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 4091 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4092 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4093 | /* add scheme */ |
| 4094 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4095 | trash.len += rule->rdr_len; |
| 4096 | } |
| 4097 | else { |
| 4098 | /* add scheme with executing log format */ |
| 4099 | 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] | 4100 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4101 | /* check if we can add scheme + "://" + host + path */ |
| 4102 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 4103 | return 0; |
| 4104 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4105 | /* add "://" */ |
| 4106 | memcpy(trash.str + trash.len, "://", 3); |
| 4107 | trash.len += 3; |
| 4108 | |
| 4109 | /* add host */ |
| 4110 | memcpy(trash.str + trash.len, host, hostlen); |
| 4111 | trash.len += hostlen; |
| 4112 | |
| 4113 | /* add path */ |
| 4114 | memcpy(trash.str + trash.len, path, pathlen); |
| 4115 | trash.len += pathlen; |
| 4116 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4117 | /* 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] | 4118 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 4119 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 4120 | if (trash.len > trash.size - 5) |
| 4121 | return 0; |
| 4122 | trash.str[trash.len] = '/'; |
| 4123 | trash.len++; |
| 4124 | } |
| 4125 | |
| 4126 | break; |
| 4127 | } |
| 4128 | case REDIRECT_TYPE_PREFIX: { |
| 4129 | const char *path; |
| 4130 | int pathlen; |
| 4131 | |
| 4132 | path = http_get_path(txn); |
| 4133 | /* build message using path */ |
| 4134 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4135 | 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] | 4136 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 4137 | int qs = 0; |
| 4138 | while (qs < pathlen) { |
| 4139 | if (path[qs] == '?') { |
| 4140 | pathlen = qs; |
| 4141 | break; |
| 4142 | } |
| 4143 | qs++; |
| 4144 | } |
| 4145 | } |
| 4146 | } else { |
| 4147 | path = "/"; |
| 4148 | pathlen = 1; |
| 4149 | } |
| 4150 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4151 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4152 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 4153 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4154 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4155 | /* add prefix. Note that if prefix == "/", we don't want to |
| 4156 | * add anything, otherwise it makes it hard for the user to |
| 4157 | * configure a self-redirection. |
| 4158 | */ |
| 4159 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 4160 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4161 | trash.len += rule->rdr_len; |
| 4162 | } |
| 4163 | } |
| 4164 | else { |
| 4165 | /* add prefix with executing log format */ |
| 4166 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 4167 | |
| 4168 | /* Check length */ |
| 4169 | if (trash.len + pathlen > trash.size - 4) |
| 4170 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4171 | } |
| 4172 | |
| 4173 | /* add path */ |
| 4174 | memcpy(trash.str + trash.len, path, pathlen); |
| 4175 | trash.len += pathlen; |
| 4176 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4177 | /* 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] | 4178 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 4179 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 4180 | if (trash.len > trash.size - 5) |
| 4181 | return 0; |
| 4182 | trash.str[trash.len] = '/'; |
| 4183 | trash.len++; |
| 4184 | } |
| 4185 | |
| 4186 | break; |
| 4187 | } |
| 4188 | case REDIRECT_TYPE_LOCATION: |
| 4189 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4190 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4191 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 4192 | return 0; |
| 4193 | |
| 4194 | /* add location */ |
| 4195 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4196 | trash.len += rule->rdr_len; |
| 4197 | } |
| 4198 | else { |
| 4199 | /* add location with executing log format */ |
| 4200 | 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] | 4201 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4202 | /* Check left length */ |
| 4203 | if (trash.len > trash.size - 4) |
| 4204 | return 0; |
| 4205 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4206 | break; |
| 4207 | } |
| 4208 | |
| 4209 | if (rule->cookie_len) { |
| 4210 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 4211 | trash.len += 14; |
| 4212 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 4213 | trash.len += rule->cookie_len; |
| 4214 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 4215 | trash.len += 2; |
| 4216 | } |
| 4217 | |
| 4218 | /* add end of headers and the keep-alive/close status. |
| 4219 | * We may choose to set keep-alive if the Location begins |
| 4220 | * with a slash, because the client will come back to the |
| 4221 | * same server. |
| 4222 | */ |
| 4223 | txn->status = rule->code; |
| 4224 | /* let's log the request time */ |
| 4225 | s->logs.tv_request = now; |
| 4226 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4227 | if (*location == '/' && |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4228 | (req->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 2de8a50 | 2015-05-28 17:23:54 +0200 | [diff] [blame] | 4229 | ((!(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] | 4230 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4231 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4232 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4233 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4234 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4235 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 4236 | trash.len += 30; |
| 4237 | } else { |
| 4238 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 4239 | trash.len += 24; |
| 4240 | } |
| 4241 | } |
| 4242 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 4243 | trash.len += 4; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4244 | bo_inject(res->chn, trash.str, trash.len); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4245 | /* "eat" the request */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4246 | bi_fast_delete(req->chn->buf, req->sov); |
| 4247 | req->next -= req->sov; |
| 4248 | req->sov = 0; |
| 4249 | s->req.analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4250 | s->res.analysers = AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4251 | req->msg_state = HTTP_MSG_CLOSED; |
| 4252 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 4253 | /* Trim any possible response */ |
| 4254 | res->chn->buf->i = 0; |
| 4255 | res->next = res->sov = 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4256 | } else { |
| 4257 | /* keep-alive not possible */ |
| 4258 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4259 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 4260 | trash.len += 29; |
| 4261 | } else { |
| 4262 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 4263 | trash.len += 23; |
| 4264 | } |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4265 | stream_int_retnclose(&s->si[0], &trash); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4266 | req->chn->analysers = 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4267 | } |
| 4268 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4269 | if (!(s->flags & SF_ERR_MASK)) |
| 4270 | s->flags |= SF_ERR_LOCAL; |
| 4271 | if (!(s->flags & SF_FINST_MASK)) |
| 4272 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4273 | |
| 4274 | return 1; |
| 4275 | } |
| 4276 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4277 | /* This stream analyser runs all HTTP request processing which is common to |
| 4278 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 4279 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4280 | * 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] | 4281 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 4282 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4283 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4284 | 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] | 4285 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4286 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4287 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4288 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4289 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4290 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4291 | enum rule_result verdict; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4292 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4293 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4294 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4295 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4296 | } |
| 4297 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4298 | 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] | 4299 | now_ms, __FUNCTION__, |
| 4300 | s, |
| 4301 | req, |
| 4302 | req->rex, req->wex, |
| 4303 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4304 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4305 | req->analysers); |
| 4306 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4307 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4308 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4309 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4310 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4311 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4312 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 4313 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4314 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4315 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 4316 | goto return_prx_yield; |
| 4317 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4318 | case HTTP_RULE_RES_CONT: |
| 4319 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4320 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4321 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4322 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4323 | if (txn->flags & TX_CLTARPIT) |
| 4324 | goto tarpit; |
| 4325 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4326 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4327 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4328 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4329 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4330 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4331 | goto done; |
| 4332 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4333 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4334 | goto return_bad_req; |
| 4335 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4336 | } |
| 4337 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4338 | /* OK at this stage, we know that the request was accepted according to |
| 4339 | * the http-request rules, we can check for the stats. Note that the |
| 4340 | * URI is detected *before* the req* rules in order not to be affected |
| 4341 | * by a possible reqrep, while they are processed *after* so that a |
| 4342 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4343 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4344 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4345 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4346 | 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] | 4347 | txn->status = 500; |
| 4348 | s->logs.tv_request = now; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4349 | 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] | 4350 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4351 | if (!(s->flags & SF_ERR_MASK)) |
| 4352 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4353 | goto return_prx_cond; |
| 4354 | } |
| 4355 | |
| 4356 | /* parse the whole stats request and extract the relevant information */ |
| 4357 | http_handle_stats(s, req); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4358 | 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] | 4359 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4360 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4361 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4362 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4363 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4364 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4365 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4366 | } |
| 4367 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4368 | /* evaluate the req* rules except reqadd */ |
| 4369 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4370 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4371 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4372 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4373 | if (txn->flags & TX_CLDENY) |
| 4374 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4375 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4376 | if (txn->flags & TX_CLTARPIT) |
| 4377 | goto tarpit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4378 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4379 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4380 | /* add request headers from the rule sets in the same order */ |
| 4381 | list_for_each_entry(wl, &px->req_add, list) { |
| 4382 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4383 | 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] | 4384 | ret = acl_pass(ret); |
| 4385 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4386 | ret = !ret; |
| 4387 | if (!ret) |
| 4388 | continue; |
| 4389 | } |
| 4390 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4391 | 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] | 4392 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4393 | } |
| 4394 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4395 | |
| 4396 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4397 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4398 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4399 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4400 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4401 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4402 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 4403 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 4404 | if (!(s->flags & SF_FINST_MASK)) |
| 4405 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4406 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4407 | /* we may want to compress the stats page */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4408 | if (sess->fe->comp || s->be->comp) |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4409 | select_compression_request_header(s, req->buf); |
| 4410 | |
| 4411 | /* 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] | 4412 | 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] | 4413 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4414 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4415 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4416 | /* check whether we have some ACLs set to redirect this request */ |
| 4417 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4418 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4419 | int ret; |
| 4420 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4421 | 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] | 4422 | ret = acl_pass(ret); |
| 4423 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4424 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4425 | if (!ret) |
| 4426 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4427 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4428 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4429 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4430 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4431 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4432 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4433 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4434 | * If this happens, then the data will not come immediately, so we must |
| 4435 | * send all what we have without waiting. Note that due to the small gain |
| 4436 | * 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] | 4437 | * 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] | 4438 | * itself once used. |
| 4439 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4440 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4441 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4442 | done: /* done with this analyser, continue with next ones that the calling |
| 4443 | * points will have set, if any. |
| 4444 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4445 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4446 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4447 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4448 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4449 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4450 | tarpit: |
| 4451 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4452 | * which may be the same as the connect timeout if unspecified. |
| 4453 | * If unset, then set it to zero because we really want it to |
| 4454 | * eventually expire. We build the tarpit as an analyser. |
| 4455 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4456 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4457 | |
| 4458 | /* wipe the request out so that we can drop the connection early |
| 4459 | * if the client closes first. |
| 4460 | */ |
| 4461 | channel_dont_connect(req); |
| 4462 | req->analysers = 0; /* remove switching rules etc... */ |
| 4463 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4464 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4465 | if (!req->analyse_exp) |
| 4466 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4467 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4468 | sess->fe->fe_counters.denied_req++; |
| 4469 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4470 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4471 | if (sess->listener->counters) |
| 4472 | sess->listener->counters->denied_req++; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4473 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4474 | |
| 4475 | deny: /* this request was blocked (denied) */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4476 | txn->flags |= TX_CLDENY; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 4477 | txn->status = http_err_codes[txn->rule_deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4478 | s->logs.tv_request = now; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 4479 | 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] | 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++; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4486 | goto return_prx_cond; |
| 4487 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4488 | return_bad_req: |
| 4489 | /* We centralize bad requests processing here */ |
| 4490 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4491 | /* we detected a parsing error. We want to archive this request |
| 4492 | * in the dedicated proxy area for later troubleshooting. |
| 4493 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4494 | 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] | 4495 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4496 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4497 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4498 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4499 | 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] | 4500 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4501 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4502 | if (sess->listener->counters) |
| 4503 | sess->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4504 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4505 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4506 | if (!(s->flags & SF_ERR_MASK)) |
| 4507 | s->flags |= SF_ERR_PRXCOND; |
| 4508 | if (!(s->flags & SF_FINST_MASK)) |
| 4509 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4510 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4511 | req->analysers = 0; |
| 4512 | req->analyse_exp = TICK_ETERNITY; |
| 4513 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4514 | |
| 4515 | return_prx_yield: |
| 4516 | channel_dont_connect(req); |
| 4517 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4518 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4519 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4520 | /* This function performs all the processing enabled for the current request. |
| 4521 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4522 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4523 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4524 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4525 | 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] | 4526 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4527 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4528 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4529 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 4530 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4531 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4532 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4533 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4534 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4535 | return 0; |
| 4536 | } |
| 4537 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4538 | 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] | 4539 | now_ms, __FUNCTION__, |
| 4540 | s, |
| 4541 | req, |
| 4542 | req->rex, req->wex, |
| 4543 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4544 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4545 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4546 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4547 | if (sess->fe->comp || s->be->comp) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 4548 | select_compression_request_header(s, req->buf); |
| 4549 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4550 | /* |
| 4551 | * Right now, we know that we have processed the entire headers |
| 4552 | * and that unwanted requests have been filtered out. We can do |
| 4553 | * whatever we want with the remaining request. Also, now we |
| 4554 | * may have separate values for ->fe, ->be. |
| 4555 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4556 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4557 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4558 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4559 | * incoming request. Note that this requires that a connection is |
| 4560 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4561 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4562 | 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] | 4563 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4564 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4565 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4566 | /* Note that for now we don't reuse existing proxy connections */ |
Willy Tarreau | 973a542 | 2015-08-05 21:47:23 +0200 | [diff] [blame] | 4567 | if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4568 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4569 | txn->status = 500; |
| 4570 | req->analysers = 0; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4571 | 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] | 4572 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4573 | if (!(s->flags & SF_ERR_MASK)) |
| 4574 | s->flags |= SF_ERR_RESOURCE; |
| 4575 | if (!(s->flags & SF_FINST_MASK)) |
| 4576 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4577 | |
| 4578 | return 0; |
| 4579 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4580 | |
| 4581 | path = http_get_path(txn); |
| 4582 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4583 | 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] | 4584 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4585 | /* if the path was found, we have to remove everything between |
| 4586 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4587 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4588 | * u_l characters by a single "/". |
| 4589 | */ |
| 4590 | if (path) { |
| 4591 | char *cur_ptr = req->buf->p; |
| 4592 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4593 | int delta; |
| 4594 | |
| 4595 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4596 | http_msg_move_end(&txn->req, delta); |
| 4597 | cur_end += delta; |
| 4598 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4599 | goto return_bad_req; |
| 4600 | } |
| 4601 | else { |
| 4602 | char *cur_ptr = req->buf->p; |
| 4603 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4604 | int delta; |
| 4605 | |
| 4606 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4607 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4608 | http_msg_move_end(&txn->req, delta); |
| 4609 | cur_end += delta; |
| 4610 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4611 | goto return_bad_req; |
| 4612 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4613 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4614 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4615 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4616 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4617 | * Note that doing so might move headers in the request, but |
| 4618 | * the fields will stay coherent and the URI will not move. |
| 4619 | * This should only be performed in the backend. |
| 4620 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 4621 | if ((s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4622 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 4623 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4624 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4625 | /* add unique-id if "header-unique-id" is specified */ |
| 4626 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4627 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4628 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4629 | goto return_bad_req; |
| 4630 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4631 | 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] | 4632 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4633 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4634 | if (sess->fe->header_unique_id && s->unique_id) { |
| 4635 | 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] | 4636 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4637 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4638 | 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] | 4639 | goto return_bad_req; |
| 4640 | } |
| 4641 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4642 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4643 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4644 | * asks for it. |
| 4645 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4646 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4647 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4648 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 4649 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 4650 | 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] | 4651 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4652 | /* The header is set to be added only if none is present |
| 4653 | * and we found it, so don't do anything. |
| 4654 | */ |
| 4655 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4656 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4657 | /* Add an X-Forwarded-For header unless the source IP is |
| 4658 | * in the 'except' network range. |
| 4659 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4660 | if ((!sess->fe->except_mask.s_addr || |
| 4661 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 4662 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4663 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4664 | (((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] | 4665 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4666 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4667 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4668 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4669 | |
| 4670 | /* Note: we rely on the backend to get the header name to be used for |
| 4671 | * x-forwarded-for, because the header is really meant for the backends. |
| 4672 | * However, if the backend did not specify any option, we have to rely |
| 4673 | * on the frontend's header name. |
| 4674 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4675 | if (s->be->fwdfor_hdr_len) { |
| 4676 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4677 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4678 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4679 | len = sess->fe->fwdfor_hdr_len; |
| 4680 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4681 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4682 | 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] | 4683 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4684 | 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] | 4685 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4686 | } |
| 4687 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4688 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4689 | /* FIXME: for the sake of completeness, we should also support |
| 4690 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4691 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4692 | int len; |
| 4693 | char pn[INET6_ADDRSTRLEN]; |
| 4694 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4695 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4696 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4697 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4698 | /* Note: we rely on the backend to get the header name to be used for |
| 4699 | * x-forwarded-for, because the header is really meant for the backends. |
| 4700 | * However, if the backend did not specify any option, we have to rely |
| 4701 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4702 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4703 | if (s->be->fwdfor_hdr_len) { |
| 4704 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4705 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4706 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4707 | len = sess->fe->fwdfor_hdr_len; |
| 4708 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4709 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4710 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4711 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4712 | 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] | 4713 | goto return_bad_req; |
| 4714 | } |
| 4715 | } |
| 4716 | |
| 4717 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4718 | * 10: add X-Original-To if either the frontend or the backend |
| 4719 | * asks for it. |
| 4720 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4721 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4722 | |
| 4723 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4724 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4725 | /* Add an X-Original-To header unless the destination IP is |
| 4726 | * in the 'except' network range. |
| 4727 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4728 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4729 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4730 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4731 | ((!sess->fe->except_mask_to.s_addr || |
| 4732 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 4733 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4734 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4735 | (((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] | 4736 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4737 | int len; |
| 4738 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4739 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4740 | |
| 4741 | /* Note: we rely on the backend to get the header name to be used for |
| 4742 | * x-original-to, because the header is really meant for the backends. |
| 4743 | * However, if the backend did not specify any option, we have to rely |
| 4744 | * on the frontend's header name. |
| 4745 | */ |
| 4746 | if (s->be->orgto_hdr_len) { |
| 4747 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4748 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4749 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4750 | len = sess->fe->orgto_hdr_len; |
| 4751 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4752 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4753 | 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] | 4754 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4755 | 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] | 4756 | goto return_bad_req; |
| 4757 | } |
| 4758 | } |
| 4759 | } |
| 4760 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4761 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4762 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4763 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4764 | * "Upgrade" is present in the Connection header. |
| 4765 | */ |
| 4766 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4767 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4768 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4769 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4770 | unsigned int want_flags = 0; |
| 4771 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4772 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4773 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4774 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4775 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4776 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4777 | want_flags |= TX_CON_CLO_SET; |
| 4778 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4779 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4780 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4781 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4782 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4783 | want_flags |= TX_CON_KAL_SET; |
| 4784 | } |
| 4785 | |
| 4786 | 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] | 4787 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4788 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4789 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4790 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4791 | /* If we have no server assigned yet and we're balancing on url_param |
| 4792 | * with a POST request, we may be interested in checking the body for |
| 4793 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4794 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4795 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4796 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4797 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4798 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4799 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4800 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4801 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4802 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4803 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4804 | #ifdef TCP_QUICKACK |
| 4805 | /* We expect some data from the client. Unless we know for sure |
| 4806 | * we already have a full request, we have to re-enable quick-ack |
| 4807 | * in case we previously disabled it, otherwise we might cause |
| 4808 | * the client to delay further data. |
| 4809 | */ |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4810 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4811 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4812 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4813 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4814 | 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] | 4815 | #endif |
| 4816 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4817 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4818 | /************************************************************* |
| 4819 | * OK, that's finished for the headers. We have done what we * |
| 4820 | * could. Let's switch to the DATA state. * |
| 4821 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4822 | req->analyse_exp = TICK_ETERNITY; |
| 4823 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4824 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4825 | /* if the server closes the connection, we want to immediately react |
| 4826 | * and close the socket to save packets and syscalls. |
| 4827 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4828 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4829 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4830 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4831 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4832 | /* OK let's go on with the BODY now */ |
| 4833 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4834 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4835 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4836 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4837 | /* we detected a parsing error. We want to archive this request |
| 4838 | * in the dedicated proxy area for later troubleshooting. |
| 4839 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4840 | 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] | 4841 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4842 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4843 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4844 | txn->status = 400; |
| 4845 | req->analysers = 0; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4846 | 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] | 4847 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4848 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4849 | if (sess->listener->counters) |
| 4850 | sess->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4851 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4852 | if (!(s->flags & SF_ERR_MASK)) |
| 4853 | s->flags |= SF_ERR_PRXCOND; |
| 4854 | if (!(s->flags & SF_FINST_MASK)) |
| 4855 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4856 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4857 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4858 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4859 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4860 | * returns zero, at the beginning because it prevents any other processing |
| 4861 | * from occurring, and at the end because it terminates the request. |
| 4862 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4863 | 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] | 4864 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4865 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4866 | |
| 4867 | /* This connection is being tarpitted. The CLIENT side has |
| 4868 | * already set the connect expiration date to the right |
| 4869 | * timeout. We just have to check that the client is still |
| 4870 | * there and that the timeout has not expired. |
| 4871 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4872 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4873 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4874 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4875 | return 0; |
| 4876 | |
| 4877 | /* We will set the queue timer to the time spent, just for |
| 4878 | * logging purposes. We fake a 500 server error, so that the |
| 4879 | * attacker will not suspect his connection has been tarpitted. |
| 4880 | * It will not cause trouble to the logs because we can exclude |
| 4881 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4882 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4883 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4884 | |
| 4885 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4886 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4887 | 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] | 4888 | |
| 4889 | req->analysers = 0; |
| 4890 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4891 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4892 | if (!(s->flags & SF_ERR_MASK)) |
| 4893 | s->flags |= SF_ERR_PRXCOND; |
| 4894 | if (!(s->flags & SF_FINST_MASK)) |
| 4895 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4896 | return 0; |
| 4897 | } |
| 4898 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4899 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4900 | * for either the buffer to be full, or the full advertised contents to have |
| 4901 | * reached the buffer. It must only be called after the standard HTTP request |
| 4902 | * processing has occurred, because it expects the request to be parsed and will |
| 4903 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4904 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4905 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4906 | * 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] | 4907 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4908 | 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] | 4909 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4910 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4911 | struct http_txn *txn = s->txn; |
| 4912 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4913 | |
| 4914 | /* We have to parse the HTTP request body to find any required data. |
| 4915 | * "balance url_param check_post" should have been the only way to get |
| 4916 | * into this. We were brought here after HTTP header analysis, so all |
| 4917 | * related structures are ready. |
| 4918 | */ |
| 4919 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4920 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4921 | /* This is the first call */ |
| 4922 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4923 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4924 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4925 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4926 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4927 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4928 | */ |
| 4929 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4930 | struct hdr_ctx ctx; |
| 4931 | ctx.idx = 0; |
| 4932 | /* Expect is allowed in 1.1, look for it */ |
| 4933 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4934 | 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] | 4935 | bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4936 | } |
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 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4939 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4940 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4941 | /* 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] | 4942 | * req->buf->p still points to the beginning of the message. We |
| 4943 | * must save the body in msg->next because it survives buffer |
| 4944 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4945 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4946 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4947 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4948 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4949 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4950 | else |
| 4951 | msg->msg_state = HTTP_MSG_DATA; |
| 4952 | } |
| 4953 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4954 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4955 | /* 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] | 4956 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4957 | goto missing_data; |
| 4958 | |
| 4959 | /* OK we have everything we need now */ |
| 4960 | goto http_end; |
| 4961 | } |
| 4962 | |
| 4963 | /* OK here we're parsing a chunked-encoded message */ |
| 4964 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4965 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4966 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4967 | * 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] | 4968 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4969 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4970 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4971 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4972 | if (!ret) |
| 4973 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4974 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4975 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4976 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4977 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4978 | } |
| 4979 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4980 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4981 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 4982 | * for at least a whole chunk or the whole content length bytes after |
| 4983 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4984 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4985 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4986 | goto http_end; |
| 4987 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4988 | 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] | 4989 | goto http_end; |
| 4990 | |
| 4991 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4992 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4993 | * we have the maximum we can expect. |
| 4994 | */ |
| 4995 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4996 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4997 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4998 | 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] | 4999 | txn->status = 408; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5000 | 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] | 5001 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5002 | if (!(s->flags & SF_ERR_MASK)) |
| 5003 | s->flags |= SF_ERR_CLITO; |
| 5004 | if (!(s->flags & SF_FINST_MASK)) |
| 5005 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5006 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5007 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5008 | |
| 5009 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 5010 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5011 | /* Not enough data. We'll re-use the http-request |
| 5012 | * timeout here. Ideally, we should set the timeout |
| 5013 | * relative to the accept() date. We just set the |
| 5014 | * request timeout once at the beginning of the |
| 5015 | * request. |
| 5016 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5017 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5018 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 5019 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5020 | return 0; |
| 5021 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5022 | |
| 5023 | http_end: |
| 5024 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 5025 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 5026 | req->analysers &= ~an_bit; |
| 5027 | req->analyse_exp = TICK_ETERNITY; |
| 5028 | return 1; |
| 5029 | |
| 5030 | return_bad_req: /* let's centralize all bad requests */ |
| 5031 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5032 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5033 | 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] | 5034 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5035 | if (!(s->flags & SF_ERR_MASK)) |
| 5036 | s->flags |= SF_ERR_PRXCOND; |
| 5037 | if (!(s->flags & SF_FINST_MASK)) |
| 5038 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5039 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5040 | return_err_msg: |
| 5041 | req->analysers = 0; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5042 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5043 | if (sess->listener->counters) |
| 5044 | sess->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5045 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5046 | } |
| 5047 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5048 | /* send a server's name with an outgoing request over an established connection. |
| 5049 | * Note: this function is designed to be called once the request has been scheduled |
| 5050 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 5051 | * proceeding. |
| 5052 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 5053 | 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] | 5054 | |
| 5055 | struct hdr_ctx ctx; |
| 5056 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5057 | char *hdr_name = be->server_id_hdr_name; |
| 5058 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 5059 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5060 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5061 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5062 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 5063 | ctx.idx = 0; |
| 5064 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 5065 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5066 | if (old_o) { |
| 5067 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5068 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5069 | txn->req.next += old_o; |
| 5070 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5071 | } |
| 5072 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5073 | old_i = chn->buf->i; |
| 5074 | 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] | 5075 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5076 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5077 | } |
| 5078 | |
| 5079 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5080 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5081 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 5082 | hdr_val += hdr_name_len; |
| 5083 | *hdr_val++ = ':'; |
| 5084 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5085 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 5086 | 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] | 5087 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5088 | if (old_o) { |
| 5089 | /* If this was a forwarded request, we must readjust the amount of |
| 5090 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5091 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 5092 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5093 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5094 | old_o += chn->buf->i - old_i; |
| 5095 | b_adv(chn->buf, old_o); |
| 5096 | txn->req.next -= old_o; |
| 5097 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5098 | } |
| 5099 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5100 | return 0; |
| 5101 | } |
| 5102 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5103 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 5104 | * right now but it works. |
| 5105 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5106 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5107 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5108 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 5109 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5110 | struct connection *srv_conn; |
| 5111 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 5112 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5113 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5114 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 5115 | * server's connections. We need a safer way to reinitialize buffer |
| 5116 | * flags. We also need a more accurate method for computing per-request |
| 5117 | * data. |
| 5118 | */ |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5119 | srv_conn = objt_conn(s->si[1].end); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5120 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5121 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 5122 | * to the server. |
| 5123 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5124 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5125 | !si_conn_ready(&s->si[1])) { |
| 5126 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 5127 | si_shutr(&s->si[1]); |
| 5128 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5129 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5130 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5131 | if (s->flags & SF_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5132 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 5133 | if (unlikely(s->srv_conn)) |
| 5134 | sess_change_server(s, NULL); |
| 5135 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5136 | |
| 5137 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5138 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5139 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5140 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5141 | int n; |
| 5142 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5143 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5144 | if (n < 1 || n > 5) |
| 5145 | n = 0; |
| 5146 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5147 | if (fe->mode == PR_MODE_HTTP) { |
| 5148 | fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5149 | if (s->comp_algo && (s->flags & SF_COMP_READY)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5150 | fe->fe_counters.p.http.comp_rsp++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5151 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5152 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5153 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5154 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5155 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5156 | if (s->comp_algo && (s->flags & SF_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5157 | s->be->be_counters.p.http.comp_rsp++; |
| 5158 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5159 | } |
| 5160 | |
| 5161 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5162 | s->logs.bytes_in -= s->req.buf->i; |
| 5163 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5164 | |
| 5165 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5166 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5167 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5168 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5169 | s->do_log(s); |
| 5170 | } |
| 5171 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 5172 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5173 | stream_stop_content_counters(s); |
| 5174 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 5175 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5176 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 5177 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 5178 | tv_zero(&s->logs.tv_request); |
| 5179 | s->logs.t_queue = -1; |
| 5180 | s->logs.t_connect = -1; |
| 5181 | s->logs.t_data = -1; |
| 5182 | s->logs.t_close = 0; |
| 5183 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 5184 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 5185 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5186 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 5187 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5188 | |
| 5189 | if (s->pend_pos) |
| 5190 | pendconn_free(s->pend_pos); |
| 5191 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5192 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5193 | if (s->flags & SF_CURR_SESS) { |
| 5194 | s->flags &= ~SF_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5195 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5196 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5197 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 5198 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5199 | } |
| 5200 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5201 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5202 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5203 | /* only release our endpoint if we don't intend to reuse the |
| 5204 | * connection. |
| 5205 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5206 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5207 | !si_conn_ready(&s->si[1])) { |
| 5208 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5209 | srv_conn = NULL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5210 | } |
| 5211 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5212 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 5213 | s->si[1].err_type = SI_ET_NONE; |
| 5214 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 5215 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5216 | 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] | 5217 | 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); |
| 5218 | 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] | 5219 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 5220 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 5221 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 5222 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5223 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5224 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5225 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5226 | |
| 5227 | if (prev_status == 401 || prev_status == 407) { |
| 5228 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 5229 | * a chance of being able to send the visitor again to the same |
| 5230 | * server over the same connection. This is required by some |
| 5231 | * broken protocols such as NTLM, and anyway whenever there is |
| 5232 | * an opportunity for sending the challenge to the proper place, |
| 5233 | * it's better to do it (at least it helps with debugging). |
| 5234 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5235 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | bd99d58 | 2015-09-02 10:40:43 +0200 | [diff] [blame] | 5236 | if (srv_conn) |
| 5237 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5238 | } |
| 5239 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5240 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5241 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5242 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5243 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5244 | s->req.flags |= CF_NEVER_WAIT; |
| 5245 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 5246 | } |
| 5247 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5248 | /* if the request buffer is not empty, it means we're |
| 5249 | * about to process another request, so send pending |
| 5250 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5251 | * Just don't do this if the buffer is close to be full, |
| 5252 | * because the request will wait for it to flush a little |
| 5253 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5254 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5255 | if (s->req.buf->i) { |
| 5256 | if (s->res.buf->o && |
| 5257 | !buffer_full(s->res.buf, global.tune.maxrewrite) && |
| 5258 | bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite) |
| 5259 | s->res.flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5260 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5261 | |
| 5262 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5263 | channel_auto_read(&s->req); |
| 5264 | channel_auto_close(&s->req); |
| 5265 | channel_auto_read(&s->res); |
| 5266 | channel_auto_close(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5267 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5268 | /* we're in keep-alive with an idle connection, monitor it */ |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 5269 | if (srv_conn) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5270 | srv = objt_server(srv_conn->target); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5271 | if (!srv) |
| 5272 | si_idle_conn(&s->si[1], NULL); |
| 5273 | else if ((srv_conn->flags & CO_FL_PRIVATE) || |
| 5274 | ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)) |
| 5275 | si_idle_conn(&s->si[1], &srv->priv_conns); |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 5276 | else if (prev_flags & TX_NOT_FIRST) |
| 5277 | /* note: we check the request, not the connection, but |
| 5278 | * this is valid for strategies SAFE and AGGR, and in |
| 5279 | * case of ALWS, we don't care anyway. |
| 5280 | */ |
| 5281 | si_idle_conn(&s->si[1], &srv->safe_conns); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5282 | else |
| 5283 | si_idle_conn(&s->si[1], &srv->idle_conns); |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 5284 | } |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5285 | |
Willy Tarreau | c29d0cd | 2015-09-23 12:18:14 +0200 | [diff] [blame] | 5286 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5287 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5288 | } |
| 5289 | |
| 5290 | |
| 5291 | /* This function updates the request state machine according to the response |
| 5292 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5293 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5294 | * it is only used to find when a request/response couple is complete. Both |
| 5295 | * this function and its equivalent should loop until both return zero. It |
| 5296 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5297 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5298 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5299 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5300 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5301 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5302 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5303 | unsigned int old_state = txn->req.msg_state; |
| 5304 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5305 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 5306 | return 0; |
| 5307 | |
| 5308 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5309 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5310 | * We can shut the read side unless we want to abort_on_close, |
| 5311 | * or we have a POST request. The issue with POST requests is |
| 5312 | * that some browsers still send a CRLF after the request, and |
| 5313 | * this CRLF must be read so that it does not remain in the kernel |
| 5314 | * buffers, otherwise a close could cause an RST on some systems |
| 5315 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5316 | * Note that if we're using keep-alive on the client side, we'd |
| 5317 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5318 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5319 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5320 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5321 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5322 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5323 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 5324 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5325 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5326 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5327 | /* if the server closes the connection, we want to immediately react |
| 5328 | * and close the socket to save packets and syscalls. |
| 5329 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5330 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5331 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5332 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5333 | goto wait_other_side; |
| 5334 | |
| 5335 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5336 | /* The server has not finished to respond, so we |
| 5337 | * don't want to move in order not to upset it. |
| 5338 | */ |
| 5339 | goto wait_other_side; |
| 5340 | } |
| 5341 | |
| 5342 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5343 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5344 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5345 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5346 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5347 | goto wait_other_side; |
| 5348 | } |
| 5349 | |
| 5350 | /* When we get here, it means that both the request and the |
| 5351 | * response have finished receiving. Depending on the connection |
| 5352 | * mode, we'll have to wait for the last bytes to leave in either |
| 5353 | * direction, and sometimes for a close to be effective. |
| 5354 | */ |
| 5355 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5356 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5357 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5358 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5359 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5360 | } |
| 5361 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5362 | /* Option forceclose is set, or either side wants to close, |
| 5363 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5364 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5365 | * once both states are CLOSED. |
| 5366 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5367 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5368 | channel_shutr_now(chn); |
| 5369 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5370 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5371 | } |
| 5372 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5373 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5374 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5375 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5376 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5377 | channel_auto_read(chn); |
| 5378 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5379 | chn->flags |= CF_NEVER_WAIT; |
| 5380 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5381 | } |
| 5382 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5383 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5384 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5385 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5386 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5387 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5388 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5389 | goto http_msg_closing; |
| 5390 | } |
| 5391 | else { |
| 5392 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5393 | goto http_msg_closed; |
| 5394 | } |
| 5395 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5396 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5397 | } |
| 5398 | |
| 5399 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5400 | http_msg_closing: |
| 5401 | /* nothing else to forward, just waiting for the output buffer |
| 5402 | * to be empty and for the shutw_now to take effect. |
| 5403 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5404 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5405 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5406 | goto http_msg_closed; |
| 5407 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5408 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5409 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5410 | goto wait_other_side; |
| 5411 | } |
| 5412 | } |
| 5413 | |
| 5414 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5415 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5416 | /* see above in MSG_DONE why we only do this in these states */ |
| 5417 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5418 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5419 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5420 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5421 | goto wait_other_side; |
| 5422 | } |
| 5423 | |
| 5424 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5425 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5426 | } |
| 5427 | |
| 5428 | |
| 5429 | /* This function updates the response state machine according to the request |
| 5430 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5431 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5432 | * it is only used to find when a request/response couple is complete. Both |
| 5433 | * this function and its equivalent should loop until both return zero. It |
| 5434 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5435 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5436 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5437 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5438 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5439 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5440 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5441 | unsigned int old_state = txn->rsp.msg_state; |
| 5442 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5443 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5444 | return 0; |
| 5445 | |
| 5446 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5447 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5448 | * still monitor the server connection for a possible close |
| 5449 | * while the request is being uploaded, so we don't disable |
| 5450 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5451 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5452 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5453 | |
| 5454 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5455 | goto wait_other_side; |
| 5456 | |
| 5457 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5458 | /* The client seems to still be sending data, probably |
| 5459 | * because we got an error response during an upload. |
| 5460 | * We have the choice of either breaking the connection |
| 5461 | * or letting it pass through. Let's do the later. |
| 5462 | */ |
| 5463 | goto wait_other_side; |
| 5464 | } |
| 5465 | |
| 5466 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5467 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5468 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5469 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5470 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5471 | goto wait_other_side; |
| 5472 | } |
| 5473 | |
| 5474 | /* When we get here, it means that both the request and the |
| 5475 | * response have finished receiving. Depending on the connection |
| 5476 | * mode, we'll have to wait for the last bytes to leave in either |
| 5477 | * direction, and sometimes for a close to be effective. |
| 5478 | */ |
| 5479 | |
| 5480 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5481 | /* Server-close mode : shut read and wait for the request |
| 5482 | * side to close its output buffer. The caller will detect |
| 5483 | * when we're in DONE and the other is in CLOSED and will |
| 5484 | * catch that for the final cleanup. |
| 5485 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5486 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5487 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5488 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5489 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5490 | /* Option forceclose is set, or either side wants to close, |
| 5491 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5492 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5493 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5494 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5495 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5496 | channel_shutr_now(chn); |
| 5497 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5498 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5499 | } |
| 5500 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5501 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5502 | * need to forward remaining data. Keep-alive will need to monitor |
| 5503 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5504 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5505 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5506 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5507 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5508 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5509 | } |
| 5510 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5511 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5512 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5513 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5514 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5515 | goto http_msg_closing; |
| 5516 | } |
| 5517 | else { |
| 5518 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5519 | goto http_msg_closed; |
| 5520 | } |
| 5521 | } |
| 5522 | goto wait_other_side; |
| 5523 | } |
| 5524 | |
| 5525 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5526 | http_msg_closing: |
| 5527 | /* nothing else to forward, just waiting for the output buffer |
| 5528 | * to be empty and for the shutw_now to take effect. |
| 5529 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5530 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5531 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5532 | goto http_msg_closed; |
| 5533 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5534 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5535 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5536 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5537 | if (objt_server(s->target)) |
| 5538 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5539 | goto wait_other_side; |
| 5540 | } |
| 5541 | } |
| 5542 | |
| 5543 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5544 | http_msg_closed: |
| 5545 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5546 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5547 | channel_auto_close(chn); |
| 5548 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5549 | goto wait_other_side; |
| 5550 | } |
| 5551 | |
| 5552 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5553 | /* We force the response to leave immediately if we're waiting for the |
| 5554 | * other side, since there is no pending shutdown to push it out. |
| 5555 | */ |
| 5556 | if (!channel_is_empty(chn)) |
| 5557 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5558 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5559 | } |
| 5560 | |
| 5561 | |
| 5562 | /* Resync the request and response state machines. Return 1 if either state |
| 5563 | * changes. |
| 5564 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5565 | int http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5566 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5567 | struct http_txn *txn = s->txn; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5568 | int old_req_state = txn->req.msg_state; |
| 5569 | int old_res_state = txn->rsp.msg_state; |
| 5570 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5571 | http_sync_req_state(s); |
| 5572 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5573 | if (!http_sync_res_state(s)) |
| 5574 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5575 | if (!http_sync_req_state(s)) |
| 5576 | break; |
| 5577 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5578 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5579 | /* OK, both state machines agree on a compatible state. |
| 5580 | * There are a few cases we're interested in : |
| 5581 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5582 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5583 | * directions, so let's simply disable both analysers. |
| 5584 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5585 | * request. |
| 5586 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5587 | * with server-close mode means we've completed one request and we |
| 5588 | * must re-initialize the server connection. |
| 5589 | */ |
| 5590 | |
| 5591 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5592 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5593 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5594 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5595 | s->req.analysers = 0; |
| 5596 | channel_auto_close(&s->req); |
| 5597 | channel_auto_read(&s->req); |
| 5598 | s->res.analysers = 0; |
| 5599 | channel_auto_close(&s->res); |
| 5600 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5601 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5602 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5603 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5604 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5605 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5606 | s->res.analysers = 0; |
| 5607 | channel_auto_close(&s->res); |
| 5608 | channel_auto_read(&s->res); |
| 5609 | s->req.analysers = 0; |
| 5610 | channel_abort(&s->req); |
| 5611 | channel_auto_close(&s->req); |
| 5612 | channel_auto_read(&s->req); |
| 5613 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5614 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5615 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5616 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5617 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5618 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5619 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5620 | /* server-close/keep-alive: terminate this transaction, |
| 5621 | * possibly killing the server connection and reinitialize |
| 5622 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5623 | */ |
| 5624 | http_end_txn_clean_session(s); |
| 5625 | } |
| 5626 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5627 | return txn->req.msg_state != old_req_state || |
| 5628 | txn->rsp.msg_state != old_res_state; |
| 5629 | } |
| 5630 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5631 | /* This function is an analyser which forwards request body (including chunk |
| 5632 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5633 | * zero byte. The only situation where it must not be called is when we're in |
| 5634 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5635 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5636 | * 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] | 5637 | * 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] | 5638 | * 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] | 5639 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5640 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5641 | 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] | 5642 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5643 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5644 | struct http_txn *txn = s->txn; |
| 5645 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5646 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5647 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5648 | return 0; |
| 5649 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5650 | 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] | 5651 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5652 | /* Output closed while we were sending data. We must abort and |
| 5653 | * wake the other side up. |
| 5654 | */ |
| 5655 | msg->msg_state = HTTP_MSG_ERROR; |
| 5656 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5657 | return 1; |
| 5658 | } |
| 5659 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5660 | /* Note that we don't have to send 100-continue back because we don't |
| 5661 | * need the data to complete our job, and it's up to the server to |
| 5662 | * decide whether to return 100, 417 or anything else in return of |
| 5663 | * an "Expect: 100-continue" header. |
| 5664 | */ |
| 5665 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5666 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5667 | /* we have msg->sov which points to the first byte of message |
| 5668 | * body, and req->buf.p still points to the beginning of the |
| 5669 | * message. We forward the headers now, as we don't need them |
| 5670 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5671 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5672 | b_adv(req->buf, msg->sov); |
| 5673 | msg->next -= msg->sov; |
| 5674 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5675 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5676 | /* The previous analysers guarantee that the state is somewhere |
| 5677 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 5678 | * msg->next are always correct. |
| 5679 | */ |
| 5680 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5681 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 5682 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5683 | else |
| 5684 | msg->msg_state = HTTP_MSG_DATA; |
| 5685 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5686 | } |
| 5687 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5688 | /* Some post-connect processing might want us to refrain from starting to |
| 5689 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5690 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5691 | */ |
| 5692 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5693 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5694 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5695 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5696 | goto missing_data; |
| 5697 | } |
| 5698 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5699 | } |
| 5700 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5701 | /* in most states, we should abort in case of early close */ |
| 5702 | channel_auto_close(req); |
| 5703 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5704 | if (req->to_forward) { |
| 5705 | /* We can't process the buffer's contents yet */ |
| 5706 | req->flags |= CF_WAKE_WRITE; |
| 5707 | goto missing_data; |
| 5708 | } |
| 5709 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5710 | while (1) { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5711 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5712 | /* must still forward */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5713 | /* we may have some pending data starting at req->buf->p */ |
| 5714 | if (msg->chunk_len > req->buf->i - msg->next) { |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5715 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5716 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5717 | } |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5718 | msg->next += msg->chunk_len; |
| 5719 | msg->chunk_len = 0; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5720 | |
| 5721 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5722 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5723 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5724 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5725 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5726 | } |
| 5727 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5728 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 5729 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5730 | * TRAILERS state. |
| 5731 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5732 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5733 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5734 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5735 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5736 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5737 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5738 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5739 | 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] | 5740 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5741 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5742 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5743 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5744 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5745 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5746 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5747 | |
| 5748 | if (ret == 0) |
| 5749 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5750 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5751 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5752 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5753 | 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] | 5754 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5755 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5756 | /* we're in MSG_CHUNK_SIZE now */ |
| 5757 | } |
| 5758 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5759 | int ret = http_forward_trailers(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_TRAILERS, 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 HTTP_MSG_DONE now */ |
| 5770 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5771 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5772 | int old_state = msg->msg_state; |
| 5773 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5774 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5775 | |
| 5776 | /* we may have some pending data starting at req->buf->p |
| 5777 | * such as last chunk of data or trailers. |
| 5778 | */ |
| 5779 | b_adv(req->buf, msg->next); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5780 | if (unlikely(!(s->req.flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5781 | msg->sov -= msg->next; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5782 | msg->next = 0; |
| 5783 | |
Willy Tarreau | bbfb6c4 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5784 | /* we don't want to forward closes on DONE except in |
| 5785 | * tunnel mode. |
| 5786 | */ |
| 5787 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5788 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5789 | if (http_resync_states(s)) { |
| 5790 | /* some state changes occurred, maybe the analyser |
| 5791 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 5792 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5793 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5794 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5795 | /* request errors are most likely due to |
| 5796 | * the server aborting the transfer. |
| 5797 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5798 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5799 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5800 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5801 | 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] | 5802 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5803 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5804 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5805 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5806 | |
| 5807 | /* If "option abortonclose" is set on the backend, we |
| 5808 | * want to monitor the client's connection and forward |
| 5809 | * any shutdown notification to the server, which will |
| 5810 | * decide whether to close or to go on processing the |
Willy Tarreau | bbfb6c4 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5811 | * request. We only do that in tunnel mode, and not in |
| 5812 | * other modes since it can be abused to exhaust source |
| 5813 | * ports. |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5814 | */ |
| 5815 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5816 | channel_auto_read(req); |
Willy Tarreau | bbfb6c4 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5817 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 5818 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 5819 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5820 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5821 | } |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5822 | else if (s->txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5823 | /* POST requests may require to read extra CRLF |
| 5824 | * sent by broken browsers and which could cause |
| 5825 | * an RST to be sent upon close on some systems |
| 5826 | * (eg: Linux). |
| 5827 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5828 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5829 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5830 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5831 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5832 | } |
| 5833 | } |
| 5834 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5835 | missing_data: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5836 | /* we may have some pending data starting at req->buf->p */ |
| 5837 | b_adv(req->buf, msg->next); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5838 | if (unlikely(!(s->req.flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5839 | msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i); |
| 5840 | |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5841 | msg->next = 0; |
| 5842 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
| 5843 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5844 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5845 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5846 | if (!(s->flags & SF_ERR_MASK)) |
| 5847 | s->flags |= SF_ERR_CLICL; |
| 5848 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5849 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5850 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5851 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5852 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5853 | } |
| 5854 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5855 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5856 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5857 | if (objt_server(s->target)) |
| 5858 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5859 | |
| 5860 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5861 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5862 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5863 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5864 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5865 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5866 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5867 | /* 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] | 5868 | * 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] | 5869 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5870 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5871 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5872 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5873 | /* 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] | 5874 | * 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] | 5875 | * 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] | 5876 | * modes are already handled by the stream sock layer. We must not do |
| 5877 | * this in content-length mode because it could present the MSG_MORE |
| 5878 | * flag with the last block of forwarded data, which would cause an |
| 5879 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5880 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5881 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5882 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5883 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5884 | return 0; |
| 5885 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5886 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5887 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5888 | if (sess->listener->counters) |
| 5889 | sess->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5890 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5891 | return_bad_req_stats_ok: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5892 | /* we may have some pending data starting at req->buf->p */ |
| 5893 | b_adv(req->buf, msg->next); |
| 5894 | msg->next = 0; |
| 5895 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5896 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5897 | if (txn->status) { |
| 5898 | /* 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] | 5899 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5900 | } else { |
| 5901 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5902 | 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] | 5903 | } |
| 5904 | req->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5905 | 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] | 5906 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5907 | if (!(s->flags & SF_ERR_MASK)) |
| 5908 | s->flags |= SF_ERR_PRXCOND; |
| 5909 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5910 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5911 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5912 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5913 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5914 | } |
| 5915 | return 0; |
| 5916 | |
| 5917 | aborted_xfer: |
| 5918 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5919 | if (txn->status) { |
| 5920 | /* 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] | 5921 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5922 | } else { |
| 5923 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5924 | 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] | 5925 | } |
| 5926 | req->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5927 | 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] | 5928 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5929 | sess->fe->fe_counters.srv_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5930 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5931 | if (objt_server(s->target)) |
| 5932 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5933 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5934 | if (!(s->flags & SF_ERR_MASK)) |
| 5935 | s->flags |= SF_ERR_SRVCL; |
| 5936 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5937 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5938 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5939 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5940 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5941 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5942 | return 0; |
| 5943 | } |
| 5944 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5945 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5946 | * processing can continue on next analysers, or zero if it either needs more |
| 5947 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5948 | * 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] | 5949 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5950 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5951 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5952 | 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] | 5953 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5954 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5955 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5956 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5957 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5958 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5959 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5960 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5961 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5962 | 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] | 5963 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5964 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5965 | rep, |
| 5966 | rep->rex, rep->wex, |
| 5967 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5968 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5969 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5970 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5971 | /* |
| 5972 | * Now parse the partial (or complete) lines. |
| 5973 | * We will check the response syntax, and also join multi-line |
| 5974 | * headers. An index of all the lines will be elaborated while |
| 5975 | * parsing. |
| 5976 | * |
| 5977 | * For the parsing, we use a 28 states FSM. |
| 5978 | * |
| 5979 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5980 | * rep->buf->p = beginning of response |
| 5981 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5982 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5983 | * msg->eol = end of current header or line (LF or CRLF) |
| 5984 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5985 | */ |
| 5986 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5987 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5988 | /* There's a protected area at the end of the buffer for rewriting |
| 5989 | * purposes. We don't want to start to parse the request if the |
| 5990 | * protected area is affected, because we may have to move processed |
| 5991 | * data later, which is much more complicated. |
| 5992 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5993 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5994 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5995 | /* some data has still not left the buffer, wake us once that's done */ |
| 5996 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5997 | goto abort_response; |
| 5998 | channel_dont_close(rep); |
| 5999 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 6000 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 6001 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 6002 | } |
| 6003 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 6004 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 6005 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 6006 | buffer_slow_realign(rep->buf); |
| 6007 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6008 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 6009 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 6010 | } |
| 6011 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6012 | /* 1: we might have to print this header in debug mode */ |
| 6013 | if (unlikely((global.mode & MODE_DEBUG) && |
| 6014 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 6015 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6016 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6017 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6018 | sol = rep->buf->p; |
| 6019 | 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] | 6020 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6021 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6022 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 6023 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6024 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6025 | while (cur_idx) { |
| 6026 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 6027 | debug_hdr("srvhdr", s, sol, eol); |
| 6028 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 6029 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 6030 | } |
| 6031 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6032 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6033 | /* |
| 6034 | * Now we quickly check if we have found a full valid response. |
| 6035 | * If not so, we check the FD and buffer states before leaving. |
| 6036 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6037 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6038 | * responses are checked first. |
| 6039 | * |
| 6040 | * Depending on whether the client is still there or not, we |
| 6041 | * may send an error response back or not. Note that normally |
| 6042 | * we should only check for HTTP status there, and check I/O |
| 6043 | * errors somewhere else. |
| 6044 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6045 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6046 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6047 | /* Invalid response */ |
| 6048 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 6049 | /* we detected a parsing error. We want to archive this response |
| 6050 | * in the dedicated proxy area for later troubleshooting. |
| 6051 | */ |
| 6052 | hdr_response_bad: |
| 6053 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6054 | 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] | 6055 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6056 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6057 | if (objt_server(s->target)) { |
| 6058 | objt_server(s->target)->counters.failed_resp++; |
| 6059 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6060 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 6061 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6062 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6063 | rep->analysers = 0; |
| 6064 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6065 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6066 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6067 | 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] | 6068 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6069 | if (!(s->flags & SF_ERR_MASK)) |
| 6070 | s->flags |= SF_ERR_PRXCOND; |
| 6071 | if (!(s->flags & SF_FINST_MASK)) |
| 6072 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6073 | |
| 6074 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6075 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6076 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6077 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6078 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 6079 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6080 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6081 | goto hdr_response_bad; |
| 6082 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6083 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6084 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6085 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6086 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6087 | 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] | 6088 | else if (txn->flags & TX_NOT_FIRST) |
| 6089 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6090 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6091 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6092 | if (objt_server(s->target)) { |
| 6093 | objt_server(s->target)->counters.failed_resp++; |
| 6094 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6095 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 6096 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6097 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6098 | rep->analysers = 0; |
| 6099 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6100 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6101 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6102 | 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] | 6103 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6104 | if (!(s->flags & SF_ERR_MASK)) |
| 6105 | s->flags |= SF_ERR_SRVCL; |
| 6106 | if (!(s->flags & SF_FINST_MASK)) |
| 6107 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 6108 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6109 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6110 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 6111 | /* read timeout : return a 504 to the client. */ |
| 6112 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6113 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6114 | 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] | 6115 | else if (txn->flags & TX_NOT_FIRST) |
| 6116 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6117 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6118 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6119 | if (objt_server(s->target)) { |
| 6120 | objt_server(s->target)->counters.failed_resp++; |
| 6121 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6122 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6123 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6124 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6125 | rep->analysers = 0; |
| 6126 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6127 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6128 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6129 | 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] | 6130 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6131 | if (!(s->flags & SF_ERR_MASK)) |
| 6132 | s->flags |= SF_ERR_SRVTO; |
| 6133 | if (!(s->flags & SF_FINST_MASK)) |
| 6134 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6135 | return 0; |
| 6136 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 6137 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6138 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6139 | 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] | 6140 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6141 | s->be->be_counters.cli_aborts++; |
| 6142 | if (objt_server(s->target)) |
| 6143 | objt_server(s->target)->counters.cli_aborts++; |
| 6144 | |
| 6145 | rep->analysers = 0; |
| 6146 | channel_auto_close(rep); |
| 6147 | |
| 6148 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6149 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6150 | 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] | 6151 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6152 | if (!(s->flags & SF_ERR_MASK)) |
| 6153 | s->flags |= SF_ERR_CLICL; |
| 6154 | if (!(s->flags & SF_FINST_MASK)) |
| 6155 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6156 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6157 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6158 | return 0; |
| 6159 | } |
| 6160 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6161 | /* 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] | 6162 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6163 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6164 | 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] | 6165 | else if (txn->flags & TX_NOT_FIRST) |
| 6166 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6167 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6168 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6169 | if (objt_server(s->target)) { |
| 6170 | objt_server(s->target)->counters.failed_resp++; |
| 6171 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6172 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6173 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6174 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6175 | rep->analysers = 0; |
| 6176 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6177 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6178 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6179 | 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] | 6180 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6181 | if (!(s->flags & SF_ERR_MASK)) |
| 6182 | s->flags |= SF_ERR_SRVCL; |
| 6183 | if (!(s->flags & SF_FINST_MASK)) |
| 6184 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6185 | return 0; |
| 6186 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6187 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6188 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6189 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6190 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6191 | 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] | 6192 | else if (txn->flags & TX_NOT_FIRST) |
| 6193 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6194 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6195 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6196 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6197 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6198 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6199 | if (!(s->flags & SF_ERR_MASK)) |
| 6200 | s->flags |= SF_ERR_CLICL; |
| 6201 | if (!(s->flags & SF_FINST_MASK)) |
| 6202 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6203 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6204 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6205 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6206 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6207 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6208 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 6209 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6210 | return 0; |
| 6211 | } |
| 6212 | |
| 6213 | /* More interesting part now : we know that we have a complete |
| 6214 | * response which at least looks like HTTP. We have an indicator |
| 6215 | * of each header's length, so we can parse them quickly. |
| 6216 | */ |
| 6217 | |
| 6218 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6219 | 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] | 6220 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6221 | /* |
| 6222 | * 1: get the status code |
| 6223 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6224 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6225 | if (n < 1 || n > 5) |
| 6226 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6227 | /* when the client triggers a 4xx from the server, it's most often due |
| 6228 | * to a missing object or permission. These events should be tracked |
| 6229 | * because if they happen often, it may indicate a brute force or a |
| 6230 | * vulnerability scan. |
| 6231 | */ |
| 6232 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6233 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6234 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6235 | if (objt_server(s->target)) |
| 6236 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6237 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 6238 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 6239 | * exactly one digit "." one digit. This check may be disabled using |
| 6240 | * option accept-invalid-http-response. |
| 6241 | */ |
| 6242 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 6243 | if (msg->sl.st.v_l != 8) { |
| 6244 | msg->err_pos = 0; |
| 6245 | goto hdr_response_bad; |
| 6246 | } |
| 6247 | |
| 6248 | if (rep->buf->p[4] != '/' || |
| 6249 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 6250 | rep->buf->p[6] != '.' || |
| 6251 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 6252 | msg->err_pos = 4; |
| 6253 | goto hdr_response_bad; |
| 6254 | } |
| 6255 | } |
| 6256 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6257 | /* check if the response is HTTP/1.1 or above */ |
| 6258 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6259 | ((rep->buf->p[5] > '1') || |
| 6260 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6261 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6262 | |
| 6263 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 6264 | 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] | 6265 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6266 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6267 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6268 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6269 | 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] | 6270 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 6271 | /* Adjust server's health based on status code. Note: status codes 501 |
| 6272 | * and 505 are triggered on demand by client request, so we must not |
| 6273 | * count them as server failures. |
| 6274 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6275 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6276 | 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] | 6277 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6278 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6279 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6280 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6281 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6282 | /* |
| 6283 | * 2: check for cacheability. |
| 6284 | */ |
| 6285 | |
| 6286 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6287 | case 100: |
| 6288 | /* |
| 6289 | * We may be facing a 100-continue response, in which case this |
| 6290 | * is not the right response, and we're waiting for the next one. |
| 6291 | * Let's allow this response to go to the client and wait for the |
| 6292 | * next one. |
| 6293 | */ |
| 6294 | hdr_idx_init(&txn->hdr_idx); |
| 6295 | msg->next -= channel_forward(rep, msg->next); |
| 6296 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 6297 | txn->status = 0; |
| 6298 | s->logs.t_data = -1; /* was not a response yet */ |
| 6299 | goto next_one; |
| 6300 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6301 | case 200: |
| 6302 | case 203: |
| 6303 | case 206: |
| 6304 | case 300: |
| 6305 | case 301: |
| 6306 | case 410: |
| 6307 | /* RFC2616 @13.4: |
| 6308 | * "A response received with a status code of |
| 6309 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 6310 | * by a cache (...) unless a cache-control |
| 6311 | * directive prohibits caching." |
| 6312 | * |
| 6313 | * RFC2616 @9.5: POST method : |
| 6314 | * "Responses to this method are not cacheable, |
| 6315 | * unless the response includes appropriate |
| 6316 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6317 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6318 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6319 | ((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] | 6320 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 6321 | break; |
| 6322 | default: |
| 6323 | break; |
| 6324 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6325 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6326 | /* |
| 6327 | * 3: we may need to capture headers |
| 6328 | */ |
| 6329 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6330 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6331 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6332 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6333 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6334 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 6335 | * by RFC7230#3.3.3 : |
| 6336 | * |
| 6337 | * The length of a message body is determined by one of the following |
| 6338 | * (in order of precedence): |
| 6339 | * |
| 6340 | * 1. Any response to a HEAD request and any response with a 1xx |
| 6341 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 6342 | * code is always terminated by the first empty line after the |
| 6343 | * header fields, regardless of the header fields present in the |
| 6344 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6345 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6346 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 6347 | * the connection will become a tunnel immediately after the empty |
| 6348 | * line that concludes the header fields. A client MUST ignore any |
| 6349 | * Content-Length or Transfer-Encoding header fields received in |
| 6350 | * such a message. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6351 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6352 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 6353 | * transfer coding (Section 4.1) is the final encoding, the message |
| 6354 | * body length is determined by reading and decoding the chunked |
| 6355 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6356 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6357 | * If a Transfer-Encoding header field is present in a response and |
| 6358 | * the chunked transfer coding is not the final encoding, the |
| 6359 | * message body length is determined by reading the connection until |
| 6360 | * it is closed by the server. If a Transfer-Encoding header field |
| 6361 | * is present in a request and the chunked transfer coding is not |
| 6362 | * the final encoding, the message body length cannot be determined |
| 6363 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 6364 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6365 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6366 | * If a message is received with both a Transfer-Encoding and a |
| 6367 | * Content-Length header field, the Transfer-Encoding overrides the |
| 6368 | * Content-Length. Such a message might indicate an attempt to |
| 6369 | * perform request smuggling (Section 9.5) or response splitting |
| 6370 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 6371 | * remove the received Content-Length field prior to forwarding such |
| 6372 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6373 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6374 | * 4. If a message is received without Transfer-Encoding and with |
| 6375 | * either multiple Content-Length header fields having differing |
| 6376 | * field-values or a single Content-Length header field having an |
| 6377 | * invalid value, then the message framing is invalid and the |
| 6378 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 6379 | * request message, the server MUST respond with a 400 (Bad Request) |
| 6380 | * status code and then close the connection. If this is a response |
| 6381 | * message received by a proxy, the proxy MUST close the connection |
| 6382 | * to the server, discard the received response, and send a 502 (Bad |
| 6383 | * Gateway) response to the client. If this is a response message |
| 6384 | * received by a user agent, the user agent MUST close the |
| 6385 | * connection to the server and discard the received response. |
| 6386 | * |
| 6387 | * 5. If a valid Content-Length header field is present without |
| 6388 | * Transfer-Encoding, its decimal value defines the expected message |
| 6389 | * body length in octets. If the sender closes the connection or |
| 6390 | * the recipient times out before the indicated number of octets are |
| 6391 | * received, the recipient MUST consider the message to be |
| 6392 | * incomplete and close the connection. |
| 6393 | * |
| 6394 | * 6. If this is a request message and none of the above are true, then |
| 6395 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6396 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6397 | * 7. Otherwise, this is a response message without a declared message |
| 6398 | * body length, so the message body length is determined by the |
| 6399 | * number of octets received prior to the server closing the |
| 6400 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6401 | */ |
| 6402 | |
| 6403 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6404 | * 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] | 6405 | * 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] | 6406 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6407 | */ |
| 6408 | if (txn->meth == HTTP_METH_HEAD || |
| 6409 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6410 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6411 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 6412 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6413 | goto skip_content_length; |
| 6414 | } |
| 6415 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6416 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6417 | ctx.idx = 0; |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 6418 | 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] | 6419 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6420 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6421 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6422 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6423 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6424 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6425 | break; |
| 6426 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6427 | } |
| 6428 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6429 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6430 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6431 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6432 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 6433 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 6434 | } |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6435 | 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] | 6436 | signed long long cl; |
| 6437 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6438 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6439 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6440 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6441 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6442 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6443 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6444 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6445 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6446 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6447 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6448 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6449 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6450 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6451 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6452 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6453 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6454 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6455 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6456 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6457 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6458 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6459 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6460 | } |
| 6461 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6462 | if (sess->fe->comp || s->be->comp) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6463 | select_compression_response_header(s, rep->buf); |
| 6464 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6465 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6466 | /* Now we have to check if we need to modify the Connection header. |
| 6467 | * This is more difficult on the response than it is on the request, |
| 6468 | * because we can have two different HTTP versions and we don't know |
| 6469 | * how the client will interprete a response. For instance, let's say |
| 6470 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6471 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6472 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6473 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6474 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6475 | * indicating how a request MAY be understood by the client. In case |
| 6476 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6477 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6478 | * will fall back to explicit close. Note that we won't take risks with |
| 6479 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6480 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6481 | */ |
| 6482 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6483 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6484 | txn->status == 101)) { |
| 6485 | /* Either we've established an explicit tunnel, or we're |
| 6486 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6487 | * to understand the next protocols. We have to switch to tunnel |
| 6488 | * mode, so that we transfer the request and responses then let |
| 6489 | * this protocol pass unmodified. When we later implement specific |
| 6490 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6491 | * header which contains information about that protocol for |
| 6492 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6493 | */ |
| 6494 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6495 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6496 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6497 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6498 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6499 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6500 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6501 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6502 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6503 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6504 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6505 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6506 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6507 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6508 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6509 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6510 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6511 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6512 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6513 | /* now adjust header transformations depending on current state */ |
| 6514 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6515 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6516 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6517 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6518 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6519 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6520 | else { /* SCL / KAL */ |
| 6521 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6522 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6523 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6524 | } |
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 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6527 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6528 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6529 | /* Some keep-alive responses are converted to Server-close if |
| 6530 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6531 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6532 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6533 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6534 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6535 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6536 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6537 | } |
| 6538 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6539 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6540 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6541 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6542 | /* end of job, return OK */ |
| 6543 | rep->analysers &= ~an_bit; |
| 6544 | rep->analyse_exp = TICK_ETERNITY; |
| 6545 | channel_auto_close(rep); |
| 6546 | return 1; |
| 6547 | |
| 6548 | abort_keep_alive: |
| 6549 | /* A keep-alive request to the server failed on a network error. |
| 6550 | * The client is required to retry. We need to close without returning |
| 6551 | * any other information so that the client retries. |
| 6552 | */ |
| 6553 | txn->status = 0; |
| 6554 | rep->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6555 | s->req.analysers = 0; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6556 | channel_auto_close(rep); |
| 6557 | s->logs.logwait = 0; |
| 6558 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6559 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6560 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6561 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6562 | return 0; |
| 6563 | } |
| 6564 | |
| 6565 | /* This function performs all the processing enabled for the current response. |
| 6566 | * 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] | 6567 | * 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] | 6568 | * other functions. It works like process_request (see indications above). |
| 6569 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6570 | 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] | 6571 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6572 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6573 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6574 | struct http_msg *msg = &txn->rsp; |
| 6575 | struct proxy *cur_proxy; |
| 6576 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 6577 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6578 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6579 | 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] | 6580 | now_ms, __FUNCTION__, |
| 6581 | s, |
| 6582 | rep, |
| 6583 | rep->rex, rep->wex, |
| 6584 | rep->flags, |
| 6585 | rep->buf->i, |
| 6586 | rep->analysers); |
| 6587 | |
| 6588 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6589 | return 0; |
| 6590 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6591 | /* The stats applet needs to adjust the Connection header but we don't |
| 6592 | * apply any filter there. |
| 6593 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6594 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 6595 | rep->analysers &= ~an_bit; |
| 6596 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6597 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6598 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6599 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6600 | /* |
| 6601 | * We will have to evaluate the filters. |
| 6602 | * As opposed to version 1.2, now they will be evaluated in the |
| 6603 | * filters order and not in the header order. This means that |
| 6604 | * each filter has to be validated among all headers. |
| 6605 | * |
| 6606 | * Filters are tried with ->be first, then with ->fe if it is |
| 6607 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6608 | * |
| 6609 | * Maybe we are in resume condiion. In this case I choose the |
| 6610 | * "struct proxy" which contains the rule list matching the resume |
| 6611 | * pointer. If none of theses "struct proxy" match, I initialise |
| 6612 | * the process with the first one. |
| 6613 | * |
| 6614 | * In fact, I check only correspondance betwwen the current list |
| 6615 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 6616 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6617 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6618 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 6619 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6620 | else |
| 6621 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6622 | while (1) { |
| 6623 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6624 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6625 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6626 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 6627 | 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] | 6628 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6629 | if (ret == HTTP_RULE_RES_BADREQ) |
| 6630 | goto return_srv_prx_502; |
| 6631 | |
| 6632 | if (ret == HTTP_RULE_RES_DONE) { |
| 6633 | rep->analysers &= ~an_bit; |
| 6634 | rep->analyse_exp = TICK_ETERNITY; |
| 6635 | return 1; |
| 6636 | } |
| 6637 | } |
| 6638 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6639 | /* we need to be called again. */ |
| 6640 | if (ret == HTTP_RULE_RES_YIELD) { |
| 6641 | channel_dont_close(rep); |
| 6642 | return 0; |
| 6643 | } |
| 6644 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6645 | /* try headers filters */ |
| 6646 | if (rule_set->rsp_exp != NULL) { |
| 6647 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6648 | return_bad_resp: |
| 6649 | if (objt_server(s->target)) { |
| 6650 | objt_server(s->target)->counters.failed_resp++; |
| 6651 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6652 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6653 | s->be->be_counters.failed_resp++; |
| 6654 | return_srv_prx_502: |
| 6655 | rep->analysers = 0; |
| 6656 | txn->status = 502; |
| 6657 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6658 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6659 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6660 | 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] | 6661 | if (!(s->flags & SF_ERR_MASK)) |
| 6662 | s->flags |= SF_ERR_PRXCOND; |
| 6663 | if (!(s->flags & SF_FINST_MASK)) |
| 6664 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6665 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6666 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6667 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6668 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6669 | /* has the response been denied ? */ |
| 6670 | if (txn->flags & TX_SVDENY) { |
| 6671 | if (objt_server(s->target)) |
| 6672 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6673 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6674 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6675 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6676 | if (sess->listener->counters) |
| 6677 | sess->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6678 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6679 | goto return_srv_prx_502; |
| 6680 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6681 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6682 | /* add response headers from the rule sets in the same order */ |
| 6683 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6684 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6685 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6686 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6687 | 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] | 6688 | ret = acl_pass(ret); |
| 6689 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6690 | ret = !ret; |
| 6691 | if (!ret) |
| 6692 | continue; |
| 6693 | } |
| 6694 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6695 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6696 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6697 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6698 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6699 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6700 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6701 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6702 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6703 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6704 | /* After this point, this anayzer can't return yield, so we can |
| 6705 | * remove the bit corresponding to this analyzer from the list. |
| 6706 | * |
| 6707 | * Note that the intermediate returns and goto found previously |
| 6708 | * reset the analyzers. |
| 6709 | */ |
| 6710 | rep->analysers &= ~an_bit; |
| 6711 | rep->analyse_exp = TICK_ETERNITY; |
| 6712 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6713 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6714 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6715 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6716 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6717 | /* |
| 6718 | * Now check for a server cookie. |
| 6719 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 6720 | 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] | 6721 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6722 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6723 | /* |
| 6724 | * Check for cache-control or pragma headers if required. |
| 6725 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6726 | 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] | 6727 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6728 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6729 | /* |
| 6730 | * Add server cookie in the response if needed |
| 6731 | */ |
| 6732 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6733 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6734 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6735 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6736 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6737 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6738 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6739 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6740 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6741 | /* the server is known, it's not the one the client requested, or the |
| 6742 | * cookie's last seen date needs to be refreshed. We have to |
| 6743 | * insert a set-cookie here, except if we want to insert only on POST |
| 6744 | * requests and this one isn't. Note that servers which don't have cookies |
| 6745 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6746 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6747 | if (!objt_server(s->target)->cookie) { |
| 6748 | chunk_printf(&trash, |
| 6749 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6750 | s->be->cookie_name); |
| 6751 | } |
| 6752 | else { |
| 6753 | 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] | 6754 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6755 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6756 | /* emit last_date, which is mandatory */ |
| 6757 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6758 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6759 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6760 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6761 | if (s->be->cookie_maxlife) { |
| 6762 | /* emit first_date, which is either the original one or |
| 6763 | * the current date. |
| 6764 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6765 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6766 | s30tob64(txn->cookie_first_date ? |
| 6767 | txn->cookie_first_date >> 2 : |
| 6768 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6769 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6770 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6771 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6772 | chunk_appendf(&trash, "; path=/"); |
| 6773 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6774 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6775 | if (s->be->cookie_domain) |
| 6776 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6777 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6778 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6779 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6780 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6781 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6782 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6783 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6784 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6785 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6786 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6787 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6788 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6789 | /* the server did not change, only the date was updated */ |
| 6790 | txn->flags |= TX_SCK_UPDATED; |
| 6791 | else |
| 6792 | txn->flags |= TX_SCK_INSERTED; |
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 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6795 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6796 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6797 | * 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] | 6798 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6799 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6800 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6801 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6802 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6803 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6804 | "Cache-control: private", 22) < 0)) |
| 6805 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6806 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6807 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6808 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6809 | /* |
| 6810 | * Check if result will be cacheable with a cookie. |
| 6811 | * We'll block the response if security checks have caught |
| 6812 | * nasty things such as a cacheable cookie. |
| 6813 | */ |
| 6814 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6815 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6816 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6817 | /* we're in presence of a cacheable response containing |
| 6818 | * a set-cookie header. We'll block it as requested by |
| 6819 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6820 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6821 | if (objt_server(s->target)) |
| 6822 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6823 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6824 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6825 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6826 | if (sess->listener->counters) |
| 6827 | sess->listener->counters->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6828 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6829 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6830 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6831 | send_log(s->be, LOG_ALERT, |
| 6832 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6833 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6834 | goto return_srv_prx_502; |
| 6835 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6836 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6837 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6838 | /* |
| 6839 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6840 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6841 | * 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] | 6842 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6843 | * want to touch any 101 response either since it's switching to another |
| 6844 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6845 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6846 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6847 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6848 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6849 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6850 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6851 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6852 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6853 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6854 | /* we want a keep-alive response here. Keep-alive header |
| 6855 | * required if either side is not 1.1. |
| 6856 | */ |
| 6857 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6858 | want_flags |= TX_CON_KAL_SET; |
| 6859 | } |
| 6860 | else { |
| 6861 | /* we want a close response here. Close header required if |
| 6862 | * the server is 1.1, regardless of the client. |
| 6863 | */ |
| 6864 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6865 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6866 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6867 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6868 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6869 | http_change_connection_header(txn, msg, want_flags); |
| 6870 | } |
| 6871 | |
| 6872 | skip_header_mangling: |
| 6873 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
| 6874 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 6875 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6876 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6877 | /* if the user wants to log as soon as possible, without counting |
| 6878 | * bytes from the server, then this is the right moment. We have |
| 6879 | * to temporarily assign bytes_out to log what we currently have. |
| 6880 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6881 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6882 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6883 | s->logs.bytes_out = txn->rsp.eoh; |
| 6884 | s->do_log(s); |
| 6885 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6886 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6887 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6888 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6889 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6890 | /* This function is an analyser which forwards response body (including chunk |
| 6891 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6892 | * zero byte. The only situation where it must not be called is when we're in |
| 6893 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6894 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6895 | * 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] | 6896 | * 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] | 6897 | * |
| 6898 | * It is capable of compressing response data both in content-length mode and |
| 6899 | * in chunked mode. The state machines follows different flows depending on |
| 6900 | * whether content-length and chunked modes are used, since there are no |
| 6901 | * trailers in content-length : |
| 6902 | * |
| 6903 | * chk-mode cl-mode |
| 6904 | * ,----- BODY -----. |
| 6905 | * / \ |
| 6906 | * V size > 0 V chk-mode |
| 6907 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6908 | * | | size == 0 | last byte | |
| 6909 | * | v final crlf v inspected | |
| 6910 | * | TRAILERS -----------> DONE | |
| 6911 | * | | |
| 6912 | * `----------------------------------------------' |
| 6913 | * |
| 6914 | * Compression only happens in the DATA state, and must be flushed in final |
| 6915 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6916 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6917 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6918 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6919 | 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] | 6920 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6921 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6922 | struct http_txn *txn = s->txn; |
| 6923 | struct http_msg *msg = &s->txn->rsp; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6924 | static struct buffer *tmpbuf = &buf_empty; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6925 | int compressing = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6926 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6927 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6928 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6929 | return 0; |
| 6930 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6931 | 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] | 6932 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6933 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6934 | /* Output closed while we were sending data. We must abort and |
| 6935 | * wake the other side up. |
| 6936 | */ |
| 6937 | msg->msg_state = HTTP_MSG_ERROR; |
| 6938 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6939 | return 1; |
| 6940 | } |
| 6941 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6942 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6943 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6944 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 6945 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6946 | /* we have msg->sov which points to the first byte of message |
| 6947 | * body, and res->buf.p still points to the beginning of the |
| 6948 | * message. We forward the headers now, as we don't need them |
| 6949 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6950 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6951 | b_adv(res->buf, msg->sov); |
| 6952 | msg->next -= msg->sov; |
| 6953 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6954 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6955 | /* The previous analysers guarantee that the state is somewhere |
| 6956 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 6957 | * msg->next are always correct. |
| 6958 | */ |
| 6959 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 6960 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 6961 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6962 | else |
| 6963 | msg->msg_state = HTTP_MSG_DATA; |
| 6964 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6965 | } |
| 6966 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6967 | if (res->to_forward) { |
| 6968 | /* We can't process the buffer's contents yet */ |
| 6969 | res->flags |= CF_WAKE_WRITE; |
| 6970 | goto missing_data; |
| 6971 | } |
| 6972 | |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6973 | if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) { |
| 6974 | /* We need a compression buffer in the DATA state to put the |
| 6975 | * output of compressed data, and in CRLF state to let the |
| 6976 | * TRAILERS state finish the job of removing the trailing CRLF. |
| 6977 | */ |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6978 | if (unlikely(!tmpbuf->size)) { |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6979 | /* this is the first time we need the compression buffer */ |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 6980 | if (b_alloc(&tmpbuf) == NULL) |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6981 | goto aborted_xfer; /* no memory */ |
| 6982 | } |
| 6983 | |
| 6984 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6985 | if (ret < 0) { |
| 6986 | res->flags |= CF_WAKE_WRITE; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6987 | goto missing_data; /* not enough spaces in buffers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6988 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6989 | compressing = 1; |
| 6990 | } |
| 6991 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6992 | while (1) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6993 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6994 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6995 | /* we may have some pending data starting at res->buf->p */ |
| 6996 | if (unlikely(s->comp_algo)) { |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 6997 | ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6998 | if (ret < 0) |
| 6999 | goto aborted_xfer; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7000 | |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 7001 | if (msg->chunk_len) { |
| 7002 | /* input empty or output full */ |
| 7003 | if (res->buf->i > msg->next) |
| 7004 | res->flags |= CF_WAKE_WRITE; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7005 | goto missing_data; |
| 7006 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 7007 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7008 | else { |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 7009 | if (msg->chunk_len > res->buf->i - msg->next) { |
| 7010 | /* output full */ |
| 7011 | res->flags |= CF_WAKE_WRITE; |
| 7012 | goto missing_data; |
| 7013 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7014 | msg->next += msg->chunk_len; |
| 7015 | msg->chunk_len = 0; |
| 7016 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 7017 | |
| 7018 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 7019 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 7020 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 7021 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 7022 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7023 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 7024 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7025 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 7026 | |
| 7027 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 7028 | /* we want the CRLF after the data */ |
| 7029 | |
| 7030 | ret = http_skip_chunk_crlf(msg); |
| 7031 | if (ret == 0) |
| 7032 | goto missing_data; |
| 7033 | else if (ret < 0) { |
| 7034 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7035 | 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] | 7036 | goto return_bad_res; |
| 7037 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7038 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 7039 | |
| 7040 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 7041 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 7042 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 7043 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7044 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7045 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7046 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 7047 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7048 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 7049 | else if (ret < 0) { |
| 7050 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7051 | 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] | 7052 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 7053 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 7054 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7055 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 7056 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7057 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7058 | if (unlikely(compressing)) { |
| 7059 | /* we need to flush output contents before syncing FSMs */ |
| 7060 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 7061 | compressing = 0; |
| 7062 | } |
| 7063 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7064 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7065 | if (ret == 0) |
| 7066 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 7067 | else if (ret < 0) { |
| 7068 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7069 | 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] | 7070 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 7071 | } |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7072 | /* we're in HTTP_MSG_DONE now, fall through */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7073 | |
| 7074 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7075 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7076 | if (unlikely(compressing)) { |
| 7077 | /* we need to flush output contents before syncing FSMs */ |
| 7078 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 7079 | compressing = 0; |
| 7080 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7081 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7082 | /* we may have some pending data starting at res->buf->p |
| 7083 | * such as a last chunk of data or trailers. |
| 7084 | */ |
| 7085 | b_adv(res->buf, msg->next); |
| 7086 | msg->next = 0; |
| 7087 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7088 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 7089 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7090 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 7091 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 7092 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 7093 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7094 | if (http_resync_states(s)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7095 | /* some state changes occurred, maybe the analyser |
| 7096 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 7097 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 7098 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7099 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 7100 | /* response errors are most likely due to |
| 7101 | * the client aborting the transfer. |
| 7102 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7103 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 7104 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 7105 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7106 | 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] | 7107 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 7108 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7109 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 7110 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7111 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7112 | } |
| 7113 | } |
| 7114 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7115 | missing_data: |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7116 | /* we may have some pending data starting at res->buf->p */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7117 | if (unlikely(compressing)) { |
| 7118 | 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] | 7119 | compressing = 0; |
| 7120 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 7121 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7122 | if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) { |
| 7123 | b_adv(res->buf, msg->next); |
| 7124 | msg->next = 0; |
| 7125 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 7126 | } |
| 7127 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 7128 | if (res->flags & CF_SHUTW) |
| 7129 | goto aborted_xfer; |
| 7130 | |
| 7131 | /* stop waiting for data if the input is closed before the end. If the |
| 7132 | * client side was already closed, it means that the client has aborted, |
| 7133 | * so we don't want to count this as a server abort. Otherwise it's a |
| 7134 | * server abort. |
| 7135 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7136 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7137 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 7138 | goto aborted_xfer; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7139 | if (!(s->flags & SF_ERR_MASK)) |
| 7140 | s->flags |= SF_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7141 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7142 | if (objt_server(s->target)) |
| 7143 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7144 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7145 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 7146 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7147 | /* 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] | 7148 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7149 | goto return_bad_res; |
| 7150 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7151 | /* 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] | 7152 | * 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] | 7153 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 7154 | * close. |
| 7155 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 7156 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7157 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 7158 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 7159 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7160 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7161 | /* 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] | 7162 | * 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] | 7163 | * 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] | 7164 | * modes are already handled by the stream sock layer. We must not do |
| 7165 | * this in content-length mode because it could present the MSG_MORE |
| 7166 | * flag with the last block of forwarded data, which would cause an |
| 7167 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 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 | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7170 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7171 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7172 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7173 | return 0; |
| 7174 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7175 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7176 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7177 | if (objt_server(s->target)) |
| 7178 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7179 | |
| 7180 | return_bad_res_stats_ok: |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 7181 | if (unlikely(compressing)) { |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7182 | 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] | 7183 | compressing = 0; |
| 7184 | } |
| 7185 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7186 | /* we may have some pending data starting at res->buf->p */ |
| 7187 | if (s->comp_algo == NULL) { |
| 7188 | b_adv(res->buf, msg->next); |
| 7189 | msg->next = 0; |
| 7190 | } |
| 7191 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7192 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 7193 | /* don't send any error message as we're in the body */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 7194 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7195 | res->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7196 | 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] | 7197 | if (objt_server(s->target)) |
| 7198 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7199 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7200 | if (!(s->flags & SF_ERR_MASK)) |
| 7201 | s->flags |= SF_ERR_PRXCOND; |
| 7202 | if (!(s->flags & SF_FINST_MASK)) |
| 7203 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7204 | return 0; |
| 7205 | |
| 7206 | aborted_xfer: |
Willy Tarreau | 6fef8ae | 2014-04-22 21:22:06 +0200 | [diff] [blame] | 7207 | if (unlikely(compressing)) { |
| 7208 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
| 7209 | compressing = 0; |
| 7210 | } |
| 7211 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7212 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 7213 | /* don't send any error message as we're in the body */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 7214 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7215 | res->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7216 | 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] | 7217 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7218 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7219 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7220 | if (objt_server(s->target)) |
| 7221 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7222 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7223 | if (!(s->flags & SF_ERR_MASK)) |
| 7224 | s->flags |= SF_ERR_CLICL; |
| 7225 | if (!(s->flags & SF_FINST_MASK)) |
| 7226 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7227 | return 0; |
| 7228 | } |
| 7229 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7230 | /* Iterate the same filter through all request headers. |
| 7231 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7232 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7233 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7234 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7235 | 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] | 7236 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7237 | char *cur_ptr, *cur_end, *cur_next; |
| 7238 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7239 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7240 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7241 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 7242 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7243 | last_hdr = 0; |
| 7244 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7245 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7246 | old_idx = 0; |
| 7247 | |
| 7248 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7249 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7250 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7251 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7252 | (exp->action == ACT_ALLOW || |
| 7253 | exp->action == ACT_DENY || |
| 7254 | exp->action == ACT_TARPIT)) |
| 7255 | return 0; |
| 7256 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7257 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7258 | if (!cur_idx) |
| 7259 | break; |
| 7260 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7261 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7262 | cur_ptr = cur_next; |
| 7263 | cur_end = cur_ptr + cur_hdr->len; |
| 7264 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7265 | |
| 7266 | /* Now we have one header between cur_ptr and cur_end, |
| 7267 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7268 | */ |
| 7269 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7270 | 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] | 7271 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7272 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7273 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7274 | last_hdr = 1; |
| 7275 | break; |
| 7276 | |
| 7277 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7278 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7279 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7280 | break; |
| 7281 | |
| 7282 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7283 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7284 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7285 | break; |
| 7286 | |
| 7287 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7288 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7289 | if (trash.len < 0) |
| 7290 | return -1; |
| 7291 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7292 | 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] | 7293 | /* FIXME: if the user adds a newline in the replacement, the |
| 7294 | * index will not be recalculated for now, and the new line |
| 7295 | * will not be counted as a new header. |
| 7296 | */ |
| 7297 | |
| 7298 | cur_end += delta; |
| 7299 | cur_next += delta; |
| 7300 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7301 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7302 | break; |
| 7303 | |
| 7304 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7305 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7306 | cur_next += delta; |
| 7307 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7308 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7309 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7310 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7311 | cur_hdr->len = 0; |
| 7312 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7313 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7314 | break; |
| 7315 | |
| 7316 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7317 | } |
| 7318 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7319 | /* keep the link from this header to next one in case of later |
| 7320 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7321 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7322 | old_idx = cur_idx; |
| 7323 | } |
| 7324 | return 0; |
| 7325 | } |
| 7326 | |
| 7327 | |
| 7328 | /* Apply the filter to the request line. |
| 7329 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7330 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7331 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7332 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7333 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7334 | 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] | 7335 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7336 | char *cur_ptr, *cur_end; |
| 7337 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7338 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7339 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7340 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7341 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7342 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7343 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7344 | (exp->action == ACT_ALLOW || |
| 7345 | exp->action == ACT_DENY || |
| 7346 | exp->action == ACT_TARPIT)) |
| 7347 | return 0; |
| 7348 | else if (exp->action == ACT_REMOVE) |
| 7349 | return 0; |
| 7350 | |
| 7351 | done = 0; |
| 7352 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7353 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7354 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7355 | |
| 7356 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7357 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7358 | 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] | 7359 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7360 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7361 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7362 | done = 1; |
| 7363 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7364 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7365 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7366 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7367 | done = 1; |
| 7368 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7369 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7370 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7371 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7372 | done = 1; |
| 7373 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7374 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7375 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7376 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7377 | if (trash.len < 0) |
| 7378 | return -1; |
| 7379 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7380 | 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] | 7381 | /* FIXME: if the user adds a newline in the replacement, the |
| 7382 | * index will not be recalculated for now, and the new line |
| 7383 | * will not be counted as a new header. |
| 7384 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7385 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7386 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7387 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7388 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7389 | HTTP_MSG_RQMETH, |
| 7390 | cur_ptr, cur_end + 1, |
| 7391 | NULL, NULL); |
| 7392 | if (unlikely(!cur_end)) |
| 7393 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7394 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7395 | /* we have a full request and we know that we have either a CR |
| 7396 | * or an LF at <ptr>. |
| 7397 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7398 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7399 | 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] | 7400 | /* there is no point trying this regex on headers */ |
| 7401 | return 1; |
| 7402 | } |
| 7403 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7404 | return done; |
| 7405 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7406 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7407 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7408 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7409 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7410 | * 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] | 7411 | * 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] | 7412 | * unparsable request. Since it can manage the switch to another backend, it |
| 7413 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7414 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7415 | 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] | 7416 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7417 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7418 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7419 | struct hdr_exp *exp; |
| 7420 | |
| 7421 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7422 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7423 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7424 | /* |
| 7425 | * The interleaving of transformations and verdicts |
| 7426 | * makes it difficult to decide to continue or stop |
| 7427 | * the evaluation. |
| 7428 | */ |
| 7429 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7430 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7431 | break; |
| 7432 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7433 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7434 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7435 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7436 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7437 | |
| 7438 | /* if this filter had a condition, evaluate it now and skip to |
| 7439 | * next filter if the condition does not match. |
| 7440 | */ |
| 7441 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7442 | 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] | 7443 | ret = acl_pass(ret); |
| 7444 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7445 | ret = !ret; |
| 7446 | |
| 7447 | if (!ret) |
| 7448 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7449 | } |
| 7450 | |
| 7451 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7452 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7453 | if (unlikely(ret < 0)) |
| 7454 | return -1; |
| 7455 | |
| 7456 | if (likely(ret == 0)) { |
| 7457 | /* The filter did not match the request, it can be |
| 7458 | * iterated through all headers. |
| 7459 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7460 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7461 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7462 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7463 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7464 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7465 | } |
| 7466 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7467 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7468 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7469 | * same way as with headers above except that the semi-colon also ends a token. |
| 7470 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7471 | * return a valid result. |
| 7472 | */ |
| 7473 | char *find_cookie_value_end(char *s, const char *e) |
| 7474 | { |
| 7475 | int quoted, qdpair; |
| 7476 | |
| 7477 | quoted = qdpair = 0; |
| 7478 | for (; s < e; s++) { |
| 7479 | if (qdpair) qdpair = 0; |
| 7480 | else if (quoted) { |
| 7481 | if (*s == '\\') qdpair = 1; |
| 7482 | else if (*s == '"') quoted = 0; |
| 7483 | } |
| 7484 | else if (*s == '"') quoted = 1; |
| 7485 | else if (*s == ',' || *s == ';') return s; |
| 7486 | } |
| 7487 | return s; |
| 7488 | } |
| 7489 | |
| 7490 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7491 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7492 | * the first delimiter is updated if required. The function tries as much as |
| 7493 | * possible to respect the following principles : |
| 7494 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7495 | * colon, in which case <next> is simply removed |
| 7496 | * - set exactly one space character after the new first delimiter, unless |
| 7497 | * there are not enough characters in the block being moved to do so. |
| 7498 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7499 | * one. |
| 7500 | * |
| 7501 | * It is the caller's responsibility to ensure that : |
| 7502 | * - <from> points to a valid delimiter or the colon ; |
| 7503 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7504 | * - there are non-space chars before <from> ; |
| 7505 | * - there is a CR/LF at or after <next>. |
| 7506 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7507 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7508 | { |
| 7509 | char *prev = *from; |
| 7510 | |
| 7511 | if (*prev == ':') { |
| 7512 | /* We're removing the first value, preserve the colon and add a |
| 7513 | * space if possible. |
| 7514 | */ |
| 7515 | if (!http_is_crlf[(unsigned char)*next]) |
| 7516 | next++; |
| 7517 | prev++; |
| 7518 | if (prev < next) |
| 7519 | *prev++ = ' '; |
| 7520 | |
| 7521 | while (http_is_spht[(unsigned char)*next]) |
| 7522 | next++; |
| 7523 | } else { |
| 7524 | /* Remove useless spaces before the old delimiter. */ |
| 7525 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 7526 | prev--; |
| 7527 | *from = prev; |
| 7528 | |
| 7529 | /* copy the delimiter and if possible a space if we're |
| 7530 | * not at the end of the line. |
| 7531 | */ |
| 7532 | if (!http_is_crlf[(unsigned char)*next]) { |
| 7533 | *prev++ = *next++; |
| 7534 | if (prev + 1 < next) |
| 7535 | *prev++ = ' '; |
| 7536 | while (http_is_spht[(unsigned char)*next]) |
| 7537 | next++; |
| 7538 | } |
| 7539 | } |
| 7540 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7541 | } |
| 7542 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7543 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7544 | * 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] | 7545 | * desirable to call it only when needed. This code is quite complex because |
| 7546 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7547 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7548 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7549 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7550 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7551 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7552 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7553 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7554 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7555 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7556 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7557 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7558 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7559 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7560 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7561 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7562 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7563 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7564 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7565 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7566 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7567 | hdr_beg = hdr_next; |
| 7568 | hdr_end = hdr_beg + cur_hdr->len; |
| 7569 | hdr_next = hdr_end + cur_hdr->cr + 1; |
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 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7572 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7573 | * "Cookie:" headers. |
| 7574 | */ |
| 7575 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7576 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7577 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7578 | old_idx = cur_idx; |
| 7579 | continue; |
| 7580 | } |
| 7581 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7582 | del_from = NULL; /* nothing to be deleted */ |
| 7583 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7584 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7585 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7586 | * attributes whose name begin with a '$', and associate them with |
| 7587 | * the right cookie, if we want to delete this cookie. |
| 7588 | * So there are 3 cases for each cookie read : |
| 7589 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7590 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7591 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7592 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7593 | * "special" cookie. |
| 7594 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7595 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7596 | * *MUST* delete it. |
| 7597 | * |
| 7598 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7599 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7600 | * the RFC explicitly allows spaces before it, and not within the |
| 7601 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7602 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7603 | * after the equal sign too, resulting in some (rare) buggy |
| 7604 | * implementations trying to do that. So let's do what servers do. |
| 7605 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7606 | * allowed quoted strings in values, with any possible character |
| 7607 | * after a backslash, including control chars and delimitors, which |
| 7608 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7609 | * within values even without quotes. |
| 7610 | * |
| 7611 | * We have to keep multiple pointers in order to support cookie |
| 7612 | * removal at the beginning, middle or end of header without |
| 7613 | * corrupting the header. All of these headers are valid : |
| 7614 | * |
| 7615 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7616 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7617 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7618 | * | | | | | | | | | |
| 7619 | * | | | | | | | | hdr_end <--+ |
| 7620 | * | | | | | | | +--> next |
| 7621 | * | | | | | | +----> val_end |
| 7622 | * | | | | | +-----------> val_beg |
| 7623 | * | | | | +--------------> equal |
| 7624 | * | | | +----------------> att_end |
| 7625 | * | | +---------------------> att_beg |
| 7626 | * | +--------------------------> prev |
| 7627 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7628 | */ |
| 7629 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7630 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7631 | /* Iterate through all cookies on this line */ |
| 7632 | |
| 7633 | /* find att_beg */ |
| 7634 | att_beg = prev + 1; |
| 7635 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7636 | att_beg++; |
| 7637 | |
| 7638 | /* find att_end : this is the first character after the last non |
| 7639 | * space before the equal. It may be equal to hdr_end. |
| 7640 | */ |
| 7641 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7642 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7643 | while (equal < hdr_end) { |
| 7644 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7645 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7646 | if (http_is_spht[(unsigned char)*equal++]) |
| 7647 | continue; |
| 7648 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7649 | } |
| 7650 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7651 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7652 | * is between <att_beg> and <equal>, both may be identical. |
| 7653 | */ |
| 7654 | |
| 7655 | /* look for end of cookie if there is an equal sign */ |
| 7656 | if (equal < hdr_end && *equal == '=') { |
| 7657 | /* look for the beginning of the value */ |
| 7658 | val_beg = equal + 1; |
| 7659 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7660 | val_beg++; |
| 7661 | |
| 7662 | /* find the end of the value, respecting quotes */ |
| 7663 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7664 | |
| 7665 | /* make val_end point to the first white space or delimitor after the value */ |
| 7666 | val_end = next; |
| 7667 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7668 | val_end--; |
| 7669 | } else { |
| 7670 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7671 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7672 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7673 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7674 | * they will automatically be removed if a header before them is removed, |
| 7675 | * since they're supposed to be linked together. |
| 7676 | */ |
| 7677 | if (*att_beg == '$') |
| 7678 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7679 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7680 | /* Ignore cookies with no equal sign */ |
| 7681 | if (equal == next) { |
| 7682 | /* This is not our cookie, so we must preserve it. But if we already |
| 7683 | * scheduled another cookie for removal, we cannot remove the |
| 7684 | * complete header, but we can remove the previous block itself. |
| 7685 | */ |
| 7686 | preserve_hdr = 1; |
| 7687 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7688 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7689 | val_end += delta; |
| 7690 | next += delta; |
| 7691 | hdr_end += delta; |
| 7692 | hdr_next += delta; |
| 7693 | cur_hdr->len += delta; |
| 7694 | http_msg_move_end(&txn->req, delta); |
| 7695 | prev = del_from; |
| 7696 | del_from = NULL; |
| 7697 | } |
| 7698 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7699 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7700 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7701 | /* if there are spaces around the equal sign, we need to |
| 7702 | * strip them otherwise we'll get trouble for cookie captures, |
| 7703 | * or even for rewrites. Since this happens extremely rarely, |
| 7704 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7705 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7706 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7707 | int stripped_before = 0; |
| 7708 | int stripped_after = 0; |
| 7709 | |
| 7710 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7711 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7712 | equal += stripped_before; |
| 7713 | val_beg += stripped_before; |
| 7714 | } |
| 7715 | |
| 7716 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7717 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7718 | val_beg += stripped_after; |
| 7719 | stripped_before += stripped_after; |
| 7720 | } |
| 7721 | |
| 7722 | val_end += stripped_before; |
| 7723 | next += stripped_before; |
| 7724 | hdr_end += stripped_before; |
| 7725 | hdr_next += stripped_before; |
| 7726 | cur_hdr->len += stripped_before; |
| 7727 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7728 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7729 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7730 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7731 | /* First, let's see if we want to capture this cookie. We check |
| 7732 | * that we don't already have a client side cookie, because we |
| 7733 | * can only capture one. Also as an optimisation, we ignore |
| 7734 | * cookies shorter than the declared name. |
| 7735 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7736 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7737 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7738 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7739 | int log_len = val_end - att_beg; |
| 7740 | |
| 7741 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7742 | Alert("HTTP logging : out of memory.\n"); |
| 7743 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7744 | if (log_len > sess->fe->capture_len) |
| 7745 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7746 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7747 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7748 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7749 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7750 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7751 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7752 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7753 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7754 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7755 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7756 | * For cookies in prefix mode, the form is : |
| 7757 | * |
| 7758 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7759 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7760 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7761 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7762 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7763 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7764 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7765 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7766 | * have the server ID between val_beg and delim, and the original cookie between |
| 7767 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7768 | * |
| 7769 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7770 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7771 | * | || || | |+-> next |
| 7772 | * | || || | +--> val_end |
| 7773 | * | || || +---------> delim |
| 7774 | * | || |+------------> val_beg |
| 7775 | * | || +-------------> att_end = equal |
| 7776 | * | |+-----------------> att_beg |
| 7777 | * | +------------------> prev |
| 7778 | * +-------------------------> hdr_beg |
| 7779 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7780 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7781 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7782 | for (delim = val_beg; delim < val_end; delim++) |
| 7783 | if (*delim == COOKIE_DELIM) |
| 7784 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7785 | } else { |
| 7786 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7787 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7788 | /* Now check if the cookie contains a date field, which would |
| 7789 | * appear after a vertical bar ('|') just after the server name |
| 7790 | * and before the delimiter. |
| 7791 | */ |
| 7792 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7793 | if (vbar1) { |
| 7794 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7795 | * right is the last seen date. It is a base64 encoded |
| 7796 | * 30-bit value representing the UNIX date since the |
| 7797 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7798 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7799 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7800 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7801 | if (val_end - vbar1 >= 5) { |
| 7802 | val = b64tos30(vbar1); |
| 7803 | if (val > 0) |
| 7804 | txn->cookie_last_date = val << 2; |
| 7805 | } |
| 7806 | /* look for a second vertical bar */ |
| 7807 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7808 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7809 | val = b64tos30(vbar1 + 1); |
| 7810 | if (val > 0) |
| 7811 | txn->cookie_first_date = val << 2; |
| 7812 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7813 | } |
| 7814 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7815 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7816 | /* if the cookie has an expiration date and the proxy wants to check |
| 7817 | * it, then we do that now. We first check if the cookie is too old, |
| 7818 | * then only if it has expired. We detect strict overflow because the |
| 7819 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7820 | * in the future are ignored if their offset is beyond one day. This |
| 7821 | * allows an admin to fix timezone issues without expiring everyone |
| 7822 | * and at the same time avoids keeping unwanted side effects for too |
| 7823 | * long. |
| 7824 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7825 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7826 | (((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] | 7827 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7828 | txn->flags &= ~TX_CK_MASK; |
| 7829 | txn->flags |= TX_CK_OLD; |
| 7830 | delim = val_beg; // let's pretend we have not found the cookie |
| 7831 | txn->cookie_first_date = 0; |
| 7832 | txn->cookie_last_date = 0; |
| 7833 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7834 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7835 | (((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] | 7836 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7837 | txn->flags &= ~TX_CK_MASK; |
| 7838 | txn->flags |= TX_CK_EXPIRED; |
| 7839 | delim = val_beg; // let's pretend we have not found the cookie |
| 7840 | txn->cookie_first_date = 0; |
| 7841 | txn->cookie_last_date = 0; |
| 7842 | } |
| 7843 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7844 | /* Here, we'll look for the first running server which supports the cookie. |
| 7845 | * This allows to share a same cookie between several servers, for example |
| 7846 | * to dedicate backup servers to specific servers only. |
| 7847 | * However, to prevent clients from sticking to cookie-less backup server |
| 7848 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7849 | * empty cookies and mark them as invalid. |
| 7850 | * The same behaviour is applied when persistence must be ignored. |
| 7851 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7852 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7853 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7854 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7855 | while (srv) { |
| 7856 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7857 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7858 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7859 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7860 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7861 | /* we found the server and we can use it */ |
| 7862 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7863 | 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] | 7864 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7865 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7866 | break; |
| 7867 | } else { |
| 7868 | /* we found a server, but it's down, |
| 7869 | * mark it as such and go on in case |
| 7870 | * another one is available. |
| 7871 | */ |
| 7872 | txn->flags &= ~TX_CK_MASK; |
| 7873 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7874 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7875 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7876 | srv = srv->next; |
| 7877 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7878 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7879 | 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] | 7880 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7881 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7882 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7883 | txn->flags |= TX_CK_UNUSED; |
| 7884 | else |
| 7885 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7886 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7887 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7888 | /* depending on the cookie mode, we may have to either : |
| 7889 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7890 | * the server never sees it ; |
| 7891 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7892 | * application cookie so that it does not get accidentely removed later, |
| 7893 | * if we're in cookie prefix mode |
| 7894 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7895 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7896 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7897 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7898 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7899 | val_end += delta; |
| 7900 | next += delta; |
| 7901 | hdr_end += delta; |
| 7902 | hdr_next += delta; |
| 7903 | cur_hdr->len += delta; |
| 7904 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7905 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7906 | del_from = NULL; |
| 7907 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7908 | } |
| 7909 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7910 | (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] | 7911 | del_from = prev; |
| 7912 | } |
| 7913 | } else { |
| 7914 | /* This is not our cookie, so we must preserve it. But if we already |
| 7915 | * scheduled another cookie for removal, we cannot remove the |
| 7916 | * complete header, but we can remove the previous block itself. |
| 7917 | */ |
| 7918 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7919 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7920 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7921 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7922 | if (att_beg >= del_from) |
| 7923 | att_beg += delta; |
| 7924 | if (att_end >= del_from) |
| 7925 | att_end += delta; |
| 7926 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7927 | val_end += delta; |
| 7928 | next += delta; |
| 7929 | hdr_end += delta; |
| 7930 | hdr_next += delta; |
| 7931 | cur_hdr->len += delta; |
| 7932 | http_msg_move_end(&txn->req, delta); |
| 7933 | prev = del_from; |
| 7934 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7935 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7936 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7937 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7938 | /* continue with next cookie on this header line */ |
| 7939 | att_beg = next; |
| 7940 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7941 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7942 | /* There are no more cookies on this line. |
| 7943 | * We may still have one (or several) marked for deletion at the |
| 7944 | * end of the line. We must do this now in two ways : |
| 7945 | * - if some cookies must be preserved, we only delete from the |
| 7946 | * mark to the end of line ; |
| 7947 | * - if nothing needs to be preserved, simply delete the whole header |
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 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7950 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7951 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7952 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7953 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7954 | cur_hdr->len += delta; |
| 7955 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7956 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7957 | |
| 7958 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7959 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7960 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7961 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7962 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7963 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7964 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7965 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7966 | } |
| 7967 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7968 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7969 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7970 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7971 | } |
| 7972 | |
| 7973 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7974 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7975 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7976 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7977 | 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] | 7978 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7979 | char *cur_ptr, *cur_end, *cur_next; |
| 7980 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7981 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7982 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7983 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7984 | |
| 7985 | last_hdr = 0; |
| 7986 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7987 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7988 | old_idx = 0; |
| 7989 | |
| 7990 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7991 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7992 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7993 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7994 | (exp->action == ACT_ALLOW || |
| 7995 | exp->action == ACT_DENY)) |
| 7996 | return 0; |
| 7997 | |
| 7998 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7999 | if (!cur_idx) |
| 8000 | break; |
| 8001 | |
| 8002 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8003 | cur_ptr = cur_next; |
| 8004 | cur_end = cur_ptr + cur_hdr->len; |
| 8005 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8006 | |
| 8007 | /* Now we have one header between cur_ptr and cur_end, |
| 8008 | * and the next header starts at cur_next. |
| 8009 | */ |
| 8010 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 8011 | 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] | 8012 | switch (exp->action) { |
| 8013 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8014 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8015 | last_hdr = 1; |
| 8016 | break; |
| 8017 | |
| 8018 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8019 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8020 | last_hdr = 1; |
| 8021 | break; |
| 8022 | |
| 8023 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8024 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 8025 | if (trash.len < 0) |
| 8026 | return -1; |
| 8027 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8028 | 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] | 8029 | /* FIXME: if the user adds a newline in the replacement, the |
| 8030 | * index will not be recalculated for now, and the new line |
| 8031 | * will not be counted as a new header. |
| 8032 | */ |
| 8033 | |
| 8034 | cur_end += delta; |
| 8035 | cur_next += delta; |
| 8036 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8037 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8038 | break; |
| 8039 | |
| 8040 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8041 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8042 | cur_next += delta; |
| 8043 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8044 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8045 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8046 | txn->hdr_idx.used--; |
| 8047 | cur_hdr->len = 0; |
| 8048 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8049 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8050 | break; |
| 8051 | |
| 8052 | } |
| 8053 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8054 | |
| 8055 | /* keep the link from this header to next one in case of later |
| 8056 | * removal of next header. |
| 8057 | */ |
| 8058 | old_idx = cur_idx; |
| 8059 | } |
| 8060 | return 0; |
| 8061 | } |
| 8062 | |
| 8063 | |
| 8064 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 8065 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 8066 | * or -1 if a replacement resulted in an invalid status line. |
| 8067 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8068 | 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] | 8069 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8070 | char *cur_ptr, *cur_end; |
| 8071 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8072 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8073 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8074 | |
| 8075 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8076 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8077 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8078 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8079 | (exp->action == ACT_ALLOW || |
| 8080 | exp->action == ACT_DENY)) |
| 8081 | return 0; |
| 8082 | else if (exp->action == ACT_REMOVE) |
| 8083 | return 0; |
| 8084 | |
| 8085 | done = 0; |
| 8086 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8087 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 8088 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8089 | |
| 8090 | /* Now we have the status line between cur_ptr and cur_end */ |
| 8091 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 8092 | 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] | 8093 | switch (exp->action) { |
| 8094 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8095 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8096 | done = 1; |
| 8097 | break; |
| 8098 | |
| 8099 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8100 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8101 | done = 1; |
| 8102 | break; |
| 8103 | |
| 8104 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8105 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 8106 | if (trash.len < 0) |
| 8107 | return -1; |
| 8108 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8109 | 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] | 8110 | /* FIXME: if the user adds a newline in the replacement, the |
| 8111 | * index will not be recalculated for now, and the new line |
| 8112 | * will not be counted as a new header. |
| 8113 | */ |
| 8114 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8115 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8116 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8117 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 8118 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8119 | cur_ptr, cur_end + 1, |
| 8120 | NULL, NULL); |
| 8121 | if (unlikely(!cur_end)) |
| 8122 | return -1; |
| 8123 | |
| 8124 | /* we have a full respnse and we know that we have either a CR |
| 8125 | * or an LF at <ptr>. |
| 8126 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8127 | 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] | 8128 | 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] | 8129 | /* there is no point trying this regex on headers */ |
| 8130 | return 1; |
| 8131 | } |
| 8132 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8133 | return done; |
| 8134 | } |
| 8135 | |
| 8136 | |
| 8137 | |
| 8138 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8139 | * 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] | 8140 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 8141 | * unparsable response. |
| 8142 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8143 | 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] | 8144 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8145 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8146 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8147 | struct hdr_exp *exp; |
| 8148 | |
| 8149 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8150 | int ret; |
| 8151 | |
| 8152 | /* |
| 8153 | * The interleaving of transformations and verdicts |
| 8154 | * makes it difficult to decide to continue or stop |
| 8155 | * the evaluation. |
| 8156 | */ |
| 8157 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8158 | if (txn->flags & TX_SVDENY) |
| 8159 | break; |
| 8160 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8161 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8162 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 8163 | exp->action == ACT_PASS)) { |
| 8164 | exp = exp->next; |
| 8165 | continue; |
| 8166 | } |
| 8167 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8168 | /* if this filter had a condition, evaluate it now and skip to |
| 8169 | * next filter if the condition does not match. |
| 8170 | */ |
| 8171 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8172 | 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] | 8173 | ret = acl_pass(ret); |
| 8174 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 8175 | ret = !ret; |
| 8176 | if (!ret) |
| 8177 | continue; |
| 8178 | } |
| 8179 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8180 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8181 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8182 | if (unlikely(ret < 0)) |
| 8183 | return -1; |
| 8184 | |
| 8185 | if (likely(ret == 0)) { |
| 8186 | /* The filter did not match the response, it can be |
| 8187 | * iterated through all headers. |
| 8188 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8189 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 8190 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8191 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8192 | } |
| 8193 | return 0; |
| 8194 | } |
| 8195 | |
| 8196 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8197 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 8198 | * 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] | 8199 | * desirable to call it only when needed. This function is also used when we |
| 8200 | * 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] | 8201 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8202 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8203 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8204 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8205 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8206 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8207 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8208 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8209 | char *hdr_beg, *hdr_end, *hdr_next; |
| 8210 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8211 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8212 | /* Iterate through the headers. |
| 8213 | * we start with the start line. |
| 8214 | */ |
| 8215 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8216 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8217 | |
| 8218 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 8219 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8220 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8221 | |
| 8222 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8223 | hdr_beg = hdr_next; |
| 8224 | hdr_end = hdr_beg + cur_hdr->len; |
| 8225 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8226 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8227 | /* We have one full header between hdr_beg and hdr_end, and the |
| 8228 | * next header starts at hdr_next. We're only interested in |
| 8229 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8230 | */ |
| 8231 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8232 | is_cookie2 = 0; |
| 8233 | prev = hdr_beg + 10; |
| 8234 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8235 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8236 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 8237 | if (!val) { |
| 8238 | old_idx = cur_idx; |
| 8239 | continue; |
| 8240 | } |
| 8241 | is_cookie2 = 1; |
| 8242 | prev = hdr_beg + 11; |
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 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 8246 | * <prev> points to the colon. |
| 8247 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8248 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8249 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8250 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8251 | * check-cache is enabled) and we are not interested in checking |
| 8252 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8253 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 8254 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8255 | return; |
| 8256 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8257 | /* OK so now we know we have to process this response cookie. |
| 8258 | * The format of the Set-Cookie header is slightly different |
| 8259 | * from the format of the Cookie header in that it does not |
| 8260 | * support the comma as a cookie delimiter (thus the header |
| 8261 | * cannot be folded) because the Expires attribute described in |
| 8262 | * the original Netscape's spec may contain an unquoted date |
| 8263 | * with a comma inside. We have to live with this because |
| 8264 | * many browsers don't support Max-Age and some browsers don't |
| 8265 | * support quoted strings. However the Set-Cookie2 header is |
| 8266 | * clean. |
| 8267 | * |
| 8268 | * We have to keep multiple pointers in order to support cookie |
| 8269 | * removal at the beginning, middle or end of header without |
| 8270 | * corrupting the header (in case of set-cookie2). A special |
| 8271 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8272 | * fields after the first semi-colon. The <next> pointer points |
| 8273 | * either to the end of line (set-cookie) or next unquoted comma |
| 8274 | * (set-cookie2). All of these headers are valid : |
| 8275 | * |
| 8276 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8277 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8278 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8279 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8280 | * | | | | | | | | | | |
| 8281 | * | | | | | | | | +-> next hdr_end <--+ |
| 8282 | * | | | | | | | +------------> scav |
| 8283 | * | | | | | | +--------------> val_end |
| 8284 | * | | | | | +--------------------> val_beg |
| 8285 | * | | | | +----------------------> equal |
| 8286 | * | | | +------------------------> att_end |
| 8287 | * | | +----------------------------> att_beg |
| 8288 | * | +------------------------------> prev |
| 8289 | * +-----------------------------------------> hdr_beg |
| 8290 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8291 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8292 | for (; prev < hdr_end; prev = next) { |
| 8293 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8294 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8295 | /* find att_beg */ |
| 8296 | att_beg = prev + 1; |
| 8297 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8298 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8299 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8300 | /* find att_end : this is the first character after the last non |
| 8301 | * space before the equal. It may be equal to hdr_end. |
| 8302 | */ |
| 8303 | equal = att_end = att_beg; |
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 | while (equal < hdr_end) { |
| 8306 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8307 | break; |
| 8308 | if (http_is_spht[(unsigned char)*equal++]) |
| 8309 | continue; |
| 8310 | att_end = equal; |
| 8311 | } |
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 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8314 | * is between <att_beg> and <equal>, both may be identical. |
| 8315 | */ |
| 8316 | |
| 8317 | /* look for end of cookie if there is an equal sign */ |
| 8318 | if (equal < hdr_end && *equal == '=') { |
| 8319 | /* look for the beginning of the value */ |
| 8320 | val_beg = equal + 1; |
| 8321 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8322 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8323 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8324 | /* find the end of the value, respecting quotes */ |
| 8325 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8326 | |
| 8327 | /* make val_end point to the first white space or delimitor after the value */ |
| 8328 | val_end = next; |
| 8329 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8330 | val_end--; |
| 8331 | } else { |
| 8332 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8333 | val_beg = val_end = next = equal; |
| 8334 | } |
| 8335 | |
| 8336 | if (next < hdr_end) { |
| 8337 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8338 | * a colon or semi-colon before the end. So skip all attr-value |
| 8339 | * pairs and look for the next comma. For Set-Cookie, since |
| 8340 | * commas are permitted in values, skip to the end. |
| 8341 | */ |
| 8342 | if (is_cookie2) |
| 8343 | next = find_hdr_value_end(next, hdr_end); |
| 8344 | else |
| 8345 | next = hdr_end; |
| 8346 | } |
| 8347 | |
| 8348 | /* Now everything is as on the diagram above */ |
| 8349 | |
| 8350 | /* Ignore cookies with no equal sign */ |
| 8351 | if (equal == val_end) |
| 8352 | continue; |
| 8353 | |
| 8354 | /* If there are spaces around the equal sign, we need to |
| 8355 | * strip them otherwise we'll get trouble for cookie captures, |
| 8356 | * or even for rewrites. Since this happens extremely rarely, |
| 8357 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8358 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8359 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8360 | int stripped_before = 0; |
| 8361 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8362 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8363 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8364 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8365 | equal += stripped_before; |
| 8366 | val_beg += stripped_before; |
| 8367 | } |
| 8368 | |
| 8369 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8370 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8371 | val_beg += stripped_after; |
| 8372 | stripped_before += stripped_after; |
| 8373 | } |
| 8374 | |
| 8375 | val_end += stripped_before; |
| 8376 | next += stripped_before; |
| 8377 | hdr_end += stripped_before; |
| 8378 | hdr_next += stripped_before; |
| 8379 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8380 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8381 | } |
| 8382 | |
| 8383 | /* First, let's see if we want to capture this cookie. We check |
| 8384 | * that we don't already have a server side cookie, because we |
| 8385 | * can only capture one. Also as an optimisation, we ignore |
| 8386 | * cookies shorter than the declared name. |
| 8387 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8388 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8389 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8390 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 8391 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8392 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8393 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8394 | Alert("HTTP logging : out of memory.\n"); |
| 8395 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8396 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8397 | if (log_len > sess->fe->capture_len) |
| 8398 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8399 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8400 | txn->srv_cookie[log_len] = 0; |
| 8401 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8402 | } |
| 8403 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8404 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8405 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8406 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8407 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8408 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8409 | /* assume passive cookie by default */ |
| 8410 | txn->flags &= ~TX_SCK_MASK; |
| 8411 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8412 | |
| 8413 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8414 | * this occurrence because we'll insert another one later. |
| 8415 | * 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] | 8416 | * a direct access. |
| 8417 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8418 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8419 | /* The "preserve" flag was set, we don't want to touch the |
| 8420 | * server's cookie. |
| 8421 | */ |
| 8422 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8423 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8424 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8425 | /* this cookie must be deleted */ |
| 8426 | if (*prev == ':' && next == hdr_end) { |
| 8427 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8428 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8429 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8430 | txn->hdr_idx.used--; |
| 8431 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8432 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8433 | hdr_next += delta; |
| 8434 | http_msg_move_end(&txn->rsp, delta); |
| 8435 | /* note: while both invalid now, <next> and <hdr_end> |
| 8436 | * are still equal, so the for() will stop as expected. |
| 8437 | */ |
| 8438 | } else { |
| 8439 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8440 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8441 | next = prev; |
| 8442 | hdr_end += delta; |
| 8443 | hdr_next += delta; |
| 8444 | cur_hdr->len += delta; |
| 8445 | http_msg_move_end(&txn->rsp, delta); |
| 8446 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8447 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8448 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8449 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8450 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8451 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8452 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8453 | * with this server since we know it. |
| 8454 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8455 | 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] | 8456 | next += delta; |
| 8457 | hdr_end += delta; |
| 8458 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8459 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8460 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8461 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8462 | txn->flags &= ~TX_SCK_MASK; |
| 8463 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8464 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8465 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8466 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8467 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8468 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8469 | 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] | 8470 | next += delta; |
| 8471 | hdr_end += delta; |
| 8472 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8473 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8474 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8475 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8476 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8477 | txn->flags &= ~TX_SCK_MASK; |
| 8478 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8479 | } |
| 8480 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8481 | /* that's done for this cookie, check the next one on the same |
| 8482 | * line when next != hdr_end (only if is_cookie2). |
| 8483 | */ |
| 8484 | } |
| 8485 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8486 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8487 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8488 | } |
| 8489 | |
| 8490 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8491 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8492 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8493 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8494 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8495 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8496 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8497 | char *p1, *p2; |
| 8498 | |
| 8499 | char *cur_ptr, *cur_end, *cur_next; |
| 8500 | int cur_idx; |
| 8501 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8502 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8503 | return; |
| 8504 | |
| 8505 | /* Iterate through the headers. |
| 8506 | * we start with the start line. |
| 8507 | */ |
| 8508 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8509 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8510 | |
| 8511 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8512 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8513 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8514 | |
| 8515 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8516 | cur_ptr = cur_next; |
| 8517 | cur_end = cur_ptr + cur_hdr->len; |
| 8518 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8519 | |
| 8520 | /* We have one full header between cur_ptr and cur_end, and the |
| 8521 | * next header starts at cur_next. We're only interested in |
| 8522 | * "Cookie:" headers. |
| 8523 | */ |
| 8524 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8525 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8526 | if (val) { |
| 8527 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8528 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8529 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8530 | return; |
| 8531 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8532 | } |
| 8533 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8534 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8535 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8536 | continue; |
| 8537 | |
| 8538 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8539 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8540 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8541 | |
| 8542 | if (p1 >= cur_end) /* no more info */ |
| 8543 | continue; |
| 8544 | |
| 8545 | /* p1 is at the beginning of the value */ |
| 8546 | p2 = p1; |
| 8547 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8548 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8549 | p2++; |
| 8550 | |
| 8551 | /* we have a complete value between p1 and p2 */ |
| 8552 | if (p2 < cur_end && *p2 == '=') { |
| 8553 | /* we have something of the form no-cache="set-cookie" */ |
| 8554 | if ((cur_end - p1 >= 21) && |
| 8555 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8556 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8557 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8558 | continue; |
| 8559 | } |
| 8560 | |
| 8561 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8562 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8563 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8564 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8565 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8566 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8567 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8568 | return; |
| 8569 | } |
| 8570 | |
| 8571 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8572 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8573 | continue; |
| 8574 | } |
| 8575 | } |
| 8576 | } |
| 8577 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8578 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8579 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8580 | * 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] | 8581 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8582 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8583 | * 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] | 8584 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8585 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8586 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8587 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8588 | 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] | 8589 | { |
| 8590 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8591 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8592 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8593 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8594 | if (!uri_auth) |
| 8595 | return 0; |
| 8596 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8597 | 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] | 8598 | return 0; |
| 8599 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8600 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8601 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8602 | return 0; |
| 8603 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8604 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8605 | return 0; |
| 8606 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8607 | return 1; |
| 8608 | } |
| 8609 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8610 | /* |
| 8611 | * 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] | 8612 | * By default it tries to report the error position as msg->err_pos. However if |
| 8613 | * this one is not set, it will then report msg->next, which is the last known |
| 8614 | * 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] | 8615 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8616 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8617 | void http_capture_bad_message(struct error_snapshot *es, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8618 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8619 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8620 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8621 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8622 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8623 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8624 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8625 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8626 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8627 | len1 = MIN(len1, es->len); |
| 8628 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8629 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8630 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8631 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8632 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8633 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8634 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8635 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8636 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8637 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8638 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8639 | es->when = date; // user-visible date |
| 8640 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8641 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8642 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8643 | if (objt_conn(sess->origin)) |
| 8644 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8645 | else |
| 8646 | memset(&es->src, 0, sizeof(es->src)); |
| 8647 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8648 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8649 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8650 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8651 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8652 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8653 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8654 | es->b_out = chn->buf->o; |
| 8655 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8656 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8657 | es->m_clen = msg->chunk_len; |
| 8658 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8659 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8660 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8661 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8662 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8663 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8664 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8665 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8666 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8667 | * 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] | 8668 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8669 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8670 | * 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] | 8671 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8672 | 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] | 8673 | struct hdr_idx *idx, int occ, |
| 8674 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8675 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8676 | struct hdr_ctx local_ctx; |
| 8677 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8678 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8679 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8680 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8681 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8682 | if (!ctx) { |
| 8683 | local_ctx.idx = 0; |
| 8684 | ctx = &local_ctx; |
| 8685 | } |
| 8686 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8687 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8688 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8689 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8690 | occ--; |
| 8691 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8692 | *vptr = ctx->line + ctx->val; |
| 8693 | *vlen = ctx->vlen; |
| 8694 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8695 | } |
| 8696 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8697 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8698 | } |
| 8699 | |
| 8700 | /* negative occurrence, we scan all the list then walk back */ |
| 8701 | if (-occ > MAX_HDR_HISTORY) |
| 8702 | return 0; |
| 8703 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8704 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8705 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8706 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8707 | len_hist[hist_ptr] = ctx->vlen; |
| 8708 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8709 | hist_ptr = 0; |
| 8710 | found++; |
| 8711 | } |
| 8712 | if (-occ > found) |
| 8713 | return 0; |
| 8714 | /* 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] | 8715 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8716 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8717 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8718 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8719 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8720 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8721 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8722 | *vptr = ptr_hist[hist_ptr]; |
| 8723 | *vlen = len_hist[hist_ptr]; |
| 8724 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8725 | } |
| 8726 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8727 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8728 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8729 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8730 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8731 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8732 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8733 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8734 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8735 | * line header values and does not stop at commas. |
| 8736 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8737 | */ |
| 8738 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8739 | struct hdr_idx *idx, int occ, |
| 8740 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8741 | { |
| 8742 | struct hdr_ctx local_ctx; |
| 8743 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8744 | int len_hist[MAX_HDR_HISTORY]; |
| 8745 | unsigned int hist_ptr; |
| 8746 | int found; |
| 8747 | |
| 8748 | if (!ctx) { |
| 8749 | local_ctx.idx = 0; |
| 8750 | ctx = &local_ctx; |
| 8751 | } |
| 8752 | |
| 8753 | if (occ >= 0) { |
| 8754 | /* search from the beginning */ |
| 8755 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8756 | occ--; |
| 8757 | if (occ <= 0) { |
| 8758 | *vptr = ctx->line + ctx->val; |
| 8759 | *vlen = ctx->vlen; |
| 8760 | return 1; |
| 8761 | } |
| 8762 | } |
| 8763 | return 0; |
| 8764 | } |
| 8765 | |
| 8766 | /* negative occurrence, we scan all the list then walk back */ |
| 8767 | if (-occ > MAX_HDR_HISTORY) |
| 8768 | return 0; |
| 8769 | |
| 8770 | found = hist_ptr = 0; |
| 8771 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8772 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8773 | len_hist[hist_ptr] = ctx->vlen; |
| 8774 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8775 | hist_ptr = 0; |
| 8776 | found++; |
| 8777 | } |
| 8778 | if (-occ > found) |
| 8779 | return 0; |
| 8780 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8781 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8782 | */ |
| 8783 | hist_ptr += occ; |
| 8784 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8785 | hist_ptr -= MAX_HDR_HISTORY; |
| 8786 | *vptr = ptr_hist[hist_ptr]; |
| 8787 | *vlen = len_hist[hist_ptr]; |
| 8788 | return 1; |
| 8789 | } |
| 8790 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8791 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8792 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8793 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8794 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8795 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8796 | 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] | 8797 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8798 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8799 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8800 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8801 | 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] | 8802 | dir, |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8803 | 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] | 8804 | 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] | 8805 | |
| 8806 | for (max = 0; start + max < end; max++) |
| 8807 | if (start[max] == '\r' || start[max] == '\n') |
| 8808 | break; |
| 8809 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8810 | UBOUND(max, trash.size - trash.len - 3); |
| 8811 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8812 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8813 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8814 | } |
| 8815 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8816 | |
| 8817 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 8818 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 8819 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 8820 | * assigned to the stream and returned. |
| 8821 | */ |
| 8822 | struct http_txn *http_alloc_txn(struct stream *s) |
| 8823 | { |
| 8824 | struct http_txn *txn = s->txn; |
| 8825 | |
| 8826 | if (txn) |
| 8827 | return txn; |
| 8828 | |
| 8829 | txn = pool_alloc2(pool2_http_txn); |
| 8830 | if (!txn) |
| 8831 | return txn; |
| 8832 | |
| 8833 | txn->hdr_idx.size = global.tune.max_http_hdr; |
| 8834 | txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx); |
| 8835 | if (!txn->hdr_idx.v) { |
| 8836 | pool_free2(pool2_http_txn, txn); |
| 8837 | return NULL; |
| 8838 | } |
| 8839 | |
| 8840 | s->txn = txn; |
| 8841 | return txn; |
| 8842 | } |
| 8843 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8844 | void http_txn_reset_req(struct http_txn *txn) |
| 8845 | { |
| 8846 | txn->req.flags = 0; |
| 8847 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 8848 | txn->req.next = 0; |
| 8849 | txn->req.chunk_len = 0LL; |
| 8850 | txn->req.body_len = 0LL; |
| 8851 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8852 | } |
| 8853 | |
| 8854 | void http_txn_reset_res(struct http_txn *txn) |
| 8855 | { |
| 8856 | txn->rsp.flags = 0; |
| 8857 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 8858 | txn->rsp.next = 0; |
| 8859 | txn->rsp.chunk_len = 0LL; |
| 8860 | txn->rsp.body_len = 0LL; |
| 8861 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 8862 | } |
| 8863 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8864 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8865 | * 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] | 8866 | * the required fields are properly allocated and that we only need to (re)init |
| 8867 | * them. This should be used before processing any new request. |
| 8868 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8869 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8870 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8871 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8872 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8873 | |
| 8874 | txn->flags = 0; |
| 8875 | txn->status = -1; |
| 8876 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8877 | txn->cookie_first_date = 0; |
| 8878 | txn->cookie_last_date = 0; |
| 8879 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8880 | txn->srv_cookie = NULL; |
| 8881 | txn->cli_cookie = NULL; |
| 8882 | txn->uri = NULL; |
| 8883 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8884 | http_txn_reset_req(txn); |
| 8885 | http_txn_reset_res(txn); |
| 8886 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8887 | txn->req.chn = &s->req; |
| 8888 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8889 | |
| 8890 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8891 | |
| 8892 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8893 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8894 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8895 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8896 | if (txn->hdr_idx.v) |
| 8897 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 8898 | |
| 8899 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 8900 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8901 | } |
| 8902 | |
| 8903 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8904 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8905 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8906 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8907 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8908 | |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8909 | /* release any possible compression context */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8910 | if (s->flags & SF_COMP_READY) |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8911 | s->comp_algo->end(&s->comp_ctx); |
| 8912 | s->comp_algo = NULL; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8913 | s->flags &= ~SF_COMP_READY; |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8914 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8915 | /* these ones will have been dynamically allocated */ |
| 8916 | pool_free2(pool2_requri, txn->uri); |
| 8917 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8918 | pool_free2(pool2_capture, txn->srv_cookie); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8919 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8920 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8921 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8922 | txn->uri = NULL; |
| 8923 | txn->srv_cookie = NULL; |
| 8924 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8925 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8926 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8927 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8928 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8929 | pool_free2(h->pool, s->req_cap[h->index]); |
| 8930 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8931 | } |
| 8932 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8933 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8934 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8935 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8936 | pool_free2(h->pool, s->res_cap[h->index]); |
| 8937 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8938 | } |
| 8939 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 8940 | vars_prune(&s->vars_txn, s); |
| 8941 | vars_prune(&s->vars_reqres, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8942 | } |
| 8943 | |
| 8944 | /* 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] | 8945 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8946 | { |
| 8947 | http_end_txn(s); |
| 8948 | http_init_txn(s); |
| 8949 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 8950 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 8951 | * any rulelist match the NULL pointer. |
| 8952 | */ |
| 8953 | s->current_rule_list = NULL; |
| 8954 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8955 | s->be = strm_fe(s); |
| 8956 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8957 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8958 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8959 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8960 | /* re-init store persistence */ |
| 8961 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8962 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8963 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8964 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8965 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8966 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8967 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8968 | /* We must trim any excess data from the response buffer, because we |
| 8969 | * may have blocked an invalid response from a server that we don't |
| 8970 | * want to accidentely forward once we disable the analysers, nor do |
| 8971 | * we want those data to come along with next response. A typical |
| 8972 | * example of such data would be from a buggy server responding to |
| 8973 | * a HEAD with some data, or sending more than the advertised |
| 8974 | * content-length. |
| 8975 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8976 | if (unlikely(s->res.buf->i)) |
| 8977 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8978 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8979 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8980 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8981 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8982 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8983 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8984 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8985 | s->req.rex = TICK_ETERNITY; |
| 8986 | s->req.wex = TICK_ETERNITY; |
| 8987 | s->req.analyse_exp = TICK_ETERNITY; |
| 8988 | s->res.rex = TICK_ETERNITY; |
| 8989 | s->res.wex = TICK_ETERNITY; |
| 8990 | s->res.analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8991 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8992 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8993 | void free_http_res_rules(struct list *r) |
| 8994 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8995 | struct act_rule *tr, *pr; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8996 | |
| 8997 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8998 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8999 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9000 | free(pr); |
| 9001 | } |
| 9002 | } |
| 9003 | |
| 9004 | void free_http_req_rules(struct list *r) |
| 9005 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9006 | struct act_rule *tr, *pr; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9007 | |
| 9008 | list_for_each_entry_safe(pr, tr, r, list) { |
| 9009 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9010 | if (pr->action == ACT_HTTP_REQ_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9011 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9012 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9013 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9014 | free(pr); |
| 9015 | } |
| 9016 | } |
| 9017 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9018 | /* parse an "http-request" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9019 | 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] | 9020 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9021 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 9022 | struct action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9023 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9024 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9025 | |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9026 | rule = (struct act_rule*)calloc(1, sizeof(struct act_rule)); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9027 | if (!rule) { |
| 9028 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9029 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9030 | } |
| 9031 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9032 | rule->deny_status = HTTP_ERR_403; |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9033 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9034 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9035 | cur_arg = 1; |
Willy Tarreau | 5bd6759 | 2014-04-28 22:00:46 +0200 | [diff] [blame] | 9036 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) { |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9037 | int code; |
| 9038 | int hc; |
| 9039 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9040 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9041 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9042 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 9043 | cur_arg++; |
| 9044 | if (!args[cur_arg]) { |
| 9045 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 9046 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9047 | goto out_err; |
| 9048 | } |
| 9049 | |
| 9050 | code = atol(args[cur_arg]); |
| 9051 | cur_arg++; |
| 9052 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 9053 | if (http_err_codes[hc] == code) { |
| 9054 | rule->deny_status = hc; |
| 9055 | break; |
| 9056 | } |
| 9057 | } |
| 9058 | |
| 9059 | if (hc >= HTTP_ERR_SIZE) { |
| 9060 | Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n", |
| 9061 | file, linenum, code); |
| 9062 | } |
| 9063 | } |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 9064 | } else if (!strcmp(args[0], "tarpit")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9065 | rule->action = ACT_HTTP_REQ_TARPIT; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 9066 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9067 | } else if (!strcmp(args[0], "auth")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9068 | rule->action = ACT_HTTP_REQ_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9069 | cur_arg = 1; |
| 9070 | |
| 9071 | while(*args[cur_arg]) { |
| 9072 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9073 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9074 | cur_arg+=2; |
| 9075 | continue; |
| 9076 | } else |
| 9077 | break; |
| 9078 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9079 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9080 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9081 | cur_arg = 1; |
| 9082 | |
| 9083 | if (!*args[cur_arg] || |
| 9084 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9085 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 9086 | file, linenum, args[0]); |
| 9087 | goto out_err; |
| 9088 | } |
| 9089 | rule->arg.nice = atoi(args[cur_arg]); |
| 9090 | if (rule->arg.nice < -1024) |
| 9091 | rule->arg.nice = -1024; |
| 9092 | else if (rule->arg.nice > 1024) |
| 9093 | rule->arg.nice = 1024; |
| 9094 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9095 | } else if (!strcmp(args[0], "set-tos")) { |
| 9096 | #ifdef IP_TOS |
| 9097 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9098 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9099 | cur_arg = 1; |
| 9100 | |
| 9101 | if (!*args[cur_arg] || |
| 9102 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9103 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9104 | file, linenum, args[0]); |
| 9105 | goto out_err; |
| 9106 | } |
| 9107 | |
| 9108 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9109 | if (err && *err != '\0') { |
| 9110 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9111 | file, linenum, err, args[0]); |
| 9112 | goto out_err; |
| 9113 | } |
| 9114 | cur_arg++; |
| 9115 | #else |
| 9116 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9117 | goto out_err; |
| 9118 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9119 | } else if (!strcmp(args[0], "set-mark")) { |
| 9120 | #ifdef SO_MARK |
| 9121 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9122 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9123 | cur_arg = 1; |
| 9124 | |
| 9125 | if (!*args[cur_arg] || |
| 9126 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9127 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9128 | file, linenum, args[0]); |
| 9129 | goto out_err; |
| 9130 | } |
| 9131 | |
| 9132 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9133 | if (err && *err != '\0') { |
| 9134 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9135 | file, linenum, err, args[0]); |
| 9136 | goto out_err; |
| 9137 | } |
| 9138 | cur_arg++; |
| 9139 | global.last_checks |= LSTCHK_NETADM; |
| 9140 | #else |
| 9141 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9142 | goto out_err; |
| 9143 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9144 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9145 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9146 | cur_arg = 1; |
| 9147 | |
| 9148 | if (!*args[cur_arg] || |
| 9149 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9150 | bad_log_level: |
| 9151 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9152 | file, linenum, args[0]); |
| 9153 | goto out_err; |
| 9154 | } |
| 9155 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9156 | rule->arg.loglevel = -1; |
| 9157 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 9158 | goto bad_log_level; |
| 9159 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9160 | } 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] | 9161 | 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] | 9162 | cur_arg = 1; |
| 9163 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 9164 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9165 | (*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] | 9166 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9167 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9168 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9169 | } |
| 9170 | |
| 9171 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9172 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9173 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 9174 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9175 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9176 | 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] | 9177 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9178 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9179 | free(proxy->conf.lfs_file); |
| 9180 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9181 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9182 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9183 | } 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] | 9184 | 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] | 9185 | cur_arg = 1; |
| 9186 | |
| 9187 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 9188 | (*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] | 9189 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 9190 | file, linenum, args[0]); |
| 9191 | goto out_err; |
| 9192 | } |
| 9193 | |
| 9194 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9195 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9196 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9197 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9198 | error = NULL; |
| 9199 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9200 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9201 | args[cur_arg + 1], error); |
| 9202 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9203 | goto out_err; |
| 9204 | } |
| 9205 | |
| 9206 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9207 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9208 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9209 | file, linenum); |
| 9210 | |
| 9211 | free(proxy->conf.lfs_file); |
| 9212 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9213 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9214 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9215 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9216 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9217 | cur_arg = 1; |
| 9218 | |
| 9219 | if (!*args[cur_arg] || |
| 9220 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9221 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9222 | file, linenum, args[0]); |
| 9223 | goto out_err; |
| 9224 | } |
| 9225 | |
| 9226 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9227 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9228 | |
| 9229 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9230 | free(proxy->conf.lfs_file); |
| 9231 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9232 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9233 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9234 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9235 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 9236 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9237 | struct sample_expr *expr; |
| 9238 | unsigned int where; |
| 9239 | char *err = NULL; |
| 9240 | |
| 9241 | cur_arg = 1; |
| 9242 | proxy->conf.args.ctx = ARGC_TRK; |
| 9243 | |
| 9244 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9245 | if (!expr) { |
| 9246 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9247 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9248 | free(err); |
| 9249 | goto out_err; |
| 9250 | } |
| 9251 | |
| 9252 | where = 0; |
| 9253 | if (proxy->cap & PR_CAP_FE) |
| 9254 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9255 | if (proxy->cap & PR_CAP_BE) |
| 9256 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9257 | |
| 9258 | if (!(expr->fetch->val & where)) { |
| 9259 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9260 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9261 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9262 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9263 | free(expr); |
| 9264 | goto out_err; |
| 9265 | } |
| 9266 | |
| 9267 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9268 | cur_arg++; |
| 9269 | if (!args[cur_arg]) { |
| 9270 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 9271 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9272 | free(expr); |
| 9273 | goto out_err; |
| 9274 | } |
| 9275 | /* we copy the table name for now, it will be resolved later */ |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9276 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9277 | cur_arg++; |
| 9278 | } |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9279 | rule->arg.trk_ctr.expr = expr; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9280 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9281 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9282 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9283 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9284 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9285 | 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] | 9286 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9287 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9288 | goto out_err; |
| 9289 | } |
| 9290 | |
| 9291 | /* this redirect rule might already contain a parsed condition which |
| 9292 | * we'll pass to the http-request rule. |
| 9293 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9294 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9295 | rule->arg.redir = redir; |
| 9296 | rule->cond = redir->cond; |
| 9297 | redir->cond = NULL; |
| 9298 | cur_arg = 2; |
| 9299 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9300 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9301 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9302 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9303 | /* |
| 9304 | * '+ 8' for 'add-acl(' |
| 9305 | * '- 9' for 'add-acl(' + trailing ')' |
| 9306 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9307 | 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] | 9308 | |
| 9309 | cur_arg = 1; |
| 9310 | |
| 9311 | if (!*args[cur_arg] || |
| 9312 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9313 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9314 | file, linenum, args[0]); |
| 9315 | goto out_err; |
| 9316 | } |
| 9317 | |
| 9318 | LIST_INIT(&rule->arg.map.key); |
| 9319 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9320 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9321 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9322 | file, linenum); |
| 9323 | free(proxy->conf.lfs_file); |
| 9324 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9325 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9326 | cur_arg += 1; |
| 9327 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9328 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9329 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9330 | /* |
| 9331 | * '+ 8' for 'del-acl(' |
| 9332 | * '- 9' for 'del-acl(' + trailing ')' |
| 9333 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9334 | 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] | 9335 | |
| 9336 | cur_arg = 1; |
| 9337 | |
| 9338 | if (!*args[cur_arg] || |
| 9339 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9340 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9341 | file, linenum, args[0]); |
| 9342 | goto out_err; |
| 9343 | } |
| 9344 | |
| 9345 | LIST_INIT(&rule->arg.map.key); |
| 9346 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9347 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9348 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9349 | file, linenum); |
| 9350 | free(proxy->conf.lfs_file); |
| 9351 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9352 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9353 | cur_arg += 1; |
| 9354 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9355 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9356 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9357 | /* |
| 9358 | * '+ 8' for 'del-map(' |
| 9359 | * '- 9' for 'del-map(' + trailing ')' |
| 9360 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9361 | 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] | 9362 | |
| 9363 | cur_arg = 1; |
| 9364 | |
| 9365 | if (!*args[cur_arg] || |
| 9366 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9367 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9368 | file, linenum, args[0]); |
| 9369 | goto out_err; |
| 9370 | } |
| 9371 | |
| 9372 | LIST_INIT(&rule->arg.map.key); |
| 9373 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9374 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9375 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9376 | file, linenum); |
| 9377 | free(proxy->conf.lfs_file); |
| 9378 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9379 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9380 | cur_arg += 1; |
| 9381 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9382 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9383 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9384 | /* |
| 9385 | * '+ 8' for 'set-map(' |
| 9386 | * '- 9' for 'set-map(' + trailing ')' |
| 9387 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9388 | 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] | 9389 | |
| 9390 | cur_arg = 1; |
| 9391 | |
| 9392 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9393 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9394 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9395 | file, linenum, args[0]); |
| 9396 | goto out_err; |
| 9397 | } |
| 9398 | |
| 9399 | LIST_INIT(&rule->arg.map.key); |
| 9400 | LIST_INIT(&rule->arg.map.value); |
| 9401 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9402 | |
| 9403 | /* key pattern */ |
| 9404 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9405 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9406 | file, linenum); |
| 9407 | |
| 9408 | /* value pattern */ |
| 9409 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9410 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9411 | file, linenum); |
| 9412 | free(proxy->conf.lfs_file); |
| 9413 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9414 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9415 | |
| 9416 | cur_arg += 2; |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 9417 | } else if (strncmp(args[0], "set-src", 7) == 0) { |
| 9418 | struct sample_expr *expr; |
| 9419 | unsigned int where; |
| 9420 | char *err = NULL; |
| 9421 | |
| 9422 | cur_arg = 1; |
| 9423 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9424 | |
| 9425 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9426 | if (!expr) { |
| 9427 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9428 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9429 | free(err); |
| 9430 | goto out_err; |
| 9431 | } |
| 9432 | |
| 9433 | where = 0; |
| 9434 | if (proxy->cap & PR_CAP_FE) |
| 9435 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9436 | if (proxy->cap & PR_CAP_BE) |
| 9437 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9438 | |
| 9439 | if (!(expr->fetch->val & where)) { |
| 9440 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9441 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9442 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9443 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9444 | free(expr); |
| 9445 | goto out_err; |
| 9446 | } |
| 9447 | |
Thierry FOURNIER | a002dc9 | 2015-07-31 08:50:51 +0200 | [diff] [blame] | 9448 | rule->arg.expr = expr; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9449 | rule->action = ACT_HTTP_REQ_SET_SRC; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9450 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9451 | char *errmsg = NULL; |
| 9452 | cur_arg = 1; |
| 9453 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9454 | rule->from = ACT_F_HTTP_REQ; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9455 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9456 | 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] | 9457 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9458 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9459 | free(errmsg); |
| 9460 | goto out_err; |
| 9461 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9462 | } else { |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 9463 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'set-var', 'set-src', but got '%s'%s.\n", |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9464 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9465 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9466 | } |
| 9467 | |
| 9468 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9469 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9470 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9471 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9472 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9473 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9474 | file, linenum, args[0], errmsg); |
| 9475 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9476 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9477 | } |
| 9478 | rule->cond = cond; |
| 9479 | } |
| 9480 | else if (*args[cur_arg]) { |
| 9481 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9482 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9483 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9484 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9485 | } |
| 9486 | |
| 9487 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9488 | out_err: |
| 9489 | free(rule); |
| 9490 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9491 | } |
| 9492 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9493 | /* parse an "http-respose" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9494 | 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] | 9495 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9496 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 9497 | struct action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9498 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9499 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9500 | |
| 9501 | rule = calloc(1, sizeof(*rule)); |
| 9502 | if (!rule) { |
| 9503 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9504 | goto out_err; |
| 9505 | } |
| 9506 | |
| 9507 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9508 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9509 | cur_arg = 1; |
| 9510 | } else if (!strcmp(args[0], "deny")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9511 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9512 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9513 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9514 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9515 | cur_arg = 1; |
| 9516 | |
| 9517 | if (!*args[cur_arg] || |
| 9518 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9519 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9520 | file, linenum, args[0]); |
| 9521 | goto out_err; |
| 9522 | } |
| 9523 | rule->arg.nice = atoi(args[cur_arg]); |
| 9524 | if (rule->arg.nice < -1024) |
| 9525 | rule->arg.nice = -1024; |
| 9526 | else if (rule->arg.nice > 1024) |
| 9527 | rule->arg.nice = 1024; |
| 9528 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9529 | } else if (!strcmp(args[0], "set-tos")) { |
| 9530 | #ifdef IP_TOS |
| 9531 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9532 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9533 | cur_arg = 1; |
| 9534 | |
| 9535 | if (!*args[cur_arg] || |
| 9536 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9537 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9538 | file, linenum, args[0]); |
| 9539 | goto out_err; |
| 9540 | } |
| 9541 | |
| 9542 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9543 | if (err && *err != '\0') { |
| 9544 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9545 | file, linenum, err, args[0]); |
| 9546 | goto out_err; |
| 9547 | } |
| 9548 | cur_arg++; |
| 9549 | #else |
| 9550 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9551 | goto out_err; |
| 9552 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9553 | } else if (!strcmp(args[0], "set-mark")) { |
| 9554 | #ifdef SO_MARK |
| 9555 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9556 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9557 | cur_arg = 1; |
| 9558 | |
| 9559 | if (!*args[cur_arg] || |
| 9560 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9561 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9562 | file, linenum, args[0]); |
| 9563 | goto out_err; |
| 9564 | } |
| 9565 | |
| 9566 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9567 | if (err && *err != '\0') { |
| 9568 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9569 | file, linenum, err, args[0]); |
| 9570 | goto out_err; |
| 9571 | } |
| 9572 | cur_arg++; |
| 9573 | global.last_checks |= LSTCHK_NETADM; |
| 9574 | #else |
| 9575 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9576 | goto out_err; |
| 9577 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9578 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9579 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9580 | cur_arg = 1; |
| 9581 | |
| 9582 | if (!*args[cur_arg] || |
| 9583 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9584 | bad_log_level: |
| 9585 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9586 | file, linenum, args[0]); |
| 9587 | goto out_err; |
| 9588 | } |
| 9589 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9590 | rule->arg.loglevel = -1; |
| 9591 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 9592 | goto bad_log_level; |
| 9593 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9594 | } 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] | 9595 | 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] | 9596 | cur_arg = 1; |
| 9597 | |
| 9598 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9599 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9600 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9601 | file, linenum, args[0]); |
| 9602 | goto out_err; |
| 9603 | } |
| 9604 | |
| 9605 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9606 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9607 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9608 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9609 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9610 | 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] | 9611 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9612 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9613 | free(proxy->conf.lfs_file); |
| 9614 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9615 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9616 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9617 | } 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] | 9618 | 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] | 9619 | cur_arg = 1; |
| 9620 | |
| 9621 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9622 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9623 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9624 | file, linenum, args[0]); |
| 9625 | goto out_err; |
| 9626 | } |
| 9627 | |
| 9628 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9629 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9630 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9631 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9632 | error = NULL; |
| 9633 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9634 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9635 | args[cur_arg + 1], error); |
| 9636 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9637 | goto out_err; |
| 9638 | } |
| 9639 | |
| 9640 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9641 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9642 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9643 | file, linenum); |
| 9644 | |
| 9645 | free(proxy->conf.lfs_file); |
| 9646 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9647 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9648 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9649 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9650 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9651 | cur_arg = 1; |
| 9652 | |
| 9653 | if (!*args[cur_arg] || |
| 9654 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9655 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9656 | file, linenum, args[0]); |
| 9657 | goto out_err; |
| 9658 | } |
| 9659 | |
| 9660 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9661 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9662 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9663 | proxy->conf.args.ctx = ARGC_HRS; |
| 9664 | free(proxy->conf.lfs_file); |
| 9665 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9666 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9667 | cur_arg += 1; |
| 9668 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9669 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9670 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9671 | /* |
| 9672 | * '+ 8' for 'add-acl(' |
| 9673 | * '- 9' for 'add-acl(' + trailing ')' |
| 9674 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9675 | 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] | 9676 | |
| 9677 | cur_arg = 1; |
| 9678 | |
| 9679 | if (!*args[cur_arg] || |
| 9680 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9681 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9682 | file, linenum, args[0]); |
| 9683 | goto out_err; |
| 9684 | } |
| 9685 | |
| 9686 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9687 | proxy->conf.args.ctx = ARGC_HRS; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9688 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9689 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9690 | file, linenum); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9691 | free(proxy->conf.lfs_file); |
| 9692 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9693 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9694 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9695 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9696 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9697 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9698 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9699 | /* |
| 9700 | * '+ 8' for 'del-acl(' |
| 9701 | * '- 9' for 'del-acl(' + trailing ')' |
| 9702 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9703 | 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] | 9704 | |
| 9705 | cur_arg = 1; |
| 9706 | |
| 9707 | if (!*args[cur_arg] || |
| 9708 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9709 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9710 | file, linenum, args[0]); |
| 9711 | goto out_err; |
| 9712 | } |
| 9713 | |
| 9714 | LIST_INIT(&rule->arg.map.key); |
| 9715 | proxy->conf.args.ctx = ARGC_HRS; |
| 9716 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9717 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9718 | file, linenum); |
| 9719 | free(proxy->conf.lfs_file); |
| 9720 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9721 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9722 | cur_arg += 1; |
| 9723 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9724 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9725 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9726 | /* |
| 9727 | * '+ 8' for 'del-map(' |
| 9728 | * '- 9' for 'del-map(' + trailing ')' |
| 9729 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9730 | 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] | 9731 | |
| 9732 | cur_arg = 1; |
| 9733 | |
| 9734 | if (!*args[cur_arg] || |
| 9735 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9736 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9737 | file, linenum, args[0]); |
| 9738 | goto out_err; |
| 9739 | } |
| 9740 | |
| 9741 | LIST_INIT(&rule->arg.map.key); |
| 9742 | proxy->conf.args.ctx = ARGC_HRS; |
| 9743 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9744 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9745 | file, linenum); |
| 9746 | free(proxy->conf.lfs_file); |
| 9747 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9748 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9749 | cur_arg += 1; |
| 9750 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9751 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9752 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9753 | /* |
| 9754 | * '+ 8' for 'set-map(' |
| 9755 | * '- 9' for 'set-map(' + trailing ')' |
| 9756 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9757 | 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] | 9758 | |
| 9759 | cur_arg = 1; |
| 9760 | |
| 9761 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9762 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9763 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9764 | file, linenum, args[0]); |
| 9765 | goto out_err; |
| 9766 | } |
| 9767 | |
| 9768 | LIST_INIT(&rule->arg.map.key); |
| 9769 | LIST_INIT(&rule->arg.map.value); |
| 9770 | |
| 9771 | proxy->conf.args.ctx = ARGC_HRS; |
| 9772 | |
| 9773 | /* key pattern */ |
| 9774 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9775 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9776 | file, linenum); |
| 9777 | |
| 9778 | /* value pattern */ |
| 9779 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9780 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9781 | file, linenum); |
| 9782 | |
| 9783 | free(proxy->conf.lfs_file); |
| 9784 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9785 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9786 | |
| 9787 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9788 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9789 | struct redirect_rule *redir; |
| 9790 | char *errmsg = NULL; |
| 9791 | |
| 9792 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) { |
| 9793 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9794 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9795 | goto out_err; |
| 9796 | } |
| 9797 | |
| 9798 | /* this redirect rule might already contain a parsed condition which |
| 9799 | * we'll pass to the http-request rule. |
| 9800 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9801 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9802 | rule->arg.redir = redir; |
| 9803 | rule->cond = redir->cond; |
| 9804 | redir->cond = NULL; |
| 9805 | cur_arg = 2; |
| 9806 | return rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9807 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9808 | char *errmsg = NULL; |
| 9809 | cur_arg = 1; |
| 9810 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9811 | rule->from = ACT_F_HTTP_RES; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9812 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9813 | 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] | 9814 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9815 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9816 | free(errmsg); |
| 9817 | goto out_err; |
| 9818 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9819 | } else { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 9820 | Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'del-acl', 'add-acl', 'del-map', 'set-map', 'set-var' but got '%s'%s.\n", |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9821 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 9822 | goto out_err; |
| 9823 | } |
| 9824 | |
| 9825 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9826 | struct acl_cond *cond; |
| 9827 | char *errmsg = NULL; |
| 9828 | |
| 9829 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9830 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9831 | file, linenum, args[0], errmsg); |
| 9832 | free(errmsg); |
| 9833 | goto out_err; |
| 9834 | } |
| 9835 | rule->cond = cond; |
| 9836 | } |
| 9837 | else if (*args[cur_arg]) { |
| 9838 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9839 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9840 | file, linenum, args[0], args[cur_arg]); |
| 9841 | goto out_err; |
| 9842 | } |
| 9843 | |
| 9844 | return rule; |
| 9845 | out_err: |
| 9846 | free(rule); |
| 9847 | return NULL; |
| 9848 | } |
| 9849 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9850 | /* 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] | 9851 | * 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] | 9852 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 9853 | * 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] | 9854 | */ |
| 9855 | 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] | 9856 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9857 | { |
| 9858 | struct redirect_rule *rule; |
| 9859 | int cur_arg; |
| 9860 | int type = REDIRECT_TYPE_NONE; |
| 9861 | int code = 302; |
| 9862 | const char *destination = NULL; |
| 9863 | const char *cookie = NULL; |
| 9864 | int cookie_set = 0; |
| 9865 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9866 | struct acl_cond *cond = NULL; |
| 9867 | |
| 9868 | cur_arg = 0; |
| 9869 | while (*(args[cur_arg])) { |
| 9870 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9871 | if (!*args[cur_arg + 1]) |
| 9872 | goto missing_arg; |
| 9873 | |
| 9874 | type = REDIRECT_TYPE_LOCATION; |
| 9875 | cur_arg++; |
| 9876 | destination = args[cur_arg]; |
| 9877 | } |
| 9878 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9879 | if (!*args[cur_arg + 1]) |
| 9880 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9881 | type = REDIRECT_TYPE_PREFIX; |
| 9882 | cur_arg++; |
| 9883 | destination = args[cur_arg]; |
| 9884 | } |
| 9885 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9886 | if (!*args[cur_arg + 1]) |
| 9887 | goto missing_arg; |
| 9888 | |
| 9889 | type = REDIRECT_TYPE_SCHEME; |
| 9890 | cur_arg++; |
| 9891 | destination = args[cur_arg]; |
| 9892 | } |
| 9893 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9894 | if (!*args[cur_arg + 1]) |
| 9895 | goto missing_arg; |
| 9896 | |
| 9897 | cur_arg++; |
| 9898 | cookie = args[cur_arg]; |
| 9899 | cookie_set = 1; |
| 9900 | } |
| 9901 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9902 | if (!*args[cur_arg + 1]) |
| 9903 | goto missing_arg; |
| 9904 | |
| 9905 | cur_arg++; |
| 9906 | cookie = args[cur_arg]; |
| 9907 | cookie_set = 0; |
| 9908 | } |
| 9909 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9910 | if (!*args[cur_arg + 1]) |
| 9911 | goto missing_arg; |
| 9912 | |
| 9913 | cur_arg++; |
| 9914 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9915 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9916 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9917 | "'%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] | 9918 | args[cur_arg - 1], args[cur_arg]); |
| 9919 | return NULL; |
| 9920 | } |
| 9921 | } |
| 9922 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9923 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9924 | } |
| 9925 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9926 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9927 | } |
| 9928 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9929 | strcmp(args[cur_arg], "unless") == 0) { |
| 9930 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 9931 | if (!cond) { |
| 9932 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9933 | return NULL; |
| 9934 | } |
| 9935 | break; |
| 9936 | } |
| 9937 | else { |
| 9938 | memprintf(errmsg, |
| 9939 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9940 | args[cur_arg]); |
| 9941 | return NULL; |
| 9942 | } |
| 9943 | cur_arg++; |
| 9944 | } |
| 9945 | |
| 9946 | if (type == REDIRECT_TYPE_NONE) { |
| 9947 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9948 | return NULL; |
| 9949 | } |
| 9950 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9951 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 9952 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 9953 | return NULL; |
| 9954 | } |
| 9955 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9956 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 9957 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9958 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9959 | |
| 9960 | if (!use_fmt) { |
| 9961 | /* old-style static redirect rule */ |
| 9962 | rule->rdr_str = strdup(destination); |
| 9963 | rule->rdr_len = strlen(destination); |
| 9964 | } |
| 9965 | else { |
| 9966 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9967 | |
| 9968 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9969 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9970 | * makes it hard for the user to configure a self-redirection. |
| 9971 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9972 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9973 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9974 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9975 | dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR |
| 9976 | : (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] | 9977 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9978 | free(curproxy->conf.lfs_file); |
| 9979 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9980 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9981 | } |
| 9982 | } |
| 9983 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9984 | if (cookie) { |
| 9985 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9986 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9987 | */ |
| 9988 | rule->cookie_len = strlen(cookie); |
| 9989 | if (cookie_set) { |
| 9990 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9991 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9992 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9993 | rule->cookie_len += 9; |
| 9994 | } else { |
| 9995 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9996 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9997 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9998 | rule->cookie_len += 20; |
| 9999 | } |
| 10000 | } |
| 10001 | rule->type = type; |
| 10002 | rule->code = code; |
| 10003 | rule->flags = flags; |
| 10004 | LIST_INIT(&rule->list); |
| 10005 | return rule; |
| 10006 | |
| 10007 | missing_arg: |
| 10008 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 10009 | return NULL; |
| 10010 | } |
| 10011 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10012 | /************************************************************************/ |
| 10013 | /* The code below is dedicated to ACL parsing and matching */ |
| 10014 | /************************************************************************/ |
| 10015 | |
| 10016 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10017 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 10018 | * which means that a request or response is ready. If some data is missing, |
| 10019 | * 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] | 10020 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 10021 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10022 | * |
| 10023 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10024 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 10025 | * decide whether or not an HTTP message is present ; |
| 10026 | * 0 if the requested data cannot be fetched or if it is certain that |
| 10027 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10028 | * 1 if an HTTP message is ready |
| 10029 | */ |
James Rosewell | 91a41cb | 2015-09-18 17:11:16 +0100 | [diff] [blame] | 10030 | 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] | 10031 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10032 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10033 | struct http_txn *txn; |
| 10034 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10035 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 10036 | /* Note: this function may only be used from places where |
| 10037 | * http_init_txn() has already been done, and implies that <s>, |
| 10038 | * <txn>, and <hdr_idx.v> are properly set. An extra check protects |
| 10039 | * against an eventual mistake in the fetch capability matrix. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10040 | */ |
| 10041 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10042 | if (!s) |
| 10043 | return 0; |
Thierry FOURNIER | ed08d6a | 2015-09-24 08:40:18 +0200 | [diff] [blame] | 10044 | if (!s->txn) { |
| 10045 | if (unlikely(!http_alloc_txn(s))) |
| 10046 | return 0; /* not enough memory */ |
| 10047 | http_init_txn(s); |
| 10048 | } |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10049 | txn = s->txn; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10050 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10051 | |
| 10052 | /* Check for a dependency on a request */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10053 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10054 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10055 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10056 | /* If the buffer does not leave enough free space at the end, |
| 10057 | * we must first realign it. |
| 10058 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10059 | if (s->req.buf->p > s->req.buf->data && |
| 10060 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 10061 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10062 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10063 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 10064 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10065 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10066 | |
| 10067 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10068 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10069 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 10070 | |
| 10071 | /* Still no valid request ? */ |
| 10072 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 10073 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10074 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10075 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10076 | } |
| 10077 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10078 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10079 | return 0; |
| 10080 | } |
| 10081 | |
| 10082 | /* OK we just got a valid HTTP request. We have some minor |
| 10083 | * preparation to perform so that further checks can rely |
| 10084 | * on HTTP tests. |
| 10085 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10086 | |
| 10087 | /* If the request was parsed but was too large, we must absolutely |
| 10088 | * return an error so that it is not processed. At the moment this |
| 10089 | * cannot happen, but if the parsers are to change in the future, |
| 10090 | * we want this check to be maintained. |
| 10091 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10092 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 10093 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10094 | msg->msg_state = HTTP_MSG_ERROR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10095 | smp->data.u.sint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10096 | return 1; |
| 10097 | } |
| 10098 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10099 | 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] | 10100 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 10101 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10102 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10103 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 10104 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10105 | } |
| 10106 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10107 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10108 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10109 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10110 | |
| 10111 | /* otherwise everything's ready for the request */ |
| 10112 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10113 | else { |
| 10114 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10115 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 10116 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10117 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10118 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10119 | } |
| 10120 | |
| 10121 | /* everything's OK */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10122 | smp->data.u.sint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10123 | return 1; |
| 10124 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10125 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10126 | /* 1. Check on METHOD |
| 10127 | * We use the pre-parsed method if it is known, and store its number as an |
| 10128 | * integer. If it is unknown, we use the pointer and the length. |
| 10129 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10130 | 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] | 10131 | { |
| 10132 | int len, meth; |
| 10133 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 10134 | len = strlen(text); |
| 10135 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10136 | |
| 10137 | pattern->val.i = meth; |
| 10138 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 10139 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10140 | pattern->len = len; |
| 10141 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10142 | else { |
| 10143 | pattern->ptr.str = NULL; |
| 10144 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10145 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10146 | return 1; |
| 10147 | } |
| 10148 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10149 | /* This function fetches the method of current HTTP request and stores |
| 10150 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 10151 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 10152 | * in <len> and <ptr> is NULL ; |
| 10153 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 10154 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10155 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10156 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10157 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10158 | 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] | 10159 | { |
| 10160 | int meth; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10161 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10162 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10163 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10164 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10165 | meth = txn->meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10166 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10167 | smp->data.u.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10168 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10169 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 10170 | /* ensure the indexes are not affected */ |
| 10171 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10172 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10173 | smp->data.u.meth.str.len = txn->req.sl.rq.m_l; |
| 10174 | smp->data.u.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10175 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10176 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10177 | return 1; |
| 10178 | } |
| 10179 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10180 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10181 | 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] | 10182 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10183 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10184 | struct pattern_list *lst; |
| 10185 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10186 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10187 | list_for_each_entry(lst, &expr->patterns, list) { |
| 10188 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10189 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10190 | /* well-known method */ |
| 10191 | if (pattern->val.i != HTTP_METH_OTHER) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10192 | if (smp->data.u.meth.meth == pattern->val.i) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10193 | return pattern; |
| 10194 | else |
| 10195 | continue; |
| 10196 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10197 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10198 | /* Other method, we must compare the strings */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10199 | if (pattern->len != smp->data.u.meth.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10200 | continue; |
| 10201 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10202 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10203 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) || |
| 10204 | (!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] | 10205 | return pattern; |
| 10206 | } |
| 10207 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10208 | } |
| 10209 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10210 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10211 | 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] | 10212 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10213 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10214 | char *ptr; |
| 10215 | int len; |
| 10216 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10217 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10218 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10219 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10220 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10221 | |
| 10222 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10223 | if (len <= 0) |
| 10224 | return 0; |
| 10225 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10226 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10227 | smp->data.u.str.str = ptr; |
| 10228 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10229 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10230 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10231 | return 1; |
| 10232 | } |
| 10233 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10234 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10235 | 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] | 10236 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10237 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10238 | char *ptr; |
| 10239 | int len; |
| 10240 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10241 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10242 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10243 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10244 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10245 | return 0; |
| 10246 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10247 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10248 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10249 | |
| 10250 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10251 | if (len <= 0) |
| 10252 | return 0; |
| 10253 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10254 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10255 | smp->data.u.str.str = ptr; |
| 10256 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10257 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10258 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10259 | return 1; |
| 10260 | } |
| 10261 | |
| 10262 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10263 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10264 | 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] | 10265 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10266 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10267 | char *ptr; |
| 10268 | int len; |
| 10269 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10270 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10271 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10272 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10273 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10274 | return 0; |
| 10275 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10276 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10277 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10278 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10279 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10280 | smp->data.u.sint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10281 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10282 | return 1; |
| 10283 | } |
| 10284 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10285 | /* returns the longest available part of the body. This requires that the body |
| 10286 | * has been waited for using http-buffer-request. |
| 10287 | */ |
| 10288 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10289 | 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] | 10290 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10291 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10292 | struct http_msg *msg; |
| 10293 | unsigned long len; |
| 10294 | unsigned long block1; |
| 10295 | char *body; |
| 10296 | struct chunk *temp; |
| 10297 | |
| 10298 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10299 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10300 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10301 | msg = &txn->req; |
| 10302 | else |
| 10303 | msg = &txn->rsp; |
| 10304 | |
| 10305 | len = http_body_bytes(msg); |
| 10306 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 10307 | |
| 10308 | block1 = len; |
| 10309 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 10310 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 10311 | |
| 10312 | if (block1 == len) { |
| 10313 | /* buffer is not wrapped (or empty) */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10314 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10315 | smp->data.u.str.str = body; |
| 10316 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10317 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 10318 | } |
| 10319 | else { |
| 10320 | /* buffer is wrapped, we need to defragment it */ |
| 10321 | temp = get_trash_chunk(); |
| 10322 | memcpy(temp->str, body, block1); |
| 10323 | memcpy(temp->str + block1, msg->chn->buf->data, len - block1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10324 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10325 | smp->data.u.str.str = temp->str; |
| 10326 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10327 | smp->flags = SMP_F_VOL_TEST; |
| 10328 | } |
| 10329 | return 1; |
| 10330 | } |
| 10331 | |
| 10332 | |
| 10333 | /* returns the available length of the body. This requires that the body |
| 10334 | * has been waited for using http-buffer-request. |
| 10335 | */ |
| 10336 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10337 | 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] | 10338 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10339 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10340 | struct http_msg *msg; |
| 10341 | |
| 10342 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10343 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10344 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10345 | msg = &txn->req; |
| 10346 | else |
| 10347 | msg = &txn->rsp; |
| 10348 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10349 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10350 | smp->data.u.sint = http_body_bytes(msg); |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10351 | |
| 10352 | smp->flags = SMP_F_VOL_TEST; |
| 10353 | return 1; |
| 10354 | } |
| 10355 | |
| 10356 | |
| 10357 | /* returns the advertised length of the body, or the advertised size of the |
| 10358 | * chunks available in the buffer. This requires that the body has been waited |
| 10359 | * for using http-buffer-request. |
| 10360 | */ |
| 10361 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10362 | 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] | 10363 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10364 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10365 | struct http_msg *msg; |
| 10366 | |
| 10367 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10368 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10369 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10370 | msg = &txn->req; |
| 10371 | else |
| 10372 | msg = &txn->rsp; |
| 10373 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10374 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10375 | smp->data.u.sint = msg->body_len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10376 | |
| 10377 | smp->flags = SMP_F_VOL_TEST; |
| 10378 | return 1; |
| 10379 | } |
| 10380 | |
| 10381 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10382 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10383 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10384 | 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] | 10385 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10386 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10387 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10388 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10389 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10390 | txn = smp->strm->txn; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10391 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10392 | smp->data.u.str.len = txn->req.sl.rq.u_l; |
| 10393 | 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] | 10394 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10395 | return 1; |
| 10396 | } |
| 10397 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10398 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10399 | 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] | 10400 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10401 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10402 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10403 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10404 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10405 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10406 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10407 | 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] | 10408 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10409 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10410 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10411 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10412 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10413 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10414 | return 1; |
| 10415 | } |
| 10416 | |
| 10417 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10418 | 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] | 10419 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10420 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10421 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10422 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10423 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10424 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10425 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10426 | 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] | 10427 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10428 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10429 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10430 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10431 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10432 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10433 | return 1; |
| 10434 | } |
| 10435 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10436 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10437 | * Accepts an optional argument of type string containing the header field name, |
| 10438 | * and an optional argument of type signed or unsigned integer to request an |
| 10439 | * 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] | 10440 | * headers are considered from the first one. It does not stop on commas and |
| 10441 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10442 | */ |
| 10443 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10444 | 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] | 10445 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10446 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10447 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10448 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10449 | int occ = 0; |
| 10450 | const char *name_str = NULL; |
| 10451 | int name_len = 0; |
| 10452 | |
| 10453 | if (!ctx) { |
| 10454 | /* first call */ |
| 10455 | ctx = &static_hdr_ctx; |
| 10456 | ctx->idx = 0; |
| 10457 | smp->ctx.a[0] = ctx; |
| 10458 | } |
| 10459 | |
| 10460 | if (args) { |
| 10461 | if (args[0].type != ARGT_STR) |
| 10462 | return 0; |
| 10463 | name_str = args[0].data.str.str; |
| 10464 | name_len = args[0].data.str.len; |
| 10465 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10466 | if (args[1].type == ARGT_SINT) |
| 10467 | occ = args[1].data.sint; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10468 | } |
| 10469 | |
| 10470 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10471 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10472 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10473 | 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] | 10474 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10475 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10476 | /* search for header from the beginning */ |
| 10477 | ctx->idx = 0; |
| 10478 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10479 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10480 | /* no explicit occurrence and single fetch => last header by default */ |
| 10481 | occ = -1; |
| 10482 | |
| 10483 | if (!occ) |
| 10484 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10485 | smp->flags |= SMP_F_NOT_LAST; |
| 10486 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10487 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10488 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10489 | 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] | 10490 | return 1; |
| 10491 | |
| 10492 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10493 | return 0; |
| 10494 | } |
| 10495 | |
| 10496 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10497 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10498 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10499 | */ |
| 10500 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10501 | 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] | 10502 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10503 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10504 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10505 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10506 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10507 | const char *name = NULL; |
| 10508 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10509 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10510 | if (args && args->type == ARGT_STR) { |
| 10511 | name = args->data.str.str; |
| 10512 | len = args->data.str.len; |
| 10513 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10514 | |
| 10515 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10516 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10517 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10518 | 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] | 10519 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10520 | ctx.idx = 0; |
| 10521 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10522 | 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] | 10523 | cnt++; |
| 10524 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10525 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10526 | smp->data.u.sint = cnt; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10527 | smp->flags = SMP_F_VOL_HDR; |
| 10528 | return 1; |
| 10529 | } |
| 10530 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10531 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10532 | 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] | 10533 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10534 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10535 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10536 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10537 | struct chunk *temp; |
| 10538 | char del = ','; |
| 10539 | |
| 10540 | if (args && args->type == ARGT_STR) |
| 10541 | del = *args[0].data.str.str; |
| 10542 | |
| 10543 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10544 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10545 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10546 | 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] | 10547 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10548 | temp = get_trash_chunk(); |
| 10549 | |
| 10550 | ctx.idx = 0; |
| 10551 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 10552 | if (temp->len) |
| 10553 | temp->str[temp->len++] = del; |
| 10554 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 10555 | temp->len += ctx.del; |
| 10556 | } |
| 10557 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10558 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10559 | smp->data.u.str.str = temp->str; |
| 10560 | smp->data.u.str.len = temp->len; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10561 | smp->flags = SMP_F_VOL_HDR; |
| 10562 | return 1; |
| 10563 | } |
| 10564 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10565 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10566 | * Accepts an optional argument of type string containing the header field name, |
| 10567 | * and an optional argument of type signed or unsigned integer to request an |
| 10568 | * 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] | 10569 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10570 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10571 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10572 | 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] | 10573 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10574 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10575 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10576 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10577 | int occ = 0; |
| 10578 | const char *name_str = NULL; |
| 10579 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10580 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10581 | if (!ctx) { |
| 10582 | /* first call */ |
| 10583 | ctx = &static_hdr_ctx; |
| 10584 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10585 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10586 | } |
| 10587 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10588 | if (args) { |
| 10589 | if (args[0].type != ARGT_STR) |
| 10590 | return 0; |
| 10591 | name_str = args[0].data.str.str; |
| 10592 | name_len = args[0].data.str.len; |
| 10593 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10594 | if (args[1].type == ARGT_SINT) |
| 10595 | occ = args[1].data.sint; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10596 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10597 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10598 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10599 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10600 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10601 | 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] | 10602 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10603 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10604 | /* search for header from the beginning */ |
| 10605 | ctx->idx = 0; |
| 10606 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10607 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10608 | /* no explicit occurrence and single fetch => last header by default */ |
| 10609 | occ = -1; |
| 10610 | |
| 10611 | if (!occ) |
| 10612 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10613 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10614 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10615 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10616 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10617 | 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] | 10618 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10619 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10620 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10621 | return 0; |
| 10622 | } |
| 10623 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10624 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10625 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10626 | */ |
| 10627 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10628 | 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] | 10629 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10630 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10631 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10632 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10633 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10634 | const char *name = NULL; |
| 10635 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10636 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10637 | if (args && args->type == ARGT_STR) { |
| 10638 | name = args->data.str.str; |
| 10639 | len = args->data.str.len; |
| 10640 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10641 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10642 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10643 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10644 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10645 | 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] | 10646 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10647 | ctx.idx = 0; |
| 10648 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10649 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10650 | cnt++; |
| 10651 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10652 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10653 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10654 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10655 | return 1; |
| 10656 | } |
| 10657 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10658 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10659 | * takes a mandatory argument of type string and an optional one of type int |
| 10660 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10661 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10662 | */ |
| 10663 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10664 | 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] | 10665 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10666 | int ret = smp_fetch_hdr(args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10667 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10668 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10669 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10670 | 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] | 10671 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10672 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10673 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10674 | } |
| 10675 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10676 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10677 | * and an optional one of type int to designate a specific occurrence. |
| 10678 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10679 | */ |
| 10680 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10681 | 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] | 10682 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10683 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10684 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10685 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10686 | if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10687 | smp->data.type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10688 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10689 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10690 | struct chunk *temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10691 | if (smp->data.u.str.len < temp->size - 1) { |
| 10692 | memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len); |
| 10693 | temp->str[smp->data.u.str.len] = '\0'; |
| 10694 | if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10695 | smp->data.type = SMP_T_IPV6; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10696 | break; |
| 10697 | } |
| 10698 | } |
| 10699 | } |
| 10700 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10701 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10702 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10703 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10704 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10705 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10706 | } |
| 10707 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10708 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10709 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10710 | */ |
| 10711 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10712 | 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] | 10713 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10714 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10715 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10716 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10717 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10718 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10719 | txn = smp->strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10720 | 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] | 10721 | ptr = http_get_path(txn); |
| 10722 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10723 | return 0; |
| 10724 | |
| 10725 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10726 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10727 | smp->data.u.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10728 | |
| 10729 | while (ptr < end && *ptr != '?') |
| 10730 | ptr++; |
| 10731 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10732 | smp->data.u.str.len = ptr - smp->data.u.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10733 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10734 | return 1; |
| 10735 | } |
| 10736 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10737 | /* This produces a concatenation of the first occurrence of the Host header |
| 10738 | * followed by the path component if it begins with a slash ('/'). This means |
| 10739 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10740 | * If no Host header is found, then the path is returned as-is. The returned |
| 10741 | * 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] | 10742 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10743 | */ |
| 10744 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10745 | 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] | 10746 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10747 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10748 | char *ptr, *end, *beg; |
| 10749 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10750 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10751 | |
| 10752 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10753 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10754 | txn = smp->strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10755 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10756 | 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] | 10757 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10758 | |
| 10759 | /* 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] | 10760 | temp = get_trash_chunk(); |
| 10761 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10762 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10763 | smp->data.u.str.str = temp->str; |
| 10764 | smp->data.u.str.len = ctx.vlen; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10765 | |
| 10766 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10767 | 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] | 10768 | beg = http_get_path(txn); |
| 10769 | if (!beg) |
| 10770 | beg = end; |
| 10771 | |
| 10772 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10773 | |
| 10774 | if (beg < ptr && *beg == '/') { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10775 | memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg); |
| 10776 | smp->data.u.str.len += ptr - beg; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10777 | } |
| 10778 | |
| 10779 | smp->flags = SMP_F_VOL_1ST; |
| 10780 | return 1; |
| 10781 | } |
| 10782 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10783 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10784 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10785 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10786 | * 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] | 10787 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10788 | * 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] | 10789 | * high-traffic sites without having to store whole paths. |
| 10790 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10791 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10792 | 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] | 10793 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10794 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10795 | struct hdr_ctx ctx; |
| 10796 | unsigned int hash = 0; |
| 10797 | char *ptr, *beg, *end; |
| 10798 | int len; |
| 10799 | |
| 10800 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10801 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10802 | txn = smp->strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10803 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10804 | 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] | 10805 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10806 | ptr = ctx.line + ctx.val; |
| 10807 | len = ctx.vlen; |
| 10808 | while (len--) |
| 10809 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10810 | } |
| 10811 | |
| 10812 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10813 | 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] | 10814 | beg = http_get_path(txn); |
| 10815 | if (!beg) |
| 10816 | beg = end; |
| 10817 | |
| 10818 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10819 | |
| 10820 | if (beg < ptr && *beg == '/') { |
| 10821 | while (beg < ptr) |
| 10822 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10823 | } |
| 10824 | hash = full_hash(hash); |
| 10825 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10826 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10827 | smp->data.u.sint = hash; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10828 | smp->flags = SMP_F_VOL_1ST; |
| 10829 | return 1; |
| 10830 | } |
| 10831 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10832 | /* 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] | 10833 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10834 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10835 | * 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] | 10836 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10837 | * 8 bytes would still work. |
| 10838 | */ |
| 10839 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10840 | 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] | 10841 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10842 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10843 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10844 | |
| 10845 | if (!cli_conn) |
| 10846 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10847 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10848 | if (!smp_fetch_base32(args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10849 | return 0; |
| 10850 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10851 | temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10852 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10853 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10854 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10855 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10856 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10857 | 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] | 10858 | temp->len += 4; |
| 10859 | break; |
| 10860 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10861 | 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] | 10862 | temp->len += 16; |
| 10863 | break; |
| 10864 | default: |
| 10865 | return 0; |
| 10866 | } |
| 10867 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10868 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10869 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10870 | return 1; |
| 10871 | } |
| 10872 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10873 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10874 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10875 | * of type string carrying the whole query string. |
| 10876 | */ |
| 10877 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10878 | 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] | 10879 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10880 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10881 | char *ptr, *end; |
| 10882 | |
| 10883 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10884 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10885 | txn = smp->strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10886 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 10887 | end = ptr + txn->req.sl.rq.u_l; |
| 10888 | |
| 10889 | /* look up the '?' */ |
| 10890 | do { |
| 10891 | if (ptr == end) |
| 10892 | return 0; |
| 10893 | } while (*ptr++ != '?'); |
| 10894 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10895 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10896 | smp->data.u.str.str = ptr; |
| 10897 | smp->data.u.str.len = end - ptr; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10898 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10899 | return 1; |
| 10900 | } |
| 10901 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10902 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10903 | 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] | 10904 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10905 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10906 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10907 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10908 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10909 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10910 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10911 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10912 | smp->data.u.sint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10913 | return 1; |
| 10914 | } |
| 10915 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10916 | /* return a valid test if the current request is the first one on the connection */ |
| 10917 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10918 | 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] | 10919 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10920 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10921 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10922 | return 1; |
| 10923 | } |
| 10924 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10925 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10926 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10927 | 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] | 10928 | { |
| 10929 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10930 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10931 | return 0; |
| 10932 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10933 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10934 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10935 | if (!get_http_auth(smp->strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10936 | return 0; |
| 10937 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10938 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10939 | 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] | 10940 | smp->strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10941 | return 1; |
| 10942 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10943 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10944 | /* Accepts exactly 1 argument of type userlist */ |
| 10945 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10946 | 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] | 10947 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10948 | if (!args || args->type != ARGT_USR) |
| 10949 | return 0; |
| 10950 | |
| 10951 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10952 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10953 | if (!get_http_auth(smp->strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10954 | return 0; |
| 10955 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10956 | /* if the user does not belong to the userlist or has a wrong password, |
| 10957 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10958 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10959 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10960 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 10961 | smp->strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10962 | return 0; |
| 10963 | |
| 10964 | /* pat_match_auth() will need the user list */ |
| 10965 | smp->ctx.a[0] = args->data.usr; |
| 10966 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10967 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10968 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10969 | smp->data.u.str.str = smp->strm->txn->auth.user; |
| 10970 | smp->data.u.str.len = strlen(smp->strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10971 | |
| 10972 | return 1; |
| 10973 | } |
| 10974 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10975 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10976 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10977 | * the cookie value is returned into *value and *value_l, and the function |
| 10978 | * returns a pointer to the next pointer to search from if the value was found. |
| 10979 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10980 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10981 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10982 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10983 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10984 | * list is expected. |
| 10985 | */ |
| 10986 | static char * |
| 10987 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10988 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10989 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10990 | { |
| 10991 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10992 | char *next; |
| 10993 | |
| 10994 | /* we search at least a cookie name followed by an equal, and more |
| 10995 | * generally something like this : |
| 10996 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10997 | */ |
| 10998 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10999 | /* Iterate through all cookies on this line */ |
| 11000 | |
| 11001 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 11002 | att_beg++; |
| 11003 | |
| 11004 | /* find att_end : this is the first character after the last non |
| 11005 | * space before the equal. It may be equal to hdr_end. |
| 11006 | */ |
| 11007 | equal = att_end = att_beg; |
| 11008 | |
| 11009 | while (equal < hdr_end) { |
| 11010 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 11011 | break; |
| 11012 | if (http_is_spht[(unsigned char)*equal++]) |
| 11013 | continue; |
| 11014 | att_end = equal; |
| 11015 | } |
| 11016 | |
| 11017 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 11018 | * is between <att_beg> and <equal>, both may be identical. |
| 11019 | */ |
| 11020 | |
| 11021 | /* look for end of cookie if there is an equal sign */ |
| 11022 | if (equal < hdr_end && *equal == '=') { |
| 11023 | /* look for the beginning of the value */ |
| 11024 | val_beg = equal + 1; |
| 11025 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 11026 | val_beg++; |
| 11027 | |
| 11028 | /* find the end of the value, respecting quotes */ |
| 11029 | next = find_cookie_value_end(val_beg, hdr_end); |
| 11030 | |
| 11031 | /* make val_end point to the first white space or delimitor after the value */ |
| 11032 | val_end = next; |
| 11033 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 11034 | val_end--; |
| 11035 | } else { |
| 11036 | val_beg = val_end = next = equal; |
| 11037 | } |
| 11038 | |
| 11039 | /* We have nothing to do with attributes beginning with '$'. However, |
| 11040 | * they will automatically be removed if a header before them is removed, |
| 11041 | * since they're supposed to be linked together. |
| 11042 | */ |
| 11043 | if (*att_beg == '$') |
| 11044 | continue; |
| 11045 | |
| 11046 | /* Ignore cookies with no equal sign */ |
| 11047 | if (equal == next) |
| 11048 | continue; |
| 11049 | |
| 11050 | /* Now we have the cookie name between att_beg and att_end, and |
| 11051 | * its value between val_beg and val_end. |
| 11052 | */ |
| 11053 | |
| 11054 | if (att_end - att_beg == cookie_name_l && |
| 11055 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 11056 | /* let's return this value and indicate where to go on from */ |
| 11057 | *value = val_beg; |
| 11058 | *value_l = val_end - val_beg; |
| 11059 | return next + 1; |
| 11060 | } |
| 11061 | |
| 11062 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 11063 | if (!list) |
| 11064 | break; |
| 11065 | } |
| 11066 | |
| 11067 | return NULL; |
| 11068 | } |
| 11069 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11070 | /* Fetch a captured HTTP request header. The index is the position of |
| 11071 | * the "capture" option in the configuration file |
| 11072 | */ |
| 11073 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11074 | 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] | 11075 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11076 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11077 | int idx; |
| 11078 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11079 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11080 | return 0; |
| 11081 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11082 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11083 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11084 | 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] | 11085 | return 0; |
| 11086 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11087 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11088 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11089 | smp->data.u.str.str = smp->strm->req_cap[idx]; |
| 11090 | smp->data.u.str.len = strlen(smp->strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11091 | |
| 11092 | return 1; |
| 11093 | } |
| 11094 | |
| 11095 | /* Fetch a captured HTTP response header. The index is the position of |
| 11096 | * the "capture" option in the configuration file |
| 11097 | */ |
| 11098 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11099 | 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] | 11100 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11101 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11102 | int idx; |
| 11103 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11104 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11105 | return 0; |
| 11106 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11107 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11108 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11109 | 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] | 11110 | return 0; |
| 11111 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11112 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11113 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11114 | smp->data.u.str.str = smp->strm->res_cap[idx]; |
| 11115 | smp->data.u.str.len = strlen(smp->strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11116 | |
| 11117 | return 1; |
| 11118 | } |
| 11119 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11120 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 11121 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11122 | 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] | 11123 | { |
| 11124 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11125 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11126 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11127 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11128 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11129 | return 0; |
| 11130 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11131 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11132 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11133 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11134 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11135 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11136 | temp = get_trash_chunk(); |
| 11137 | temp->str = txn->uri; |
| 11138 | temp->len = ptr - txn->uri; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11139 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11140 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11141 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11142 | |
| 11143 | return 1; |
| 11144 | |
| 11145 | } |
| 11146 | |
| 11147 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 11148 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11149 | 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] | 11150 | { |
| 11151 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11152 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11153 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11154 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11155 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11156 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11157 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11158 | ptr = txn->uri; |
| 11159 | |
| 11160 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11161 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11162 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11163 | if (!*ptr) |
| 11164 | return 0; |
| 11165 | |
| 11166 | ptr++; /* skip the space */ |
| 11167 | |
| 11168 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11169 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11170 | if (!ptr) |
| 11171 | return 0; |
| 11172 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 11173 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11174 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11175 | smp->data.u.str = *temp; |
| 11176 | smp->data.u.str.len = ptr - temp->str; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11177 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11178 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11179 | |
| 11180 | return 1; |
| 11181 | } |
| 11182 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11183 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 11184 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11185 | */ |
| 11186 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11187 | 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] | 11188 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11189 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11190 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11191 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11192 | return 0; |
| 11193 | |
| 11194 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11195 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11196 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11197 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11198 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11199 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11200 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11201 | smp->flags = SMP_F_CONST; |
| 11202 | return 1; |
| 11203 | |
| 11204 | } |
| 11205 | |
| 11206 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 11207 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11208 | */ |
| 11209 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11210 | 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] | 11211 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11212 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11213 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11214 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11215 | return 0; |
| 11216 | |
| 11217 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11218 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11219 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11220 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11221 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11222 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11223 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11224 | smp->flags = SMP_F_CONST; |
| 11225 | return 1; |
| 11226 | |
| 11227 | } |
| 11228 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11229 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11230 | /* 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] | 11231 | * 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] | 11232 | * 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] | 11233 | * 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] | 11234 | * 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] | 11235 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 11236 | * 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] | 11237 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 11238 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11239 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11240 | 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] | 11241 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11242 | struct http_txn *txn; |
| 11243 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11244 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11245 | const struct http_msg *msg; |
| 11246 | const char *hdr_name; |
| 11247 | int hdr_name_len; |
| 11248 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11249 | int occ = 0; |
| 11250 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11251 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11252 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11253 | return 0; |
| 11254 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11255 | if (!ctx) { |
| 11256 | /* first call */ |
| 11257 | ctx = &static_hdr_ctx; |
| 11258 | ctx->idx = 0; |
| 11259 | smp->ctx.a[2] = ctx; |
| 11260 | } |
| 11261 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11262 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11263 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11264 | txn = smp->strm->txn; |
| 11265 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11266 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11267 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11268 | msg = &txn->req; |
| 11269 | hdr_name = "Cookie"; |
| 11270 | hdr_name_len = 6; |
| 11271 | } else { |
| 11272 | msg = &txn->rsp; |
| 11273 | hdr_name = "Set-Cookie"; |
| 11274 | hdr_name_len = 10; |
| 11275 | } |
| 11276 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11277 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11278 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 11279 | occ = -1; |
| 11280 | |
| 11281 | /* OK so basically here, either we want only one value and it's the |
| 11282 | * last one, or we want to iterate over all of them and we fetch the |
| 11283 | * next one. |
| 11284 | */ |
| 11285 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11286 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11287 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11288 | /* search for the header from the beginning, we must first initialize |
| 11289 | * the search parameters. |
| 11290 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11291 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11292 | ctx->idx = 0; |
| 11293 | } |
| 11294 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11295 | smp->flags |= SMP_F_VOL_HDR; |
| 11296 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11297 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11298 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 11299 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11300 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 11301 | goto out; |
| 11302 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11303 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11304 | continue; |
| 11305 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11306 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 11307 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11308 | } |
| 11309 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11310 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11311 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11312 | 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] | 11313 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11314 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11315 | &smp->data.u.str.str, |
| 11316 | &smp->data.u.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11317 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11318 | found = 1; |
| 11319 | if (occ >= 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11320 | /* one value was returned into smp->data.u.str.{str,len} */ |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11321 | smp->flags |= SMP_F_NOT_LAST; |
| 11322 | return 1; |
| 11323 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11324 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11325 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11326 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11327 | /* all cookie headers and values were scanned. If we're looking for the |
| 11328 | * last occurrence, we may return it now. |
| 11329 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11330 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11331 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11332 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11333 | } |
| 11334 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11335 | /* 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] | 11336 | * 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] | 11337 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 11338 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11339 | */ |
| 11340 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11341 | 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] | 11342 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11343 | struct http_txn *txn; |
| 11344 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11345 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11346 | const struct http_msg *msg; |
| 11347 | const char *hdr_name; |
| 11348 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11349 | int cnt; |
| 11350 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11351 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11352 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11353 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11354 | return 0; |
| 11355 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11356 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11357 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11358 | txn = smp->strm->txn; |
| 11359 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11360 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11361 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11362 | msg = &txn->req; |
| 11363 | hdr_name = "Cookie"; |
| 11364 | hdr_name_len = 6; |
| 11365 | } else { |
| 11366 | msg = &txn->rsp; |
| 11367 | hdr_name = "Set-Cookie"; |
| 11368 | hdr_name_len = 10; |
| 11369 | } |
| 11370 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11371 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 11372 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11373 | ctx.idx = 0; |
| 11374 | cnt = 0; |
| 11375 | |
| 11376 | while (1) { |
| 11377 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 11378 | if (!val_beg) { |
| 11379 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 11380 | break; |
| 11381 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11382 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11383 | continue; |
| 11384 | |
| 11385 | val_beg = ctx.line + ctx.val; |
| 11386 | val_end = val_beg + ctx.vlen; |
| 11387 | } |
| 11388 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11389 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11390 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11391 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11392 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11393 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11394 | &smp->data.u.str.str, |
| 11395 | &smp->data.u.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11396 | cnt++; |
| 11397 | } |
| 11398 | } |
| 11399 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11400 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11401 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11402 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11403 | return 1; |
| 11404 | } |
| 11405 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11406 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 11407 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 11408 | */ |
| 11409 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11410 | 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] | 11411 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11412 | int ret = smp_fetch_cookie(args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11413 | |
| 11414 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11415 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11416 | 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] | 11417 | } |
| 11418 | |
| 11419 | return ret; |
| 11420 | } |
| 11421 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11422 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11423 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11424 | /************************************************************************/ |
| 11425 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11426 | /* |
| 11427 | * Given a path string and its length, find the position of beginning of the |
| 11428 | * query string. Returns NULL if no query string is found in the path. |
| 11429 | * |
| 11430 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11431 | * |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11432 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11433 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11434 | 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] | 11435 | { |
| 11436 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11437 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11438 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11439 | return p ? p + 1 : NULL; |
| 11440 | } |
| 11441 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11442 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11443 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11444 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11445 | } |
| 11446 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11447 | /* after increasing a pointer value, it can exceed the first buffer |
| 11448 | * size. This function transform the value of <ptr> according with |
| 11449 | * the expected position. <chunks> is an array of the one or two |
| 11450 | * avalaible chunks. The first value is the start of the first chunk, |
| 11451 | * the second value if the end+1 of the first chunks. The third value |
| 11452 | * is NULL or the start of the second chunk and the fourth value is |
| 11453 | * the end+1 of the second chunk. The function returns 1 if does a |
| 11454 | * wrap, else returns 0. |
| 11455 | */ |
| 11456 | static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr) |
| 11457 | { |
| 11458 | if (*ptr < chunks[1]) |
| 11459 | return 0; |
| 11460 | if (!chunks[2]) |
| 11461 | return 0; |
| 11462 | *ptr = chunks[2] + ( *ptr - chunks[1] ); |
| 11463 | return 1; |
| 11464 | } |
| 11465 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11466 | /* |
| 11467 | * Given a url parameter, find the starting position of the first occurence, |
| 11468 | * or NULL if the parameter is not found. |
| 11469 | * |
| 11470 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11471 | * the function will return query_string+8. |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11472 | * |
| 11473 | * Warning:this function returns a pointer that can be point to the first chunk |
| 11474 | * or the second chunk. The caller must be check the position before using the |
| 11475 | * result. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11476 | */ |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11477 | static const char * |
| 11478 | find_url_param_pos(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11479 | const char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11480 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11481 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11482 | const char *pos, *last, *equal; |
| 11483 | const char **bufs = chunks; |
| 11484 | int l1, l2; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11485 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11486 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11487 | pos = bufs[0]; |
| 11488 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11489 | while (pos <= last) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11490 | /* Check the equal. */ |
| 11491 | equal = pos + url_param_name_l; |
| 11492 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 11493 | if (equal >= chunks[3]) |
| 11494 | return NULL; |
| 11495 | } else { |
| 11496 | if (equal >= chunks[1]) |
| 11497 | return NULL; |
| 11498 | } |
| 11499 | if (*equal == '=') { |
| 11500 | if (pos + url_param_name_l > last) { |
| 11501 | /* process wrap case, we detect a wrap. In this case, the |
| 11502 | * comparison is performed in two parts. |
| 11503 | */ |
| 11504 | |
| 11505 | /* This is the end, we dont have any other chunk. */ |
| 11506 | if (bufs != chunks || !bufs[2]) |
| 11507 | return NULL; |
| 11508 | |
| 11509 | /* Compute the length of each part of the comparison. */ |
| 11510 | l1 = last - pos; |
| 11511 | l2 = url_param_name_l - l1; |
| 11512 | |
| 11513 | /* The second buffer is too short to contain the compared string. */ |
| 11514 | if (bufs[2] + l2 > bufs[3]) |
| 11515 | return NULL; |
| 11516 | |
| 11517 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 11518 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 11519 | return pos; |
| 11520 | |
| 11521 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 11522 | bufs += 2; |
| 11523 | pos = bufs[0] + l2; |
| 11524 | last = bufs[1]; |
| 11525 | |
| 11526 | } else { |
| 11527 | /* process a simple comparison. */ |
| 11528 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) { |
| 11529 | return pos; } |
| 11530 | pos += url_param_name_l + 1; |
| 11531 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 11532 | last = bufs[2]; |
| 11533 | } |
| 11534 | } |
| 11535 | |
| 11536 | while (1) { |
| 11537 | /* Look for the next delimiter. */ |
| 11538 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
| 11539 | pos++; |
| 11540 | if (pos < last) |
| 11541 | break; |
| 11542 | /* process buffer wrapping. */ |
| 11543 | if (bufs != chunks || !bufs[2]) |
| 11544 | return NULL; |
| 11545 | bufs += 2; |
| 11546 | pos = bufs[0]; |
| 11547 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11548 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11549 | pos++; |
| 11550 | } |
| 11551 | return NULL; |
| 11552 | } |
| 11553 | |
| 11554 | /* |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11555 | * Given a url parameter name and a query string, returns its value and size |
| 11556 | * into *value and *value_l respectively, and returns non-zero. An empty |
| 11557 | * url_param_name matches the first available parameter. If the parameter is |
| 11558 | * not found, zero is returned and value/value_l are not touched. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11559 | */ |
| 11560 | static int |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11561 | find_next_url_param(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11562 | const char* url_param_name, size_t url_param_name_l, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11563 | const char **vstart, const char **vend, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11564 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11565 | const char *arg_start, *qs_end; |
| 11566 | const char *value_start, *value_end; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11567 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11568 | arg_start = chunks[0]; |
| 11569 | qs_end = chunks[1]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11570 | if (url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11571 | /* Looks for an argument name. */ |
| 11572 | arg_start = find_url_param_pos(chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11573 | url_param_name, url_param_name_l, |
| 11574 | delim); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11575 | /* Check for wrapping. */ |
| 11576 | if (arg_start > qs_end) |
| 11577 | qs_end = chunks[3]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11578 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11579 | if (!arg_start) |
| 11580 | return 0; |
| 11581 | |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11582 | if (!url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11583 | while (1) { |
| 11584 | /* looks for the first argument. */ |
| 11585 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 11586 | if (!value_start) { |
| 11587 | |
| 11588 | /* Check for wrapping. */ |
| 11589 | if (arg_start >= chunks[0] && |
| 11590 | arg_start <= chunks[1] && |
| 11591 | chunks[2]) { |
| 11592 | arg_start = chunks[2]; |
| 11593 | qs_end = chunks[3]; |
| 11594 | continue; |
| 11595 | } |
| 11596 | return 0; |
| 11597 | } |
| 11598 | break; |
| 11599 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11600 | value_start++; |
| 11601 | } |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11602 | else { |
| 11603 | /* Jump the argument length. */ |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11604 | value_start = arg_start + url_param_name_l + 1; |
| 11605 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11606 | /* Check for pointer wrapping. */ |
| 11607 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 11608 | /* Update the end pointer. */ |
| 11609 | qs_end = chunks[3]; |
| 11610 | |
| 11611 | /* Check for overflow. */ |
| 11612 | if (value_start > qs_end) |
| 11613 | return 0; |
| 11614 | } |
| 11615 | } |
| 11616 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11617 | value_end = value_start; |
| 11618 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11619 | while (1) { |
| 11620 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
| 11621 | value_end++; |
| 11622 | if (value_end < qs_end) |
| 11623 | break; |
| 11624 | /* process buffer wrapping. */ |
| 11625 | if (value_end >= chunks[0] && |
| 11626 | value_end <= chunks[1] && |
| 11627 | chunks[2]) { |
| 11628 | value_end = chunks[2]; |
| 11629 | qs_end = chunks[3]; |
| 11630 | continue; |
| 11631 | } |
| 11632 | break; |
| 11633 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11634 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11635 | *vstart = value_start; |
| 11636 | *vend = value_end; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 11637 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11638 | } |
| 11639 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11640 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 11641 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 11642 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 11643 | * pointers are updated for next iteration before leaving. |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11644 | */ |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11645 | static int |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11646 | 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] | 11647 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11648 | const char *vstart, *vend; |
| 11649 | struct chunk *temp; |
| 11650 | const char **chunks = (const char **)smp->ctx.a; |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11651 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11652 | if (!find_next_url_param(chunks, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11653 | name, name_len, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11654 | &vstart, &vend, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11655 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11656 | return 0; |
| 11657 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11658 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 11659 | * if the value is wrapped, copy-it in a buffer. |
| 11660 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11661 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11662 | if (chunks[2] && |
| 11663 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 11664 | vend >= chunks[2] && vend <= chunks[3]) { |
| 11665 | /* Wrapped case. */ |
| 11666 | temp = get_trash_chunk(); |
| 11667 | memcpy(temp->str, vstart, chunks[1] - vstart); |
| 11668 | memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11669 | smp->data.u.str.str = temp->str; |
| 11670 | smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11671 | } else { |
| 11672 | /* Contiguous case. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11673 | smp->data.u.str.str = (char *)vstart; |
| 11674 | smp->data.u.str.len = vend - vstart; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11675 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11676 | } |
| 11677 | |
| 11678 | /* Update context, check wrapping. */ |
| 11679 | chunks[0] = vend; |
| 11680 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 11681 | chunks[1] = chunks[3]; |
| 11682 | chunks[2] = NULL; |
| 11683 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11684 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11685 | if (chunks[0] < chunks[1]) |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11686 | smp->flags |= SMP_F_NOT_LAST; |
| 11687 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11688 | return 1; |
| 11689 | } |
| 11690 | |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11691 | /* This function iterates over each parameter of the query string. It uses |
| 11692 | * 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] | 11693 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 11694 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 11695 | * considered. It supports an optional delimiter argument for the beginning of |
| 11696 | * the string in args[1], which defaults to "?". |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11697 | */ |
| 11698 | static int |
| 11699 | smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11700 | { |
| 11701 | struct http_msg *msg; |
| 11702 | char delim = '?'; |
| 11703 | const char *name; |
| 11704 | int name_len; |
| 11705 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11706 | if (!args || |
| 11707 | (args[0].type && args[0].type != ARGT_STR) || |
| 11708 | (args[1].type && args[1].type != ARGT_STR)) |
| 11709 | return 0; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11710 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11711 | name = ""; |
| 11712 | name_len = 0; |
| 11713 | if (args->type == ARGT_STR) { |
| 11714 | name = args->data.str.str; |
| 11715 | name_len = args->data.str.len; |
| 11716 | } |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11717 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11718 | if (args[1].type) |
| 11719 | delim = *args[1].data.str.str; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11720 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11721 | if (!smp->ctx.a[0]) { // first call, find the query string |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11722 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11723 | |
| 11724 | msg = &smp->strm->txn->req; |
| 11725 | |
| 11726 | smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u, |
| 11727 | msg->sl.rq.u_l, delim); |
| 11728 | if (!smp->ctx.a[0]) |
| 11729 | return 0; |
| 11730 | |
| 11731 | 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] | 11732 | |
| 11733 | /* Assume that the context is filled with NULL pointer |
| 11734 | * before the first call. |
| 11735 | * smp->ctx.a[2] = NULL; |
| 11736 | * smp->ctx.a[3] = NULL; |
| 11737 | */ |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11738 | } |
| 11739 | |
| 11740 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 11741 | } |
| 11742 | |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11743 | /* This function iterates over each parameter of the body. This requires |
| 11744 | * 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] | 11745 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 11746 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 11747 | * optional second part if the body wraps at the end of the buffer. An optional |
| 11748 | * parameter name is passed in args[0], otherwise any parameter is considered. |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11749 | */ |
| 11750 | static int |
| 11751 | smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11752 | { |
| 11753 | struct http_txn *txn = smp->strm->txn; |
| 11754 | struct http_msg *msg; |
| 11755 | unsigned long len; |
| 11756 | unsigned long block1; |
| 11757 | char *body; |
| 11758 | const char *name; |
| 11759 | int name_len; |
| 11760 | |
| 11761 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 11762 | return 0; |
| 11763 | |
| 11764 | name = ""; |
| 11765 | name_len = 0; |
| 11766 | if (args[0].type == ARGT_STR) { |
| 11767 | name = args[0].data.str.str; |
| 11768 | name_len = args[0].data.str.len; |
| 11769 | } |
| 11770 | |
| 11771 | if (!smp->ctx.a[0]) { // first call, find the query string |
| 11772 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11773 | |
| 11774 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
| 11775 | msg = &txn->req; |
| 11776 | else |
| 11777 | msg = &txn->rsp; |
| 11778 | |
| 11779 | len = http_body_bytes(msg); |
| 11780 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 11781 | |
| 11782 | block1 = len; |
| 11783 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 11784 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 11785 | |
| 11786 | if (block1 == len) { |
| 11787 | /* buffer is not wrapped (or empty) */ |
| 11788 | smp->ctx.a[0] = body; |
| 11789 | smp->ctx.a[1] = body + len; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11790 | |
| 11791 | /* Assume that the context is filled with NULL pointer |
| 11792 | * before the first call. |
| 11793 | * smp->ctx.a[2] = NULL; |
| 11794 | * smp->ctx.a[3] = NULL; |
| 11795 | */ |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11796 | } |
| 11797 | else { |
| 11798 | /* buffer is wrapped, we need to defragment it */ |
| 11799 | smp->ctx.a[0] = body; |
| 11800 | smp->ctx.a[1] = body + block1; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11801 | smp->ctx.a[2] = msg->chn->buf->data; |
| 11802 | smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 ); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11803 | } |
| 11804 | } |
| 11805 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 11806 | } |
| 11807 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11808 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11809 | * above). |
| 11810 | */ |
| 11811 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11812 | 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] | 11813 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11814 | int ret = smp_fetch_url_param(args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11815 | |
| 11816 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11817 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11818 | 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] | 11819 | } |
| 11820 | |
| 11821 | return ret; |
| 11822 | } |
| 11823 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11824 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11825 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11826 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11827 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11828 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11829 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11830 | * high-traffic sites without having to store whole paths. |
| 11831 | * this differs from the base32 functions in that it includes the url parameters |
| 11832 | * as well as the path |
| 11833 | */ |
| 11834 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11835 | 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] | 11836 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11837 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11838 | struct hdr_ctx ctx; |
| 11839 | unsigned int hash = 0; |
| 11840 | char *ptr, *beg, *end; |
| 11841 | int len; |
| 11842 | |
| 11843 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11844 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11845 | txn = smp->strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11846 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11847 | 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] | 11848 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11849 | ptr = ctx.line + ctx.val; |
| 11850 | len = ctx.vlen; |
| 11851 | while (len--) |
| 11852 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11853 | } |
| 11854 | |
| 11855 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11856 | 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] | 11857 | beg = http_get_path(txn); |
| 11858 | if (!beg) |
| 11859 | beg = end; |
| 11860 | |
| 11861 | for (ptr = beg; ptr < end ; ptr++); |
| 11862 | |
| 11863 | if (beg < ptr && *beg == '/') { |
| 11864 | while (beg < ptr) |
| 11865 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11866 | } |
| 11867 | hash = full_hash(hash); |
| 11868 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11869 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11870 | smp->data.u.sint = hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11871 | smp->flags = SMP_F_VOL_1ST; |
| 11872 | return 1; |
| 11873 | } |
| 11874 | |
| 11875 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11876 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11877 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11878 | * on the source address length. The URL hash is stored before the address so |
| 11879 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11880 | * 8 bytes would still work. |
| 11881 | */ |
| 11882 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11883 | 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] | 11884 | { |
| 11885 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11886 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11887 | unsigned int hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11888 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11889 | if (!smp_fetch_url32(args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11890 | return 0; |
| 11891 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11892 | /* The returned hash is a 32 bytes integer. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11893 | hash = smp->data.u.sint; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11894 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11895 | temp = get_trash_chunk(); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11896 | memcpy(temp->str + temp->len, &hash, sizeof(hash)); |
| 11897 | temp->len += sizeof(hash); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11898 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11899 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11900 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11901 | 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] | 11902 | temp->len += 4; |
| 11903 | break; |
| 11904 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11905 | 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] | 11906 | temp->len += 16; |
| 11907 | break; |
| 11908 | default: |
| 11909 | return 0; |
| 11910 | } |
| 11911 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11912 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11913 | smp->data.type = SMP_T_BIN; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11914 | return 1; |
| 11915 | } |
| 11916 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11917 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11918 | * keyword. These keywords support an optional positive or negative occurrence |
| 11919 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11920 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11921 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11922 | * error message in case of error, that the caller is responsible for freeing. |
| 11923 | * The initial location must either be freeable or NULL. |
| 11924 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 11925 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11926 | { |
| 11927 | 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] | 11928 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11929 | return 0; |
| 11930 | } |
| 11931 | return 1; |
| 11932 | } |
| 11933 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11934 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11935 | * adds an optional offset found in args[0] and emits a string representing |
| 11936 | * the date in RFC-1123/5322 format. |
| 11937 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11938 | 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] | 11939 | { |
| 11940 | const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; |
| 11941 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11942 | struct chunk *temp; |
| 11943 | struct tm *tm; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11944 | /* 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] | 11945 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11946 | |
| 11947 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11948 | if (args && (args[0].type == ARGT_SINT)) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11949 | curr_date += args[0].data.sint; |
| 11950 | |
| 11951 | tm = gmtime(&curr_date); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 11952 | if (!tm) |
| 11953 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11954 | |
| 11955 | temp = get_trash_chunk(); |
| 11956 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11957 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11958 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11959 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11960 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11961 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11962 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11963 | return 1; |
| 11964 | } |
| 11965 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11966 | /* Match language range with language tag. RFC2616 14.4: |
| 11967 | * |
| 11968 | * A language-range matches a language-tag if it exactly equals |
| 11969 | * the tag, or if it exactly equals a prefix of the tag such |
| 11970 | * that the first tag character following the prefix is "-". |
| 11971 | * |
| 11972 | * Return 1 if the strings match, else return 0. |
| 11973 | */ |
| 11974 | static inline int language_range_match(const char *range, int range_len, |
| 11975 | const char *tag, int tag_len) |
| 11976 | { |
| 11977 | const char *end = range + range_len; |
| 11978 | const char *tend = tag + tag_len; |
| 11979 | while (range < end) { |
| 11980 | if (*range == '-' && tag == tend) |
| 11981 | return 1; |
| 11982 | if (*range != *tag || tag == tend) |
| 11983 | return 0; |
| 11984 | range++; |
| 11985 | tag++; |
| 11986 | } |
| 11987 | /* Return true only if the last char of the tag is matched. */ |
| 11988 | return tag == tend; |
| 11989 | } |
| 11990 | |
| 11991 | /* 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] | 11992 | 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] | 11993 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11994 | const char *al = smp->data.u.str.str; |
| 11995 | const char *end = al + smp->data.u.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11996 | const char *token; |
| 11997 | int toklen; |
| 11998 | int qvalue; |
| 11999 | const char *str; |
| 12000 | const char *w; |
| 12001 | int best_q = 0; |
| 12002 | |
| 12003 | /* Set the constant to the sample, because the output of the |
| 12004 | * function will be peek in the constant configuration string. |
| 12005 | */ |
| 12006 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12007 | smp->data.u.str.size = 0; |
| 12008 | smp->data.u.str.str = ""; |
| 12009 | smp->data.u.str.len = 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12010 | |
| 12011 | /* Parse the accept language */ |
| 12012 | while (1) { |
| 12013 | |
| 12014 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12015 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12016 | al++; |
| 12017 | if (al >= end) |
| 12018 | break; |
| 12019 | |
| 12020 | /* Start of the fisrt word. */ |
| 12021 | token = al; |
| 12022 | |
| 12023 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12024 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12025 | al++; |
| 12026 | if (al == token) |
| 12027 | goto expect_comma; |
| 12028 | |
| 12029 | /* Length of the token. */ |
| 12030 | toklen = al - token; |
| 12031 | qvalue = 1000; |
| 12032 | |
| 12033 | /* Check if the token exists in the list. If the token not exists, |
| 12034 | * jump to the next token. |
| 12035 | */ |
| 12036 | str = args[0].data.str.str; |
| 12037 | w = str; |
| 12038 | while (1) { |
| 12039 | if (*str == ';' || *str == '\0') { |
| 12040 | if (language_range_match(token, toklen, w, str-w)) |
| 12041 | goto look_for_q; |
| 12042 | if (*str == '\0') |
| 12043 | goto expect_comma; |
| 12044 | w = str + 1; |
| 12045 | } |
| 12046 | str++; |
| 12047 | } |
| 12048 | goto expect_comma; |
| 12049 | |
| 12050 | look_for_q: |
| 12051 | |
| 12052 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12053 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12054 | al++; |
| 12055 | if (al >= end) |
| 12056 | goto process_value; |
| 12057 | |
| 12058 | /* If ',' is found, process the result */ |
| 12059 | if (*al == ',') |
| 12060 | goto process_value; |
| 12061 | |
| 12062 | /* If the character is different from ';', look |
| 12063 | * for the end of the header part in best effort. |
| 12064 | */ |
| 12065 | if (*al != ';') |
| 12066 | goto expect_comma; |
| 12067 | |
| 12068 | /* Assumes that the char is ';', now expect "q=". */ |
| 12069 | al++; |
| 12070 | |
| 12071 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12072 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12073 | al++; |
| 12074 | if (al >= end) |
| 12075 | goto process_value; |
| 12076 | |
| 12077 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 12078 | if (*al != 'q') |
| 12079 | goto process_value; |
| 12080 | al++; |
| 12081 | |
| 12082 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12083 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12084 | al++; |
| 12085 | if (al >= end) |
| 12086 | goto process_value; |
| 12087 | |
| 12088 | /* Expect '='. If no '=', continue in best effort */ |
| 12089 | if (*al != '=') |
| 12090 | goto process_value; |
| 12091 | al++; |
| 12092 | |
| 12093 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12094 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12095 | al++; |
| 12096 | if (al >= end) |
| 12097 | goto process_value; |
| 12098 | |
| 12099 | /* Parse the q value. */ |
| 12100 | qvalue = parse_qvalue(al, &al); |
| 12101 | |
| 12102 | process_value: |
| 12103 | |
| 12104 | /* If the new q value is the best q value, then store the associated |
| 12105 | * language in the response. If qvalue is the biggest value (1000), |
| 12106 | * break the process. |
| 12107 | */ |
| 12108 | if (qvalue > best_q) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12109 | smp->data.u.str.str = (char *)w; |
| 12110 | smp->data.u.str.len = str - w; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12111 | if (qvalue >= 1000) |
| 12112 | break; |
| 12113 | best_q = qvalue; |
| 12114 | } |
| 12115 | |
| 12116 | expect_comma: |
| 12117 | |
| 12118 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 12119 | while (al < end && *al != ',') |
| 12120 | al++; |
| 12121 | if (al >= end) |
| 12122 | break; |
| 12123 | |
| 12124 | /* Comma is found, jump it and restart the analyzer. */ |
| 12125 | al++; |
| 12126 | } |
| 12127 | |
| 12128 | /* Set default value if required. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12129 | if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) { |
| 12130 | smp->data.u.str.str = args[1].data.str.str; |
| 12131 | smp->data.u.str.len = args[1].data.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12132 | } |
| 12133 | |
| 12134 | /* Return true only if a matching language was found. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12135 | return smp->data.u.str.len != 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12136 | } |
| 12137 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12138 | /* This fetch url-decode any input string. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12139 | 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] | 12140 | { |
| 12141 | /* If the constant flag is set or if not size is avalaible at |
| 12142 | * the end of the buffer, copy the string in other buffer |
| 12143 | * before decoding. |
| 12144 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12145 | 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] | 12146 | struct chunk *str = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12147 | memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len); |
| 12148 | smp->data.u.str.str = str->str; |
| 12149 | smp->data.u.str.size = str->size; |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12150 | smp->flags &= ~SMP_F_CONST; |
| 12151 | } |
| 12152 | |
| 12153 | /* Add final \0 required by url_decode(), and convert the input string. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12154 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 12155 | smp->data.u.str.len = url_decode(smp->data.u.str.str); |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12156 | return 1; |
| 12157 | } |
| 12158 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12159 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 12160 | { |
| 12161 | struct proxy *fe = strm_fe(smp->strm); |
| 12162 | int idx, i; |
| 12163 | struct cap_hdr *hdr; |
| 12164 | int len; |
| 12165 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12166 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12167 | return 0; |
| 12168 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12169 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12170 | |
| 12171 | /* Check the availibity of the capture id. */ |
| 12172 | if (idx > fe->nb_req_cap - 1) |
| 12173 | return 0; |
| 12174 | |
| 12175 | /* Look for the original configuration. */ |
| 12176 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 12177 | hdr != NULL && i != idx ; |
| 12178 | i--, hdr = hdr->next); |
| 12179 | if (!hdr) |
| 12180 | return 0; |
| 12181 | |
| 12182 | /* check for the memory allocation */ |
| 12183 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12184 | smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12185 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12186 | return 0; |
| 12187 | |
| 12188 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12189 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12190 | if (len > hdr->len) |
| 12191 | len = hdr->len; |
| 12192 | |
| 12193 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12194 | memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12195 | smp->strm->req_cap[idx][len] = '\0'; |
| 12196 | |
| 12197 | return 1; |
| 12198 | } |
| 12199 | |
| 12200 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 12201 | { |
| 12202 | struct proxy *fe = strm_fe(smp->strm); |
| 12203 | int idx, i; |
| 12204 | struct cap_hdr *hdr; |
| 12205 | int len; |
| 12206 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12207 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12208 | return 0; |
| 12209 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12210 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12211 | |
| 12212 | /* Check the availibity of the capture id. */ |
| 12213 | if (idx > fe->nb_rsp_cap - 1) |
| 12214 | return 0; |
| 12215 | |
| 12216 | /* Look for the original configuration. */ |
| 12217 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 12218 | hdr != NULL && i != idx ; |
| 12219 | i--, hdr = hdr->next); |
| 12220 | if (!hdr) |
| 12221 | return 0; |
| 12222 | |
| 12223 | /* check for the memory allocation */ |
| 12224 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12225 | smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12226 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12227 | return 0; |
| 12228 | |
| 12229 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12230 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12231 | if (len > hdr->len) |
| 12232 | len = hdr->len; |
| 12233 | |
| 12234 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12235 | memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12236 | smp->strm->res_cap[idx][len] = '\0'; |
| 12237 | |
| 12238 | return 1; |
| 12239 | } |
| 12240 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12241 | /* 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] | 12242 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12243 | * the relevant part of the request line accordingly. Then it updates various |
| 12244 | * pointers to the next elements which were moved, and the total buffer length. |
| 12245 | * 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] | 12246 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 12247 | * error, though this can be revisited when this code is finally exploited. |
| 12248 | * |
| 12249 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 12250 | * query string and 3 to replace uri. |
| 12251 | * |
| 12252 | * In query string case, the mark question '?' must be set at the start of the |
| 12253 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12254 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12255 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12256 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12257 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12258 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12259 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12260 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12261 | int delta; |
| 12262 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12263 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12264 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12265 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12266 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 12267 | |
| 12268 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12269 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12270 | txn->req.sl.rq.m_l += delta; |
| 12271 | txn->req.sl.rq.u += delta; |
| 12272 | txn->req.sl.rq.v += delta; |
| 12273 | break; |
| 12274 | |
| 12275 | case 1: // path |
| 12276 | cur_ptr = http_get_path(txn); |
| 12277 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12278 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12279 | |
| 12280 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12281 | 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] | 12282 | cur_end++; |
| 12283 | |
| 12284 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12285 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12286 | txn->req.sl.rq.u_l += delta; |
| 12287 | txn->req.sl.rq.v += delta; |
| 12288 | break; |
| 12289 | |
| 12290 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12291 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12292 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12293 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12294 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 12295 | cur_ptr++; |
| 12296 | |
| 12297 | /* skip the question mark or indicate that we must insert it |
| 12298 | * (but only if the format string is not empty then). |
| 12299 | */ |
| 12300 | if (cur_ptr < cur_end) |
| 12301 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12302 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12303 | offset = 0; |
| 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 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12312 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12313 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12314 | |
| 12315 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12316 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12317 | txn->req.sl.rq.u_l += delta; |
| 12318 | txn->req.sl.rq.v += delta; |
| 12319 | break; |
| 12320 | |
| 12321 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12322 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12323 | } |
| 12324 | |
| 12325 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12326 | 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] | 12327 | txn->req.sl.rq.l += delta; |
| 12328 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12329 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12330 | return 0; |
| 12331 | } |
| 12332 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12333 | /* This function replace the HTTP status code and the associated message. The |
| 12334 | * variable <status> contains the new status code. This function never fails. |
| 12335 | */ |
| 12336 | void http_set_status(unsigned int status, struct stream *s) |
| 12337 | { |
| 12338 | struct http_txn *txn = s->txn; |
| 12339 | char *cur_ptr, *cur_end; |
| 12340 | int delta; |
| 12341 | char *res; |
| 12342 | int c_l; |
| 12343 | const char *msg; |
| 12344 | int msg_len; |
| 12345 | |
| 12346 | chunk_reset(&trash); |
| 12347 | |
| 12348 | res = ultoa_o(status, trash.str, trash.size); |
| 12349 | c_l = res - trash.str; |
| 12350 | |
| 12351 | trash.str[c_l] = ' '; |
| 12352 | trash.len = c_l + 1; |
| 12353 | |
| 12354 | msg = get_reason(status); |
| 12355 | msg_len = strlen(msg); |
| 12356 | |
| 12357 | strncpy(&trash.str[trash.len], msg, trash.size - trash.len); |
| 12358 | trash.len += msg_len; |
| 12359 | |
| 12360 | cur_ptr = s->res.buf->p + txn->rsp.sl.st.c; |
| 12361 | cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l; |
| 12362 | |
| 12363 | /* commit changes and adjust message */ |
| 12364 | delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len); |
| 12365 | |
| 12366 | /* adjust res line offsets and lengths */ |
| 12367 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 12368 | txn->rsp.sl.st.c_l = c_l; |
| 12369 | txn->rsp.sl.st.r_l = msg_len; |
| 12370 | |
| 12371 | delta = trash.len - (cur_end - cur_ptr); |
| 12372 | txn->rsp.sl.st.l += delta; |
| 12373 | txn->hdr_idx.v[0].len += delta; |
| 12374 | http_msg_move_end(&txn->rsp, delta); |
| 12375 | } |
| 12376 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12377 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 12378 | * 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] | 12379 | * the action to be performed in <http.action>, previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12380 | * parse_set_req_line(). The replacement action is excuted by the function |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 12381 | * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error |
| 12382 | * occurs the action is canceled, but the rule processing continue. |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12383 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12384 | enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px, |
| 12385 | struct session *sess, struct stream *s) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12386 | { |
| 12387 | chunk_reset(&trash); |
| 12388 | |
| 12389 | /* If we have to create a query string, prepare a '?'. */ |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12390 | if (rule->arg.http.action == 2) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12391 | trash.str[trash.len++] = '?'; |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12392 | 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] | 12393 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12394 | 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] | 12395 | return ACT_RET_CONT; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12396 | } |
| 12397 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12398 | /* This function is just a compliant action wrapper for "set-status". */ |
| 12399 | enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px, |
| 12400 | struct session *sess, struct stream *s) |
| 12401 | { |
| 12402 | http_set_status(rule->arg.status.code, s); |
| 12403 | return ACT_RET_CONT; |
| 12404 | } |
| 12405 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12406 | /* parse an http-request action among : |
| 12407 | * set-method |
| 12408 | * set-path |
| 12409 | * set-query |
| 12410 | * set-uri |
| 12411 | * |
| 12412 | * All of them accept a single argument of type string representing a log-format. |
| 12413 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 12414 | * 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] | 12415 | * 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] | 12416 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12417 | enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, |
| 12418 | struct act_rule *rule, char **err) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12419 | { |
| 12420 | int cur_arg = *orig_arg; |
| 12421 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12422 | rule->action = ACT_CUSTOM; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12423 | |
| 12424 | switch (args[0][4]) { |
| 12425 | case 'm' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12426 | rule->arg.http.action = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12427 | rule->action_ptr = http_action_set_req_line; |
| 12428 | break; |
| 12429 | case 'p' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12430 | rule->arg.http.action = 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12431 | rule->action_ptr = http_action_set_req_line; |
| 12432 | break; |
| 12433 | case 'q' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12434 | rule->arg.http.action = 2; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12435 | rule->action_ptr = http_action_set_req_line; |
| 12436 | break; |
| 12437 | case 'u' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12438 | rule->arg.http.action = 3; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12439 | rule->action_ptr = http_action_set_req_line; |
| 12440 | break; |
| 12441 | default: |
| 12442 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12443 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12444 | } |
| 12445 | |
| 12446 | if (!*args[cur_arg] || |
| 12447 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 12448 | memprintf(err, "expects exactly 1 argument <format>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12449 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12450 | } |
| 12451 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12452 | LIST_INIT(&rule->arg.http.logfmt); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12453 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12454 | 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] | 12455 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 12456 | proxy->conf.args.file, proxy->conf.args.line); |
| 12457 | |
| 12458 | (*orig_arg)++; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12459 | return ACT_RET_PRS_OK; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12460 | } |
| 12461 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12462 | /* parse set-status action: |
| 12463 | * This action accepts a single argument of type int representing |
| 12464 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 12465 | * ACT_RET_PRS_ERR on error. |
| 12466 | */ |
| 12467 | enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 12468 | struct act_rule *rule, char **err) |
| 12469 | { |
| 12470 | char *error; |
| 12471 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12472 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12473 | rule->action_ptr = action_http_set_status; |
| 12474 | |
| 12475 | /* Check if an argument is available */ |
| 12476 | if (!*args[*orig_arg]) { |
| 12477 | memprintf(err, "expects exactly 1 argument <status>"); |
| 12478 | return ACT_RET_PRS_ERR; |
| 12479 | } |
| 12480 | |
| 12481 | /* convert status code as integer */ |
| 12482 | rule->arg.status.code = strtol(args[*orig_arg], &error, 10); |
| 12483 | if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) { |
| 12484 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 12485 | return ACT_RET_PRS_ERR; |
| 12486 | } |
| 12487 | |
| 12488 | (*orig_arg)++; |
| 12489 | return ACT_RET_PRS_OK; |
| 12490 | } |
| 12491 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12492 | /* This function executes the "capture" action. It executes a fetch expression, |
| 12493 | * turns the result into a string and puts it in a capture slot. It always |
| 12494 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 12495 | * processing continues. |
| 12496 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12497 | enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px, |
| 12498 | struct session *sess, struct stream *s) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12499 | { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12500 | struct sample *key; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12501 | struct cap_hdr *h = rule->arg.cap.hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12502 | char **cap = s->req_cap; |
| 12503 | int len; |
| 12504 | |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12505 | 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] | 12506 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12507 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12508 | |
| 12509 | if (cap[h->index] == NULL) |
| 12510 | cap[h->index] = pool_alloc2(h->pool); |
| 12511 | |
| 12512 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12513 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12514 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12515 | len = key->data.u.str.len; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12516 | if (len > h->len) |
| 12517 | len = h->len; |
| 12518 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12519 | memcpy(cap[h->index], key->data.u.str.str, len); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12520 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12521 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12522 | } |
| 12523 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12524 | /* This function executes the "capture" action and store the result in a |
| 12525 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12526 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12527 | * error occurs the action is cancelled, but the rule processing continues. |
| 12528 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12529 | enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px, |
| 12530 | struct session *sess, struct stream *s) |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12531 | { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12532 | struct sample *key; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12533 | struct cap_hdr *h; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12534 | char **cap = s->req_cap; |
| 12535 | struct proxy *fe = strm_fe(s); |
| 12536 | int len; |
| 12537 | int i; |
| 12538 | |
| 12539 | /* Look for the original configuration. */ |
| 12540 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12541 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12542 | i--, h = h->next); |
| 12543 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12544 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12545 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12546 | 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] | 12547 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12548 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12549 | |
| 12550 | if (cap[h->index] == NULL) |
| 12551 | cap[h->index] = pool_alloc2(h->pool); |
| 12552 | |
| 12553 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12554 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12555 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12556 | len = key->data.u.str.len; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12557 | if (len > h->len) |
| 12558 | len = h->len; |
| 12559 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12560 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12561 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12562 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12563 | } |
| 12564 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12565 | /* parse an "http-request capture" action. It takes a single argument which is |
| 12566 | * 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] | 12567 | * 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] | 12568 | * 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] | 12569 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12570 | enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12571 | struct act_rule *rule, char **err) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12572 | { |
| 12573 | struct sample_expr *expr; |
| 12574 | struct cap_hdr *hdr; |
| 12575 | int cur_arg; |
Willy Tarreau | 3986ac1 | 2015-05-08 16:13:42 +0200 | [diff] [blame] | 12576 | int len = 0; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12577 | |
| 12578 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12579 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12580 | strcmp(args[cur_arg], "unless") == 0) |
| 12581 | break; |
| 12582 | |
| 12583 | if (cur_arg < *orig_arg + 3) { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12584 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12585 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12586 | } |
| 12587 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12588 | cur_arg = *orig_arg; |
| 12589 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12590 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12591 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12592 | |
| 12593 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 12594 | memprintf(err, |
| 12595 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12596 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12597 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12598 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12599 | } |
| 12600 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12601 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12602 | memprintf(err, "expects 'len or 'id'"); |
| 12603 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12604 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12605 | } |
| 12606 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12607 | if (strcmp(args[cur_arg], "len") == 0) { |
| 12608 | cur_arg++; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12609 | |
| 12610 | if (!(px->cap & PR_CAP_FE)) { |
| 12611 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12612 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12613 | } |
| 12614 | |
| 12615 | proxy->conf.args.ctx = ARGC_CAP; |
| 12616 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12617 | if (!args[cur_arg]) { |
| 12618 | memprintf(err, "missing length value"); |
| 12619 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12620 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12621 | } |
| 12622 | /* we copy the table name for now, it will be resolved later */ |
| 12623 | len = atoi(args[cur_arg]); |
| 12624 | if (len <= 0) { |
| 12625 | memprintf(err, "length must be > 0"); |
| 12626 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12627 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12628 | } |
| 12629 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12630 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12631 | if (!len) { |
| 12632 | memprintf(err, "a positive 'len' argument is mandatory"); |
| 12633 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12634 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12635 | } |
| 12636 | |
| 12637 | hdr = calloc(sizeof(struct cap_hdr), 1); |
| 12638 | hdr->next = px->req_cap; |
| 12639 | hdr->name = NULL; /* not a header capture */ |
| 12640 | hdr->namelen = 0; |
| 12641 | hdr->len = len; |
| 12642 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 12643 | hdr->index = px->nb_req_cap++; |
| 12644 | |
| 12645 | px->req_cap = hdr; |
| 12646 | px->to_log |= LW_REQHDR; |
| 12647 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12648 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12649 | rule->action_ptr = http_action_req_capture; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12650 | rule->arg.cap.expr = expr; |
| 12651 | rule->arg.cap.hdr = hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12652 | } |
| 12653 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12654 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 12655 | int id; |
| 12656 | char *error; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12657 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12658 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12659 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12660 | if (!args[cur_arg]) { |
| 12661 | memprintf(err, "missing id value"); |
| 12662 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12663 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12664 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12665 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12666 | id = strtol(args[cur_arg], &error, 10); |
| 12667 | if (*error != '\0') { |
| 12668 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12669 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12670 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12671 | } |
| 12672 | cur_arg++; |
| 12673 | |
| 12674 | proxy->conf.args.ctx = ARGC_CAP; |
| 12675 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12676 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12677 | rule->action_ptr = http_action_req_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12678 | rule->arg.capid.expr = expr; |
| 12679 | rule->arg.capid.idx = id; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12680 | } |
| 12681 | |
| 12682 | else { |
| 12683 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 12684 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12685 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12686 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12687 | |
| 12688 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12689 | return ACT_RET_PRS_OK; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12690 | } |
| 12691 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12692 | /* This function executes the "capture" action and store the result in a |
| 12693 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12694 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12695 | * error occurs the action is cancelled, but the rule processing continues. |
| 12696 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12697 | enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px, |
| 12698 | struct session *sess, struct stream *s) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12699 | { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12700 | struct sample *key; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12701 | struct cap_hdr *h; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12702 | char **cap = s->res_cap; |
| 12703 | struct proxy *fe = strm_fe(s); |
| 12704 | int len; |
| 12705 | int i; |
| 12706 | |
| 12707 | /* Look for the original configuration. */ |
| 12708 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12709 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12710 | i--, h = h->next); |
| 12711 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12712 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12713 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12714 | 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] | 12715 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12716 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12717 | |
| 12718 | if (cap[h->index] == NULL) |
| 12719 | cap[h->index] = pool_alloc2(h->pool); |
| 12720 | |
| 12721 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12722 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12723 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12724 | len = key->data.u.str.len; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12725 | if (len > h->len) |
| 12726 | len = h->len; |
| 12727 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12728 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12729 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12730 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12731 | } |
| 12732 | |
| 12733 | /* parse an "http-response capture" action. It takes a single argument which is |
| 12734 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 12735 | * 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] | 12736 | * 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] | 12737 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12738 | enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12739 | struct act_rule *rule, char **err) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12740 | { |
| 12741 | struct sample_expr *expr; |
| 12742 | int cur_arg; |
| 12743 | int id; |
| 12744 | char *error; |
| 12745 | |
| 12746 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12747 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12748 | strcmp(args[cur_arg], "unless") == 0) |
| 12749 | break; |
| 12750 | |
| 12751 | if (cur_arg < *orig_arg + 3) { |
| 12752 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12753 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12754 | } |
| 12755 | |
| 12756 | cur_arg = *orig_arg; |
| 12757 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12758 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12759 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12760 | |
| 12761 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 12762 | memprintf(err, |
| 12763 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12764 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12765 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12766 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12767 | } |
| 12768 | |
| 12769 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12770 | memprintf(err, "expects 'len or 'id'"); |
| 12771 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12772 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12773 | } |
| 12774 | |
| 12775 | if (strcmp(args[cur_arg], "id") != 0) { |
| 12776 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 12777 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12778 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12779 | } |
| 12780 | |
| 12781 | cur_arg++; |
| 12782 | |
| 12783 | if (!args[cur_arg]) { |
| 12784 | memprintf(err, "missing id value"); |
| 12785 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12786 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12787 | } |
| 12788 | |
| 12789 | id = strtol(args[cur_arg], &error, 10); |
| 12790 | if (*error != '\0') { |
| 12791 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 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 | cur_arg++; |
| 12796 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12797 | proxy->conf.args.ctx = ARGC_CAP; |
| 12798 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12799 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12800 | rule->action_ptr = http_action_res_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12801 | rule->arg.capid.expr = expr; |
| 12802 | rule->arg.capid.idx = id; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12803 | |
| 12804 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12805 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12806 | } |
| 12807 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12808 | /* |
| 12809 | * Return the struct http_req_action_kw associated to a keyword. |
| 12810 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12811 | struct action_kw *action_http_req_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12812 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12813 | return action_lookup(&http_req_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12814 | } |
| 12815 | |
| 12816 | /* |
| 12817 | * Return the struct http_res_action_kw associated to a keyword. |
| 12818 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12819 | struct action_kw *action_http_res_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12820 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12821 | return action_lookup(&http_res_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12822 | } |
| 12823 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12824 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12825 | /* All supported ACL keywords must be declared here. */ |
| 12826 | /************************************************************************/ |
| 12827 | |
| 12828 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12829 | * Please take care of keeping this list alphabetically sorted. |
| 12830 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12831 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12832 | { "base", "base", PAT_MATCH_STR }, |
| 12833 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 12834 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 12835 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 12836 | { "base_end", "base", PAT_MATCH_END }, |
| 12837 | { "base_len", "base", PAT_MATCH_LEN }, |
| 12838 | { "base_reg", "base", PAT_MATCH_REG }, |
| 12839 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 12840 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12841 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 12842 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 12843 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 12844 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 12845 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 12846 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 12847 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 12848 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12849 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12850 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 12851 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 12852 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 12853 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 12854 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 12855 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 12856 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 12857 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12858 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12859 | /* these two declarations uses strings with list storage (in place |
| 12860 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 12861 | * and delete functions are relative to the list management. The parse |
| 12862 | * and match method are related to the corresponding fetch methods. This |
| 12863 | * is very particular ACL declaration mode. |
| 12864 | */ |
| 12865 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 12866 | { "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] | 12867 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12868 | { "path", "path", PAT_MATCH_STR }, |
| 12869 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 12870 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 12871 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 12872 | { "path_end", "path", PAT_MATCH_END }, |
| 12873 | { "path_len", "path", PAT_MATCH_LEN }, |
| 12874 | { "path_reg", "path", PAT_MATCH_REG }, |
| 12875 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12876 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12877 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 12878 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
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 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 12881 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 12882 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 12883 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 12884 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 12885 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 12886 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 12887 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
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 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 12890 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 12891 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 12892 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 12893 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 12894 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 12895 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 12896 | { "shdr_sub", "res.hdr", 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 | { "url", "url", PAT_MATCH_STR }, |
| 12899 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 12900 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 12901 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 12902 | { "url_end", "url", PAT_MATCH_END }, |
| 12903 | { "url_len", "url", PAT_MATCH_LEN }, |
| 12904 | { "url_reg", "url", PAT_MATCH_REG }, |
| 12905 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12906 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12907 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 12908 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 12909 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 12910 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 12911 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 12912 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 12913 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 12914 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12915 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12916 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12917 | }}; |
| 12918 | |
| 12919 | /************************************************************************/ |
| 12920 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12921 | /************************************************************************/ |
| 12922 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12923 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12924 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12925 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12926 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12927 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 12928 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12929 | { "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] | 12930 | |
| 12931 | /* retrieve these captures from the HTTP logs */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12932 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12933 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12934 | { "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] | 12935 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12936 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 12937 | { "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] | 12938 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12939 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 12940 | * are only here to match the ACL's name, are request-only and are used |
| 12941 | * for ACL compatibility only. |
| 12942 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12943 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12944 | { "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] | 12945 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12946 | { "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] | 12947 | |
| 12948 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 12949 | * only here to match the ACL's name, are request-only and are used for |
| 12950 | * ACL compatibility only. |
| 12951 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12952 | { "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] | 12953 | { "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] | 12954 | { "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] | 12955 | { "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] | 12956 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 12957 | { "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] | 12958 | { "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] | 12959 | { "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] | 12960 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12961 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 12962 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12963 | |
| 12964 | /* HTTP protocol on the request path */ |
| 12965 | { "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] | 12966 | { "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] | 12967 | |
| 12968 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12969 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12970 | { "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] | 12971 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 12972 | { "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] | 12973 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12974 | { "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] | 12975 | { "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] | 12976 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12977 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12978 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 12979 | { "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] | 12980 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12981 | /* 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] | 12982 | { "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] | 12983 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12984 | { "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] | 12985 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12986 | { "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] | 12987 | { "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] | 12988 | { "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] | 12989 | { "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] | 12990 | { "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] | 12991 | { "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] | 12992 | { "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] | 12993 | |
| 12994 | /* 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] | 12995 | { "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] | 12996 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12997 | { "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] | 12998 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12999 | { "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] | 13000 | { "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] | 13001 | { "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] | 13002 | { "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] | 13003 | { "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] | 13004 | { "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] | 13005 | { "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] | 13006 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13007 | /* 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] | 13008 | { "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] | 13009 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 13010 | { "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] | 13011 | { "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] | 13012 | |
| 13013 | /* 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] | 13014 | { "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] | 13015 | { "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] | 13016 | { "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] | 13017 | { "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] | 13018 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13019 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13020 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13021 | { "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] | 13022 | { "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] | 13023 | { "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] | 13024 | { "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] | 13025 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13026 | { "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] | 13027 | { "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] | 13028 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 13029 | }}; |
| 13030 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13031 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13032 | /************************************************************************/ |
| 13033 | /* All supported converter keywords must be declared here. */ |
| 13034 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13035 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 13036 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13037 | { "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] | 13038 | { "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] | 13039 | { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 13040 | { "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] | 13041 | { "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] | 13042 | { NULL, NULL, 0, 0, 0 }, |
| 13043 | }}; |
| 13044 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 13045 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13046 | /************************************************************************/ |
| 13047 | /* All supported http-request action keywords must be declared here. */ |
| 13048 | /************************************************************************/ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 13049 | struct action_kw_list http_req_actions = { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13050 | .kw = { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 13051 | { "capture", parse_http_req_capture }, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13052 | { "set-method", parse_set_req_line }, |
| 13053 | { "set-path", parse_set_req_line }, |
| 13054 | { "set-query", parse_set_req_line }, |
| 13055 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 13056 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13057 | } |
| 13058 | }; |
| 13059 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 13060 | struct action_kw_list http_res_actions = { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13061 | .kw = { |
| 13062 | { "capture", parse_http_res_capture }, |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 13063 | { "set-status", parse_http_set_status }, |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13064 | { NULL, NULL } |
| 13065 | } |
| 13066 | }; |
| 13067 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13068 | __attribute__((constructor)) |
| 13069 | static void __http_protocol_init(void) |
| 13070 | { |
| 13071 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 13072 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13073 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13074 | http_req_keywords_register(&http_req_actions); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13075 | http_res_keywords_register(&http_res_actions); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13076 | } |
| 13077 | |
| 13078 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 13079 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 13080 | * Local variables: |
| 13081 | * c-indent-level: 8 |
| 13082 | * c-basic-offset: 8 |
| 13083 | * End: |
| 13084 | */ |