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 : |
| 469 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 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] = { |
| 474 | [HTTP_METH_NONE] = { "", 0 }, |
| 475 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 476 | [HTTP_METH_GET] = { "GET", 3 }, |
| 477 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 478 | [HTTP_METH_POST] = { "POST", 4 }, |
| 479 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 480 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 481 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 482 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 483 | }; |
| 484 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 485 | /* 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] | 486 | * 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] | 487 | * RFC2616 for those chars. |
| 488 | */ |
| 489 | |
| 490 | const char http_is_spht[256] = { |
| 491 | [' '] = 1, ['\t'] = 1, |
| 492 | }; |
| 493 | |
| 494 | const char http_is_crlf[256] = { |
| 495 | ['\r'] = 1, ['\n'] = 1, |
| 496 | }; |
| 497 | |
| 498 | const char http_is_lws[256] = { |
| 499 | [' '] = 1, ['\t'] = 1, |
| 500 | ['\r'] = 1, ['\n'] = 1, |
| 501 | }; |
| 502 | |
| 503 | const char http_is_sep[256] = { |
| 504 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 505 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 506 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 507 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 508 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 509 | }; |
| 510 | |
| 511 | const char http_is_ctl[256] = { |
| 512 | [0 ... 31] = 1, |
| 513 | [127] = 1, |
| 514 | }; |
| 515 | |
| 516 | /* |
| 517 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 518 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 519 | * them correctly. Instead, define every non-CTL char's status. |
| 520 | */ |
| 521 | const char http_is_token[256] = { |
| 522 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 523 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 524 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 525 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 526 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 527 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 528 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 529 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 530 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 531 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 532 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 533 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 534 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 535 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 536 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 537 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 538 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 539 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 540 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 541 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 542 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 543 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 544 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 545 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 546 | }; |
| 547 | |
| 548 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 549 | /* |
| 550 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 551 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 552 | */ |
| 553 | const char http_is_ver_token[256] = { |
| 554 | ['.'] = 1, ['/'] = 1, |
| 555 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 556 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
Thierry FOURNIER | 63d692c | 2015-02-28 19:03:56 +0100 | [diff] [blame] | 557 | ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1, |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 558 | }; |
| 559 | |
| 560 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 561 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 562 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 563 | * 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] | 564 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 565 | * of headers is automatically adjusted. The number of bytes added is returned |
| 566 | * on success, otherwise <0 is returned indicating an error. |
| 567 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 568 | 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] | 569 | { |
| 570 | int bytes, len; |
| 571 | |
| 572 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 573 | 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] | 574 | if (!bytes) |
| 575 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 576 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 577 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 578 | } |
| 579 | |
| 580 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 581 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 582 | * 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] | 583 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 584 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 585 | * of headers is automatically adjusted. The number of bytes added is returned |
| 586 | * on success, otherwise <0 is returned indicating an error. |
| 587 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 588 | int http_header_add_tail2(struct http_msg *msg, |
| 589 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 590 | { |
| 591 | int bytes; |
| 592 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 593 | 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] | 594 | if (!bytes) |
| 595 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 596 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 597 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 598 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 599 | |
| 600 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 601 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 602 | * If so, returns the position of the first non-space character relative to |
| 603 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 604 | * to return a pointer to the place after the first space. Returns 0 if the |
| 605 | * header name does not match. Checks are case-insensitive. |
| 606 | */ |
| 607 | int http_header_match2(const char *hdr, const char *end, |
| 608 | const char *name, int len) |
| 609 | { |
| 610 | const char *val; |
| 611 | |
| 612 | if (hdr + len >= end) |
| 613 | return 0; |
| 614 | if (hdr[len] != ':') |
| 615 | return 0; |
| 616 | if (strncasecmp(hdr, name, len) != 0) |
| 617 | return 0; |
| 618 | val = hdr + len + 1; |
| 619 | while (val < end && HTTP_IS_SPHT(*val)) |
| 620 | val++; |
| 621 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 622 | return len + 2; /* we may replace starting from second space */ |
| 623 | return val - hdr; |
| 624 | } |
| 625 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 626 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 627 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 628 | * structure holds everything necessary to use the header and find next |
| 629 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 630 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 631 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 632 | * http_find_header2() except that it is designed to work with full-line headers |
| 633 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 634 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 635 | * header is rescanned. This allows rescanning after a header deletion. |
| 636 | */ |
| 637 | int http_find_full_header2(const char *name, int len, |
| 638 | char *sol, struct hdr_idx *idx, |
| 639 | struct hdr_ctx *ctx) |
| 640 | { |
| 641 | char *eol, *sov; |
| 642 | int cur_idx, old_idx; |
| 643 | |
| 644 | cur_idx = ctx->idx; |
| 645 | if (cur_idx) { |
| 646 | /* We have previously returned a header, let's search another one */ |
| 647 | sol = ctx->line; |
| 648 | eol = sol + idx->v[cur_idx].len; |
| 649 | goto next_hdr; |
| 650 | } |
| 651 | |
| 652 | /* first request for this header */ |
| 653 | sol += hdr_idx_first_pos(idx); |
| 654 | old_idx = 0; |
| 655 | cur_idx = hdr_idx_first_idx(idx); |
| 656 | while (cur_idx) { |
| 657 | eol = sol + idx->v[cur_idx].len; |
| 658 | |
| 659 | if (len == 0) { |
| 660 | /* No argument was passed, we want any header. |
| 661 | * To achieve this, we simply build a fake request. */ |
| 662 | while (sol + len < eol && sol[len] != ':') |
| 663 | len++; |
| 664 | name = sol; |
| 665 | } |
| 666 | |
| 667 | if ((len < eol - sol) && |
| 668 | (sol[len] == ':') && |
| 669 | (strncasecmp(sol, name, len) == 0)) { |
| 670 | ctx->del = len; |
| 671 | sov = sol + len + 1; |
| 672 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 673 | sov++; |
| 674 | |
| 675 | ctx->line = sol; |
| 676 | ctx->prev = old_idx; |
| 677 | ctx->idx = cur_idx; |
| 678 | ctx->val = sov - sol; |
| 679 | ctx->tws = 0; |
| 680 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 681 | eol--; |
| 682 | ctx->tws++; |
| 683 | } |
| 684 | ctx->vlen = eol - sov; |
| 685 | return 1; |
| 686 | } |
| 687 | next_hdr: |
| 688 | sol = eol + idx->v[cur_idx].cr + 1; |
| 689 | old_idx = cur_idx; |
| 690 | cur_idx = idx->v[cur_idx].next; |
| 691 | } |
| 692 | return 0; |
| 693 | } |
| 694 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 695 | /* Find the first or next header field in message buffer <sol> using headers |
| 696 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 697 | * everything necessary to use the header and find next occurrence. If its |
| 698 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 699 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 700 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 701 | * no header name. |
| 702 | */ |
| 703 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 704 | { |
| 705 | char *eol, *sov; |
| 706 | int cur_idx, old_idx; |
| 707 | int len; |
| 708 | |
| 709 | cur_idx = ctx->idx; |
| 710 | if (cur_idx) { |
| 711 | /* We have previously returned a header, let's search another one */ |
| 712 | sol = ctx->line; |
| 713 | eol = sol + idx->v[cur_idx].len; |
| 714 | goto next_hdr; |
| 715 | } |
| 716 | |
| 717 | /* first request for this header */ |
| 718 | sol += hdr_idx_first_pos(idx); |
| 719 | old_idx = 0; |
| 720 | cur_idx = hdr_idx_first_idx(idx); |
| 721 | while (cur_idx) { |
| 722 | eol = sol + idx->v[cur_idx].len; |
| 723 | |
| 724 | len = 0; |
| 725 | while (1) { |
| 726 | if (len >= eol - sol) |
| 727 | goto next_hdr; |
| 728 | if (sol[len] == ':') |
| 729 | break; |
| 730 | len++; |
| 731 | } |
| 732 | |
| 733 | ctx->del = len; |
| 734 | sov = sol + len + 1; |
| 735 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 736 | sov++; |
| 737 | |
| 738 | ctx->line = sol; |
| 739 | ctx->prev = old_idx; |
| 740 | ctx->idx = cur_idx; |
| 741 | ctx->val = sov - sol; |
| 742 | ctx->tws = 0; |
| 743 | |
| 744 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 745 | eol--; |
| 746 | ctx->tws++; |
| 747 | } |
| 748 | ctx->vlen = eol - sov; |
| 749 | return 1; |
| 750 | |
| 751 | next_hdr: |
| 752 | sol = eol + idx->v[cur_idx].cr + 1; |
| 753 | old_idx = cur_idx; |
| 754 | cur_idx = idx->v[cur_idx].next; |
| 755 | } |
| 756 | return 0; |
| 757 | } |
| 758 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 759 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 760 | * par 2.2 for more information. Note that it requires a valid header to return |
| 761 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 762 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 763 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 764 | { |
| 765 | int quoted, qdpair; |
| 766 | |
| 767 | quoted = qdpair = 0; |
| 768 | for (; s < e; s++) { |
| 769 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 770 | else if (quoted) { |
| 771 | if (*s == '\\') qdpair = 1; |
| 772 | else if (*s == '"') quoted = 0; |
| 773 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 774 | else if (*s == '"') quoted = 1; |
| 775 | else if (*s == ',') return s; |
| 776 | } |
| 777 | return s; |
| 778 | } |
| 779 | |
| 780 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 781 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 782 | * structure holds everything necessary to use the header and find next |
| 783 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 784 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 785 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 786 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 787 | * is NULL when searching for a new values of a header, the current header is |
| 788 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 789 | */ |
| 790 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 791 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 792 | struct hdr_ctx *ctx) |
| 793 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 794 | char *eol, *sov; |
| 795 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 796 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 797 | cur_idx = ctx->idx; |
| 798 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 799 | /* We have previously returned a value, let's search |
| 800 | * another one on the same line. |
| 801 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 802 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 803 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 804 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 805 | eol = sol + idx->v[cur_idx].len; |
| 806 | |
| 807 | if (sov >= eol) |
| 808 | /* no more values in this header */ |
| 809 | goto next_hdr; |
| 810 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 811 | /* values remaining for this header, skip the comma but save it |
| 812 | * for later use (eg: for header deletion). |
| 813 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 814 | sov++; |
| 815 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 816 | sov++; |
| 817 | |
| 818 | goto return_hdr; |
| 819 | } |
| 820 | |
| 821 | /* first request for this header */ |
| 822 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 823 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 824 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 825 | while (cur_idx) { |
| 826 | eol = sol + idx->v[cur_idx].len; |
| 827 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 828 | if (len == 0) { |
| 829 | /* No argument was passed, we want any header. |
| 830 | * To achieve this, we simply build a fake request. */ |
| 831 | while (sol + len < eol && sol[len] != ':') |
| 832 | len++; |
| 833 | name = sol; |
| 834 | } |
| 835 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 836 | if ((len < eol - sol) && |
| 837 | (sol[len] == ':') && |
| 838 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 839 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 840 | sov = sol + len + 1; |
| 841 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 842 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 843 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 844 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 845 | ctx->prev = old_idx; |
| 846 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 847 | ctx->idx = cur_idx; |
| 848 | ctx->val = sov - sol; |
| 849 | |
| 850 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 851 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 852 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 853 | eol--; |
| 854 | ctx->tws++; |
| 855 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 856 | ctx->vlen = eol - sov; |
| 857 | return 1; |
| 858 | } |
| 859 | next_hdr: |
| 860 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 861 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 862 | cur_idx = idx->v[cur_idx].next; |
| 863 | } |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 868 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 869 | struct hdr_ctx *ctx) |
| 870 | { |
| 871 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 872 | } |
| 873 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 874 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 875 | * the http_find_header functions. The value is removed, as well as surrounding |
| 876 | * 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] | 877 | * 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] | 878 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 879 | * no more header, so any processing may stop. The ctx is always left in a form |
| 880 | * that can be handled by http_find_header2() to find next occurrence. |
| 881 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 882 | 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] | 883 | { |
| 884 | int cur_idx = ctx->idx; |
| 885 | char *sol = ctx->line; |
| 886 | struct hdr_idx_elem *hdr; |
| 887 | int delta, skip_comma; |
| 888 | |
| 889 | if (!cur_idx) |
| 890 | return 0; |
| 891 | |
| 892 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 893 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 894 | /* 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] | 895 | 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] | 896 | http_msg_move_end(msg, delta); |
| 897 | idx->used--; |
| 898 | hdr->len = 0; /* unused entry */ |
| 899 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 900 | if (idx->tail == ctx->idx) |
| 901 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 902 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 903 | 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] | 904 | 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] | 905 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 906 | return ctx->idx; |
| 907 | } |
| 908 | |
| 909 | /* 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] | 910 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 911 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 912 | */ |
| 913 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 914 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 915 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 916 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 917 | NULL, 0); |
| 918 | hdr->len += delta; |
| 919 | http_msg_move_end(msg, delta); |
| 920 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 921 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 922 | return ctx->idx; |
| 923 | } |
| 924 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 925 | /* This function handles a server error at the stream interface level. The |
| 926 | * stream interface is assumed to be already in a closed state. An optional |
| 927 | * message is copied into the input buffer, and an HTTP status code stored. |
| 928 | * 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] | 929 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 930 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 931 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 932 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 933 | { |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 934 | channel_auto_read(si_oc(si)); |
| 935 | channel_abort(si_oc(si)); |
| 936 | channel_auto_close(si_oc(si)); |
| 937 | channel_erase(si_oc(si)); |
| 938 | channel_auto_close(si_ic(si)); |
| 939 | channel_auto_read(si_ic(si)); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 940 | if (status > 0 && msg) { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 941 | s->txn->status = status; |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 942 | bo_inject(si_ic(si), msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 943 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 944 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 945 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 946 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 947 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 948 | } |
| 949 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 950 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 951 | * and message. |
| 952 | */ |
| 953 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 954 | struct chunk *http_error_message(struct stream *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 955 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 956 | if (s->be->errmsg[msgnum].str) |
| 957 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 958 | else if (strm_fe(s)->errmsg[msgnum].str) |
| 959 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 960 | else |
| 961 | return &http_err_chunks[msgnum]; |
| 962 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 963 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 964 | /* |
| 965 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 966 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 967 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 968 | 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] | 969 | { |
| 970 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 971 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 972 | |
| 973 | m = ((unsigned)*str - 'A'); |
| 974 | |
| 975 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 976 | for (h = http_methods[m]; h->len > 0; h++) { |
| 977 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 978 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 979 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 980 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 981 | }; |
| 982 | return HTTP_METH_OTHER; |
| 983 | } |
| 984 | return HTTP_METH_NONE; |
| 985 | |
| 986 | } |
| 987 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 988 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 989 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 990 | * It is returned otherwise. |
| 991 | */ |
| 992 | static char * |
| 993 | http_get_path(struct http_txn *txn) |
| 994 | { |
| 995 | char *ptr, *end; |
| 996 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 997 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 998 | end = ptr + txn->req.sl.rq.u_l; |
| 999 | |
| 1000 | if (ptr >= end) |
| 1001 | return NULL; |
| 1002 | |
| 1003 | /* RFC2616, par. 5.1.2 : |
| 1004 | * Request-URI = "*" | absuri | abspath | authority |
| 1005 | */ |
| 1006 | |
| 1007 | if (*ptr == '*') |
| 1008 | return NULL; |
| 1009 | |
| 1010 | if (isalpha((unsigned char)*ptr)) { |
| 1011 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1012 | ptr++; |
| 1013 | while (ptr < end && |
| 1014 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 1015 | ptr++; |
| 1016 | /* skip '://' */ |
| 1017 | if (ptr == end || *ptr++ != ':') |
| 1018 | return NULL; |
| 1019 | if (ptr == end || *ptr++ != '/') |
| 1020 | return NULL; |
| 1021 | if (ptr == end || *ptr++ != '/') |
| 1022 | return NULL; |
| 1023 | } |
| 1024 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1025 | |
| 1026 | while (ptr < end && *ptr != '/') |
| 1027 | ptr++; |
| 1028 | |
| 1029 | if (ptr == end) |
| 1030 | return NULL; |
| 1031 | |
| 1032 | /* OK, we got the '/' ! */ |
| 1033 | return ptr; |
| 1034 | } |
| 1035 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 1036 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 1037 | * If not found, return NULL. It is returned otherwise. |
| 1038 | */ |
| 1039 | static char * |
| 1040 | http_get_path_from_string(char *str) |
| 1041 | { |
| 1042 | char *ptr = str; |
| 1043 | |
| 1044 | /* RFC2616, par. 5.1.2 : |
| 1045 | * Request-URI = "*" | absuri | abspath | authority |
| 1046 | */ |
| 1047 | |
| 1048 | if (*ptr == '*') |
| 1049 | return NULL; |
| 1050 | |
| 1051 | if (isalpha((unsigned char)*ptr)) { |
| 1052 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1053 | ptr++; |
| 1054 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 1055 | ptr++; |
| 1056 | /* skip '://' */ |
| 1057 | if (*ptr == '\0' || *ptr++ != ':') |
| 1058 | return NULL; |
| 1059 | if (*ptr == '\0' || *ptr++ != '/') |
| 1060 | return NULL; |
| 1061 | if (*ptr == '\0' || *ptr++ != '/') |
| 1062 | return NULL; |
| 1063 | } |
| 1064 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1065 | |
| 1066 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 1067 | ptr++; |
| 1068 | |
| 1069 | if (*ptr == '\0' || *ptr == ' ') |
| 1070 | return NULL; |
| 1071 | |
| 1072 | /* OK, we got the '/' ! */ |
| 1073 | return ptr; |
| 1074 | } |
| 1075 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 1076 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 1077 | * in redirect mode. This may only be called just after the stream interface |
| 1078 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 1079 | * follow normal proxy processing. NOTE: this function is designed to support |
| 1080 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1081 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1082 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1083 | { |
| 1084 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1085 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1086 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1087 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1088 | |
| 1089 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1090 | trash.len = strlen(HTTP_302); |
| 1091 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1092 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1093 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1094 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1095 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1096 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1097 | return; |
| 1098 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1099 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1100 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1101 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 1102 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1103 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1104 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1105 | /* 3: add the request URI. Since it was already forwarded, we need |
| 1106 | * to temporarily rewind the buffer. |
| 1107 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1108 | txn = s->txn; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1109 | b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1110 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1111 | path = http_get_path(txn); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1112 | 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] | 1113 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1114 | b_adv(s->req.buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1115 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1116 | if (!path) |
| 1117 | return; |
| 1118 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1119 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1120 | return; |
| 1121 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1122 | memcpy(trash.str + trash.len, path, len); |
| 1123 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1124 | |
| 1125 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1126 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 1127 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1128 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1129 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 1130 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1131 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1132 | |
| 1133 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1134 | si_shutr(si); |
| 1135 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1136 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1137 | si->state = SI_ST_CLO; |
| 1138 | |
| 1139 | /* send the message */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1140 | 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] | 1141 | |
| 1142 | /* 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] | 1143 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 1144 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1145 | } |
| 1146 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1147 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1148 | * that the server side is closed. Note that err_type is actually a |
| 1149 | * bitmask, where almost only aborts may be cumulated with other |
| 1150 | * values. We consider that aborted operations are more important |
| 1151 | * than timeouts or errors due to the fact that nobody else in the |
| 1152 | * logs might explain incomplete retries. All others should avoid |
| 1153 | * being cumulated. It should normally not be possible to have multiple |
| 1154 | * 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] | 1155 | * Note that connection errors appearing on the second request of a keep-alive |
| 1156 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1157 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1158 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1159 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1160 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1161 | |
| 1162 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1163 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1164 | 503, 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_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1166 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1167 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1168 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1169 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1170 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1171 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1172 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1173 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1174 | 503, 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_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1176 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1177 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1178 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1179 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1180 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
| 1181 | 503, (s->flags & SF_SRV_REUSED) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1182 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1183 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1184 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1185 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1186 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1187 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1188 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1189 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1190 | } |
| 1191 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1192 | extern const char sess_term_cond[8]; |
| 1193 | extern const char sess_fin_state[8]; |
| 1194 | extern const char *monthname[12]; |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 1195 | struct pool_head *pool2_http_txn; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1196 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1197 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1198 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1199 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1200 | /* |
| 1201 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1202 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1203 | */ |
| 1204 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1205 | char **cap, struct cap_hdr *cap_hdr) |
| 1206 | { |
| 1207 | char *eol, *sol, *col, *sov; |
| 1208 | int cur_idx; |
| 1209 | struct cap_hdr *h; |
| 1210 | int len; |
| 1211 | |
| 1212 | sol = som + hdr_idx_first_pos(idx); |
| 1213 | cur_idx = hdr_idx_first_idx(idx); |
| 1214 | |
| 1215 | while (cur_idx) { |
| 1216 | eol = sol + idx->v[cur_idx].len; |
| 1217 | |
| 1218 | col = sol; |
| 1219 | while (col < eol && *col != ':') |
| 1220 | col++; |
| 1221 | |
| 1222 | sov = col + 1; |
| 1223 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1224 | sov++; |
| 1225 | |
| 1226 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1227 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1228 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1229 | if (cap[h->index] == NULL) |
| 1230 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1231 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1232 | |
| 1233 | if (cap[h->index] == NULL) { |
| 1234 | Alert("HTTP capture : out of memory.\n"); |
| 1235 | continue; |
| 1236 | } |
| 1237 | |
| 1238 | len = eol - sov; |
| 1239 | if (len > h->len) |
| 1240 | len = h->len; |
| 1241 | |
| 1242 | memcpy(cap[h->index], sov, len); |
| 1243 | cap[h->index][len]=0; |
| 1244 | } |
| 1245 | } |
| 1246 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1247 | cur_idx = idx->v[cur_idx].next; |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1252 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1253 | */ |
| 1254 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1255 | |
| 1256 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1257 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1258 | */ |
| 1259 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1260 | ptr++; \ |
| 1261 | if (likely(ptr < end)) \ |
| 1262 | goto good; \ |
| 1263 | else { \ |
| 1264 | state = (st); \ |
| 1265 | goto http_msg_ood; \ |
| 1266 | } \ |
| 1267 | } while (0) |
| 1268 | |
| 1269 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1270 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1271 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1272 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1273 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1274 | * will give undefined results. |
| 1275 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1276 | * and that msg->sol points to the beginning of the response. |
| 1277 | * If a complete line is found (which implies that at least one CR or LF is |
| 1278 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1279 | * returned indicating an incomplete line (which does not mean that parts have |
| 1280 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1281 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1282 | * upon next call. |
| 1283 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1284 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1285 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1286 | * 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] | 1287 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1288 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1289 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1290 | enum ht_state state, const char *ptr, const char *end, |
| 1291 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1292 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1293 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1294 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1295 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1296 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1297 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1298 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1299 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1300 | |
| 1301 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1302 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1303 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1304 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1305 | state = HTTP_MSG_ERROR; |
| 1306 | break; |
| 1307 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1308 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1309 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1310 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1311 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1312 | goto http_msg_rpcode; |
| 1313 | } |
| 1314 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1315 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1316 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1317 | state = HTTP_MSG_ERROR; |
| 1318 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1319 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1320 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1321 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1322 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1323 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1324 | |
| 1325 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1326 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1327 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1328 | } |
| 1329 | |
| 1330 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1331 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1332 | http_msg_rsp_reason: |
| 1333 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1334 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1335 | msg->sl.st.r_l = 0; |
| 1336 | goto http_msg_rpline_eol; |
| 1337 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1338 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1339 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1340 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1341 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1342 | goto http_msg_rpreason; |
| 1343 | } |
| 1344 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1345 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1346 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1347 | goto http_msg_rsp_reason; |
| 1348 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1349 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1350 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1351 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1352 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1353 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1354 | http_msg_rpline_eol: |
| 1355 | /* We have seen the end of line. Note that we do not |
| 1356 | * necessarily have the \n yet, but at least we know that we |
| 1357 | * have EITHER \r OR \n, otherwise the response would not be |
| 1358 | * complete. We can then record the response length and return |
| 1359 | * to the caller which will be able to register it. |
| 1360 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1361 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1362 | return ptr; |
| 1363 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1364 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1365 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1366 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1367 | exit(1); |
| 1368 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1369 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1373 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1374 | if (ret_state) |
| 1375 | *ret_state = state; |
| 1376 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1377 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1378 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1379 | } |
| 1380 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1381 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1382 | * This function parses a request line between <ptr> and <end>, starting with |
| 1383 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1384 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1385 | * will give undefined results. |
| 1386 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1387 | * and that msg->sol points to the beginning of the request. |
| 1388 | * If a complete line is found (which implies that at least one CR or LF is |
| 1389 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1390 | * returned indicating an incomplete line (which does not mean that parts have |
| 1391 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1392 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1393 | * upon next call. |
| 1394 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1395 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1396 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1397 | * 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] | 1398 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1399 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1400 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1401 | enum ht_state state, const char *ptr, const char *end, |
| 1402 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1403 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1404 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1405 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1406 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1407 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1408 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1409 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1410 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1411 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1412 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1413 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1414 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1415 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1416 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1417 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1418 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1419 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1420 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1421 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1422 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1423 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1424 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1425 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1426 | msg->sl.rq.v_l = 0; |
| 1427 | goto http_msg_rqline_eol; |
| 1428 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1429 | state = HTTP_MSG_ERROR; |
| 1430 | break; |
| 1431 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1432 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1433 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1434 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1435 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1436 | goto http_msg_rquri; |
| 1437 | } |
| 1438 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1439 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1440 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1441 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1442 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1443 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1444 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1445 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1446 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1447 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1448 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1449 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1450 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1451 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1452 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1453 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1454 | /* non-ASCII chars are forbidden unless option |
| 1455 | * accept-invalid-http-request is enabled in the frontend. |
| 1456 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1457 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1458 | if (msg->err_pos < -1) |
| 1459 | goto invalid_char; |
| 1460 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1461 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1462 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1463 | } |
| 1464 | |
| 1465 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1466 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1467 | goto http_msg_req09_uri_e; |
| 1468 | } |
| 1469 | |
| 1470 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1471 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1472 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1473 | state = HTTP_MSG_ERROR; |
| 1474 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1475 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1476 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1477 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1478 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1479 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1480 | goto http_msg_rqver; |
| 1481 | } |
| 1482 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1483 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1484 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1485 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1486 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1487 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1488 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1489 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1490 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1491 | |
| 1492 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1493 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1494 | http_msg_rqline_eol: |
| 1495 | /* We have seen the end of line. Note that we do not |
| 1496 | * necessarily have the \n yet, but at least we know that we |
| 1497 | * have EITHER \r OR \n, otherwise the request would not be |
| 1498 | * complete. We can then record the request length and return |
| 1499 | * to the caller which will be able to register it. |
| 1500 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1501 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1502 | return ptr; |
| 1503 | } |
| 1504 | |
| 1505 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1506 | state = HTTP_MSG_ERROR; |
| 1507 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1508 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1509 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1510 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1511 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1512 | exit(1); |
| 1513 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1514 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1515 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1516 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1517 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1518 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1519 | if (ret_state) |
| 1520 | *ret_state = state; |
| 1521 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1522 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1523 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1524 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1525 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1526 | /* |
| 1527 | * Returns the data from Authorization header. Function may be called more |
| 1528 | * than once so data is stored in txn->auth_data. When no header is found |
| 1529 | * 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] | 1530 | * searching again for something we are unable to find anyway. However, if |
| 1531 | * 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] | 1532 | * have the credentials overwritten by another stream in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1533 | */ |
| 1534 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 1535 | /* This bufffer is initialized in the file 'src/haproxy.c'. This length is |
| 1536 | * set according to global.tune.bufsize. |
| 1537 | */ |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1538 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1539 | |
| 1540 | int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1541 | get_http_auth(struct stream *s) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1542 | { |
| 1543 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1544 | struct http_txn *txn = s->txn; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1545 | struct chunk auth_method; |
| 1546 | struct hdr_ctx ctx; |
| 1547 | char *h, *p; |
| 1548 | int len; |
| 1549 | |
| 1550 | #ifdef DEBUG_AUTH |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1551 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1552 | #endif |
| 1553 | |
| 1554 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1555 | return 0; |
| 1556 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1557 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1558 | |
| 1559 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1560 | |
| 1561 | if (txn->flags & TX_USE_PX_CONN) { |
| 1562 | h = "Proxy-Authorization"; |
| 1563 | len = strlen(h); |
| 1564 | } else { |
| 1565 | h = "Authorization"; |
| 1566 | len = strlen(h); |
| 1567 | } |
| 1568 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1569 | 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] | 1570 | return 0; |
| 1571 | |
| 1572 | h = ctx.line + ctx.val; |
| 1573 | |
| 1574 | p = memchr(h, ' ', ctx.vlen); |
| 1575 | if (!p || p == h) |
| 1576 | return 0; |
| 1577 | |
| 1578 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1579 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1580 | |
| 1581 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1582 | |
| 1583 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1584 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1585 | |
| 1586 | if (len < 0) |
| 1587 | return 0; |
| 1588 | |
| 1589 | |
| 1590 | get_http_auth_buff[len] = '\0'; |
| 1591 | |
| 1592 | p = strchr(get_http_auth_buff, ':'); |
| 1593 | |
| 1594 | if (!p) |
| 1595 | return 0; |
| 1596 | |
| 1597 | txn->auth.user = get_http_auth_buff; |
| 1598 | *p = '\0'; |
| 1599 | txn->auth.pass = p+1; |
| 1600 | |
| 1601 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1602 | return 1; |
| 1603 | } |
| 1604 | |
| 1605 | return 0; |
| 1606 | } |
| 1607 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1608 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1609 | /* |
| 1610 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1611 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1612 | * ensuring that the message does not wrap. The function can be preempted |
| 1613 | * everywhere when data are missing and recalled at the exact same location |
| 1614 | * with no information loss. The message may even be realigned between two |
| 1615 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1616 | * 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] | 1617 | * fields. Note that msg->sol will be initialized after completing the first |
| 1618 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1619 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1620 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1621 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1622 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1623 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1624 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1625 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1626 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1627 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1628 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1629 | ptr = buf->p + msg->next; |
| 1630 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1631 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1632 | if (unlikely(ptr >= end)) |
| 1633 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1634 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1635 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1636 | /* |
| 1637 | * First, states that are specific to the response only. |
| 1638 | * We check them first so that request and headers are |
| 1639 | * closer to each other (accessed more often). |
| 1640 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1641 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1642 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1643 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1644 | /* we have a start of message, but we have to check |
| 1645 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1646 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1647 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1648 | if (unlikely(ptr != buf->p)) { |
| 1649 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1650 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1651 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1652 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1653 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1654 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1655 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1656 | hdr_idx_init(idx); |
| 1657 | state = HTTP_MSG_RPVER; |
| 1658 | goto http_msg_rpver; |
| 1659 | } |
| 1660 | |
| 1661 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1662 | goto http_msg_invalid; |
| 1663 | |
| 1664 | if (unlikely(*ptr == '\n')) |
| 1665 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1666 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1667 | /* stop here */ |
| 1668 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1669 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1670 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1671 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1672 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1673 | /* stop here */ |
| 1674 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1675 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1676 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1677 | case HTTP_MSG_RPVER_SP: |
| 1678 | case HTTP_MSG_RPCODE: |
| 1679 | case HTTP_MSG_RPCODE_SP: |
| 1680 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1681 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1682 | state, ptr, end, |
| 1683 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1684 | if (unlikely(!ptr)) |
| 1685 | return; |
| 1686 | |
| 1687 | /* we have a full response and we know that we have either a CR |
| 1688 | * or an LF at <ptr>. |
| 1689 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1690 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1691 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1692 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1693 | if (likely(*ptr == '\r')) |
| 1694 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1695 | goto http_msg_rpline_end; |
| 1696 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1697 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1698 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1699 | /* msg->sol must point to the first of CR or LF. */ |
| 1700 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1701 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1702 | /* stop here */ |
| 1703 | |
| 1704 | /* |
| 1705 | * Second, states that are specific to the request only |
| 1706 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1707 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1708 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1709 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1710 | /* we have a start of message, but we have to check |
| 1711 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1712 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1713 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1714 | if (likely(ptr != buf->p)) { |
| 1715 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1716 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1717 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1718 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1719 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1720 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1721 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1722 | state = HTTP_MSG_RQMETH; |
| 1723 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1724 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1725 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1726 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1727 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1728 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1729 | if (unlikely(*ptr == '\n')) |
| 1730 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1731 | 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] | 1732 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1733 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1734 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1735 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1736 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1737 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1738 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1739 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1740 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1741 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1742 | case HTTP_MSG_RQMETH_SP: |
| 1743 | case HTTP_MSG_RQURI: |
| 1744 | case HTTP_MSG_RQURI_SP: |
| 1745 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1746 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1747 | state, ptr, end, |
| 1748 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1749 | if (unlikely(!ptr)) |
| 1750 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1751 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1752 | /* we have a full request and we know that we have either a CR |
| 1753 | * or an LF at <ptr>. |
| 1754 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1755 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1756 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1757 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1758 | if (likely(*ptr == '\r')) |
| 1759 | 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] | 1760 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1761 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1762 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1763 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1764 | /* check for HTTP/0.9 request : no version information available. |
| 1765 | * msg->sol must point to the first of CR or LF. |
| 1766 | */ |
| 1767 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1768 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1769 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1770 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1771 | 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] | 1772 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1773 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1774 | /* |
| 1775 | * Common states below |
| 1776 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1777 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1778 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1779 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1780 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1781 | goto http_msg_hdr_name; |
| 1782 | } |
| 1783 | |
| 1784 | if (likely(*ptr == '\r')) |
| 1785 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1786 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1787 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1788 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1789 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1790 | /* assumes msg->sol points to the first char */ |
| 1791 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1792 | 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] | 1793 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1794 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1795 | 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] | 1796 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1797 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1798 | goto http_msg_invalid; |
| 1799 | |
| 1800 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1801 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1802 | |
| 1803 | /* and we still accept this non-token character */ |
| 1804 | 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] | 1805 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1806 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1807 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1808 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1809 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1810 | 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] | 1811 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1812 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1813 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1814 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1815 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1816 | goto http_msg_hdr_val; |
| 1817 | } |
| 1818 | |
| 1819 | if (likely(*ptr == '\r')) |
| 1820 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1821 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1822 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1823 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1824 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1825 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1826 | 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] | 1827 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1828 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1829 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1830 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1831 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1832 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1833 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1834 | goto http_msg_hdr_l1_sp; |
| 1835 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1836 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1837 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1838 | goto http_msg_complete_header; |
| 1839 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1840 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1841 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1842 | /* assumes msg->sol points to the first char, and msg->sov |
| 1843 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1844 | */ |
| 1845 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1846 | 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] | 1847 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1848 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1849 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1850 | * real header end in case it ends with lots of LWS, but is this |
| 1851 | * really needed ? |
| 1852 | */ |
| 1853 | if (likely(*ptr == '\r')) |
| 1854 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1855 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1856 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1857 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1858 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1859 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1860 | 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] | 1861 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1862 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1863 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1864 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1865 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1866 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1867 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1868 | goto http_msg_hdr_val; |
| 1869 | } |
| 1870 | http_msg_complete_header: |
| 1871 | /* |
| 1872 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1873 | * Assumes msg->sol points to the first char, msg->sov points |
| 1874 | * to the first character of the value and msg->eol to the |
| 1875 | * first CR or LF so we know how the line ends. We insert last |
| 1876 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1877 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1878 | 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] | 1879 | idx, idx->tail) < 0)) |
| 1880 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1881 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1882 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1883 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1884 | goto http_msg_hdr_name; |
| 1885 | } |
| 1886 | |
| 1887 | if (likely(*ptr == '\r')) |
| 1888 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1889 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1890 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1891 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1892 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1893 | /* Assumes msg->sol points to the first of either CR or LF. |
| 1894 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 1895 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 1896 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1897 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1898 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1899 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1900 | msg->eoh = msg->sol; |
| 1901 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1902 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1903 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1904 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1905 | |
| 1906 | case HTTP_MSG_ERROR: |
| 1907 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1908 | break; |
| 1909 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1910 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1911 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1912 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1913 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1914 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1915 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1916 | } |
| 1917 | http_msg_ood: |
| 1918 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1919 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1920 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1921 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1922 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1923 | http_msg_invalid: |
| 1924 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1925 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1926 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1927 | return; |
| 1928 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1929 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1930 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1931 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1932 | * nothing is done and 1 is returned. |
| 1933 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1934 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1935 | { |
| 1936 | int delta; |
| 1937 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1938 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1939 | |
| 1940 | if (msg->sl.rq.v_l != 0) |
| 1941 | return 1; |
| 1942 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1943 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1944 | if (txn->meth != HTTP_METH_GET) |
| 1945 | return 0; |
| 1946 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1947 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1948 | delta = 0; |
| 1949 | |
| 1950 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1951 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1952 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1953 | } |
| 1954 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1955 | 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] | 1956 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1957 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1958 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1959 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1960 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1961 | NULL, NULL); |
| 1962 | if (unlikely(!cur_end)) |
| 1963 | return 0; |
| 1964 | |
| 1965 | /* we have a full HTTP/1.0 request now and we know that |
| 1966 | * we have either a CR or an LF at <ptr>. |
| 1967 | */ |
| 1968 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1969 | return 1; |
| 1970 | } |
| 1971 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1972 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1973 | * and "keep-alive" values. If we already know that some headers may safely |
| 1974 | * 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] | 1975 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1976 | * - 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] | 1977 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1978 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1979 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1980 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1981 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1982 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1983 | 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] | 1984 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1985 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1986 | const char *hdr_val = "Connection"; |
| 1987 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1988 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1989 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1990 | return; |
| 1991 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1992 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1993 | hdr_val = "Proxy-Connection"; |
| 1994 | hdr_len = 16; |
| 1995 | } |
| 1996 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1997 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1998 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1999 | 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] | 2000 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2001 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2002 | if (to_del & 2) |
| 2003 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2004 | else |
| 2005 | txn->flags |= TX_CON_KAL_SET; |
| 2006 | } |
| 2007 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2008 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2009 | if (to_del & 1) |
| 2010 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2011 | else |
| 2012 | txn->flags |= TX_CON_CLO_SET; |
| 2013 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2014 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 2015 | txn->flags |= TX_HDR_CONN_UPG; |
| 2016 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2017 | } |
| 2018 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2019 | txn->flags |= TX_HDR_CONN_PRS; |
| 2020 | return; |
| 2021 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2022 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2023 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 2024 | * added depending on the <wanted> flags, which are exclusively composed of |
| 2025 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 2026 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 2027 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2028 | 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] | 2029 | { |
| 2030 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2031 | const char *hdr_val = "Connection"; |
| 2032 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2033 | |
| 2034 | ctx.idx = 0; |
| 2035 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2036 | |
| 2037 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2038 | hdr_val = "Proxy-Connection"; |
| 2039 | hdr_len = 16; |
| 2040 | } |
| 2041 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2042 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2043 | 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] | 2044 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2045 | if (wanted & TX_CON_KAL_SET) |
| 2046 | txn->flags |= TX_CON_KAL_SET; |
| 2047 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2048 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2049 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2050 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2051 | if (wanted & TX_CON_CLO_SET) |
| 2052 | txn->flags |= TX_CON_CLO_SET; |
| 2053 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2054 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2055 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2056 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2057 | |
| 2058 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 2059 | return; |
| 2060 | |
| 2061 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 2062 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2063 | hdr_val = "Connection: close"; |
| 2064 | hdr_len = 17; |
| 2065 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2066 | hdr_val = "Proxy-Connection: close"; |
| 2067 | hdr_len = 23; |
| 2068 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2069 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 2073 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2074 | hdr_val = "Connection: keep-alive"; |
| 2075 | hdr_len = 22; |
| 2076 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2077 | hdr_val = "Proxy-Connection: keep-alive"; |
| 2078 | hdr_len = 28; |
| 2079 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2080 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2081 | } |
| 2082 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2083 | } |
| 2084 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2085 | /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to |
| 2086 | * the first byte of data after the chunk size, so that we know we can forward |
| 2087 | * exactly msg->next bytes. msg->sol contains the exact number of bytes forming |
| 2088 | * the chunk size. That way it is always possible to differentiate between the |
| 2089 | * start of the body and the start of the data. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2090 | * 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] | 2091 | * 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] | 2092 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2093 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2094 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2095 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2096 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2097 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2098 | const char *end = buf->data + buf->size; |
| 2099 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2100 | unsigned int chunk = 0; |
| 2101 | |
| 2102 | /* The chunk size is in the following form, though we are only |
| 2103 | * interested in the size and CRLF : |
| 2104 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2105 | */ |
| 2106 | while (1) { |
| 2107 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2108 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2109 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2110 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2111 | if (c < 0) /* not a hex digit anymore */ |
| 2112 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2113 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2114 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 2115 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2116 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2117 | chunk = (chunk << 4) + c; |
| 2118 | } |
| 2119 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2120 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2121 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2122 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2123 | |
| 2124 | while (http_is_spht[(unsigned char)*ptr]) { |
| 2125 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2126 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2127 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2128 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2129 | } |
| 2130 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2131 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2132 | * for the end of chunk size. |
| 2133 | */ |
| 2134 | while (1) { |
| 2135 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2136 | /* we now have a CR or an LF at ptr */ |
| 2137 | if (likely(*ptr == '\r')) { |
| 2138 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2139 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2140 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2141 | return 0; |
| 2142 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2143 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2144 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2145 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2146 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2147 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2148 | /* done */ |
| 2149 | break; |
| 2150 | } |
| 2151 | else if (*ptr == ';') { |
| 2152 | /* chunk extension, ends at next CRLF */ |
| 2153 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2154 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2155 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2156 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2157 | |
| 2158 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2159 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2160 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2161 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2162 | return 0; |
| 2163 | } |
| 2164 | /* we have a CRLF now, loop above */ |
| 2165 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2166 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2167 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2168 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2169 | } |
| 2170 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2171 | /* 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] | 2172 | * which may or may not be present. We save that into ->next, |
| 2173 | * and the number of bytes parsed into msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2174 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2175 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2176 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2177 | msg->sol += buf->size; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2178 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2179 | msg->chunk_len = chunk; |
| 2180 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2181 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2182 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2183 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2184 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2185 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2186 | } |
| 2187 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2188 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2189 | * 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] | 2190 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2191 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2192 | * If not enough data are available, the function does not change anything |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2193 | * except maybe msg->next if it could parse some lines, and returns zero. |
| 2194 | * If a parse error is encountered, the function returns < 0 and does not |
| 2195 | * change anything except maybe msg->next. Note that the message must |
| 2196 | * already be in HTTP_MSG_TRAILERS state before calling this function, |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2197 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2198 | * forwarding, and that msg->next exactly matches the length of trailers |
| 2199 | * already parsed and not forwarded. It is also important to note that this |
| 2200 | * 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] | 2201 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2202 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2203 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2204 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2205 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2206 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2207 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2208 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2209 | const char *ptr = b_ptr(buf, msg->next); |
| 2210 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2211 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2212 | |
| 2213 | /* scan current line and stop at LF or CRLF */ |
| 2214 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2215 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2216 | return 0; |
| 2217 | |
| 2218 | if (*ptr == '\n') { |
| 2219 | if (!p1) |
| 2220 | p1 = ptr; |
| 2221 | p2 = ptr; |
| 2222 | break; |
| 2223 | } |
| 2224 | |
| 2225 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2226 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2227 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2228 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2229 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2230 | p1 = ptr; |
| 2231 | } |
| 2232 | |
| 2233 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2234 | if (ptr >= buf->data + buf->size) |
| 2235 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | /* after LF; point to beginning of next line */ |
| 2239 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2240 | if (p2 >= buf->data + buf->size) |
| 2241 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2242 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2243 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2244 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2245 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2246 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2247 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2248 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2249 | * Everything was scheduled for forwarding, there's nothing |
| 2250 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2251 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2252 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2253 | msg->msg_state = HTTP_MSG_DONE; |
| 2254 | return 1; |
| 2255 | } |
| 2256 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2257 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2258 | } |
| 2259 | } |
| 2260 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2261 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF |
| 2262 | * or a possible LF alone at the end of a chunk. It automatically adjusts |
| 2263 | * 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] | 2264 | * 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] | 2265 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2266 | * not enough data are available, the function does not change anything and |
| 2267 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2268 | * does not change anything. Note: this function is designed to parse wrapped |
| 2269 | * CRLF at the end of the buffer. |
| 2270 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2271 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2272 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2273 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2274 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2275 | int bytes; |
| 2276 | |
| 2277 | /* NB: we'll check data availabilty at the end. It's not a |
| 2278 | * problem because whatever we match first will be checked |
| 2279 | * against the correct length. |
| 2280 | */ |
| 2281 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2282 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2283 | if (*ptr == '\r') { |
| 2284 | bytes++; |
| 2285 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2286 | if (ptr >= buf->data + buf->size) |
| 2287 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2288 | } |
| 2289 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2290 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2291 | return 0; |
| 2292 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2293 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2294 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2295 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2296 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2297 | |
| 2298 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2299 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2300 | ptr = buf->data; |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2301 | /* Advance ->next to allow the CRLF to be forwarded */ |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2302 | msg->next += bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2303 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2304 | return 1; |
| 2305 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2306 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2307 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2308 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2309 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2310 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2311 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2312 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2313 | { |
| 2314 | int q = 1000; |
| 2315 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2316 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2317 | goto out; |
| 2318 | q = (*qvalue++ - '0') * 1000; |
| 2319 | |
| 2320 | if (*qvalue++ != '.') |
| 2321 | goto out; |
| 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') * 100; |
| 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') * 10; |
| 2330 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2331 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2332 | goto out; |
| 2333 | q += (*qvalue++ - '0') * 1; |
| 2334 | out: |
| 2335 | if (q > 1000) |
| 2336 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2337 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2338 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2339 | return q; |
| 2340 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2341 | |
| 2342 | /* |
| 2343 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2344 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2345 | int select_compression_request_header(struct stream *s, struct buffer *req) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2346 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2347 | struct http_txn *txn = s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2348 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2349 | struct hdr_ctx ctx; |
| 2350 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2351 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2352 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2353 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 2354 | * 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] | 2355 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 2356 | */ |
| 2357 | ctx.idx = 0; |
| 2358 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2359 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2360 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2361 | (ctx.vlen < 31 || |
| 2362 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2363 | ctx.line[ctx.val + 30] < '6' || |
| 2364 | (ctx.line[ctx.val + 30] == '6' && |
| 2365 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2366 | s->comp_algo = NULL; |
| 2367 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2368 | } |
| 2369 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2370 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2371 | 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] | 2372 | int best_q = 0; |
| 2373 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2374 | ctx.idx = 0; |
| 2375 | 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] | 2376 | const char *qval; |
| 2377 | int q; |
| 2378 | int toklen; |
| 2379 | |
| 2380 | /* try to isolate the token from the optional q-value */ |
| 2381 | toklen = 0; |
| 2382 | while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)]) |
| 2383 | toklen++; |
| 2384 | |
| 2385 | qval = ctx.line + ctx.val + toklen; |
| 2386 | while (1) { |
| 2387 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2388 | qval++; |
| 2389 | |
| 2390 | if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') { |
| 2391 | qval = NULL; |
| 2392 | break; |
| 2393 | } |
| 2394 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2395 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2396 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2397 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2398 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2399 | if (qval >= ctx.line + ctx.val + ctx.vlen) { |
| 2400 | qval = NULL; |
| 2401 | break; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2402 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2403 | if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0) |
| 2404 | break; |
| 2405 | |
| 2406 | while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';') |
| 2407 | qval++; |
| 2408 | } |
| 2409 | |
| 2410 | /* 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] | 2411 | q = qval ? parse_qvalue(qval + 2, NULL) : 1000; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2412 | |
| 2413 | if (q <= best_q) |
| 2414 | continue; |
| 2415 | |
| 2416 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 2417 | if (*(ctx.line + ctx.val) == '*' || |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2418 | 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] | 2419 | s->comp_algo = comp_algo; |
| 2420 | best_q = q; |
| 2421 | break; |
| 2422 | } |
| 2423 | } |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2428 | if (s->comp_algo) { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2429 | 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] | 2430 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2431 | ctx.idx = 0; |
| 2432 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2433 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2434 | } |
| 2435 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2436 | return 1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | /* identity is implicit does not require headers */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2440 | 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] | 2441 | 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] | 2442 | 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] | 2443 | s->comp_algo = comp_algo; |
| 2444 | return 1; |
| 2445 | } |
| 2446 | } |
| 2447 | } |
| 2448 | |
| 2449 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2450 | return 0; |
| 2451 | } |
| 2452 | |
| 2453 | /* |
| 2454 | * Selects a comression algorithm depending of the server response. |
| 2455 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2456 | int select_compression_response_header(struct stream *s, struct buffer *res) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2457 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2458 | struct http_txn *txn = s->txn; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2459 | struct http_msg *msg = &txn->rsp; |
| 2460 | struct hdr_ctx ctx; |
| 2461 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2462 | |
| 2463 | /* no common compression algorithm was found in request header */ |
| 2464 | if (s->comp_algo == NULL) |
| 2465 | goto fail; |
| 2466 | |
| 2467 | /* HTTP < 1.1 should not be compressed */ |
Willy Tarreau | 7257550 | 2013-12-24 14:41:35 +0100 | [diff] [blame] | 2468 | 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] | 2469 | goto fail; |
| 2470 | |
Jesse Hathaway | 2468d4e | 2015-03-06 20:16:15 +0000 | [diff] [blame] | 2471 | /* compress 200,201,202,203 responses only */ |
| 2472 | if ((txn->status != 200) && |
| 2473 | (txn->status != 201) && |
| 2474 | (txn->status != 202) && |
| 2475 | (txn->status != 203)) |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2476 | goto fail; |
| 2477 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2478 | /* Content-Length is null */ |
| 2479 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2480 | goto fail; |
| 2481 | |
| 2482 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2483 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2484 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2485 | goto fail; |
| 2486 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2487 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2488 | ctx.idx = 0; |
| 2489 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2490 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2491 | goto fail; |
| 2492 | } |
| 2493 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2494 | comp_type = NULL; |
| 2495 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2496 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2497 | * not listed in the "compression type" directive if any. If no content-type was |
| 2498 | * found but configuration requires one, we don't compress either. Backend has |
| 2499 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2500 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2501 | ctx.idx = 0; |
| 2502 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2503 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2504 | goto fail; |
| 2505 | |
| 2506 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2507 | (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2508 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2509 | if (ctx.vlen >= comp_type->name_len && |
| 2510 | 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] | 2511 | /* this Content-Type should be compressed */ |
| 2512 | break; |
| 2513 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2514 | /* this Content-Type should not be compressed */ |
| 2515 | if (comp_type == NULL) |
| 2516 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2517 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2518 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2519 | else { /* no content-type header */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2520 | 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] | 2521 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2522 | } |
| 2523 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2524 | /* limit compression rate */ |
| 2525 | if (global.comp_rate_lim > 0) |
| 2526 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2527 | goto fail; |
| 2528 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2529 | /* limit cpu usage */ |
| 2530 | if (idle_pct < compress_min_idle) |
| 2531 | goto fail; |
| 2532 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2533 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2534 | 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] | 2535 | goto fail; |
| 2536 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2537 | s->flags |= SF_COMP_READY; |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2538 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2539 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2540 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2541 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2542 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2543 | |
| 2544 | /* add Transfer-Encoding header */ |
| 2545 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2546 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2547 | |
| 2548 | /* |
| 2549 | * Add Content-Encoding header when it's not identity encoding. |
| 2550 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2551 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2552 | * header. |
| 2553 | */ |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2554 | 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] | 2555 | trash.len = 18; |
| 2556 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2557 | memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len); |
| 2558 | trash.len += s->comp_algo->ua_name_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2559 | trash.str[trash.len] = '\0'; |
| 2560 | 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] | 2561 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2562 | return 1; |
| 2563 | |
| 2564 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2565 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2566 | return 0; |
| 2567 | } |
| 2568 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2569 | 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] | 2570 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2571 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2572 | int tmp = TX_CON_WANT_KAL; |
| 2573 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2574 | if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 2575 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2576 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2577 | tmp = TX_CON_WANT_TUN; |
| 2578 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2579 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2580 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2581 | tmp = TX_CON_WANT_TUN; |
| 2582 | } |
| 2583 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2584 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2585 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 2586 | /* option httpclose + server_close => forceclose */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2587 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2588 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2589 | tmp = TX_CON_WANT_CLO; |
| 2590 | else |
| 2591 | tmp = TX_CON_WANT_SCL; |
| 2592 | } |
| 2593 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2594 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2595 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 2596 | tmp = TX_CON_WANT_CLO; |
| 2597 | |
| 2598 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2599 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2600 | |
| 2601 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 2602 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2603 | /* parse the Connection header and possibly clean it */ |
| 2604 | int to_del = 0; |
| 2605 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 2606 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2607 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2608 | to_del |= 2; /* remove "keep-alive" */ |
| 2609 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2610 | to_del |= 1; /* remove "close" */ |
| 2611 | http_parse_connection_header(txn, msg, to_del); |
| 2612 | } |
| 2613 | |
| 2614 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2615 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2616 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2617 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2618 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 2619 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2620 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2621 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2622 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2623 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2624 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2625 | * processing can continue on next analysers, or zero if it either needs more |
| 2626 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2627 | * 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] | 2628 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2629 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2630 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2631 | 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] | 2632 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2633 | /* |
| 2634 | * We will parse the partial (or complete) lines. |
| 2635 | * We will check the request syntax, and also join multi-line |
| 2636 | * headers. An index of all the lines will be elaborated while |
| 2637 | * parsing. |
| 2638 | * |
| 2639 | * For the parsing, we use a 28 states FSM. |
| 2640 | * |
| 2641 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2642 | * req->buf->p = beginning of request |
| 2643 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2644 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2645 | * msg->eol = end of current header or line (LF or CRLF) |
| 2646 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2647 | * |
| 2648 | * 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] | 2649 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2650 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2651 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2652 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2653 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2654 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2655 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2656 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2657 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2658 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2659 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2660 | 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] | 2661 | now_ms, __FUNCTION__, |
| 2662 | s, |
| 2663 | req, |
| 2664 | req->rex, req->wex, |
| 2665 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2666 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2667 | req->analysers); |
| 2668 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2669 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2670 | s->srv_error = http_return_srv_error; |
| 2671 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2672 | /* There's a protected area at the end of the buffer for rewriting |
| 2673 | * purposes. We don't want to start to parse the request if the |
| 2674 | * protected area is affected, because we may have to move processed |
| 2675 | * data later, which is much more complicated. |
| 2676 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2677 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2678 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 2679 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2680 | 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] | 2681 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2682 | /* 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] | 2683 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2684 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2685 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2686 | return 0; |
| 2687 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2688 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2689 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2690 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2691 | } |
| 2692 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2693 | /* Note that we have the same problem with the response ; we |
| 2694 | * may want to send a redirect, error or anything which requires |
| 2695 | * some spare space. So we'll ensure that we have at least |
| 2696 | * maxrewrite bytes available in the response buffer before |
| 2697 | * processing that one. This will only affect pipelined |
| 2698 | * keep-alive requests. |
| 2699 | */ |
| 2700 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2701 | unlikely(!channel_is_rewritable(&s->res) || |
| 2702 | bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) || |
| 2703 | bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) { |
| 2704 | if (s->res.buf->o) { |
| 2705 | 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] | 2706 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2707 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2708 | channel_dont_connect(req); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2709 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 2710 | s->res.flags |= CF_WAKE_WRITE; |
| 2711 | s->res.analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2712 | return 0; |
| 2713 | } |
| 2714 | } |
| 2715 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2716 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2717 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2718 | } |
| 2719 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2720 | /* 1: we might have to print this header in debug mode */ |
| 2721 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2722 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2723 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2724 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2725 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2726 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2727 | /* this is a bit complex : in case of error on the request line, |
| 2728 | * we know that rq.l is still zero, so we display only the part |
| 2729 | * up to the end of the line (truncated by debug_hdr). |
| 2730 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2731 | 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] | 2732 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2733 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2734 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2735 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2736 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2737 | while (cur_idx) { |
| 2738 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2739 | debug_hdr("clihdr", s, sol, eol); |
| 2740 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2741 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2742 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2743 | } |
| 2744 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2745 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2746 | /* |
| 2747 | * Now we quickly check if we have found a full valid request. |
| 2748 | * If not so, we check the FD and buffer states before leaving. |
| 2749 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2750 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2751 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2752 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 2753 | * 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] | 2754 | * 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] | 2755 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2756 | * Last, we may increase some tracked counters' http request errors on |
| 2757 | * the cases that are deliberately the client's fault. For instance, |
| 2758 | * a timeout or connection reset is not counted as an error. However |
| 2759 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2760 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2761 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2762 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2763 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2764 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2765 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2766 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2767 | stream_inc_http_req_ctr(s); |
| 2768 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2769 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2770 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2771 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2772 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2773 | /* 1: Since we are in header mode, if there's no space |
| 2774 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2775 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2776 | * must terminate it now. |
| 2777 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2778 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2779 | /* FIXME: check if URI is set and return Status |
| 2780 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2781 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2782 | stream_inc_http_req_ctr(s); |
| 2783 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2784 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2785 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2786 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2787 | goto return_bad_req; |
| 2788 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2789 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2790 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2791 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2792 | if (!(s->flags & SF_ERR_MASK)) |
| 2793 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2794 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2795 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2796 | goto failed_keep_alive; |
| 2797 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2798 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2799 | goto failed_keep_alive; |
| 2800 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2801 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2802 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2803 | 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] | 2804 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2805 | } |
| 2806 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2807 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2808 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2809 | msg->msg_state = HTTP_MSG_ERROR; |
| 2810 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2811 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2812 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2813 | proxy_inc_fe_req_ctr(sess->fe); |
| 2814 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2815 | if (sess->listener->counters) |
| 2816 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2817 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2818 | if (!(s->flags & SF_FINST_MASK)) |
| 2819 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2820 | return 0; |
| 2821 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2822 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2823 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2824 | 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] | 2825 | if (!(s->flags & SF_ERR_MASK)) |
| 2826 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2827 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2828 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2829 | goto failed_keep_alive; |
| 2830 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2831 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2832 | goto failed_keep_alive; |
| 2833 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2834 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2835 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2836 | 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] | 2837 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2838 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2839 | txn->status = 408; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2840 | 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] | 2841 | msg->msg_state = HTTP_MSG_ERROR; |
| 2842 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2843 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2844 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2845 | proxy_inc_fe_req_ctr(sess->fe); |
| 2846 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2847 | if (sess->listener->counters) |
| 2848 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2849 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2850 | if (!(s->flags & SF_FINST_MASK)) |
| 2851 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2852 | return 0; |
| 2853 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2854 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2855 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2856 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2857 | if (!(s->flags & SF_ERR_MASK)) |
| 2858 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2859 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2860 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2861 | goto failed_keep_alive; |
| 2862 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2863 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2864 | goto failed_keep_alive; |
| 2865 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2866 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2867 | 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] | 2868 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2869 | 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] | 2870 | msg->msg_state = HTTP_MSG_ERROR; |
| 2871 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2872 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2873 | stream_inc_http_err_ctr(s); |
| 2874 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2875 | proxy_inc_fe_req_ctr(sess->fe); |
| 2876 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2877 | if (sess->listener->counters) |
| 2878 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2879 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2880 | if (!(s->flags & SF_FINST_MASK)) |
| 2881 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2882 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2883 | } |
| 2884 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2885 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2886 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2887 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2888 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2889 | if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i && |
| 2890 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2891 | /* We need more data, we have to re-enable quick-ack in case we |
| 2892 | * previously disabled it, otherwise we might cause the client |
| 2893 | * to delay next data. |
| 2894 | */ |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2895 | 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] | 2896 | } |
| 2897 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2898 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2899 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2900 | /* If the client starts to talk, let's fall back to |
| 2901 | * request timeout processing. |
| 2902 | */ |
| 2903 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2904 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2905 | } |
| 2906 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2907 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2908 | if (!tick_isset(req->analyse_exp)) { |
| 2909 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2910 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2911 | tick_isset(s->be->timeout.httpka)) |
| 2912 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2913 | else |
| 2914 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2915 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2916 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2917 | /* we're not ready yet */ |
| 2918 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2919 | |
| 2920 | failed_keep_alive: |
| 2921 | /* Here we process low-level errors for keep-alive requests. In |
| 2922 | * short, if the request is not the first one and it experiences |
| 2923 | * a timeout, read error or shutdown, we just silently close so |
| 2924 | * that the client can try again. |
| 2925 | */ |
| 2926 | txn->status = 0; |
| 2927 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2928 | req->analysers = 0; |
| 2929 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2930 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2931 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2932 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2933 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2934 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2935 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2936 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2937 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2938 | * 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] | 2939 | * 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] | 2940 | * 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] | 2941 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2942 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2943 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2944 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2945 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2946 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2947 | 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] | 2948 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2949 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2950 | /* kill the pending keep-alive timeout */ |
| 2951 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2952 | req->analyse_exp = TICK_ETERNITY; |
| 2953 | } |
| 2954 | |
| 2955 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2956 | /* Maybe we found in invalid header name while we were configured not |
| 2957 | * to block on that, so we have to capture it now. |
| 2958 | */ |
| 2959 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2960 | 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] | 2961 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2962 | /* |
| 2963 | * 1: identify the method |
| 2964 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2965 | 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] | 2966 | |
| 2967 | /* we can make use of server redirect on GET and HEAD */ |
| 2968 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2969 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2970 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2971 | /* |
| 2972 | * 2: check if the URI matches the monitor_uri. |
| 2973 | * We have to do this for every request which gets in, because |
| 2974 | * the monitor-uri is defined by the frontend. |
| 2975 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2976 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 2977 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2978 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2979 | sess->fe->monitor_uri, |
| 2980 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2981 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2982 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2983 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2984 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2985 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2986 | s->flags |= SF_MONITOR; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2987 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2988 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2989 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2990 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2991 | 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] | 2992 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2993 | ret = acl_pass(ret); |
| 2994 | if (cond->pol == ACL_COND_UNLESS) |
| 2995 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2996 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2997 | if (ret) { |
| 2998 | /* we fail this request, let's return 503 service unavail */ |
| 2999 | txn->status = 503; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3000 | 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] | 3001 | if (!(s->flags & SF_ERR_MASK)) |
| 3002 | 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] | 3003 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 3004 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3005 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 3006 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3007 | /* nothing to fail, let's reply normaly */ |
| 3008 | txn->status = 200; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3009 | 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] | 3010 | if (!(s->flags & SF_ERR_MASK)) |
| 3011 | 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] | 3012 | goto return_prx_cond; |
| 3013 | } |
| 3014 | |
| 3015 | /* |
| 3016 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 3017 | * Note: we cannot log anymore if the request has been |
| 3018 | * classified as invalid. |
| 3019 | */ |
| 3020 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 3021 | /* we have a complete HTTP request that we must log */ |
| 3022 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 3023 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3024 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3025 | if (urilen >= REQURI_LEN) |
| 3026 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3027 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3028 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3029 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 3030 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3031 | s->do_log(s); |
| 3032 | } else { |
| 3033 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3034 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3035 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3036 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 3037 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 3038 | * exactly one digit "." one digit. This check may be disabled using |
| 3039 | * option accept-invalid-http-request. |
| 3040 | */ |
| 3041 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 3042 | if (msg->sl.rq.v_l != 8) { |
| 3043 | msg->err_pos = msg->sl.rq.v; |
| 3044 | goto return_bad_req; |
| 3045 | } |
| 3046 | |
| 3047 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 3048 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 3049 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 3050 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 3051 | msg->err_pos = msg->sl.rq.v + 4; |
| 3052 | goto return_bad_req; |
| 3053 | } |
| 3054 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 3055 | else { |
| 3056 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 3057 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 3058 | goto return_bad_req; |
| 3059 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 3060 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3061 | /* ... and check if the request is HTTP/1.1 or above */ |
| 3062 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3063 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 3064 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 3065 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3066 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3067 | |
| 3068 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3069 | 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] | 3070 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3071 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 3072 | * we have what looks like a proxied connection instead of a connection, |
| 3073 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 3074 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 3075 | * happen to do that despite being non-standard :-( |
| 3076 | * We consider that a request not beginning with either '/' or '*' is |
| 3077 | * a proxied connection, which covers both "scheme://location" and |
| 3078 | * CONNECT ip:port. |
| 3079 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3080 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3081 | 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] | 3082 | txn->flags |= TX_USE_PX_CONN; |
| 3083 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3084 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3085 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3086 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3087 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 3088 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3089 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 3090 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3091 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3092 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 3093 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3094 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3095 | * The length of a message body is determined by one of the following |
| 3096 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3097 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3098 | * 1. Any response to a HEAD request and any response with a 1xx |
| 3099 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 3100 | * code is always terminated by the first empty line after the |
| 3101 | * header fields, regardless of the header fields present in the |
| 3102 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3103 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3104 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 3105 | * the connection will become a tunnel immediately after the empty |
| 3106 | * line that concludes the header fields. A client MUST ignore any |
| 3107 | * Content-Length or Transfer-Encoding header fields received in |
| 3108 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3109 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3110 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 3111 | * transfer coding (Section 4.1) is the final encoding, the message |
| 3112 | * body length is determined by reading and decoding the chunked |
| 3113 | * data until the transfer coding indicates the data is complete. |
| 3114 | * |
| 3115 | * If a Transfer-Encoding header field is present in a response and |
| 3116 | * the chunked transfer coding is not the final encoding, the |
| 3117 | * message body length is determined by reading the connection until |
| 3118 | * it is closed by the server. If a Transfer-Encoding header field |
| 3119 | * is present in a request and the chunked transfer coding is not |
| 3120 | * the final encoding, the message body length cannot be determined |
| 3121 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 3122 | * status code and then close the connection. |
| 3123 | * |
| 3124 | * If a message is received with both a Transfer-Encoding and a |
| 3125 | * Content-Length header field, the Transfer-Encoding overrides the |
| 3126 | * Content-Length. Such a message might indicate an attempt to |
| 3127 | * perform request smuggling (Section 9.5) or response splitting |
| 3128 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 3129 | * remove the received Content-Length field prior to forwarding such |
| 3130 | * a message downstream. |
| 3131 | * |
| 3132 | * 4. If a message is received without Transfer-Encoding and with |
| 3133 | * either multiple Content-Length header fields having differing |
| 3134 | * field-values or a single Content-Length header field having an |
| 3135 | * invalid value, then the message framing is invalid and the |
| 3136 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 3137 | * request message, the server MUST respond with a 400 (Bad Request) |
| 3138 | * status code and then close the connection. If this is a response |
| 3139 | * message received by a proxy, the proxy MUST close the connection |
| 3140 | * to the server, discard the received response, and send a 502 (Bad |
| 3141 | * Gateway) response to the client. If this is a response message |
| 3142 | * received by a user agent, the user agent MUST close the |
| 3143 | * connection to the server and discard the received response. |
| 3144 | * |
| 3145 | * 5. If a valid Content-Length header field is present without |
| 3146 | * Transfer-Encoding, its decimal value defines the expected message |
| 3147 | * body length in octets. If the sender closes the connection or |
| 3148 | * the recipient times out before the indicated number of octets are |
| 3149 | * received, the recipient MUST consider the message to be |
| 3150 | * incomplete and close the connection. |
| 3151 | * |
| 3152 | * 6. If this is a request message and none of the above are true, then |
| 3153 | * the message body length is zero (no message body is present). |
| 3154 | * |
| 3155 | * 7. Otherwise, this is a response message without a declared message |
| 3156 | * body length, so the message body length is determined by the |
| 3157 | * number of octets received prior to the server closing the |
| 3158 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3159 | */ |
| 3160 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3161 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3162 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 3163 | 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] | 3164 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3165 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 3166 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3167 | /* chunked not last, return badreq */ |
| 3168 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3169 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3170 | } |
| 3171 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3172 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3173 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3174 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 3175 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 3176 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 3177 | } |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3178 | 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] | 3179 | signed long long cl; |
| 3180 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3181 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3182 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3183 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3184 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3185 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3186 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3187 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3188 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3189 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3190 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3191 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3192 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3193 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3194 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3195 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3196 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3197 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3198 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3199 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3200 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3201 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3202 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3203 | } |
| 3204 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3205 | /* even bodyless requests have a known length */ |
| 3206 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3207 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3208 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 3209 | * only change if both the request and the config reference something else. |
| 3210 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3211 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3212 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3213 | * if FE and BE have the same settings (common). The method consists in |
| 3214 | * checking if options changed between the two calls (implying that either |
| 3215 | * one is non-null, or one of them is non-null and we are there for the first |
| 3216 | * time. |
| 3217 | */ |
| 3218 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3219 | ((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] | 3220 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3221 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 3222 | /* we may have to wait for the request's body */ |
| 3223 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 3224 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 3225 | req->analysers |= AN_REQ_HTTP_BODY; |
| 3226 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3227 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3228 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3229 | req->analyse_exp = TICK_ETERNITY; |
| 3230 | return 1; |
| 3231 | |
| 3232 | return_bad_req: |
| 3233 | /* We centralize bad requests processing here */ |
| 3234 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3235 | /* we detected a parsing error. We want to archive this request |
| 3236 | * in the dedicated proxy area for later troubleshooting. |
| 3237 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3238 | 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] | 3239 | } |
| 3240 | |
| 3241 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3242 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3243 | 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] | 3244 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3245 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3246 | if (sess->listener->counters) |
| 3247 | sess->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3248 | |
| 3249 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3250 | if (!(s->flags & SF_ERR_MASK)) |
| 3251 | s->flags |= SF_ERR_PRXCOND; |
| 3252 | if (!(s->flags & SF_FINST_MASK)) |
| 3253 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3254 | |
| 3255 | req->analysers = 0; |
| 3256 | req->analyse_exp = TICK_ETERNITY; |
| 3257 | return 0; |
| 3258 | } |
| 3259 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3260 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3261 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3262 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3263 | * and program a response message if something was unexpected. It cannot fail |
| 3264 | * 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] | 3265 | * 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] | 3266 | * 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] | 3267 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3268 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3269 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3270 | { |
| 3271 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3272 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3273 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3274 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3275 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3276 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3277 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3278 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3279 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3280 | appctx = si_appctx(si); |
| 3281 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3282 | appctx->st1 = appctx->st2 = 0; |
| 3283 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3284 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3285 | 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] | 3286 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3287 | |
| 3288 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3289 | lookup = uri + uri_auth->uri_len; |
| 3290 | |
| 3291 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3292 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3293 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3294 | break; |
| 3295 | } |
| 3296 | } |
| 3297 | |
| 3298 | if (uri_auth->refresh) { |
| 3299 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3300 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3301 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3302 | break; |
| 3303 | } |
| 3304 | } |
| 3305 | } |
| 3306 | |
| 3307 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3308 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3309 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3310 | break; |
| 3311 | } |
| 3312 | } |
| 3313 | |
| 3314 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3315 | if (memcmp(h, ";st=", 4) == 0) { |
| 3316 | int i; |
| 3317 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3318 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3319 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3320 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3321 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3322 | break; |
| 3323 | } |
| 3324 | } |
| 3325 | break; |
| 3326 | } |
| 3327 | } |
| 3328 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3329 | appctx->ctx.stats.scope_str = 0; |
| 3330 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3331 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3332 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3333 | int itx = 0; |
| 3334 | const char *h2; |
| 3335 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3336 | const char *err; |
| 3337 | |
| 3338 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3339 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3340 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3341 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3342 | itx++; |
| 3343 | h++; |
| 3344 | } |
| 3345 | |
| 3346 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3347 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3348 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3349 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3350 | /* 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] | 3351 | memcpy(scope_txt, h2, itx); |
| 3352 | scope_txt[itx] = '\0'; |
| 3353 | err = invalid_char(scope_txt); |
| 3354 | if (err) { |
| 3355 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3356 | appctx->ctx.stats.scope_str = 0; |
| 3357 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3358 | } |
| 3359 | break; |
| 3360 | } |
| 3361 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3362 | |
| 3363 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3364 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3365 | int ret = 1; |
| 3366 | |
| 3367 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3368 | 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] | 3369 | ret = acl_pass(ret); |
| 3370 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3371 | ret = !ret; |
| 3372 | } |
| 3373 | |
| 3374 | if (ret) { |
| 3375 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3376 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3377 | break; |
| 3378 | } |
| 3379 | } |
| 3380 | |
| 3381 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3382 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3383 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3384 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3385 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 3386 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3387 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3388 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3389 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3390 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3391 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3392 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3393 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3394 | else { |
| 3395 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3396 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3397 | } |
| 3398 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3399 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3400 | return 1; |
| 3401 | } |
| 3402 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3403 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3404 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3405 | */ |
Thierry FOURNIER | 7fe75e0 | 2015-03-16 12:03:44 +0100 | [diff] [blame] | 3406 | void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3407 | { |
| 3408 | #ifdef IP_TOS |
| 3409 | if (from.ss_family == AF_INET) |
| 3410 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3411 | #endif |
| 3412 | #ifdef IPV6_TCLASS |
| 3413 | if (from.ss_family == AF_INET6) { |
| 3414 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3415 | /* v4-mapped addresses need IP_TOS */ |
| 3416 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3417 | else |
| 3418 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3419 | } |
| 3420 | #endif |
| 3421 | } |
| 3422 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3423 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3424 | const char* name, unsigned int name_len, |
| 3425 | const char *str, struct my_regex *re, |
| 3426 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3427 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3428 | struct hdr_ctx ctx; |
| 3429 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3430 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3431 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 3432 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3433 | struct chunk *output = get_trash_chunk(); |
| 3434 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3435 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3436 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3437 | /* Choose the header browsing function. */ |
| 3438 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3439 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3440 | http_find_hdr_func = http_find_header2; |
| 3441 | break; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3442 | case ACT_HTTP_REPLACE_HDR: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3443 | http_find_hdr_func = http_find_full_header2; |
| 3444 | break; |
| 3445 | default: /* impossible */ |
| 3446 | return -1; |
| 3447 | } |
| 3448 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3449 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 3450 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3451 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3452 | char *val = ctx.line + ctx.val; |
| 3453 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3454 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3455 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 3456 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3457 | |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3458 | output->len = exp_replace(output->str, output->size, val, str, pmatch); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3459 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3460 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3461 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3462 | 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] | 3463 | |
| 3464 | hdr->len += delta; |
| 3465 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3466 | |
| 3467 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3468 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3469 | } |
| 3470 | |
| 3471 | return 0; |
| 3472 | } |
| 3473 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3474 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3475 | const char* name, unsigned int name_len, |
| 3476 | struct list *fmt, struct my_regex *re, |
| 3477 | int action) |
| 3478 | { |
| 3479 | struct chunk *replace = get_trash_chunk(); |
| 3480 | |
| 3481 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 3482 | if (replace->len >= replace->size - 1) |
| 3483 | return -1; |
| 3484 | |
| 3485 | return http_transform_header_str(s, msg, name, name_len, replace->str, re, action); |
| 3486 | } |
| 3487 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3488 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3489 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3490 | * further processing of the request (auth, deny, ...), and defaults to |
| 3491 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3492 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 3493 | * on txn->flags if it encounters a tarpit rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3494 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3495 | enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3496 | 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] | 3497 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3498 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3499 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3500 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3501 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3502 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3503 | const char *auth_realm; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3504 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3505 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3506 | * resume condition. If a resume is needed it is always in the action |
| 3507 | * and never in the ACL or converters. In this case, we initialise the |
| 3508 | * current rule, and go to the action execution point. |
| 3509 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3510 | if (s->current_rule) { |
| 3511 | rule = s->current_rule; |
| 3512 | s->current_rule = NULL; |
| 3513 | if (s->current_rule_list == rules) |
| 3514 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3515 | } |
| 3516 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3517 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3518 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3519 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3520 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3521 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3522 | int ret; |
| 3523 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3524 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3525 | ret = acl_pass(ret); |
| 3526 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3527 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3528 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3529 | |
| 3530 | if (!ret) /* condition not matched */ |
| 3531 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3532 | } |
| 3533 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3534 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3535 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3536 | case ACT_ACTION_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3537 | return HTTP_RULE_RES_STOP; |
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_ACTION_DENY: |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 3540 | txn->rule_deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3541 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3542 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3543 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3544 | txn->flags |= TX_CLTARPIT; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 3545 | txn->rule_deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3546 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3547 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3548 | case ACT_HTTP_REQ_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3549 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3550 | auth_realm = rule->arg.auth.realm; |
| 3551 | if (!auth_realm) { |
| 3552 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3553 | auth_realm = STATS_DEFAULT_REALM; |
| 3554 | else |
| 3555 | auth_realm = px->id; |
| 3556 | } |
| 3557 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3558 | * count one error, because normal browsing won't significantly |
| 3559 | * increase the counter but brute force attempts will. |
| 3560 | */ |
| 3561 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3562 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
| 3563 | stream_int_retnclose(&s->si[0], &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3564 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3565 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3566 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3567 | case ACT_HTTP_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3568 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3569 | return HTTP_RULE_RES_BADREQ; |
| 3570 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3571 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3572 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3573 | s->task->nice = rule->arg.nice; |
| 3574 | break; |
| 3575 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3576 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3577 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3578 | 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] | 3579 | break; |
| 3580 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3581 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3582 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3583 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3584 | 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] | 3585 | #endif |
| 3586 | break; |
| 3587 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3588 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3589 | s->logs.level = rule->arg.loglevel; |
| 3590 | break; |
| 3591 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3592 | case ACT_HTTP_REPLACE_HDR: |
| 3593 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3594 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 3595 | rule->arg.hdr_add.name_len, |
| 3596 | &rule->arg.hdr_add.fmt, |
| 3597 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3598 | return HTTP_RULE_RES_BADREQ; |
| 3599 | break; |
| 3600 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3601 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3602 | ctx.idx = 0; |
| 3603 | /* remove all occurrences of the header */ |
| 3604 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3605 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3606 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3607 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3608 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3609 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3610 | case ACT_HTTP_SET_HDR: |
| 3611 | case ACT_HTTP_ADD_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3612 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3613 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3614 | trash.len = rule->arg.hdr_add.name_len; |
| 3615 | trash.str[trash.len++] = ':'; |
| 3616 | trash.str[trash.len++] = ' '; |
| 3617 | 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] | 3618 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3619 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3620 | /* remove all occurrences of the header */ |
| 3621 | ctx.idx = 0; |
| 3622 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3623 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3624 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3625 | } |
| 3626 | } |
| 3627 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3628 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3629 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3630 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3631 | case ACT_HTTP_DEL_ACL: |
| 3632 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3633 | struct pat_ref *ref; |
| 3634 | char *key; |
| 3635 | int len; |
| 3636 | |
| 3637 | /* collect reference */ |
| 3638 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3639 | if (!ref) |
| 3640 | continue; |
| 3641 | |
| 3642 | /* collect key */ |
| 3643 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3644 | key = trash.str; |
| 3645 | key[len] = '\0'; |
| 3646 | |
| 3647 | /* perform update */ |
| 3648 | /* returned code: 1=ok, 0=ko */ |
| 3649 | pat_ref_delete(ref, key); |
| 3650 | |
| 3651 | break; |
| 3652 | } |
| 3653 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3654 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3655 | struct pat_ref *ref; |
| 3656 | char *key; |
| 3657 | struct chunk *trash_key; |
| 3658 | int len; |
| 3659 | |
| 3660 | trash_key = get_trash_chunk(); |
| 3661 | |
| 3662 | /* collect reference */ |
| 3663 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3664 | if (!ref) |
| 3665 | continue; |
| 3666 | |
| 3667 | /* collect key */ |
| 3668 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3669 | key = trash_key->str; |
| 3670 | key[len] = '\0'; |
| 3671 | |
| 3672 | /* perform update */ |
| 3673 | /* add entry only if it does not already exist */ |
| 3674 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3675 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3676 | |
| 3677 | break; |
| 3678 | } |
| 3679 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3680 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3681 | struct pat_ref *ref; |
| 3682 | char *key, *value; |
| 3683 | struct chunk *trash_key, *trash_value; |
| 3684 | int len; |
| 3685 | |
| 3686 | trash_key = get_trash_chunk(); |
| 3687 | trash_value = get_trash_chunk(); |
| 3688 | |
| 3689 | /* collect reference */ |
| 3690 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3691 | if (!ref) |
| 3692 | continue; |
| 3693 | |
| 3694 | /* collect key */ |
| 3695 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3696 | key = trash_key->str; |
| 3697 | key[len] = '\0'; |
| 3698 | |
| 3699 | /* collect value */ |
| 3700 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3701 | value = trash_value->str; |
| 3702 | value[len] = '\0'; |
| 3703 | |
| 3704 | /* perform update */ |
| 3705 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3706 | /* update entry if it exists */ |
| 3707 | pat_ref_set(ref, key, value, NULL); |
| 3708 | else |
| 3709 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3710 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3711 | |
| 3712 | break; |
| 3713 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3714 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame^] | 3715 | case ACT_CUSTOM: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3716 | switch (rule->action_ptr(rule, px, s->sess, s)) { |
| 3717 | case ACT_RET_ERR: |
| 3718 | case ACT_RET_CONT: |
| 3719 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame^] | 3720 | case ACT_RET_STOP: |
| 3721 | return HTTP_RULE_RES_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3722 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3723 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3724 | return HTTP_RULE_RES_YIELD; |
| 3725 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3726 | break; |
| 3727 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3728 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3729 | /* Note: only the first valid tracking parameter of each |
| 3730 | * applies. |
| 3731 | */ |
| 3732 | |
| 3733 | if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) { |
| 3734 | struct stktable *t; |
| 3735 | struct stksess *ts; |
| 3736 | struct stktable_key *key; |
| 3737 | void *ptr; |
| 3738 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 3739 | t = rule->arg.trk_ctr.table.t; |
| 3740 | 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] | 3741 | |
| 3742 | if (key && (ts = stktable_get_entry(t, key))) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3743 | 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] | 3744 | |
| 3745 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3746 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3747 | if (ptr) |
| 3748 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3749 | |
| 3750 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3751 | if (ptr) |
| 3752 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3753 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3754 | |
| 3755 | 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] | 3756 | if (sess->fe != s->be) |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3757 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3758 | } |
| 3759 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3760 | break; |
| 3761 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3762 | case ACT_HTTP_REQ_SET_SRC: |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3763 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) { |
| 3764 | struct sample *smp; |
| 3765 | |
Thierry FOURNIER | a002dc9 | 2015-07-31 08:50:51 +0200 | [diff] [blame] | 3766 | 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] | 3767 | |
| 3768 | if (smp) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3769 | if (smp->data.type == SMP_T_IPV4) { |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3770 | ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3771 | ((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] | 3772 | ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3773 | } else if (smp->data.type == SMP_T_IPV6) { |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3774 | ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3775 | 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] | 3776 | ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0; |
| 3777 | } |
| 3778 | } |
| 3779 | } |
| 3780 | break; |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3781 | |
| 3782 | /* other flags exists, but normaly, they never be matched. */ |
| 3783 | default: |
| 3784 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3785 | } |
| 3786 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3787 | |
| 3788 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3789 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3790 | } |
| 3791 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3792 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3793 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 3794 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 3795 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 3796 | * is returned, the process can continue the evaluation of next rule list. If |
| 3797 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 3798 | * is returned, it means the operation could not be processed and a server error |
| 3799 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 3800 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 3801 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3802 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3803 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3804 | 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] | 3805 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3806 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3807 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3808 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3809 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3810 | struct hdr_ctx ctx; |
| 3811 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3812 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3813 | * resume condition. If a resume is needed it is always in the action |
| 3814 | * and never in the ACL or converters. In this case, we initialise the |
| 3815 | * current rule, and go to the action execution point. |
| 3816 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3817 | if (s->current_rule) { |
| 3818 | rule = s->current_rule; |
| 3819 | s->current_rule = NULL; |
| 3820 | if (s->current_rule_list == rules) |
| 3821 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3822 | } |
| 3823 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3824 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3825 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3826 | |
| 3827 | /* check optional condition */ |
| 3828 | if (rule->cond) { |
| 3829 | int ret; |
| 3830 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3831 | 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] | 3832 | ret = acl_pass(ret); |
| 3833 | |
| 3834 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3835 | ret = !ret; |
| 3836 | |
| 3837 | if (!ret) /* condition not matched */ |
| 3838 | continue; |
| 3839 | } |
| 3840 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3841 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3842 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3843 | case ACT_ACTION_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3844 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
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_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3847 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3848 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3849 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3850 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3851 | s->task->nice = rule->arg.nice; |
| 3852 | break; |
| 3853 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3854 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3855 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3856 | 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] | 3857 | break; |
| 3858 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3859 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3860 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3861 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3862 | 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] | 3863 | #endif |
| 3864 | break; |
| 3865 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3866 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3867 | s->logs.level = rule->arg.loglevel; |
| 3868 | break; |
| 3869 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3870 | case ACT_HTTP_REPLACE_HDR: |
| 3871 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3872 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 3873 | rule->arg.hdr_add.name_len, |
| 3874 | &rule->arg.hdr_add.fmt, |
| 3875 | &rule->arg.hdr_add.re, rule->action)) |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3876 | return HTTP_RULE_RES_STOP; /* note: we should report an error here */ |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3877 | break; |
| 3878 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3879 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3880 | ctx.idx = 0; |
| 3881 | /* remove all occurrences of the header */ |
| 3882 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3883 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3884 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3885 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3886 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3887 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3888 | case ACT_HTTP_SET_HDR: |
| 3889 | case ACT_HTTP_ADD_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3890 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3891 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3892 | trash.len = rule->arg.hdr_add.name_len; |
| 3893 | trash.str[trash.len++] = ':'; |
| 3894 | trash.str[trash.len++] = ' '; |
| 3895 | 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] | 3896 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3897 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3898 | /* remove all occurrences of the header */ |
| 3899 | ctx.idx = 0; |
| 3900 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3901 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3902 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3903 | } |
| 3904 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3905 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3906 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3907 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3908 | case ACT_HTTP_DEL_ACL: |
| 3909 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3910 | struct pat_ref *ref; |
| 3911 | char *key; |
| 3912 | int len; |
| 3913 | |
| 3914 | /* collect reference */ |
| 3915 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3916 | if (!ref) |
| 3917 | continue; |
| 3918 | |
| 3919 | /* collect key */ |
| 3920 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3921 | key = trash.str; |
| 3922 | key[len] = '\0'; |
| 3923 | |
| 3924 | /* perform update */ |
| 3925 | /* returned code: 1=ok, 0=ko */ |
| 3926 | pat_ref_delete(ref, key); |
| 3927 | |
| 3928 | break; |
| 3929 | } |
| 3930 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3931 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3932 | struct pat_ref *ref; |
| 3933 | char *key; |
| 3934 | struct chunk *trash_key; |
| 3935 | int len; |
| 3936 | |
| 3937 | trash_key = get_trash_chunk(); |
| 3938 | |
| 3939 | /* collect reference */ |
| 3940 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3941 | if (!ref) |
| 3942 | continue; |
| 3943 | |
| 3944 | /* collect key */ |
| 3945 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3946 | key = trash_key->str; |
| 3947 | key[len] = '\0'; |
| 3948 | |
| 3949 | /* perform update */ |
| 3950 | /* check if the entry already exists */ |
| 3951 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3952 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3953 | |
| 3954 | break; |
| 3955 | } |
| 3956 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3957 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3958 | struct pat_ref *ref; |
| 3959 | char *key, *value; |
| 3960 | struct chunk *trash_key, *trash_value; |
| 3961 | int len; |
| 3962 | |
| 3963 | trash_key = get_trash_chunk(); |
| 3964 | trash_value = get_trash_chunk(); |
| 3965 | |
| 3966 | /* collect reference */ |
| 3967 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3968 | if (!ref) |
| 3969 | continue; |
| 3970 | |
| 3971 | /* collect key */ |
| 3972 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3973 | key = trash_key->str; |
| 3974 | key[len] = '\0'; |
| 3975 | |
| 3976 | /* collect value */ |
| 3977 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3978 | value = trash_value->str; |
| 3979 | value[len] = '\0'; |
| 3980 | |
| 3981 | /* perform update */ |
| 3982 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3983 | /* update entry if it exists */ |
| 3984 | pat_ref_set(ref, key, value, NULL); |
| 3985 | else |
| 3986 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3987 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3988 | |
| 3989 | break; |
| 3990 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3991 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3992 | case ACT_HTTP_REDIR: |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3993 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3994 | return HTTP_RULE_RES_BADREQ; |
| 3995 | return HTTP_RULE_RES_DONE; |
| 3996 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame^] | 3997 | case ACT_CUSTOM: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3998 | switch (rule->action_ptr(rule, px, s->sess, s)) { |
| 3999 | case ACT_RET_ERR: |
| 4000 | case ACT_RET_CONT: |
| 4001 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame^] | 4002 | case ACT_RET_STOP: |
| 4003 | return HTTP_RULE_RES_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 4004 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 4005 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4006 | return HTTP_RULE_RES_YIELD; |
| 4007 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 4008 | break; |
| 4009 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 4010 | /* other flags exists, but normaly, they never be matched. */ |
| 4011 | default: |
| 4012 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 4013 | } |
| 4014 | } |
| 4015 | |
| 4016 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 4017 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 4018 | } |
| 4019 | |
| 4020 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4021 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 4022 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 4023 | * as too large a request to build a valid response. |
| 4024 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4025 | 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] | 4026 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4027 | struct http_msg *req = &txn->req; |
| 4028 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4029 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4030 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4031 | |
| 4032 | /* build redirect message */ |
| 4033 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 4034 | case 308: |
| 4035 | msg_fmt = HTTP_308; |
| 4036 | break; |
| 4037 | case 307: |
| 4038 | msg_fmt = HTTP_307; |
| 4039 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4040 | case 303: |
| 4041 | msg_fmt = HTTP_303; |
| 4042 | break; |
| 4043 | case 301: |
| 4044 | msg_fmt = HTTP_301; |
| 4045 | break; |
| 4046 | case 302: |
| 4047 | default: |
| 4048 | msg_fmt = HTTP_302; |
| 4049 | break; |
| 4050 | } |
| 4051 | |
| 4052 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 4053 | return 0; |
| 4054 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4055 | location = trash.str + trash.len; |
| 4056 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4057 | switch(rule->type) { |
| 4058 | case REDIRECT_TYPE_SCHEME: { |
| 4059 | const char *path; |
| 4060 | const char *host; |
| 4061 | struct hdr_ctx ctx; |
| 4062 | int pathlen; |
| 4063 | int hostlen; |
| 4064 | |
| 4065 | host = ""; |
| 4066 | hostlen = 0; |
| 4067 | ctx.idx = 0; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4068 | 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] | 4069 | host = ctx.line + ctx.val; |
| 4070 | hostlen = ctx.vlen; |
| 4071 | } |
| 4072 | |
| 4073 | path = http_get_path(txn); |
| 4074 | /* build message using path */ |
| 4075 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4076 | 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] | 4077 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 4078 | int qs = 0; |
| 4079 | while (qs < pathlen) { |
| 4080 | if (path[qs] == '?') { |
| 4081 | pathlen = qs; |
| 4082 | break; |
| 4083 | } |
| 4084 | qs++; |
| 4085 | } |
| 4086 | } |
| 4087 | } else { |
| 4088 | path = "/"; |
| 4089 | pathlen = 1; |
| 4090 | } |
| 4091 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4092 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4093 | /* check if we can add scheme + "://" + host + path */ |
| 4094 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 4095 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4096 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4097 | /* add scheme */ |
| 4098 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4099 | trash.len += rule->rdr_len; |
| 4100 | } |
| 4101 | else { |
| 4102 | /* add scheme with executing log format */ |
| 4103 | 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] | 4104 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4105 | /* check if we can add scheme + "://" + host + path */ |
| 4106 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 4107 | return 0; |
| 4108 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4109 | /* add "://" */ |
| 4110 | memcpy(trash.str + trash.len, "://", 3); |
| 4111 | trash.len += 3; |
| 4112 | |
| 4113 | /* add host */ |
| 4114 | memcpy(trash.str + trash.len, host, hostlen); |
| 4115 | trash.len += hostlen; |
| 4116 | |
| 4117 | /* add path */ |
| 4118 | memcpy(trash.str + trash.len, path, pathlen); |
| 4119 | trash.len += pathlen; |
| 4120 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4121 | /* 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] | 4122 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 4123 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 4124 | if (trash.len > trash.size - 5) |
| 4125 | return 0; |
| 4126 | trash.str[trash.len] = '/'; |
| 4127 | trash.len++; |
| 4128 | } |
| 4129 | |
| 4130 | break; |
| 4131 | } |
| 4132 | case REDIRECT_TYPE_PREFIX: { |
| 4133 | const char *path; |
| 4134 | int pathlen; |
| 4135 | |
| 4136 | path = http_get_path(txn); |
| 4137 | /* build message using path */ |
| 4138 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4139 | 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] | 4140 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 4141 | int qs = 0; |
| 4142 | while (qs < pathlen) { |
| 4143 | if (path[qs] == '?') { |
| 4144 | pathlen = qs; |
| 4145 | break; |
| 4146 | } |
| 4147 | qs++; |
| 4148 | } |
| 4149 | } |
| 4150 | } else { |
| 4151 | path = "/"; |
| 4152 | pathlen = 1; |
| 4153 | } |
| 4154 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4155 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4156 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 4157 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4158 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4159 | /* add prefix. Note that if prefix == "/", we don't want to |
| 4160 | * add anything, otherwise it makes it hard for the user to |
| 4161 | * configure a self-redirection. |
| 4162 | */ |
| 4163 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 4164 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4165 | trash.len += rule->rdr_len; |
| 4166 | } |
| 4167 | } |
| 4168 | else { |
| 4169 | /* add prefix with executing log format */ |
| 4170 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 4171 | |
| 4172 | /* Check length */ |
| 4173 | if (trash.len + pathlen > trash.size - 4) |
| 4174 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4175 | } |
| 4176 | |
| 4177 | /* add path */ |
| 4178 | memcpy(trash.str + trash.len, path, pathlen); |
| 4179 | trash.len += pathlen; |
| 4180 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4181 | /* 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] | 4182 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 4183 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 4184 | if (trash.len > trash.size - 5) |
| 4185 | return 0; |
| 4186 | trash.str[trash.len] = '/'; |
| 4187 | trash.len++; |
| 4188 | } |
| 4189 | |
| 4190 | break; |
| 4191 | } |
| 4192 | case REDIRECT_TYPE_LOCATION: |
| 4193 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4194 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4195 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 4196 | return 0; |
| 4197 | |
| 4198 | /* add location */ |
| 4199 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4200 | trash.len += rule->rdr_len; |
| 4201 | } |
| 4202 | else { |
| 4203 | /* add location with executing log format */ |
| 4204 | 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] | 4205 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4206 | /* Check left length */ |
| 4207 | if (trash.len > trash.size - 4) |
| 4208 | return 0; |
| 4209 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4210 | break; |
| 4211 | } |
| 4212 | |
| 4213 | if (rule->cookie_len) { |
| 4214 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 4215 | trash.len += 14; |
| 4216 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 4217 | trash.len += rule->cookie_len; |
| 4218 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 4219 | trash.len += 2; |
| 4220 | } |
| 4221 | |
| 4222 | /* add end of headers and the keep-alive/close status. |
| 4223 | * We may choose to set keep-alive if the Location begins |
| 4224 | * with a slash, because the client will come back to the |
| 4225 | * same server. |
| 4226 | */ |
| 4227 | txn->status = rule->code; |
| 4228 | /* let's log the request time */ |
| 4229 | s->logs.tv_request = now; |
| 4230 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4231 | if (*location == '/' && |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4232 | (req->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 2de8a50 | 2015-05-28 17:23:54 +0200 | [diff] [blame] | 4233 | ((!(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] | 4234 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4235 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4236 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4237 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4238 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4239 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 4240 | trash.len += 30; |
| 4241 | } else { |
| 4242 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 4243 | trash.len += 24; |
| 4244 | } |
| 4245 | } |
| 4246 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 4247 | trash.len += 4; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4248 | bo_inject(res->chn, trash.str, trash.len); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4249 | /* "eat" the request */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4250 | bi_fast_delete(req->chn->buf, req->sov); |
| 4251 | req->next -= req->sov; |
| 4252 | req->sov = 0; |
| 4253 | s->req.analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4254 | s->res.analysers = AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4255 | req->msg_state = HTTP_MSG_CLOSED; |
| 4256 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 4257 | /* Trim any possible response */ |
| 4258 | res->chn->buf->i = 0; |
| 4259 | res->next = res->sov = 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4260 | } else { |
| 4261 | /* keep-alive not possible */ |
| 4262 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4263 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 4264 | trash.len += 29; |
| 4265 | } else { |
| 4266 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 4267 | trash.len += 23; |
| 4268 | } |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4269 | stream_int_retnclose(&s->si[0], &trash); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4270 | req->chn->analysers = 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4271 | } |
| 4272 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4273 | if (!(s->flags & SF_ERR_MASK)) |
| 4274 | s->flags |= SF_ERR_LOCAL; |
| 4275 | if (!(s->flags & SF_FINST_MASK)) |
| 4276 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4277 | |
| 4278 | return 1; |
| 4279 | } |
| 4280 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4281 | /* This stream analyser runs all HTTP request processing which is common to |
| 4282 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 4283 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4284 | * 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] | 4285 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 4286 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4287 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4288 | 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] | 4289 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4290 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4291 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4292 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4293 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4294 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4295 | enum rule_result verdict; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4296 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4297 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4298 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4299 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4300 | } |
| 4301 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4302 | 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] | 4303 | now_ms, __FUNCTION__, |
| 4304 | s, |
| 4305 | req, |
| 4306 | req->rex, req->wex, |
| 4307 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4308 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4309 | req->analysers); |
| 4310 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4311 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4312 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4313 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4314 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4315 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4316 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 4317 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4318 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4319 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 4320 | goto return_prx_yield; |
| 4321 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4322 | case HTTP_RULE_RES_CONT: |
| 4323 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4324 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4325 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4326 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4327 | if (txn->flags & TX_CLTARPIT) |
| 4328 | goto tarpit; |
| 4329 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4330 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4331 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4332 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4333 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4334 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4335 | goto done; |
| 4336 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4337 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4338 | goto return_bad_req; |
| 4339 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4340 | } |
| 4341 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4342 | /* OK at this stage, we know that the request was accepted according to |
| 4343 | * the http-request rules, we can check for the stats. Note that the |
| 4344 | * URI is detected *before* the req* rules in order not to be affected |
| 4345 | * by a possible reqrep, while they are processed *after* so that a |
| 4346 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4347 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4348 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4349 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4350 | 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] | 4351 | txn->status = 500; |
| 4352 | s->logs.tv_request = now; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4353 | 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] | 4354 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4355 | if (!(s->flags & SF_ERR_MASK)) |
| 4356 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4357 | goto return_prx_cond; |
| 4358 | } |
| 4359 | |
| 4360 | /* parse the whole stats request and extract the relevant information */ |
| 4361 | http_handle_stats(s, req); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4362 | 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] | 4363 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4364 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4365 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4366 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4367 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4368 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4369 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4370 | } |
| 4371 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4372 | /* evaluate the req* rules except reqadd */ |
| 4373 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4374 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4375 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4376 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4377 | if (txn->flags & TX_CLDENY) |
| 4378 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4379 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4380 | if (txn->flags & TX_CLTARPIT) |
| 4381 | goto tarpit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4382 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4383 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4384 | /* add request headers from the rule sets in the same order */ |
| 4385 | list_for_each_entry(wl, &px->req_add, list) { |
| 4386 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4387 | 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] | 4388 | ret = acl_pass(ret); |
| 4389 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4390 | ret = !ret; |
| 4391 | if (!ret) |
| 4392 | continue; |
| 4393 | } |
| 4394 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4395 | 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] | 4396 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4397 | } |
| 4398 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4399 | |
| 4400 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4401 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4402 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4403 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4404 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4405 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4406 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 4407 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 4408 | if (!(s->flags & SF_FINST_MASK)) |
| 4409 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4410 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4411 | /* we may want to compress the stats page */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4412 | if (sess->fe->comp || s->be->comp) |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4413 | select_compression_request_header(s, req->buf); |
| 4414 | |
| 4415 | /* 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] | 4416 | 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] | 4417 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4418 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4419 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4420 | /* check whether we have some ACLs set to redirect this request */ |
| 4421 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4422 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4423 | int ret; |
| 4424 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4425 | 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] | 4426 | ret = acl_pass(ret); |
| 4427 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4428 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4429 | if (!ret) |
| 4430 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4431 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4432 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4433 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4434 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4435 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4436 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4437 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4438 | * If this happens, then the data will not come immediately, so we must |
| 4439 | * send all what we have without waiting. Note that due to the small gain |
| 4440 | * 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] | 4441 | * 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] | 4442 | * itself once used. |
| 4443 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4444 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4445 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4446 | done: /* done with this analyser, continue with next ones that the calling |
| 4447 | * points will have set, if any. |
| 4448 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4449 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4450 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4451 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4452 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4453 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4454 | tarpit: |
| 4455 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4456 | * which may be the same as the connect timeout if unspecified. |
| 4457 | * If unset, then set it to zero because we really want it to |
| 4458 | * eventually expire. We build the tarpit as an analyser. |
| 4459 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4460 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4461 | |
| 4462 | /* wipe the request out so that we can drop the connection early |
| 4463 | * if the client closes first. |
| 4464 | */ |
| 4465 | channel_dont_connect(req); |
| 4466 | req->analysers = 0; /* remove switching rules etc... */ |
| 4467 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4468 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4469 | if (!req->analyse_exp) |
| 4470 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4471 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4472 | sess->fe->fe_counters.denied_req++; |
| 4473 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4474 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4475 | if (sess->listener->counters) |
| 4476 | sess->listener->counters->denied_req++; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4477 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4478 | |
| 4479 | deny: /* this request was blocked (denied) */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4480 | txn->flags |= TX_CLDENY; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 4481 | txn->status = http_err_codes[txn->rule_deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4482 | s->logs.tv_request = now; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 4483 | 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] | 4484 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4485 | sess->fe->fe_counters.denied_req++; |
| 4486 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4487 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4488 | if (sess->listener->counters) |
| 4489 | sess->listener->counters->denied_req++; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4490 | goto return_prx_cond; |
| 4491 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4492 | return_bad_req: |
| 4493 | /* We centralize bad requests processing here */ |
| 4494 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4495 | /* we detected a parsing error. We want to archive this request |
| 4496 | * in the dedicated proxy area for later troubleshooting. |
| 4497 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4498 | 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] | 4499 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4500 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4501 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4502 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4503 | 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] | 4504 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4505 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4506 | if (sess->listener->counters) |
| 4507 | sess->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4508 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4509 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4510 | if (!(s->flags & SF_ERR_MASK)) |
| 4511 | s->flags |= SF_ERR_PRXCOND; |
| 4512 | if (!(s->flags & SF_FINST_MASK)) |
| 4513 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4514 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4515 | req->analysers = 0; |
| 4516 | req->analyse_exp = TICK_ETERNITY; |
| 4517 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4518 | |
| 4519 | return_prx_yield: |
| 4520 | channel_dont_connect(req); |
| 4521 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4522 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4523 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4524 | /* This function performs all the processing enabled for the current request. |
| 4525 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4526 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4527 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4528 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4529 | 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] | 4530 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4531 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4532 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4533 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 4534 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4535 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4536 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4537 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4538 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4539 | return 0; |
| 4540 | } |
| 4541 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4542 | 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] | 4543 | now_ms, __FUNCTION__, |
| 4544 | s, |
| 4545 | req, |
| 4546 | req->rex, req->wex, |
| 4547 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4548 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4549 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4550 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4551 | if (sess->fe->comp || s->be->comp) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 4552 | select_compression_request_header(s, req->buf); |
| 4553 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4554 | /* |
| 4555 | * Right now, we know that we have processed the entire headers |
| 4556 | * and that unwanted requests have been filtered out. We can do |
| 4557 | * whatever we want with the remaining request. Also, now we |
| 4558 | * may have separate values for ->fe, ->be. |
| 4559 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4560 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4561 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4562 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4563 | * incoming request. Note that this requires that a connection is |
| 4564 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4565 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4566 | 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] | 4567 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4568 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4569 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4570 | /* Note that for now we don't reuse existing proxy connections */ |
Willy Tarreau | 973a542 | 2015-08-05 21:47:23 +0200 | [diff] [blame] | 4571 | if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4572 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4573 | txn->status = 500; |
| 4574 | req->analysers = 0; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4575 | 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] | 4576 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4577 | if (!(s->flags & SF_ERR_MASK)) |
| 4578 | s->flags |= SF_ERR_RESOURCE; |
| 4579 | if (!(s->flags & SF_FINST_MASK)) |
| 4580 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4581 | |
| 4582 | return 0; |
| 4583 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4584 | |
| 4585 | path = http_get_path(txn); |
| 4586 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4587 | 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] | 4588 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4589 | /* if the path was found, we have to remove everything between |
| 4590 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4591 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4592 | * u_l characters by a single "/". |
| 4593 | */ |
| 4594 | if (path) { |
| 4595 | char *cur_ptr = req->buf->p; |
| 4596 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4597 | int delta; |
| 4598 | |
| 4599 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4600 | http_msg_move_end(&txn->req, delta); |
| 4601 | cur_end += delta; |
| 4602 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4603 | goto return_bad_req; |
| 4604 | } |
| 4605 | else { |
| 4606 | char *cur_ptr = req->buf->p; |
| 4607 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4608 | int delta; |
| 4609 | |
| 4610 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4611 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4612 | http_msg_move_end(&txn->req, delta); |
| 4613 | cur_end += delta; |
| 4614 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4615 | goto return_bad_req; |
| 4616 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4617 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4618 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4619 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4620 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4621 | * Note that doing so might move headers in the request, but |
| 4622 | * the fields will stay coherent and the URI will not move. |
| 4623 | * This should only be performed in the backend. |
| 4624 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 4625 | if ((s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4626 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 4627 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4628 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4629 | /* add unique-id if "header-unique-id" is specified */ |
| 4630 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4631 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4632 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4633 | goto return_bad_req; |
| 4634 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4635 | 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] | 4636 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4637 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4638 | if (sess->fe->header_unique_id && s->unique_id) { |
| 4639 | 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] | 4640 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4641 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4642 | 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] | 4643 | goto return_bad_req; |
| 4644 | } |
| 4645 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4646 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4647 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4648 | * asks for it. |
| 4649 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4650 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4651 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4652 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 4653 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 4654 | 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] | 4655 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4656 | /* The header is set to be added only if none is present |
| 4657 | * and we found it, so don't do anything. |
| 4658 | */ |
| 4659 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4660 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4661 | /* Add an X-Forwarded-For header unless the source IP is |
| 4662 | * in the 'except' network range. |
| 4663 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4664 | if ((!sess->fe->except_mask.s_addr || |
| 4665 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 4666 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4667 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4668 | (((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] | 4669 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4670 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4671 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4672 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4673 | |
| 4674 | /* Note: we rely on the backend to get the header name to be used for |
| 4675 | * x-forwarded-for, because the header is really meant for the backends. |
| 4676 | * However, if the backend did not specify any option, we have to rely |
| 4677 | * on the frontend's header name. |
| 4678 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4679 | if (s->be->fwdfor_hdr_len) { |
| 4680 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4681 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4682 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4683 | len = sess->fe->fwdfor_hdr_len; |
| 4684 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4685 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4686 | 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] | 4687 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4688 | 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] | 4689 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4690 | } |
| 4691 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4692 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4693 | /* FIXME: for the sake of completeness, we should also support |
| 4694 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4695 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4696 | int len; |
| 4697 | char pn[INET6_ADDRSTRLEN]; |
| 4698 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4699 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4700 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4701 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4702 | /* Note: we rely on the backend to get the header name to be used for |
| 4703 | * x-forwarded-for, because the header is really meant for the backends. |
| 4704 | * However, if the backend did not specify any option, we have to rely |
| 4705 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4706 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4707 | if (s->be->fwdfor_hdr_len) { |
| 4708 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4709 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4710 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4711 | len = sess->fe->fwdfor_hdr_len; |
| 4712 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4713 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4714 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4715 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4716 | 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] | 4717 | goto return_bad_req; |
| 4718 | } |
| 4719 | } |
| 4720 | |
| 4721 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4722 | * 10: add X-Original-To if either the frontend or the backend |
| 4723 | * asks for it. |
| 4724 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4725 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4726 | |
| 4727 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4728 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4729 | /* Add an X-Original-To header unless the destination IP is |
| 4730 | * in the 'except' network range. |
| 4731 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4732 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4733 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4734 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4735 | ((!sess->fe->except_mask_to.s_addr || |
| 4736 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 4737 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4738 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4739 | (((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] | 4740 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4741 | int len; |
| 4742 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4743 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4744 | |
| 4745 | /* Note: we rely on the backend to get the header name to be used for |
| 4746 | * x-original-to, because the header is really meant for the backends. |
| 4747 | * However, if the backend did not specify any option, we have to rely |
| 4748 | * on the frontend's header name. |
| 4749 | */ |
| 4750 | if (s->be->orgto_hdr_len) { |
| 4751 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4752 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4753 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4754 | len = sess->fe->orgto_hdr_len; |
| 4755 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4756 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4757 | 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] | 4758 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4759 | 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] | 4760 | goto return_bad_req; |
| 4761 | } |
| 4762 | } |
| 4763 | } |
| 4764 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4765 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4766 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4767 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4768 | * "Upgrade" is present in the Connection header. |
| 4769 | */ |
| 4770 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4771 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4772 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4773 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4774 | unsigned int want_flags = 0; |
| 4775 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4776 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4777 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4778 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4779 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4780 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4781 | want_flags |= TX_CON_CLO_SET; |
| 4782 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4783 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4784 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4785 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4786 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4787 | want_flags |= TX_CON_KAL_SET; |
| 4788 | } |
| 4789 | |
| 4790 | 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] | 4791 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4792 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4793 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4794 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4795 | /* If we have no server assigned yet and we're balancing on url_param |
| 4796 | * with a POST request, we may be interested in checking the body for |
| 4797 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4798 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4799 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4800 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4801 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4802 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4803 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4804 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4805 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4806 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4807 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4808 | #ifdef TCP_QUICKACK |
| 4809 | /* We expect some data from the client. Unless we know for sure |
| 4810 | * we already have a full request, we have to re-enable quick-ack |
| 4811 | * in case we previously disabled it, otherwise we might cause |
| 4812 | * the client to delay further data. |
| 4813 | */ |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4814 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4815 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4816 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4817 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4818 | 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] | 4819 | #endif |
| 4820 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4821 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4822 | /************************************************************* |
| 4823 | * OK, that's finished for the headers. We have done what we * |
| 4824 | * could. Let's switch to the DATA state. * |
| 4825 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4826 | req->analyse_exp = TICK_ETERNITY; |
| 4827 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4828 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4829 | /* if the server closes the connection, we want to immediately react |
| 4830 | * and close the socket to save packets and syscalls. |
| 4831 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4832 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4833 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4834 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4835 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4836 | /* OK let's go on with the BODY now */ |
| 4837 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4838 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4839 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4840 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4841 | /* we detected a parsing error. We want to archive this request |
| 4842 | * in the dedicated proxy area for later troubleshooting. |
| 4843 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4844 | 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] | 4845 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4846 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4847 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4848 | txn->status = 400; |
| 4849 | req->analysers = 0; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4850 | 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] | 4851 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4852 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4853 | if (sess->listener->counters) |
| 4854 | sess->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4855 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4856 | if (!(s->flags & SF_ERR_MASK)) |
| 4857 | s->flags |= SF_ERR_PRXCOND; |
| 4858 | if (!(s->flags & SF_FINST_MASK)) |
| 4859 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4860 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4861 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4862 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4863 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4864 | * returns zero, at the beginning because it prevents any other processing |
| 4865 | * from occurring, and at the end because it terminates the request. |
| 4866 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4867 | 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] | 4868 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4869 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4870 | |
| 4871 | /* This connection is being tarpitted. The CLIENT side has |
| 4872 | * already set the connect expiration date to the right |
| 4873 | * timeout. We just have to check that the client is still |
| 4874 | * there and that the timeout has not expired. |
| 4875 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4876 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4877 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4878 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4879 | return 0; |
| 4880 | |
| 4881 | /* We will set the queue timer to the time spent, just for |
| 4882 | * logging purposes. We fake a 500 server error, so that the |
| 4883 | * attacker will not suspect his connection has been tarpitted. |
| 4884 | * It will not cause trouble to the logs because we can exclude |
| 4885 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4886 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4887 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4888 | |
| 4889 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4890 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4891 | 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] | 4892 | |
| 4893 | req->analysers = 0; |
| 4894 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4895 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4896 | if (!(s->flags & SF_ERR_MASK)) |
| 4897 | s->flags |= SF_ERR_PRXCOND; |
| 4898 | if (!(s->flags & SF_FINST_MASK)) |
| 4899 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4900 | return 0; |
| 4901 | } |
| 4902 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4903 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4904 | * for either the buffer to be full, or the full advertised contents to have |
| 4905 | * reached the buffer. It must only be called after the standard HTTP request |
| 4906 | * processing has occurred, because it expects the request to be parsed and will |
| 4907 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4908 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4909 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4910 | * 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] | 4911 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4912 | 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] | 4913 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4914 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4915 | struct http_txn *txn = s->txn; |
| 4916 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4917 | |
| 4918 | /* We have to parse the HTTP request body to find any required data. |
| 4919 | * "balance url_param check_post" should have been the only way to get |
| 4920 | * into this. We were brought here after HTTP header analysis, so all |
| 4921 | * related structures are ready. |
| 4922 | */ |
| 4923 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4924 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4925 | /* This is the first call */ |
| 4926 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4927 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4928 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4929 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4930 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4931 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4932 | */ |
| 4933 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4934 | struct hdr_ctx ctx; |
| 4935 | ctx.idx = 0; |
| 4936 | /* Expect is allowed in 1.1, look for it */ |
| 4937 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4938 | 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] | 4939 | bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4940 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4941 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4942 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4943 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4944 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4945 | /* 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] | 4946 | * req->buf->p still points to the beginning of the message. We |
| 4947 | * must save the body in msg->next because it survives buffer |
| 4948 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4949 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4950 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4951 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4952 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4953 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4954 | else |
| 4955 | msg->msg_state = HTTP_MSG_DATA; |
| 4956 | } |
| 4957 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4958 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4959 | /* 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] | 4960 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4961 | goto missing_data; |
| 4962 | |
| 4963 | /* OK we have everything we need now */ |
| 4964 | goto http_end; |
| 4965 | } |
| 4966 | |
| 4967 | /* OK here we're parsing a chunked-encoded message */ |
| 4968 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4969 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4970 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4971 | * 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] | 4972 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4973 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4974 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4975 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4976 | if (!ret) |
| 4977 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4978 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4979 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4980 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4981 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4982 | } |
| 4983 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4984 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4985 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 4986 | * for at least a whole chunk or the whole content length bytes after |
| 4987 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4988 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4989 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4990 | goto http_end; |
| 4991 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4992 | 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] | 4993 | goto http_end; |
| 4994 | |
| 4995 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4996 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4997 | * we have the maximum we can expect. |
| 4998 | */ |
| 4999 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 5000 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 5001 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5002 | 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] | 5003 | txn->status = 408; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5004 | 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] | 5005 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5006 | if (!(s->flags & SF_ERR_MASK)) |
| 5007 | s->flags |= SF_ERR_CLITO; |
| 5008 | if (!(s->flags & SF_FINST_MASK)) |
| 5009 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5010 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5011 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5012 | |
| 5013 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 5014 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5015 | /* Not enough data. We'll re-use the http-request |
| 5016 | * timeout here. Ideally, we should set the timeout |
| 5017 | * relative to the accept() date. We just set the |
| 5018 | * request timeout once at the beginning of the |
| 5019 | * request. |
| 5020 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5021 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5022 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 5023 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5024 | return 0; |
| 5025 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5026 | |
| 5027 | http_end: |
| 5028 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 5029 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 5030 | req->analysers &= ~an_bit; |
| 5031 | req->analyse_exp = TICK_ETERNITY; |
| 5032 | return 1; |
| 5033 | |
| 5034 | return_bad_req: /* let's centralize all bad requests */ |
| 5035 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5036 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5037 | 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] | 5038 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5039 | if (!(s->flags & SF_ERR_MASK)) |
| 5040 | s->flags |= SF_ERR_PRXCOND; |
| 5041 | if (!(s->flags & SF_FINST_MASK)) |
| 5042 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5043 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5044 | return_err_msg: |
| 5045 | req->analysers = 0; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5046 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5047 | if (sess->listener->counters) |
| 5048 | sess->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5049 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5050 | } |
| 5051 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5052 | /* send a server's name with an outgoing request over an established connection. |
| 5053 | * Note: this function is designed to be called once the request has been scheduled |
| 5054 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 5055 | * proceeding. |
| 5056 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 5057 | 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] | 5058 | |
| 5059 | struct hdr_ctx ctx; |
| 5060 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5061 | char *hdr_name = be->server_id_hdr_name; |
| 5062 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 5063 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5064 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5065 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5066 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 5067 | ctx.idx = 0; |
| 5068 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 5069 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5070 | if (old_o) { |
| 5071 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5072 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5073 | txn->req.next += old_o; |
| 5074 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5075 | } |
| 5076 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5077 | old_i = chn->buf->i; |
| 5078 | 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] | 5079 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5080 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5081 | } |
| 5082 | |
| 5083 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5084 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5085 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 5086 | hdr_val += hdr_name_len; |
| 5087 | *hdr_val++ = ':'; |
| 5088 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5089 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 5090 | 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] | 5091 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5092 | if (old_o) { |
| 5093 | /* If this was a forwarded request, we must readjust the amount of |
| 5094 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5095 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 5096 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5097 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5098 | old_o += chn->buf->i - old_i; |
| 5099 | b_adv(chn->buf, old_o); |
| 5100 | txn->req.next -= old_o; |
| 5101 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5102 | } |
| 5103 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5104 | return 0; |
| 5105 | } |
| 5106 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5107 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 5108 | * right now but it works. |
| 5109 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5110 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5111 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5112 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 5113 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5114 | struct connection *srv_conn; |
| 5115 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 5116 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5117 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5118 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 5119 | * server's connections. We need a safer way to reinitialize buffer |
| 5120 | * flags. We also need a more accurate method for computing per-request |
| 5121 | * data. |
| 5122 | */ |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5123 | srv_conn = objt_conn(s->si[1].end); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5124 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5125 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 5126 | * to the server. |
| 5127 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5128 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5129 | !si_conn_ready(&s->si[1])) { |
| 5130 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 5131 | si_shutr(&s->si[1]); |
| 5132 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5133 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5134 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5135 | if (s->flags & SF_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5136 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 5137 | if (unlikely(s->srv_conn)) |
| 5138 | sess_change_server(s, NULL); |
| 5139 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5140 | |
| 5141 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5142 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5143 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5144 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5145 | int n; |
| 5146 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5147 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5148 | if (n < 1 || n > 5) |
| 5149 | n = 0; |
| 5150 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5151 | if (fe->mode == PR_MODE_HTTP) { |
| 5152 | fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5153 | if (s->comp_algo && (s->flags & SF_COMP_READY)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5154 | fe->fe_counters.p.http.comp_rsp++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5155 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5156 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5157 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5158 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5159 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5160 | if (s->comp_algo && (s->flags & SF_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5161 | s->be->be_counters.p.http.comp_rsp++; |
| 5162 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5163 | } |
| 5164 | |
| 5165 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5166 | s->logs.bytes_in -= s->req.buf->i; |
| 5167 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5168 | |
| 5169 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5170 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5171 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5172 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5173 | s->do_log(s); |
| 5174 | } |
| 5175 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 5176 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5177 | stream_stop_content_counters(s); |
| 5178 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 5179 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5180 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 5181 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 5182 | tv_zero(&s->logs.tv_request); |
| 5183 | s->logs.t_queue = -1; |
| 5184 | s->logs.t_connect = -1; |
| 5185 | s->logs.t_data = -1; |
| 5186 | s->logs.t_close = 0; |
| 5187 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 5188 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 5189 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5190 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 5191 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5192 | |
| 5193 | if (s->pend_pos) |
| 5194 | pendconn_free(s->pend_pos); |
| 5195 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5196 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5197 | if (s->flags & SF_CURR_SESS) { |
| 5198 | s->flags &= ~SF_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5199 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5200 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5201 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 5202 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5203 | } |
| 5204 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5205 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5206 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5207 | /* only release our endpoint if we don't intend to reuse the |
| 5208 | * connection. |
| 5209 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5210 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5211 | !si_conn_ready(&s->si[1])) { |
| 5212 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5213 | srv_conn = NULL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5214 | } |
| 5215 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5216 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 5217 | s->si[1].err_type = SI_ET_NONE; |
| 5218 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 5219 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5220 | 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] | 5221 | 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); |
| 5222 | 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] | 5223 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 5224 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 5225 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 5226 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5227 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5228 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5229 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5230 | |
| 5231 | if (prev_status == 401 || prev_status == 407) { |
| 5232 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 5233 | * a chance of being able to send the visitor again to the same |
| 5234 | * server over the same connection. This is required by some |
| 5235 | * broken protocols such as NTLM, and anyway whenever there is |
| 5236 | * an opportunity for sending the challenge to the proper place, |
| 5237 | * it's better to do it (at least it helps with debugging). |
| 5238 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5239 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | bd99d58 | 2015-09-02 10:40:43 +0200 | [diff] [blame] | 5240 | if (srv_conn) |
| 5241 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5242 | } |
| 5243 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5244 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5245 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5246 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5247 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5248 | s->req.flags |= CF_NEVER_WAIT; |
| 5249 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 5250 | } |
| 5251 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5252 | /* if the request buffer is not empty, it means we're |
| 5253 | * about to process another request, so send pending |
| 5254 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5255 | * Just don't do this if the buffer is close to be full, |
| 5256 | * because the request will wait for it to flush a little |
| 5257 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5258 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5259 | if (s->req.buf->i) { |
| 5260 | if (s->res.buf->o && |
| 5261 | !buffer_full(s->res.buf, global.tune.maxrewrite) && |
| 5262 | bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite) |
| 5263 | s->res.flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5264 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5265 | |
| 5266 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5267 | channel_auto_read(&s->req); |
| 5268 | channel_auto_close(&s->req); |
| 5269 | channel_auto_read(&s->res); |
| 5270 | channel_auto_close(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5271 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5272 | /* we're in keep-alive with an idle connection, monitor it */ |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 5273 | if (srv_conn) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5274 | srv = objt_server(srv_conn->target); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5275 | if (!srv) |
| 5276 | si_idle_conn(&s->si[1], NULL); |
| 5277 | else if ((srv_conn->flags & CO_FL_PRIVATE) || |
| 5278 | ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)) |
| 5279 | si_idle_conn(&s->si[1], &srv->priv_conns); |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 5280 | else if (prev_flags & TX_NOT_FIRST) |
| 5281 | /* note: we check the request, not the connection, but |
| 5282 | * this is valid for strategies SAFE and AGGR, and in |
| 5283 | * case of ALWS, we don't care anyway. |
| 5284 | */ |
| 5285 | si_idle_conn(&s->si[1], &srv->safe_conns); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5286 | else |
| 5287 | si_idle_conn(&s->si[1], &srv->idle_conns); |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 5288 | } |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5289 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 5290 | s->req.analysers = strm_li(s)->analysers; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5291 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5292 | } |
| 5293 | |
| 5294 | |
| 5295 | /* This function updates the request state machine according to the response |
| 5296 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5297 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5298 | * it is only used to find when a request/response couple is complete. Both |
| 5299 | * this function and its equivalent should loop until both return zero. It |
| 5300 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5301 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5302 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5303 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5304 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5305 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5306 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5307 | unsigned int old_state = txn->req.msg_state; |
| 5308 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5309 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 5310 | return 0; |
| 5311 | |
| 5312 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5313 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5314 | * We can shut the read side unless we want to abort_on_close, |
| 5315 | * or we have a POST request. The issue with POST requests is |
| 5316 | * that some browsers still send a CRLF after the request, and |
| 5317 | * this CRLF must be read so that it does not remain in the kernel |
| 5318 | * buffers, otherwise a close could cause an RST on some systems |
| 5319 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5320 | * Note that if we're using keep-alive on the client side, we'd |
| 5321 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5322 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5323 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5324 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5325 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5326 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5327 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 5328 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5329 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5330 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5331 | /* if the server closes the connection, we want to immediately react |
| 5332 | * and close the socket to save packets and syscalls. |
| 5333 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5334 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5335 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5336 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5337 | goto wait_other_side; |
| 5338 | |
| 5339 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5340 | /* The server has not finished to respond, so we |
| 5341 | * don't want to move in order not to upset it. |
| 5342 | */ |
| 5343 | goto wait_other_side; |
| 5344 | } |
| 5345 | |
| 5346 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5347 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5348 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5349 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5350 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5351 | goto wait_other_side; |
| 5352 | } |
| 5353 | |
| 5354 | /* When we get here, it means that both the request and the |
| 5355 | * response have finished receiving. Depending on the connection |
| 5356 | * mode, we'll have to wait for the last bytes to leave in either |
| 5357 | * direction, and sometimes for a close to be effective. |
| 5358 | */ |
| 5359 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5360 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5361 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5362 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5363 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5364 | } |
| 5365 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5366 | /* Option forceclose is set, or either side wants to close, |
| 5367 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5368 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5369 | * once both states are CLOSED. |
| 5370 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5371 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5372 | channel_shutr_now(chn); |
| 5373 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5374 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5375 | } |
| 5376 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5377 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5378 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5379 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5380 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5381 | channel_auto_read(chn); |
| 5382 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5383 | chn->flags |= CF_NEVER_WAIT; |
| 5384 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5385 | } |
| 5386 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5387 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5388 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5389 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5390 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5391 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5392 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5393 | goto http_msg_closing; |
| 5394 | } |
| 5395 | else { |
| 5396 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5397 | goto http_msg_closed; |
| 5398 | } |
| 5399 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5400 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5401 | } |
| 5402 | |
| 5403 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5404 | http_msg_closing: |
| 5405 | /* nothing else to forward, just waiting for the output buffer |
| 5406 | * to be empty and for the shutw_now to take effect. |
| 5407 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5408 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5409 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5410 | goto http_msg_closed; |
| 5411 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5412 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5413 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5414 | goto wait_other_side; |
| 5415 | } |
| 5416 | } |
| 5417 | |
| 5418 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5419 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5420 | /* see above in MSG_DONE why we only do this in these states */ |
| 5421 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5422 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5423 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5424 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5425 | goto wait_other_side; |
| 5426 | } |
| 5427 | |
| 5428 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5429 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5430 | } |
| 5431 | |
| 5432 | |
| 5433 | /* This function updates the response state machine according to the request |
| 5434 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5435 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5436 | * it is only used to find when a request/response couple is complete. Both |
| 5437 | * this function and its equivalent should loop until both return zero. It |
| 5438 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5439 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5440 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5441 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5442 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5443 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5444 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5445 | unsigned int old_state = txn->rsp.msg_state; |
| 5446 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5447 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5448 | return 0; |
| 5449 | |
| 5450 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5451 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5452 | * still monitor the server connection for a possible close |
| 5453 | * while the request is being uploaded, so we don't disable |
| 5454 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5455 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5456 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5457 | |
| 5458 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5459 | goto wait_other_side; |
| 5460 | |
| 5461 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5462 | /* The client seems to still be sending data, probably |
| 5463 | * because we got an error response during an upload. |
| 5464 | * We have the choice of either breaking the connection |
| 5465 | * or letting it pass through. Let's do the later. |
| 5466 | */ |
| 5467 | goto wait_other_side; |
| 5468 | } |
| 5469 | |
| 5470 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5471 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5472 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5473 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5474 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5475 | goto wait_other_side; |
| 5476 | } |
| 5477 | |
| 5478 | /* When we get here, it means that both the request and the |
| 5479 | * response have finished receiving. Depending on the connection |
| 5480 | * mode, we'll have to wait for the last bytes to leave in either |
| 5481 | * direction, and sometimes for a close to be effective. |
| 5482 | */ |
| 5483 | |
| 5484 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5485 | /* Server-close mode : shut read and wait for the request |
| 5486 | * side to close its output buffer. The caller will detect |
| 5487 | * when we're in DONE and the other is in CLOSED and will |
| 5488 | * catch that for the final cleanup. |
| 5489 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5490 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5491 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5492 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5493 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5494 | /* Option forceclose is set, or either side wants to close, |
| 5495 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5496 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5497 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5498 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5499 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5500 | channel_shutr_now(chn); |
| 5501 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5502 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5503 | } |
| 5504 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5505 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5506 | * need to forward remaining data. Keep-alive will need to monitor |
| 5507 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5508 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5509 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5510 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5511 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5512 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5513 | } |
| 5514 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5515 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5516 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5517 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5518 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5519 | goto http_msg_closing; |
| 5520 | } |
| 5521 | else { |
| 5522 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5523 | goto http_msg_closed; |
| 5524 | } |
| 5525 | } |
| 5526 | goto wait_other_side; |
| 5527 | } |
| 5528 | |
| 5529 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5530 | http_msg_closing: |
| 5531 | /* nothing else to forward, just waiting for the output buffer |
| 5532 | * to be empty and for the shutw_now to take effect. |
| 5533 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5534 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5535 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5536 | goto http_msg_closed; |
| 5537 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5538 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5539 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5540 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5541 | if (objt_server(s->target)) |
| 5542 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5543 | goto wait_other_side; |
| 5544 | } |
| 5545 | } |
| 5546 | |
| 5547 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5548 | http_msg_closed: |
| 5549 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5550 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5551 | channel_auto_close(chn); |
| 5552 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5553 | goto wait_other_side; |
| 5554 | } |
| 5555 | |
| 5556 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5557 | /* We force the response to leave immediately if we're waiting for the |
| 5558 | * other side, since there is no pending shutdown to push it out. |
| 5559 | */ |
| 5560 | if (!channel_is_empty(chn)) |
| 5561 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5562 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5563 | } |
| 5564 | |
| 5565 | |
| 5566 | /* Resync the request and response state machines. Return 1 if either state |
| 5567 | * changes. |
| 5568 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5569 | int http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5570 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5571 | struct http_txn *txn = s->txn; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5572 | int old_req_state = txn->req.msg_state; |
| 5573 | int old_res_state = txn->rsp.msg_state; |
| 5574 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5575 | http_sync_req_state(s); |
| 5576 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5577 | if (!http_sync_res_state(s)) |
| 5578 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5579 | if (!http_sync_req_state(s)) |
| 5580 | break; |
| 5581 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5582 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5583 | /* OK, both state machines agree on a compatible state. |
| 5584 | * There are a few cases we're interested in : |
| 5585 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5586 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5587 | * directions, so let's simply disable both analysers. |
| 5588 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5589 | * request. |
| 5590 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5591 | * with server-close mode means we've completed one request and we |
| 5592 | * must re-initialize the server connection. |
| 5593 | */ |
| 5594 | |
| 5595 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5596 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5597 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5598 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5599 | s->req.analysers = 0; |
| 5600 | channel_auto_close(&s->req); |
| 5601 | channel_auto_read(&s->req); |
| 5602 | s->res.analysers = 0; |
| 5603 | channel_auto_close(&s->res); |
| 5604 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5605 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5606 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5607 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5608 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5609 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5610 | s->res.analysers = 0; |
| 5611 | channel_auto_close(&s->res); |
| 5612 | channel_auto_read(&s->res); |
| 5613 | s->req.analysers = 0; |
| 5614 | channel_abort(&s->req); |
| 5615 | channel_auto_close(&s->req); |
| 5616 | channel_auto_read(&s->req); |
| 5617 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5618 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5619 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5620 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5621 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5622 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5623 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5624 | /* server-close/keep-alive: terminate this transaction, |
| 5625 | * possibly killing the server connection and reinitialize |
| 5626 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5627 | */ |
| 5628 | http_end_txn_clean_session(s); |
| 5629 | } |
| 5630 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5631 | return txn->req.msg_state != old_req_state || |
| 5632 | txn->rsp.msg_state != old_res_state; |
| 5633 | } |
| 5634 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5635 | /* This function is an analyser which forwards request body (including chunk |
| 5636 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5637 | * zero byte. The only situation where it must not be called is when we're in |
| 5638 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5639 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5640 | * 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] | 5641 | * 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] | 5642 | * 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] | 5643 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5644 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5645 | 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] | 5646 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5647 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5648 | struct http_txn *txn = s->txn; |
| 5649 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5650 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5651 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5652 | return 0; |
| 5653 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5654 | 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] | 5655 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5656 | /* Output closed while we were sending data. We must abort and |
| 5657 | * wake the other side up. |
| 5658 | */ |
| 5659 | msg->msg_state = HTTP_MSG_ERROR; |
| 5660 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5661 | return 1; |
| 5662 | } |
| 5663 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5664 | /* Note that we don't have to send 100-continue back because we don't |
| 5665 | * need the data to complete our job, and it's up to the server to |
| 5666 | * decide whether to return 100, 417 or anything else in return of |
| 5667 | * an "Expect: 100-continue" header. |
| 5668 | */ |
| 5669 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5670 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5671 | /* we have msg->sov which points to the first byte of message |
| 5672 | * body, and req->buf.p still points to the beginning of the |
| 5673 | * message. We forward the headers now, as we don't need them |
| 5674 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5675 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5676 | b_adv(req->buf, msg->sov); |
| 5677 | msg->next -= msg->sov; |
| 5678 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5679 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5680 | /* The previous analysers guarantee that the state is somewhere |
| 5681 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 5682 | * msg->next are always correct. |
| 5683 | */ |
| 5684 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5685 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 5686 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5687 | else |
| 5688 | msg->msg_state = HTTP_MSG_DATA; |
| 5689 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5690 | } |
| 5691 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5692 | /* Some post-connect processing might want us to refrain from starting to |
| 5693 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5694 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5695 | */ |
| 5696 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5697 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5698 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5699 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5700 | goto missing_data; |
| 5701 | } |
| 5702 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5703 | } |
| 5704 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5705 | /* in most states, we should abort in case of early close */ |
| 5706 | channel_auto_close(req); |
| 5707 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5708 | if (req->to_forward) { |
| 5709 | /* We can't process the buffer's contents yet */ |
| 5710 | req->flags |= CF_WAKE_WRITE; |
| 5711 | goto missing_data; |
| 5712 | } |
| 5713 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5714 | while (1) { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5715 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5716 | /* must still forward */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5717 | /* we may have some pending data starting at req->buf->p */ |
| 5718 | if (msg->chunk_len > req->buf->i - msg->next) { |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5719 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5720 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5721 | } |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5722 | msg->next += msg->chunk_len; |
| 5723 | msg->chunk_len = 0; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5724 | |
| 5725 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5726 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5727 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5728 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5729 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5730 | } |
| 5731 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5732 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 5733 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5734 | * TRAILERS state. |
| 5735 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5736 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5737 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5738 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5739 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5740 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5741 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5742 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5743 | 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] | 5744 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5745 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5746 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5747 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5748 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5749 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5750 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5751 | |
| 5752 | if (ret == 0) |
| 5753 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5754 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5755 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5756 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5757 | 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] | 5758 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5759 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5760 | /* we're in MSG_CHUNK_SIZE now */ |
| 5761 | } |
| 5762 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5763 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5764 | |
| 5765 | if (ret == 0) |
| 5766 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5767 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5768 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5769 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5770 | 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] | 5771 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5772 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5773 | /* we're in HTTP_MSG_DONE now */ |
| 5774 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5775 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5776 | int old_state = msg->msg_state; |
| 5777 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5778 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5779 | |
| 5780 | /* we may have some pending data starting at req->buf->p |
| 5781 | * such as last chunk of data or trailers. |
| 5782 | */ |
| 5783 | b_adv(req->buf, msg->next); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5784 | if (unlikely(!(s->req.flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5785 | msg->sov -= msg->next; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5786 | msg->next = 0; |
| 5787 | |
Willy Tarreau | bbfb6c4 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5788 | /* we don't want to forward closes on DONE except in |
| 5789 | * tunnel mode. |
| 5790 | */ |
| 5791 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5792 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5793 | if (http_resync_states(s)) { |
| 5794 | /* some state changes occurred, maybe the analyser |
| 5795 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 5796 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5797 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5798 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5799 | /* request errors are most likely due to |
| 5800 | * the server aborting the transfer. |
| 5801 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5802 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5803 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5804 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5805 | 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] | 5806 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5807 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5808 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5809 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5810 | |
| 5811 | /* If "option abortonclose" is set on the backend, we |
| 5812 | * want to monitor the client's connection and forward |
| 5813 | * any shutdown notification to the server, which will |
| 5814 | * decide whether to close or to go on processing the |
Willy Tarreau | bbfb6c4 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5815 | * request. We only do that in tunnel mode, and not in |
| 5816 | * other modes since it can be abused to exhaust source |
| 5817 | * ports. |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5818 | */ |
| 5819 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5820 | channel_auto_read(req); |
Willy Tarreau | bbfb6c4 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5821 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 5822 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 5823 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5824 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5825 | } |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5826 | else if (s->txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5827 | /* POST requests may require to read extra CRLF |
| 5828 | * sent by broken browsers and which could cause |
| 5829 | * an RST to be sent upon close on some systems |
| 5830 | * (eg: Linux). |
| 5831 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5832 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5833 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5834 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5835 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5836 | } |
| 5837 | } |
| 5838 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5839 | missing_data: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5840 | /* we may have some pending data starting at req->buf->p */ |
| 5841 | b_adv(req->buf, msg->next); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5842 | if (unlikely(!(s->req.flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5843 | msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i); |
| 5844 | |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5845 | msg->next = 0; |
| 5846 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
| 5847 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5848 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5849 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5850 | if (!(s->flags & SF_ERR_MASK)) |
| 5851 | s->flags |= SF_ERR_CLICL; |
| 5852 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5853 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5854 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5855 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5856 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5857 | } |
| 5858 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5859 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5860 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5861 | if (objt_server(s->target)) |
| 5862 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5863 | |
| 5864 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5865 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5866 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5867 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5868 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5869 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5870 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5871 | /* 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] | 5872 | * 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] | 5873 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5874 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5875 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5876 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5877 | /* 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] | 5878 | * 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] | 5879 | * 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] | 5880 | * modes are already handled by the stream sock layer. We must not do |
| 5881 | * this in content-length mode because it could present the MSG_MORE |
| 5882 | * flag with the last block of forwarded data, which would cause an |
| 5883 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5884 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5885 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5886 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5887 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5888 | return 0; |
| 5889 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5890 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5891 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5892 | if (sess->listener->counters) |
| 5893 | sess->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5894 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5895 | return_bad_req_stats_ok: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5896 | /* we may have some pending data starting at req->buf->p */ |
| 5897 | b_adv(req->buf, msg->next); |
| 5898 | msg->next = 0; |
| 5899 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5900 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5901 | if (txn->status) { |
| 5902 | /* 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] | 5903 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5904 | } else { |
| 5905 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5906 | 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] | 5907 | } |
| 5908 | req->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5909 | 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] | 5910 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5911 | if (!(s->flags & SF_ERR_MASK)) |
| 5912 | s->flags |= SF_ERR_PRXCOND; |
| 5913 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5914 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5915 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5916 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5917 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5918 | } |
| 5919 | return 0; |
| 5920 | |
| 5921 | aborted_xfer: |
| 5922 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5923 | if (txn->status) { |
| 5924 | /* 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] | 5925 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5926 | } else { |
| 5927 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5928 | 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] | 5929 | } |
| 5930 | req->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5931 | 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] | 5932 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5933 | sess->fe->fe_counters.srv_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5934 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5935 | if (objt_server(s->target)) |
| 5936 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5937 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5938 | if (!(s->flags & SF_ERR_MASK)) |
| 5939 | s->flags |= SF_ERR_SRVCL; |
| 5940 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5941 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5942 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5943 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5944 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5945 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5946 | return 0; |
| 5947 | } |
| 5948 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5949 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5950 | * processing can continue on next analysers, or zero if it either needs more |
| 5951 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5952 | * 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] | 5953 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5954 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5955 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5956 | 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] | 5957 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5958 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5959 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5960 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5961 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5962 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5963 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5964 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5965 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5966 | 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] | 5967 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5968 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5969 | rep, |
| 5970 | rep->rex, rep->wex, |
| 5971 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5972 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5973 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5974 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5975 | /* |
| 5976 | * Now parse the partial (or complete) lines. |
| 5977 | * We will check the response syntax, and also join multi-line |
| 5978 | * headers. An index of all the lines will be elaborated while |
| 5979 | * parsing. |
| 5980 | * |
| 5981 | * For the parsing, we use a 28 states FSM. |
| 5982 | * |
| 5983 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5984 | * rep->buf->p = beginning of response |
| 5985 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5986 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5987 | * msg->eol = end of current header or line (LF or CRLF) |
| 5988 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5989 | */ |
| 5990 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5991 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5992 | /* There's a protected area at the end of the buffer for rewriting |
| 5993 | * purposes. We don't want to start to parse the request if the |
| 5994 | * protected area is affected, because we may have to move processed |
| 5995 | * data later, which is much more complicated. |
| 5996 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5997 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5998 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5999 | /* some data has still not left the buffer, wake us once that's done */ |
| 6000 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 6001 | goto abort_response; |
| 6002 | channel_dont_close(rep); |
| 6003 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 6004 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 6005 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 6006 | } |
| 6007 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 6008 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 6009 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 6010 | buffer_slow_realign(rep->buf); |
| 6011 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6012 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 6013 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 6014 | } |
| 6015 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6016 | /* 1: we might have to print this header in debug mode */ |
| 6017 | if (unlikely((global.mode & MODE_DEBUG) && |
| 6018 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 6019 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6020 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6021 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6022 | sol = rep->buf->p; |
| 6023 | 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] | 6024 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6025 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6026 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 6027 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6028 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6029 | while (cur_idx) { |
| 6030 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 6031 | debug_hdr("srvhdr", s, sol, eol); |
| 6032 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 6033 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 6034 | } |
| 6035 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6036 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6037 | /* |
| 6038 | * Now we quickly check if we have found a full valid response. |
| 6039 | * If not so, we check the FD and buffer states before leaving. |
| 6040 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6041 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6042 | * responses are checked first. |
| 6043 | * |
| 6044 | * Depending on whether the client is still there or not, we |
| 6045 | * may send an error response back or not. Note that normally |
| 6046 | * we should only check for HTTP status there, and check I/O |
| 6047 | * errors somewhere else. |
| 6048 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6049 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6050 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6051 | /* Invalid response */ |
| 6052 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 6053 | /* we detected a parsing error. We want to archive this response |
| 6054 | * in the dedicated proxy area for later troubleshooting. |
| 6055 | */ |
| 6056 | hdr_response_bad: |
| 6057 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6058 | 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] | 6059 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6060 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6061 | if (objt_server(s->target)) { |
| 6062 | objt_server(s->target)->counters.failed_resp++; |
| 6063 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6064 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 6065 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6066 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6067 | rep->analysers = 0; |
| 6068 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6069 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6070 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6071 | 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] | 6072 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6073 | if (!(s->flags & SF_ERR_MASK)) |
| 6074 | s->flags |= SF_ERR_PRXCOND; |
| 6075 | if (!(s->flags & SF_FINST_MASK)) |
| 6076 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6077 | |
| 6078 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6079 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6080 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6081 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6082 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 6083 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6084 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6085 | goto hdr_response_bad; |
| 6086 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6087 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6088 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6089 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6090 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6091 | 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] | 6092 | else if (txn->flags & TX_NOT_FIRST) |
| 6093 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6094 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6095 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6096 | if (objt_server(s->target)) { |
| 6097 | objt_server(s->target)->counters.failed_resp++; |
| 6098 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6099 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 6100 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6101 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6102 | rep->analysers = 0; |
| 6103 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6104 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6105 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6106 | 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] | 6107 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6108 | if (!(s->flags & SF_ERR_MASK)) |
| 6109 | s->flags |= SF_ERR_SRVCL; |
| 6110 | if (!(s->flags & SF_FINST_MASK)) |
| 6111 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 6112 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6113 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6114 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 6115 | /* read timeout : return a 504 to the client. */ |
| 6116 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6117 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6118 | 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] | 6119 | else if (txn->flags & TX_NOT_FIRST) |
| 6120 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6121 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6122 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6123 | if (objt_server(s->target)) { |
| 6124 | objt_server(s->target)->counters.failed_resp++; |
| 6125 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6126 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6127 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6128 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6129 | rep->analysers = 0; |
| 6130 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6131 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6132 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6133 | 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] | 6134 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6135 | if (!(s->flags & SF_ERR_MASK)) |
| 6136 | s->flags |= SF_ERR_SRVTO; |
| 6137 | if (!(s->flags & SF_FINST_MASK)) |
| 6138 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6139 | return 0; |
| 6140 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 6141 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6142 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6143 | 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] | 6144 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6145 | s->be->be_counters.cli_aborts++; |
| 6146 | if (objt_server(s->target)) |
| 6147 | objt_server(s->target)->counters.cli_aborts++; |
| 6148 | |
| 6149 | rep->analysers = 0; |
| 6150 | channel_auto_close(rep); |
| 6151 | |
| 6152 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6153 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6154 | 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] | 6155 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6156 | if (!(s->flags & SF_ERR_MASK)) |
| 6157 | s->flags |= SF_ERR_CLICL; |
| 6158 | if (!(s->flags & SF_FINST_MASK)) |
| 6159 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6160 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6161 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6162 | return 0; |
| 6163 | } |
| 6164 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6165 | /* 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] | 6166 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6167 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6168 | 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] | 6169 | else if (txn->flags & TX_NOT_FIRST) |
| 6170 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6171 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6172 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6173 | if (objt_server(s->target)) { |
| 6174 | objt_server(s->target)->counters.failed_resp++; |
| 6175 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6176 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6177 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6178 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6179 | rep->analysers = 0; |
| 6180 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6181 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6182 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6183 | 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] | 6184 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6185 | if (!(s->flags & SF_ERR_MASK)) |
| 6186 | s->flags |= SF_ERR_SRVCL; |
| 6187 | if (!(s->flags & SF_FINST_MASK)) |
| 6188 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6189 | return 0; |
| 6190 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6191 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6192 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6193 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6194 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6195 | 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] | 6196 | else if (txn->flags & TX_NOT_FIRST) |
| 6197 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6198 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6199 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6200 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6201 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6202 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6203 | if (!(s->flags & SF_ERR_MASK)) |
| 6204 | s->flags |= SF_ERR_CLICL; |
| 6205 | if (!(s->flags & SF_FINST_MASK)) |
| 6206 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6207 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6208 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6209 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6210 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6211 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6212 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 6213 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6214 | return 0; |
| 6215 | } |
| 6216 | |
| 6217 | /* More interesting part now : we know that we have a complete |
| 6218 | * response which at least looks like HTTP. We have an indicator |
| 6219 | * of each header's length, so we can parse them quickly. |
| 6220 | */ |
| 6221 | |
| 6222 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6223 | 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] | 6224 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6225 | /* |
| 6226 | * 1: get the status code |
| 6227 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6228 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6229 | if (n < 1 || n > 5) |
| 6230 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6231 | /* when the client triggers a 4xx from the server, it's most often due |
| 6232 | * to a missing object or permission. These events should be tracked |
| 6233 | * because if they happen often, it may indicate a brute force or a |
| 6234 | * vulnerability scan. |
| 6235 | */ |
| 6236 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6237 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6238 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6239 | if (objt_server(s->target)) |
| 6240 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6241 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 6242 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 6243 | * exactly one digit "." one digit. This check may be disabled using |
| 6244 | * option accept-invalid-http-response. |
| 6245 | */ |
| 6246 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 6247 | if (msg->sl.st.v_l != 8) { |
| 6248 | msg->err_pos = 0; |
| 6249 | goto hdr_response_bad; |
| 6250 | } |
| 6251 | |
| 6252 | if (rep->buf->p[4] != '/' || |
| 6253 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 6254 | rep->buf->p[6] != '.' || |
| 6255 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 6256 | msg->err_pos = 4; |
| 6257 | goto hdr_response_bad; |
| 6258 | } |
| 6259 | } |
| 6260 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6261 | /* check if the response is HTTP/1.1 or above */ |
| 6262 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6263 | ((rep->buf->p[5] > '1') || |
| 6264 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6265 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6266 | |
| 6267 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 6268 | 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] | 6269 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6270 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6271 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6272 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6273 | 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] | 6274 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 6275 | /* Adjust server's health based on status code. Note: status codes 501 |
| 6276 | * and 505 are triggered on demand by client request, so we must not |
| 6277 | * count them as server failures. |
| 6278 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6279 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6280 | 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] | 6281 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6282 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6283 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6284 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6285 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6286 | /* |
| 6287 | * 2: check for cacheability. |
| 6288 | */ |
| 6289 | |
| 6290 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6291 | case 100: |
| 6292 | /* |
| 6293 | * We may be facing a 100-continue response, in which case this |
| 6294 | * is not the right response, and we're waiting for the next one. |
| 6295 | * Let's allow this response to go to the client and wait for the |
| 6296 | * next one. |
| 6297 | */ |
| 6298 | hdr_idx_init(&txn->hdr_idx); |
| 6299 | msg->next -= channel_forward(rep, msg->next); |
| 6300 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 6301 | txn->status = 0; |
| 6302 | s->logs.t_data = -1; /* was not a response yet */ |
| 6303 | goto next_one; |
| 6304 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6305 | case 200: |
| 6306 | case 203: |
| 6307 | case 206: |
| 6308 | case 300: |
| 6309 | case 301: |
| 6310 | case 410: |
| 6311 | /* RFC2616 @13.4: |
| 6312 | * "A response received with a status code of |
| 6313 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 6314 | * by a cache (...) unless a cache-control |
| 6315 | * directive prohibits caching." |
| 6316 | * |
| 6317 | * RFC2616 @9.5: POST method : |
| 6318 | * "Responses to this method are not cacheable, |
| 6319 | * unless the response includes appropriate |
| 6320 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6321 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6322 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6323 | ((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] | 6324 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 6325 | break; |
| 6326 | default: |
| 6327 | break; |
| 6328 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6329 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6330 | /* |
| 6331 | * 3: we may need to capture headers |
| 6332 | */ |
| 6333 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6334 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6335 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6336 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6337 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6338 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 6339 | * by RFC7230#3.3.3 : |
| 6340 | * |
| 6341 | * The length of a message body is determined by one of the following |
| 6342 | * (in order of precedence): |
| 6343 | * |
| 6344 | * 1. Any response to a HEAD request and any response with a 1xx |
| 6345 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 6346 | * code is always terminated by the first empty line after the |
| 6347 | * header fields, regardless of the header fields present in the |
| 6348 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6349 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6350 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 6351 | * the connection will become a tunnel immediately after the empty |
| 6352 | * line that concludes the header fields. A client MUST ignore any |
| 6353 | * Content-Length or Transfer-Encoding header fields received in |
| 6354 | * such a message. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6355 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6356 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 6357 | * transfer coding (Section 4.1) is the final encoding, the message |
| 6358 | * body length is determined by reading and decoding the chunked |
| 6359 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6360 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6361 | * If a Transfer-Encoding header field is present in a response and |
| 6362 | * the chunked transfer coding is not the final encoding, the |
| 6363 | * message body length is determined by reading the connection until |
| 6364 | * it is closed by the server. If a Transfer-Encoding header field |
| 6365 | * is present in a request and the chunked transfer coding is not |
| 6366 | * the final encoding, the message body length cannot be determined |
| 6367 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 6368 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6369 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6370 | * If a message is received with both a Transfer-Encoding and a |
| 6371 | * Content-Length header field, the Transfer-Encoding overrides the |
| 6372 | * Content-Length. Such a message might indicate an attempt to |
| 6373 | * perform request smuggling (Section 9.5) or response splitting |
| 6374 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 6375 | * remove the received Content-Length field prior to forwarding such |
| 6376 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6377 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6378 | * 4. If a message is received without Transfer-Encoding and with |
| 6379 | * either multiple Content-Length header fields having differing |
| 6380 | * field-values or a single Content-Length header field having an |
| 6381 | * invalid value, then the message framing is invalid and the |
| 6382 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 6383 | * request message, the server MUST respond with a 400 (Bad Request) |
| 6384 | * status code and then close the connection. If this is a response |
| 6385 | * message received by a proxy, the proxy MUST close the connection |
| 6386 | * to the server, discard the received response, and send a 502 (Bad |
| 6387 | * Gateway) response to the client. If this is a response message |
| 6388 | * received by a user agent, the user agent MUST close the |
| 6389 | * connection to the server and discard the received response. |
| 6390 | * |
| 6391 | * 5. If a valid Content-Length header field is present without |
| 6392 | * Transfer-Encoding, its decimal value defines the expected message |
| 6393 | * body length in octets. If the sender closes the connection or |
| 6394 | * the recipient times out before the indicated number of octets are |
| 6395 | * received, the recipient MUST consider the message to be |
| 6396 | * incomplete and close the connection. |
| 6397 | * |
| 6398 | * 6. If this is a request message and none of the above are true, then |
| 6399 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6400 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6401 | * 7. Otherwise, this is a response message without a declared message |
| 6402 | * body length, so the message body length is determined by the |
| 6403 | * number of octets received prior to the server closing the |
| 6404 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6405 | */ |
| 6406 | |
| 6407 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6408 | * 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] | 6409 | * 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] | 6410 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6411 | */ |
| 6412 | if (txn->meth == HTTP_METH_HEAD || |
| 6413 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6414 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6415 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 6416 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6417 | goto skip_content_length; |
| 6418 | } |
| 6419 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6420 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6421 | ctx.idx = 0; |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 6422 | 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] | 6423 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6424 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6425 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6426 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6427 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6428 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6429 | break; |
| 6430 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6431 | } |
| 6432 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6433 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6434 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6435 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6436 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 6437 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 6438 | } |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6439 | 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] | 6440 | signed long long cl; |
| 6441 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6442 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6443 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6444 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6445 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6446 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6447 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6448 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6449 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6450 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6451 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6452 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6453 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6454 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6455 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6456 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6457 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6458 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6459 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6460 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6461 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6462 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6463 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6464 | } |
| 6465 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6466 | if (sess->fe->comp || s->be->comp) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6467 | select_compression_response_header(s, rep->buf); |
| 6468 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6469 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6470 | /* Now we have to check if we need to modify the Connection header. |
| 6471 | * This is more difficult on the response than it is on the request, |
| 6472 | * because we can have two different HTTP versions and we don't know |
| 6473 | * how the client will interprete a response. For instance, let's say |
| 6474 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6475 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6476 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6477 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6478 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6479 | * indicating how a request MAY be understood by the client. In case |
| 6480 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6481 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6482 | * will fall back to explicit close. Note that we won't take risks with |
| 6483 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6484 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6485 | */ |
| 6486 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6487 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6488 | txn->status == 101)) { |
| 6489 | /* Either we've established an explicit tunnel, or we're |
| 6490 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6491 | * to understand the next protocols. We have to switch to tunnel |
| 6492 | * mode, so that we transfer the request and responses then let |
| 6493 | * this protocol pass unmodified. When we later implement specific |
| 6494 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6495 | * header which contains information about that protocol for |
| 6496 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6497 | */ |
| 6498 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6499 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6500 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6501 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6502 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6503 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6504 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6505 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6506 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6507 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6508 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6509 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6510 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6511 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6512 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6513 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6514 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6515 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6516 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6517 | /* now adjust header transformations depending on current state */ |
| 6518 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6519 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6520 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6521 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6522 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6523 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6524 | else { /* SCL / KAL */ |
| 6525 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6526 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6527 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6528 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6529 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6530 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6531 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6532 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6533 | /* Some keep-alive responses are converted to Server-close if |
| 6534 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6535 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6536 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6537 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6538 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6539 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6540 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6541 | } |
| 6542 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6543 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6544 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6545 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6546 | /* end of job, return OK */ |
| 6547 | rep->analysers &= ~an_bit; |
| 6548 | rep->analyse_exp = TICK_ETERNITY; |
| 6549 | channel_auto_close(rep); |
| 6550 | return 1; |
| 6551 | |
| 6552 | abort_keep_alive: |
| 6553 | /* A keep-alive request to the server failed on a network error. |
| 6554 | * The client is required to retry. We need to close without returning |
| 6555 | * any other information so that the client retries. |
| 6556 | */ |
| 6557 | txn->status = 0; |
| 6558 | rep->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6559 | s->req.analysers = 0; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6560 | channel_auto_close(rep); |
| 6561 | s->logs.logwait = 0; |
| 6562 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6563 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6564 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6565 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6566 | return 0; |
| 6567 | } |
| 6568 | |
| 6569 | /* This function performs all the processing enabled for the current response. |
| 6570 | * 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] | 6571 | * 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] | 6572 | * other functions. It works like process_request (see indications above). |
| 6573 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6574 | 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] | 6575 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6576 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6577 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6578 | struct http_msg *msg = &txn->rsp; |
| 6579 | struct proxy *cur_proxy; |
| 6580 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 6581 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6582 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6583 | 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] | 6584 | now_ms, __FUNCTION__, |
| 6585 | s, |
| 6586 | rep, |
| 6587 | rep->rex, rep->wex, |
| 6588 | rep->flags, |
| 6589 | rep->buf->i, |
| 6590 | rep->analysers); |
| 6591 | |
| 6592 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6593 | return 0; |
| 6594 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6595 | /* The stats applet needs to adjust the Connection header but we don't |
| 6596 | * apply any filter there. |
| 6597 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6598 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 6599 | rep->analysers &= ~an_bit; |
| 6600 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6601 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6602 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6603 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6604 | /* |
| 6605 | * We will have to evaluate the filters. |
| 6606 | * As opposed to version 1.2, now they will be evaluated in the |
| 6607 | * filters order and not in the header order. This means that |
| 6608 | * each filter has to be validated among all headers. |
| 6609 | * |
| 6610 | * Filters are tried with ->be first, then with ->fe if it is |
| 6611 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6612 | * |
| 6613 | * Maybe we are in resume condiion. In this case I choose the |
| 6614 | * "struct proxy" which contains the rule list matching the resume |
| 6615 | * pointer. If none of theses "struct proxy" match, I initialise |
| 6616 | * the process with the first one. |
| 6617 | * |
| 6618 | * In fact, I check only correspondance betwwen the current list |
| 6619 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 6620 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6621 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6622 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 6623 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6624 | else |
| 6625 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6626 | while (1) { |
| 6627 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6628 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6629 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6630 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 6631 | 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] | 6632 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6633 | if (ret == HTTP_RULE_RES_BADREQ) |
| 6634 | goto return_srv_prx_502; |
| 6635 | |
| 6636 | if (ret == HTTP_RULE_RES_DONE) { |
| 6637 | rep->analysers &= ~an_bit; |
| 6638 | rep->analyse_exp = TICK_ETERNITY; |
| 6639 | return 1; |
| 6640 | } |
| 6641 | } |
| 6642 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6643 | /* we need to be called again. */ |
| 6644 | if (ret == HTTP_RULE_RES_YIELD) { |
| 6645 | channel_dont_close(rep); |
| 6646 | return 0; |
| 6647 | } |
| 6648 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6649 | /* try headers filters */ |
| 6650 | if (rule_set->rsp_exp != NULL) { |
| 6651 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6652 | return_bad_resp: |
| 6653 | if (objt_server(s->target)) { |
| 6654 | objt_server(s->target)->counters.failed_resp++; |
| 6655 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6656 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6657 | s->be->be_counters.failed_resp++; |
| 6658 | return_srv_prx_502: |
| 6659 | rep->analysers = 0; |
| 6660 | txn->status = 502; |
| 6661 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6662 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6663 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6664 | 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] | 6665 | if (!(s->flags & SF_ERR_MASK)) |
| 6666 | s->flags |= SF_ERR_PRXCOND; |
| 6667 | if (!(s->flags & SF_FINST_MASK)) |
| 6668 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6669 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6670 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6671 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6672 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6673 | /* has the response been denied ? */ |
| 6674 | if (txn->flags & TX_SVDENY) { |
| 6675 | if (objt_server(s->target)) |
| 6676 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6677 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6678 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6679 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6680 | if (sess->listener->counters) |
| 6681 | sess->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6682 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6683 | goto return_srv_prx_502; |
| 6684 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6685 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6686 | /* add response headers from the rule sets in the same order */ |
| 6687 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6688 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6689 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6690 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6691 | 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] | 6692 | ret = acl_pass(ret); |
| 6693 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6694 | ret = !ret; |
| 6695 | if (!ret) |
| 6696 | continue; |
| 6697 | } |
| 6698 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6699 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6700 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6701 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6702 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6703 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6704 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6705 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6706 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6707 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6708 | /* After this point, this anayzer can't return yield, so we can |
| 6709 | * remove the bit corresponding to this analyzer from the list. |
| 6710 | * |
| 6711 | * Note that the intermediate returns and goto found previously |
| 6712 | * reset the analyzers. |
| 6713 | */ |
| 6714 | rep->analysers &= ~an_bit; |
| 6715 | rep->analyse_exp = TICK_ETERNITY; |
| 6716 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6717 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6718 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6719 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6720 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6721 | /* |
| 6722 | * Now check for a server cookie. |
| 6723 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 6724 | 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] | 6725 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6726 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6727 | /* |
| 6728 | * Check for cache-control or pragma headers if required. |
| 6729 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6730 | 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] | 6731 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6732 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6733 | /* |
| 6734 | * Add server cookie in the response if needed |
| 6735 | */ |
| 6736 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6737 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6738 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6739 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6740 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6741 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6742 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6743 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6744 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6745 | /* the server is known, it's not the one the client requested, or the |
| 6746 | * cookie's last seen date needs to be refreshed. We have to |
| 6747 | * insert a set-cookie here, except if we want to insert only on POST |
| 6748 | * requests and this one isn't. Note that servers which don't have cookies |
| 6749 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6750 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6751 | if (!objt_server(s->target)->cookie) { |
| 6752 | chunk_printf(&trash, |
| 6753 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6754 | s->be->cookie_name); |
| 6755 | } |
| 6756 | else { |
| 6757 | 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] | 6758 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6759 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6760 | /* emit last_date, which is mandatory */ |
| 6761 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6762 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6763 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6764 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6765 | if (s->be->cookie_maxlife) { |
| 6766 | /* emit first_date, which is either the original one or |
| 6767 | * the current date. |
| 6768 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6769 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6770 | s30tob64(txn->cookie_first_date ? |
| 6771 | txn->cookie_first_date >> 2 : |
| 6772 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6773 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6774 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6775 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6776 | chunk_appendf(&trash, "; path=/"); |
| 6777 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6778 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6779 | if (s->be->cookie_domain) |
| 6780 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6781 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6782 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6783 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6784 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6785 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6786 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6787 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6788 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6789 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6790 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6791 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6792 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6793 | /* the server did not change, only the date was updated */ |
| 6794 | txn->flags |= TX_SCK_UPDATED; |
| 6795 | else |
| 6796 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6797 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6798 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6799 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6800 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6801 | * 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] | 6802 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6803 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6804 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6805 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6806 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6807 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6808 | "Cache-control: private", 22) < 0)) |
| 6809 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6810 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6811 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6812 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6813 | /* |
| 6814 | * Check if result will be cacheable with a cookie. |
| 6815 | * We'll block the response if security checks have caught |
| 6816 | * nasty things such as a cacheable cookie. |
| 6817 | */ |
| 6818 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6819 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6820 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6821 | /* we're in presence of a cacheable response containing |
| 6822 | * a set-cookie header. We'll block it as requested by |
| 6823 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6824 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6825 | if (objt_server(s->target)) |
| 6826 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6827 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6828 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6829 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6830 | if (sess->listener->counters) |
| 6831 | sess->listener->counters->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6832 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6833 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6834 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6835 | send_log(s->be, LOG_ALERT, |
| 6836 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6837 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6838 | goto return_srv_prx_502; |
| 6839 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6840 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6841 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6842 | /* |
| 6843 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6844 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6845 | * 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] | 6846 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6847 | * want to touch any 101 response either since it's switching to another |
| 6848 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6849 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6850 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6851 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6852 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6853 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6854 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6855 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6856 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6857 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6858 | /* we want a keep-alive response here. Keep-alive header |
| 6859 | * required if either side is not 1.1. |
| 6860 | */ |
| 6861 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6862 | want_flags |= TX_CON_KAL_SET; |
| 6863 | } |
| 6864 | else { |
| 6865 | /* we want a close response here. Close header required if |
| 6866 | * the server is 1.1, regardless of the client. |
| 6867 | */ |
| 6868 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6869 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6870 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6871 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6872 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6873 | http_change_connection_header(txn, msg, want_flags); |
| 6874 | } |
| 6875 | |
| 6876 | skip_header_mangling: |
| 6877 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
| 6878 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 6879 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6880 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6881 | /* if the user wants to log as soon as possible, without counting |
| 6882 | * bytes from the server, then this is the right moment. We have |
| 6883 | * to temporarily assign bytes_out to log what we currently have. |
| 6884 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6885 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6886 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6887 | s->logs.bytes_out = txn->rsp.eoh; |
| 6888 | s->do_log(s); |
| 6889 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6890 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6891 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6892 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6893 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6894 | /* This function is an analyser which forwards response body (including chunk |
| 6895 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6896 | * zero byte. The only situation where it must not be called is when we're in |
| 6897 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6898 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6899 | * 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] | 6900 | * 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] | 6901 | * |
| 6902 | * It is capable of compressing response data both in content-length mode and |
| 6903 | * in chunked mode. The state machines follows different flows depending on |
| 6904 | * whether content-length and chunked modes are used, since there are no |
| 6905 | * trailers in content-length : |
| 6906 | * |
| 6907 | * chk-mode cl-mode |
| 6908 | * ,----- BODY -----. |
| 6909 | * / \ |
| 6910 | * V size > 0 V chk-mode |
| 6911 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6912 | * | | size == 0 | last byte | |
| 6913 | * | v final crlf v inspected | |
| 6914 | * | TRAILERS -----------> DONE | |
| 6915 | * | | |
| 6916 | * `----------------------------------------------' |
| 6917 | * |
| 6918 | * Compression only happens in the DATA state, and must be flushed in final |
| 6919 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6920 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6921 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6922 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6923 | 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] | 6924 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6925 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6926 | struct http_txn *txn = s->txn; |
| 6927 | struct http_msg *msg = &s->txn->rsp; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6928 | static struct buffer *tmpbuf = &buf_empty; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6929 | int compressing = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6930 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6931 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6932 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6933 | return 0; |
| 6934 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6935 | 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] | 6936 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6937 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6938 | /* Output closed while we were sending data. We must abort and |
| 6939 | * wake the other side up. |
| 6940 | */ |
| 6941 | msg->msg_state = HTTP_MSG_ERROR; |
| 6942 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6943 | return 1; |
| 6944 | } |
| 6945 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6946 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6947 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6948 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 6949 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6950 | /* we have msg->sov which points to the first byte of message |
| 6951 | * body, and res->buf.p still points to the beginning of the |
| 6952 | * message. We forward the headers now, as we don't need them |
| 6953 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6954 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6955 | b_adv(res->buf, msg->sov); |
| 6956 | msg->next -= msg->sov; |
| 6957 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6958 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6959 | /* The previous analysers guarantee that the state is somewhere |
| 6960 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 6961 | * msg->next are always correct. |
| 6962 | */ |
| 6963 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 6964 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 6965 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6966 | else |
| 6967 | msg->msg_state = HTTP_MSG_DATA; |
| 6968 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6969 | } |
| 6970 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6971 | if (res->to_forward) { |
| 6972 | /* We can't process the buffer's contents yet */ |
| 6973 | res->flags |= CF_WAKE_WRITE; |
| 6974 | goto missing_data; |
| 6975 | } |
| 6976 | |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6977 | if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) { |
| 6978 | /* We need a compression buffer in the DATA state to put the |
| 6979 | * output of compressed data, and in CRLF state to let the |
| 6980 | * TRAILERS state finish the job of removing the trailing CRLF. |
| 6981 | */ |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6982 | if (unlikely(!tmpbuf->size)) { |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6983 | /* this is the first time we need the compression buffer */ |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 6984 | if (b_alloc(&tmpbuf) == NULL) |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6985 | goto aborted_xfer; /* no memory */ |
| 6986 | } |
| 6987 | |
| 6988 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6989 | if (ret < 0) { |
| 6990 | res->flags |= CF_WAKE_WRITE; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6991 | goto missing_data; /* not enough spaces in buffers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6992 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6993 | compressing = 1; |
| 6994 | } |
| 6995 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6996 | while (1) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6997 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6998 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6999 | /* we may have some pending data starting at res->buf->p */ |
| 7000 | if (unlikely(s->comp_algo)) { |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 7001 | ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 7002 | if (ret < 0) |
| 7003 | goto aborted_xfer; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7004 | |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 7005 | if (msg->chunk_len) { |
| 7006 | /* input empty or output full */ |
| 7007 | if (res->buf->i > msg->next) |
| 7008 | res->flags |= CF_WAKE_WRITE; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7009 | goto missing_data; |
| 7010 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 7011 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7012 | else { |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 7013 | if (msg->chunk_len > res->buf->i - msg->next) { |
| 7014 | /* output full */ |
| 7015 | res->flags |= CF_WAKE_WRITE; |
| 7016 | goto missing_data; |
| 7017 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7018 | msg->next += msg->chunk_len; |
| 7019 | msg->chunk_len = 0; |
| 7020 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 7021 | |
| 7022 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 7023 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 7024 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 7025 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 7026 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7027 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 7028 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7029 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 7030 | |
| 7031 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 7032 | /* we want the CRLF after the data */ |
| 7033 | |
| 7034 | ret = http_skip_chunk_crlf(msg); |
| 7035 | if (ret == 0) |
| 7036 | goto missing_data; |
| 7037 | else if (ret < 0) { |
| 7038 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7039 | 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] | 7040 | goto return_bad_res; |
| 7041 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7042 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 7043 | |
| 7044 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 7045 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 7046 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 7047 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7048 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7049 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7050 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 7051 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7052 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 7053 | else if (ret < 0) { |
| 7054 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7055 | 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] | 7056 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 7057 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 7058 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7059 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 7060 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7061 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7062 | if (unlikely(compressing)) { |
| 7063 | /* we need to flush output contents before syncing FSMs */ |
| 7064 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 7065 | compressing = 0; |
| 7066 | } |
| 7067 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7068 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7069 | if (ret == 0) |
| 7070 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 7071 | else if (ret < 0) { |
| 7072 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7073 | 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] | 7074 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 7075 | } |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7076 | /* we're in HTTP_MSG_DONE now, fall through */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7077 | |
| 7078 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7079 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7080 | if (unlikely(compressing)) { |
| 7081 | /* we need to flush output contents before syncing FSMs */ |
| 7082 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 7083 | compressing = 0; |
| 7084 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7085 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7086 | /* we may have some pending data starting at res->buf->p |
| 7087 | * such as a last chunk of data or trailers. |
| 7088 | */ |
| 7089 | b_adv(res->buf, msg->next); |
| 7090 | msg->next = 0; |
| 7091 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7092 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 7093 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7094 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 7095 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 7096 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 7097 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7098 | if (http_resync_states(s)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7099 | /* some state changes occurred, maybe the analyser |
| 7100 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 7101 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 7102 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7103 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 7104 | /* response errors are most likely due to |
| 7105 | * the client aborting the transfer. |
| 7106 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7107 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 7108 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 7109 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7110 | 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] | 7111 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 7112 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7113 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 7114 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7115 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7116 | } |
| 7117 | } |
| 7118 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7119 | missing_data: |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7120 | /* we may have some pending data starting at res->buf->p */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7121 | if (unlikely(compressing)) { |
| 7122 | 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] | 7123 | compressing = 0; |
| 7124 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 7125 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7126 | if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) { |
| 7127 | b_adv(res->buf, msg->next); |
| 7128 | msg->next = 0; |
| 7129 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 7130 | } |
| 7131 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 7132 | if (res->flags & CF_SHUTW) |
| 7133 | goto aborted_xfer; |
| 7134 | |
| 7135 | /* stop waiting for data if the input is closed before the end. If the |
| 7136 | * client side was already closed, it means that the client has aborted, |
| 7137 | * so we don't want to count this as a server abort. Otherwise it's a |
| 7138 | * server abort. |
| 7139 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7140 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7141 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 7142 | goto aborted_xfer; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7143 | if (!(s->flags & SF_ERR_MASK)) |
| 7144 | s->flags |= SF_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7145 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7146 | if (objt_server(s->target)) |
| 7147 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7148 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7149 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 7150 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7151 | /* 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] | 7152 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7153 | goto return_bad_res; |
| 7154 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7155 | /* 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] | 7156 | * 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] | 7157 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 7158 | * close. |
| 7159 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 7160 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7161 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 7162 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 7163 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7164 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7165 | /* 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] | 7166 | * 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] | 7167 | * 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] | 7168 | * modes are already handled by the stream sock layer. We must not do |
| 7169 | * this in content-length mode because it could present the MSG_MORE |
| 7170 | * flag with the last block of forwarded data, which would cause an |
| 7171 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7172 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 7173 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7174 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7175 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7176 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7177 | return 0; |
| 7178 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7179 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7180 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7181 | if (objt_server(s->target)) |
| 7182 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7183 | |
| 7184 | return_bad_res_stats_ok: |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 7185 | if (unlikely(compressing)) { |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7186 | 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] | 7187 | compressing = 0; |
| 7188 | } |
| 7189 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7190 | /* we may have some pending data starting at res->buf->p */ |
| 7191 | if (s->comp_algo == NULL) { |
| 7192 | b_adv(res->buf, msg->next); |
| 7193 | msg->next = 0; |
| 7194 | } |
| 7195 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7196 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 7197 | /* don't send any error message as we're in the body */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 7198 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7199 | res->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7200 | 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] | 7201 | if (objt_server(s->target)) |
| 7202 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7203 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7204 | if (!(s->flags & SF_ERR_MASK)) |
| 7205 | s->flags |= SF_ERR_PRXCOND; |
| 7206 | if (!(s->flags & SF_FINST_MASK)) |
| 7207 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7208 | return 0; |
| 7209 | |
| 7210 | aborted_xfer: |
Willy Tarreau | 6fef8ae | 2014-04-22 21:22:06 +0200 | [diff] [blame] | 7211 | if (unlikely(compressing)) { |
| 7212 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
| 7213 | compressing = 0; |
| 7214 | } |
| 7215 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7216 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 7217 | /* don't send any error message as we're in the body */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 7218 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7219 | res->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7220 | 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] | 7221 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7222 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7223 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7224 | if (objt_server(s->target)) |
| 7225 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7226 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7227 | if (!(s->flags & SF_ERR_MASK)) |
| 7228 | s->flags |= SF_ERR_CLICL; |
| 7229 | if (!(s->flags & SF_FINST_MASK)) |
| 7230 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7231 | return 0; |
| 7232 | } |
| 7233 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7234 | /* Iterate the same filter through all request headers. |
| 7235 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7236 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7237 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7238 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7239 | 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] | 7240 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7241 | char *cur_ptr, *cur_end, *cur_next; |
| 7242 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7243 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7244 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7245 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 7246 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7247 | last_hdr = 0; |
| 7248 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7249 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7250 | old_idx = 0; |
| 7251 | |
| 7252 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7253 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7254 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7255 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7256 | (exp->action == ACT_ALLOW || |
| 7257 | exp->action == ACT_DENY || |
| 7258 | exp->action == ACT_TARPIT)) |
| 7259 | return 0; |
| 7260 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7261 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7262 | if (!cur_idx) |
| 7263 | break; |
| 7264 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7265 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7266 | cur_ptr = cur_next; |
| 7267 | cur_end = cur_ptr + cur_hdr->len; |
| 7268 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7269 | |
| 7270 | /* Now we have one header between cur_ptr and cur_end, |
| 7271 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7272 | */ |
| 7273 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7274 | 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] | 7275 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7276 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7277 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7278 | last_hdr = 1; |
| 7279 | break; |
| 7280 | |
| 7281 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7282 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7283 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7284 | break; |
| 7285 | |
| 7286 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7287 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7288 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7289 | break; |
| 7290 | |
| 7291 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7292 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7293 | if (trash.len < 0) |
| 7294 | return -1; |
| 7295 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7296 | 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] | 7297 | /* FIXME: if the user adds a newline in the replacement, the |
| 7298 | * index will not be recalculated for now, and the new line |
| 7299 | * will not be counted as a new header. |
| 7300 | */ |
| 7301 | |
| 7302 | cur_end += delta; |
| 7303 | cur_next += delta; |
| 7304 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7305 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7306 | break; |
| 7307 | |
| 7308 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7309 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7310 | cur_next += delta; |
| 7311 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7312 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7313 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7314 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7315 | cur_hdr->len = 0; |
| 7316 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7317 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7318 | break; |
| 7319 | |
| 7320 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7321 | } |
| 7322 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7323 | /* keep the link from this header to next one in case of later |
| 7324 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7325 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7326 | old_idx = cur_idx; |
| 7327 | } |
| 7328 | return 0; |
| 7329 | } |
| 7330 | |
| 7331 | |
| 7332 | /* Apply the filter to the request line. |
| 7333 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7334 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7335 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7336 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7337 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7338 | 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] | 7339 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7340 | char *cur_ptr, *cur_end; |
| 7341 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7342 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7343 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7344 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7345 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7346 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7347 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7348 | (exp->action == ACT_ALLOW || |
| 7349 | exp->action == ACT_DENY || |
| 7350 | exp->action == ACT_TARPIT)) |
| 7351 | return 0; |
| 7352 | else if (exp->action == ACT_REMOVE) |
| 7353 | return 0; |
| 7354 | |
| 7355 | done = 0; |
| 7356 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7357 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7358 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7359 | |
| 7360 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7361 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7362 | 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] | 7363 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7364 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7365 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7366 | done = 1; |
| 7367 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7368 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7369 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7370 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7371 | done = 1; |
| 7372 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7373 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7374 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7375 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7376 | done = 1; |
| 7377 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7378 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7379 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7380 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7381 | if (trash.len < 0) |
| 7382 | return -1; |
| 7383 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7384 | 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] | 7385 | /* FIXME: if the user adds a newline in the replacement, the |
| 7386 | * index will not be recalculated for now, and the new line |
| 7387 | * will not be counted as a new header. |
| 7388 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7389 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7390 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7391 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7392 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7393 | HTTP_MSG_RQMETH, |
| 7394 | cur_ptr, cur_end + 1, |
| 7395 | NULL, NULL); |
| 7396 | if (unlikely(!cur_end)) |
| 7397 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7398 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7399 | /* we have a full request and we know that we have either a CR |
| 7400 | * or an LF at <ptr>. |
| 7401 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7402 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7403 | 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] | 7404 | /* there is no point trying this regex on headers */ |
| 7405 | return 1; |
| 7406 | } |
| 7407 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7408 | return done; |
| 7409 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7410 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7411 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7412 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7413 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7414 | * 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] | 7415 | * 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] | 7416 | * unparsable request. Since it can manage the switch to another backend, it |
| 7417 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7418 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7419 | 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] | 7420 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7421 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7422 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7423 | struct hdr_exp *exp; |
| 7424 | |
| 7425 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7426 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7427 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7428 | /* |
| 7429 | * The interleaving of transformations and verdicts |
| 7430 | * makes it difficult to decide to continue or stop |
| 7431 | * the evaluation. |
| 7432 | */ |
| 7433 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7434 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7435 | break; |
| 7436 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7437 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7438 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7439 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7440 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7441 | |
| 7442 | /* if this filter had a condition, evaluate it now and skip to |
| 7443 | * next filter if the condition does not match. |
| 7444 | */ |
| 7445 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7446 | 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] | 7447 | ret = acl_pass(ret); |
| 7448 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7449 | ret = !ret; |
| 7450 | |
| 7451 | if (!ret) |
| 7452 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7453 | } |
| 7454 | |
| 7455 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7456 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7457 | if (unlikely(ret < 0)) |
| 7458 | return -1; |
| 7459 | |
| 7460 | if (likely(ret == 0)) { |
| 7461 | /* The filter did not match the request, it can be |
| 7462 | * iterated through all headers. |
| 7463 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7464 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7465 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7466 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7467 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7468 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7469 | } |
| 7470 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7471 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7472 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7473 | * same way as with headers above except that the semi-colon also ends a token. |
| 7474 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7475 | * return a valid result. |
| 7476 | */ |
| 7477 | char *find_cookie_value_end(char *s, const char *e) |
| 7478 | { |
| 7479 | int quoted, qdpair; |
| 7480 | |
| 7481 | quoted = qdpair = 0; |
| 7482 | for (; s < e; s++) { |
| 7483 | if (qdpair) qdpair = 0; |
| 7484 | else if (quoted) { |
| 7485 | if (*s == '\\') qdpair = 1; |
| 7486 | else if (*s == '"') quoted = 0; |
| 7487 | } |
| 7488 | else if (*s == '"') quoted = 1; |
| 7489 | else if (*s == ',' || *s == ';') return s; |
| 7490 | } |
| 7491 | return s; |
| 7492 | } |
| 7493 | |
| 7494 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7495 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7496 | * the first delimiter is updated if required. The function tries as much as |
| 7497 | * possible to respect the following principles : |
| 7498 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7499 | * colon, in which case <next> is simply removed |
| 7500 | * - set exactly one space character after the new first delimiter, unless |
| 7501 | * there are not enough characters in the block being moved to do so. |
| 7502 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7503 | * one. |
| 7504 | * |
| 7505 | * It is the caller's responsibility to ensure that : |
| 7506 | * - <from> points to a valid delimiter or the colon ; |
| 7507 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7508 | * - there are non-space chars before <from> ; |
| 7509 | * - there is a CR/LF at or after <next>. |
| 7510 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7511 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7512 | { |
| 7513 | char *prev = *from; |
| 7514 | |
| 7515 | if (*prev == ':') { |
| 7516 | /* We're removing the first value, preserve the colon and add a |
| 7517 | * space if possible. |
| 7518 | */ |
| 7519 | if (!http_is_crlf[(unsigned char)*next]) |
| 7520 | next++; |
| 7521 | prev++; |
| 7522 | if (prev < next) |
| 7523 | *prev++ = ' '; |
| 7524 | |
| 7525 | while (http_is_spht[(unsigned char)*next]) |
| 7526 | next++; |
| 7527 | } else { |
| 7528 | /* Remove useless spaces before the old delimiter. */ |
| 7529 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 7530 | prev--; |
| 7531 | *from = prev; |
| 7532 | |
| 7533 | /* copy the delimiter and if possible a space if we're |
| 7534 | * not at the end of the line. |
| 7535 | */ |
| 7536 | if (!http_is_crlf[(unsigned char)*next]) { |
| 7537 | *prev++ = *next++; |
| 7538 | if (prev + 1 < next) |
| 7539 | *prev++ = ' '; |
| 7540 | while (http_is_spht[(unsigned char)*next]) |
| 7541 | next++; |
| 7542 | } |
| 7543 | } |
| 7544 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7545 | } |
| 7546 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7547 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7548 | * 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] | 7549 | * desirable to call it only when needed. This code is quite complex because |
| 7550 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7551 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7552 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7553 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7554 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7555 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7556 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7557 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7558 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7559 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7560 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7561 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7562 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7563 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7564 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
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 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7567 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7568 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7569 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7570 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7571 | hdr_beg = hdr_next; |
| 7572 | hdr_end = hdr_beg + cur_hdr->len; |
| 7573 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7574 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7575 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7576 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7577 | * "Cookie:" headers. |
| 7578 | */ |
| 7579 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7580 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7581 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7582 | old_idx = cur_idx; |
| 7583 | continue; |
| 7584 | } |
| 7585 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7586 | del_from = NULL; /* nothing to be deleted */ |
| 7587 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7588 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7589 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7590 | * attributes whose name begin with a '$', and associate them with |
| 7591 | * the right cookie, if we want to delete this cookie. |
| 7592 | * So there are 3 cases for each cookie read : |
| 7593 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7594 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7595 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7596 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7597 | * "special" cookie. |
| 7598 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7599 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7600 | * *MUST* delete it. |
| 7601 | * |
| 7602 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7603 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7604 | * the RFC explicitly allows spaces before it, and not within the |
| 7605 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7606 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7607 | * after the equal sign too, resulting in some (rare) buggy |
| 7608 | * implementations trying to do that. So let's do what servers do. |
| 7609 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7610 | * allowed quoted strings in values, with any possible character |
| 7611 | * after a backslash, including control chars and delimitors, which |
| 7612 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7613 | * within values even without quotes. |
| 7614 | * |
| 7615 | * We have to keep multiple pointers in order to support cookie |
| 7616 | * removal at the beginning, middle or end of header without |
| 7617 | * corrupting the header. All of these headers are valid : |
| 7618 | * |
| 7619 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7620 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7621 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7622 | * | | | | | | | | | |
| 7623 | * | | | | | | | | hdr_end <--+ |
| 7624 | * | | | | | | | +--> next |
| 7625 | * | | | | | | +----> val_end |
| 7626 | * | | | | | +-----------> val_beg |
| 7627 | * | | | | +--------------> equal |
| 7628 | * | | | +----------------> att_end |
| 7629 | * | | +---------------------> att_beg |
| 7630 | * | +--------------------------> prev |
| 7631 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7632 | */ |
| 7633 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7634 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7635 | /* Iterate through all cookies on this line */ |
| 7636 | |
| 7637 | /* find att_beg */ |
| 7638 | att_beg = prev + 1; |
| 7639 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7640 | att_beg++; |
| 7641 | |
| 7642 | /* find att_end : this is the first character after the last non |
| 7643 | * space before the equal. It may be equal to hdr_end. |
| 7644 | */ |
| 7645 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7646 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7647 | while (equal < hdr_end) { |
| 7648 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7649 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7650 | if (http_is_spht[(unsigned char)*equal++]) |
| 7651 | continue; |
| 7652 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7653 | } |
| 7654 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7655 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7656 | * is between <att_beg> and <equal>, both may be identical. |
| 7657 | */ |
| 7658 | |
| 7659 | /* look for end of cookie if there is an equal sign */ |
| 7660 | if (equal < hdr_end && *equal == '=') { |
| 7661 | /* look for the beginning of the value */ |
| 7662 | val_beg = equal + 1; |
| 7663 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7664 | val_beg++; |
| 7665 | |
| 7666 | /* find the end of the value, respecting quotes */ |
| 7667 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7668 | |
| 7669 | /* make val_end point to the first white space or delimitor after the value */ |
| 7670 | val_end = next; |
| 7671 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7672 | val_end--; |
| 7673 | } else { |
| 7674 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7675 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7676 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7677 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7678 | * they will automatically be removed if a header before them is removed, |
| 7679 | * since they're supposed to be linked together. |
| 7680 | */ |
| 7681 | if (*att_beg == '$') |
| 7682 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7683 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7684 | /* Ignore cookies with no equal sign */ |
| 7685 | if (equal == next) { |
| 7686 | /* This is not our cookie, so we must preserve it. But if we already |
| 7687 | * scheduled another cookie for removal, we cannot remove the |
| 7688 | * complete header, but we can remove the previous block itself. |
| 7689 | */ |
| 7690 | preserve_hdr = 1; |
| 7691 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7692 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7693 | val_end += delta; |
| 7694 | next += delta; |
| 7695 | hdr_end += delta; |
| 7696 | hdr_next += delta; |
| 7697 | cur_hdr->len += delta; |
| 7698 | http_msg_move_end(&txn->req, delta); |
| 7699 | prev = del_from; |
| 7700 | del_from = NULL; |
| 7701 | } |
| 7702 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7703 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7704 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7705 | /* if there are spaces around the equal sign, we need to |
| 7706 | * strip them otherwise we'll get trouble for cookie captures, |
| 7707 | * or even for rewrites. Since this happens extremely rarely, |
| 7708 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7709 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7710 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7711 | int stripped_before = 0; |
| 7712 | int stripped_after = 0; |
| 7713 | |
| 7714 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7715 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7716 | equal += stripped_before; |
| 7717 | val_beg += stripped_before; |
| 7718 | } |
| 7719 | |
| 7720 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7721 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7722 | val_beg += stripped_after; |
| 7723 | stripped_before += stripped_after; |
| 7724 | } |
| 7725 | |
| 7726 | val_end += stripped_before; |
| 7727 | next += stripped_before; |
| 7728 | hdr_end += stripped_before; |
| 7729 | hdr_next += stripped_before; |
| 7730 | cur_hdr->len += stripped_before; |
| 7731 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7732 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7733 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7734 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7735 | /* First, let's see if we want to capture this cookie. We check |
| 7736 | * that we don't already have a client side cookie, because we |
| 7737 | * can only capture one. Also as an optimisation, we ignore |
| 7738 | * cookies shorter than the declared name. |
| 7739 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7740 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7741 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7742 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7743 | int log_len = val_end - att_beg; |
| 7744 | |
| 7745 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7746 | Alert("HTTP logging : out of memory.\n"); |
| 7747 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7748 | if (log_len > sess->fe->capture_len) |
| 7749 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7750 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7751 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7752 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7753 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7754 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7755 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7756 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7757 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7758 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7759 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7760 | * For cookies in prefix mode, the form is : |
| 7761 | * |
| 7762 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7763 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7764 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7765 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7766 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7767 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7768 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7769 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7770 | * have the server ID between val_beg and delim, and the original cookie between |
| 7771 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7772 | * |
| 7773 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7774 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7775 | * | || || | |+-> next |
| 7776 | * | || || | +--> val_end |
| 7777 | * | || || +---------> delim |
| 7778 | * | || |+------------> val_beg |
| 7779 | * | || +-------------> att_end = equal |
| 7780 | * | |+-----------------> att_beg |
| 7781 | * | +------------------> prev |
| 7782 | * +-------------------------> hdr_beg |
| 7783 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7784 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7785 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7786 | for (delim = val_beg; delim < val_end; delim++) |
| 7787 | if (*delim == COOKIE_DELIM) |
| 7788 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7789 | } else { |
| 7790 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7791 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7792 | /* Now check if the cookie contains a date field, which would |
| 7793 | * appear after a vertical bar ('|') just after the server name |
| 7794 | * and before the delimiter. |
| 7795 | */ |
| 7796 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7797 | if (vbar1) { |
| 7798 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7799 | * right is the last seen date. It is a base64 encoded |
| 7800 | * 30-bit value representing the UNIX date since the |
| 7801 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7802 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7803 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7804 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7805 | if (val_end - vbar1 >= 5) { |
| 7806 | val = b64tos30(vbar1); |
| 7807 | if (val > 0) |
| 7808 | txn->cookie_last_date = val << 2; |
| 7809 | } |
| 7810 | /* look for a second vertical bar */ |
| 7811 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7812 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7813 | val = b64tos30(vbar1 + 1); |
| 7814 | if (val > 0) |
| 7815 | txn->cookie_first_date = val << 2; |
| 7816 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7817 | } |
| 7818 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7819 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7820 | /* if the cookie has an expiration date and the proxy wants to check |
| 7821 | * it, then we do that now. We first check if the cookie is too old, |
| 7822 | * then only if it has expired. We detect strict overflow because the |
| 7823 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7824 | * in the future are ignored if their offset is beyond one day. This |
| 7825 | * allows an admin to fix timezone issues without expiring everyone |
| 7826 | * and at the same time avoids keeping unwanted side effects for too |
| 7827 | * long. |
| 7828 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7829 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7830 | (((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] | 7831 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7832 | txn->flags &= ~TX_CK_MASK; |
| 7833 | txn->flags |= TX_CK_OLD; |
| 7834 | delim = val_beg; // let's pretend we have not found the cookie |
| 7835 | txn->cookie_first_date = 0; |
| 7836 | txn->cookie_last_date = 0; |
| 7837 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7838 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7839 | (((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] | 7840 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7841 | txn->flags &= ~TX_CK_MASK; |
| 7842 | txn->flags |= TX_CK_EXPIRED; |
| 7843 | delim = val_beg; // let's pretend we have not found the cookie |
| 7844 | txn->cookie_first_date = 0; |
| 7845 | txn->cookie_last_date = 0; |
| 7846 | } |
| 7847 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7848 | /* Here, we'll look for the first running server which supports the cookie. |
| 7849 | * This allows to share a same cookie between several servers, for example |
| 7850 | * to dedicate backup servers to specific servers only. |
| 7851 | * However, to prevent clients from sticking to cookie-less backup server |
| 7852 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7853 | * empty cookies and mark them as invalid. |
| 7854 | * The same behaviour is applied when persistence must be ignored. |
| 7855 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7856 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7857 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7858 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7859 | while (srv) { |
| 7860 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7861 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7862 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7863 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7864 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7865 | /* we found the server and we can use it */ |
| 7866 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7867 | 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] | 7868 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7869 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7870 | break; |
| 7871 | } else { |
| 7872 | /* we found a server, but it's down, |
| 7873 | * mark it as such and go on in case |
| 7874 | * another one is available. |
| 7875 | */ |
| 7876 | txn->flags &= ~TX_CK_MASK; |
| 7877 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7878 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7879 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7880 | srv = srv->next; |
| 7881 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7882 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7883 | 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] | 7884 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7885 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7886 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7887 | txn->flags |= TX_CK_UNUSED; |
| 7888 | else |
| 7889 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7890 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7891 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7892 | /* depending on the cookie mode, we may have to either : |
| 7893 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7894 | * the server never sees it ; |
| 7895 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7896 | * application cookie so that it does not get accidentely removed later, |
| 7897 | * if we're in cookie prefix mode |
| 7898 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7899 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7900 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7901 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7902 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7903 | val_end += delta; |
| 7904 | next += delta; |
| 7905 | hdr_end += delta; |
| 7906 | hdr_next += delta; |
| 7907 | cur_hdr->len += delta; |
| 7908 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7909 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7910 | del_from = NULL; |
| 7911 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7912 | } |
| 7913 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7914 | (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] | 7915 | del_from = prev; |
| 7916 | } |
| 7917 | } else { |
| 7918 | /* This is not our cookie, so we must preserve it. But if we already |
| 7919 | * scheduled another cookie for removal, we cannot remove the |
| 7920 | * complete header, but we can remove the previous block itself. |
| 7921 | */ |
| 7922 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7923 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7924 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7925 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7926 | if (att_beg >= del_from) |
| 7927 | att_beg += delta; |
| 7928 | if (att_end >= del_from) |
| 7929 | att_end += delta; |
| 7930 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7931 | val_end += delta; |
| 7932 | next += delta; |
| 7933 | hdr_end += delta; |
| 7934 | hdr_next += delta; |
| 7935 | cur_hdr->len += delta; |
| 7936 | http_msg_move_end(&txn->req, delta); |
| 7937 | prev = del_from; |
| 7938 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7939 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7940 | } |
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 | /* continue with next cookie on this header line */ |
| 7943 | att_beg = next; |
| 7944 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7945 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7946 | /* There are no more cookies on this line. |
| 7947 | * We may still have one (or several) marked for deletion at the |
| 7948 | * end of the line. We must do this now in two ways : |
| 7949 | * - if some cookies must be preserved, we only delete from the |
| 7950 | * mark to the end of line ; |
| 7951 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7952 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7953 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7954 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7955 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7956 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7957 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7958 | cur_hdr->len += delta; |
| 7959 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7960 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7961 | |
| 7962 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7963 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7964 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7965 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7966 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7967 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7968 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7969 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7970 | } |
| 7971 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7972 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7973 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7974 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7975 | } |
| 7976 | |
| 7977 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7978 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7979 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7980 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7981 | 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] | 7982 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7983 | char *cur_ptr, *cur_end, *cur_next; |
| 7984 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7985 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7986 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7987 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7988 | |
| 7989 | last_hdr = 0; |
| 7990 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7991 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7992 | old_idx = 0; |
| 7993 | |
| 7994 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7995 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7996 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7997 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7998 | (exp->action == ACT_ALLOW || |
| 7999 | exp->action == ACT_DENY)) |
| 8000 | return 0; |
| 8001 | |
| 8002 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 8003 | if (!cur_idx) |
| 8004 | break; |
| 8005 | |
| 8006 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8007 | cur_ptr = cur_next; |
| 8008 | cur_end = cur_ptr + cur_hdr->len; |
| 8009 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8010 | |
| 8011 | /* Now we have one header between cur_ptr and cur_end, |
| 8012 | * and the next header starts at cur_next. |
| 8013 | */ |
| 8014 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 8015 | 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] | 8016 | switch (exp->action) { |
| 8017 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8018 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8019 | last_hdr = 1; |
| 8020 | break; |
| 8021 | |
| 8022 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8023 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8024 | last_hdr = 1; |
| 8025 | break; |
| 8026 | |
| 8027 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8028 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 8029 | if (trash.len < 0) |
| 8030 | return -1; |
| 8031 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8032 | 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] | 8033 | /* FIXME: if the user adds a newline in the replacement, the |
| 8034 | * index will not be recalculated for now, and the new line |
| 8035 | * will not be counted as a new header. |
| 8036 | */ |
| 8037 | |
| 8038 | cur_end += delta; |
| 8039 | cur_next += delta; |
| 8040 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8041 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8042 | break; |
| 8043 | |
| 8044 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8045 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8046 | cur_next += delta; |
| 8047 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8048 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8049 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8050 | txn->hdr_idx.used--; |
| 8051 | cur_hdr->len = 0; |
| 8052 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8053 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8054 | break; |
| 8055 | |
| 8056 | } |
| 8057 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8058 | |
| 8059 | /* keep the link from this header to next one in case of later |
| 8060 | * removal of next header. |
| 8061 | */ |
| 8062 | old_idx = cur_idx; |
| 8063 | } |
| 8064 | return 0; |
| 8065 | } |
| 8066 | |
| 8067 | |
| 8068 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 8069 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 8070 | * or -1 if a replacement resulted in an invalid status line. |
| 8071 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8072 | 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] | 8073 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8074 | char *cur_ptr, *cur_end; |
| 8075 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8076 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8077 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8078 | |
| 8079 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8080 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8081 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8082 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8083 | (exp->action == ACT_ALLOW || |
| 8084 | exp->action == ACT_DENY)) |
| 8085 | return 0; |
| 8086 | else if (exp->action == ACT_REMOVE) |
| 8087 | return 0; |
| 8088 | |
| 8089 | done = 0; |
| 8090 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8091 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 8092 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8093 | |
| 8094 | /* Now we have the status line between cur_ptr and cur_end */ |
| 8095 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 8096 | 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] | 8097 | switch (exp->action) { |
| 8098 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8099 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8100 | done = 1; |
| 8101 | break; |
| 8102 | |
| 8103 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8104 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8105 | done = 1; |
| 8106 | break; |
| 8107 | |
| 8108 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8109 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 8110 | if (trash.len < 0) |
| 8111 | return -1; |
| 8112 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8113 | 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] | 8114 | /* FIXME: if the user adds a newline in the replacement, the |
| 8115 | * index will not be recalculated for now, and the new line |
| 8116 | * will not be counted as a new header. |
| 8117 | */ |
| 8118 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8119 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8120 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8121 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 8122 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8123 | cur_ptr, cur_end + 1, |
| 8124 | NULL, NULL); |
| 8125 | if (unlikely(!cur_end)) |
| 8126 | return -1; |
| 8127 | |
| 8128 | /* we have a full respnse and we know that we have either a CR |
| 8129 | * or an LF at <ptr>. |
| 8130 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8131 | 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] | 8132 | 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] | 8133 | /* there is no point trying this regex on headers */ |
| 8134 | return 1; |
| 8135 | } |
| 8136 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8137 | return done; |
| 8138 | } |
| 8139 | |
| 8140 | |
| 8141 | |
| 8142 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8143 | * 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] | 8144 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 8145 | * unparsable response. |
| 8146 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8147 | 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] | 8148 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8149 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8150 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8151 | struct hdr_exp *exp; |
| 8152 | |
| 8153 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8154 | int ret; |
| 8155 | |
| 8156 | /* |
| 8157 | * The interleaving of transformations and verdicts |
| 8158 | * makes it difficult to decide to continue or stop |
| 8159 | * the evaluation. |
| 8160 | */ |
| 8161 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8162 | if (txn->flags & TX_SVDENY) |
| 8163 | break; |
| 8164 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8165 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8166 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 8167 | exp->action == ACT_PASS)) { |
| 8168 | exp = exp->next; |
| 8169 | continue; |
| 8170 | } |
| 8171 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8172 | /* if this filter had a condition, evaluate it now and skip to |
| 8173 | * next filter if the condition does not match. |
| 8174 | */ |
| 8175 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8176 | 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] | 8177 | ret = acl_pass(ret); |
| 8178 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 8179 | ret = !ret; |
| 8180 | if (!ret) |
| 8181 | continue; |
| 8182 | } |
| 8183 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8184 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8185 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8186 | if (unlikely(ret < 0)) |
| 8187 | return -1; |
| 8188 | |
| 8189 | if (likely(ret == 0)) { |
| 8190 | /* The filter did not match the response, it can be |
| 8191 | * iterated through all headers. |
| 8192 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8193 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 8194 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8195 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8196 | } |
| 8197 | return 0; |
| 8198 | } |
| 8199 | |
| 8200 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8201 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 8202 | * 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] | 8203 | * desirable to call it only when needed. This function is also used when we |
| 8204 | * 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] | 8205 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8206 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8207 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8208 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8209 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8210 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8211 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8212 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8213 | char *hdr_beg, *hdr_end, *hdr_next; |
| 8214 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8215 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8216 | /* Iterate through the headers. |
| 8217 | * we start with the start line. |
| 8218 | */ |
| 8219 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8220 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8221 | |
| 8222 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 8223 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8224 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8225 | |
| 8226 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8227 | hdr_beg = hdr_next; |
| 8228 | hdr_end = hdr_beg + cur_hdr->len; |
| 8229 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8230 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8231 | /* We have one full header between hdr_beg and hdr_end, and the |
| 8232 | * next header starts at hdr_next. We're only interested in |
| 8233 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8234 | */ |
| 8235 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8236 | is_cookie2 = 0; |
| 8237 | prev = hdr_beg + 10; |
| 8238 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8239 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8240 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 8241 | if (!val) { |
| 8242 | old_idx = cur_idx; |
| 8243 | continue; |
| 8244 | } |
| 8245 | is_cookie2 = 1; |
| 8246 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8247 | } |
| 8248 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8249 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 8250 | * <prev> points to the colon. |
| 8251 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8252 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8253 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8254 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8255 | * check-cache is enabled) and we are not interested in checking |
| 8256 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8257 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 8258 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8259 | return; |
| 8260 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8261 | /* OK so now we know we have to process this response cookie. |
| 8262 | * The format of the Set-Cookie header is slightly different |
| 8263 | * from the format of the Cookie header in that it does not |
| 8264 | * support the comma as a cookie delimiter (thus the header |
| 8265 | * cannot be folded) because the Expires attribute described in |
| 8266 | * the original Netscape's spec may contain an unquoted date |
| 8267 | * with a comma inside. We have to live with this because |
| 8268 | * many browsers don't support Max-Age and some browsers don't |
| 8269 | * support quoted strings. However the Set-Cookie2 header is |
| 8270 | * clean. |
| 8271 | * |
| 8272 | * We have to keep multiple pointers in order to support cookie |
| 8273 | * removal at the beginning, middle or end of header without |
| 8274 | * corrupting the header (in case of set-cookie2). A special |
| 8275 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8276 | * fields after the first semi-colon. The <next> pointer points |
| 8277 | * either to the end of line (set-cookie) or next unquoted comma |
| 8278 | * (set-cookie2). All of these headers are valid : |
| 8279 | * |
| 8280 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8281 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8282 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8283 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8284 | * | | | | | | | | | | |
| 8285 | * | | | | | | | | +-> next hdr_end <--+ |
| 8286 | * | | | | | | | +------------> scav |
| 8287 | * | | | | | | +--------------> val_end |
| 8288 | * | | | | | +--------------------> val_beg |
| 8289 | * | | | | +----------------------> equal |
| 8290 | * | | | +------------------------> att_end |
| 8291 | * | | +----------------------------> att_beg |
| 8292 | * | +------------------------------> prev |
| 8293 | * +-----------------------------------------> hdr_beg |
| 8294 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8295 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8296 | for (; prev < hdr_end; prev = next) { |
| 8297 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8298 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8299 | /* find att_beg */ |
| 8300 | att_beg = prev + 1; |
| 8301 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8302 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8303 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8304 | /* find att_end : this is the first character after the last non |
| 8305 | * space before the equal. It may be equal to hdr_end. |
| 8306 | */ |
| 8307 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8308 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8309 | while (equal < hdr_end) { |
| 8310 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8311 | break; |
| 8312 | if (http_is_spht[(unsigned char)*equal++]) |
| 8313 | continue; |
| 8314 | att_end = equal; |
| 8315 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8316 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8317 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8318 | * is between <att_beg> and <equal>, both may be identical. |
| 8319 | */ |
| 8320 | |
| 8321 | /* look for end of cookie if there is an equal sign */ |
| 8322 | if (equal < hdr_end && *equal == '=') { |
| 8323 | /* look for the beginning of the value */ |
| 8324 | val_beg = equal + 1; |
| 8325 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8326 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8327 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8328 | /* find the end of the value, respecting quotes */ |
| 8329 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8330 | |
| 8331 | /* make val_end point to the first white space or delimitor after the value */ |
| 8332 | val_end = next; |
| 8333 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8334 | val_end--; |
| 8335 | } else { |
| 8336 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8337 | val_beg = val_end = next = equal; |
| 8338 | } |
| 8339 | |
| 8340 | if (next < hdr_end) { |
| 8341 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8342 | * a colon or semi-colon before the end. So skip all attr-value |
| 8343 | * pairs and look for the next comma. For Set-Cookie, since |
| 8344 | * commas are permitted in values, skip to the end. |
| 8345 | */ |
| 8346 | if (is_cookie2) |
| 8347 | next = find_hdr_value_end(next, hdr_end); |
| 8348 | else |
| 8349 | next = hdr_end; |
| 8350 | } |
| 8351 | |
| 8352 | /* Now everything is as on the diagram above */ |
| 8353 | |
| 8354 | /* Ignore cookies with no equal sign */ |
| 8355 | if (equal == val_end) |
| 8356 | continue; |
| 8357 | |
| 8358 | /* If there are spaces around the equal sign, we need to |
| 8359 | * strip them otherwise we'll get trouble for cookie captures, |
| 8360 | * or even for rewrites. Since this happens extremely rarely, |
| 8361 | * it does not hurt performance. |
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 (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8364 | int stripped_before = 0; |
| 8365 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8366 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8367 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8368 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8369 | equal += stripped_before; |
| 8370 | val_beg += stripped_before; |
| 8371 | } |
| 8372 | |
| 8373 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8374 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8375 | val_beg += stripped_after; |
| 8376 | stripped_before += stripped_after; |
| 8377 | } |
| 8378 | |
| 8379 | val_end += stripped_before; |
| 8380 | next += stripped_before; |
| 8381 | hdr_end += stripped_before; |
| 8382 | hdr_next += stripped_before; |
| 8383 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8384 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8385 | } |
| 8386 | |
| 8387 | /* First, let's see if we want to capture this cookie. We check |
| 8388 | * that we don't already have a server side cookie, because we |
| 8389 | * can only capture one. Also as an optimisation, we ignore |
| 8390 | * cookies shorter than the declared name. |
| 8391 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8392 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8393 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8394 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 8395 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8396 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8397 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8398 | Alert("HTTP logging : out of memory.\n"); |
| 8399 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8400 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8401 | if (log_len > sess->fe->capture_len) |
| 8402 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8403 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8404 | txn->srv_cookie[log_len] = 0; |
| 8405 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8406 | } |
| 8407 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8408 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8409 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8410 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8411 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8412 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8413 | /* assume passive cookie by default */ |
| 8414 | txn->flags &= ~TX_SCK_MASK; |
| 8415 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8416 | |
| 8417 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8418 | * this occurrence because we'll insert another one later. |
| 8419 | * 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] | 8420 | * a direct access. |
| 8421 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8422 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8423 | /* The "preserve" flag was set, we don't want to touch the |
| 8424 | * server's cookie. |
| 8425 | */ |
| 8426 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8427 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8428 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8429 | /* this cookie must be deleted */ |
| 8430 | if (*prev == ':' && next == hdr_end) { |
| 8431 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8432 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8433 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8434 | txn->hdr_idx.used--; |
| 8435 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8436 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8437 | hdr_next += delta; |
| 8438 | http_msg_move_end(&txn->rsp, delta); |
| 8439 | /* note: while both invalid now, <next> and <hdr_end> |
| 8440 | * are still equal, so the for() will stop as expected. |
| 8441 | */ |
| 8442 | } else { |
| 8443 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8444 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8445 | next = prev; |
| 8446 | hdr_end += delta; |
| 8447 | hdr_next += delta; |
| 8448 | cur_hdr->len += delta; |
| 8449 | http_msg_move_end(&txn->rsp, delta); |
| 8450 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8451 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8452 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8453 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8454 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8455 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8456 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8457 | * with this server since we know it. |
| 8458 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8459 | 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] | 8460 | next += delta; |
| 8461 | hdr_end += delta; |
| 8462 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8463 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8464 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8465 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8466 | txn->flags &= ~TX_SCK_MASK; |
| 8467 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8468 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8469 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8470 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8471 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8472 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8473 | 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] | 8474 | next += delta; |
| 8475 | hdr_end += delta; |
| 8476 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8477 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8478 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8479 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8480 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8481 | txn->flags &= ~TX_SCK_MASK; |
| 8482 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8483 | } |
| 8484 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8485 | /* that's done for this cookie, check the next one on the same |
| 8486 | * line when next != hdr_end (only if is_cookie2). |
| 8487 | */ |
| 8488 | } |
| 8489 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8490 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8491 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8492 | } |
| 8493 | |
| 8494 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8495 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8496 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8497 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8498 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8499 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8500 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8501 | char *p1, *p2; |
| 8502 | |
| 8503 | char *cur_ptr, *cur_end, *cur_next; |
| 8504 | int cur_idx; |
| 8505 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8506 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8507 | return; |
| 8508 | |
| 8509 | /* Iterate through the headers. |
| 8510 | * we start with the start line. |
| 8511 | */ |
| 8512 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8513 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8514 | |
| 8515 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8516 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8517 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8518 | |
| 8519 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8520 | cur_ptr = cur_next; |
| 8521 | cur_end = cur_ptr + cur_hdr->len; |
| 8522 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8523 | |
| 8524 | /* We have one full header between cur_ptr and cur_end, and the |
| 8525 | * next header starts at cur_next. We're only interested in |
| 8526 | * "Cookie:" headers. |
| 8527 | */ |
| 8528 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8529 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8530 | if (val) { |
| 8531 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8532 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8533 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8534 | return; |
| 8535 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8536 | } |
| 8537 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8538 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8539 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8540 | continue; |
| 8541 | |
| 8542 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8543 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8544 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8545 | |
| 8546 | if (p1 >= cur_end) /* no more info */ |
| 8547 | continue; |
| 8548 | |
| 8549 | /* p1 is at the beginning of the value */ |
| 8550 | p2 = p1; |
| 8551 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8552 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8553 | p2++; |
| 8554 | |
| 8555 | /* we have a complete value between p1 and p2 */ |
| 8556 | if (p2 < cur_end && *p2 == '=') { |
| 8557 | /* we have something of the form no-cache="set-cookie" */ |
| 8558 | if ((cur_end - p1 >= 21) && |
| 8559 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8560 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8561 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8562 | continue; |
| 8563 | } |
| 8564 | |
| 8565 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8566 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8567 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8568 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8569 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8570 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8571 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8572 | return; |
| 8573 | } |
| 8574 | |
| 8575 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8576 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8577 | continue; |
| 8578 | } |
| 8579 | } |
| 8580 | } |
| 8581 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8582 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8583 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8584 | * 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] | 8585 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8586 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8587 | * 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] | 8588 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8589 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8590 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8591 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8592 | 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] | 8593 | { |
| 8594 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8595 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8596 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8597 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8598 | if (!uri_auth) |
| 8599 | return 0; |
| 8600 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8601 | 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] | 8602 | return 0; |
| 8603 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8604 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8605 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8606 | return 0; |
| 8607 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8608 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8609 | return 0; |
| 8610 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8611 | return 1; |
| 8612 | } |
| 8613 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8614 | /* |
| 8615 | * 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] | 8616 | * By default it tries to report the error position as msg->err_pos. However if |
| 8617 | * this one is not set, it will then report msg->next, which is the last known |
| 8618 | * 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] | 8619 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8620 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8621 | void http_capture_bad_message(struct error_snapshot *es, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8622 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8623 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8624 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8625 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8626 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8627 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8628 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8629 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8630 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8631 | len1 = MIN(len1, es->len); |
| 8632 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8633 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8634 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8635 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8636 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8637 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8638 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8639 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8640 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8641 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8642 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8643 | es->when = date; // user-visible date |
| 8644 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8645 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8646 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8647 | if (objt_conn(sess->origin)) |
| 8648 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8649 | else |
| 8650 | memset(&es->src, 0, sizeof(es->src)); |
| 8651 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8652 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8653 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8654 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8655 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8656 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8657 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8658 | es->b_out = chn->buf->o; |
| 8659 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8660 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8661 | es->m_clen = msg->chunk_len; |
| 8662 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8663 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8664 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8665 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8666 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8667 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8668 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8669 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8670 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8671 | * 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] | 8672 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8673 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8674 | * 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] | 8675 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8676 | 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] | 8677 | struct hdr_idx *idx, int occ, |
| 8678 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8679 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8680 | struct hdr_ctx local_ctx; |
| 8681 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8682 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8683 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8684 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8685 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8686 | if (!ctx) { |
| 8687 | local_ctx.idx = 0; |
| 8688 | ctx = &local_ctx; |
| 8689 | } |
| 8690 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8691 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8692 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8693 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8694 | occ--; |
| 8695 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8696 | *vptr = ctx->line + ctx->val; |
| 8697 | *vlen = ctx->vlen; |
| 8698 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8699 | } |
| 8700 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8701 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8702 | } |
| 8703 | |
| 8704 | /* negative occurrence, we scan all the list then walk back */ |
| 8705 | if (-occ > MAX_HDR_HISTORY) |
| 8706 | return 0; |
| 8707 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8708 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8709 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8710 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8711 | len_hist[hist_ptr] = ctx->vlen; |
| 8712 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8713 | hist_ptr = 0; |
| 8714 | found++; |
| 8715 | } |
| 8716 | if (-occ > found) |
| 8717 | return 0; |
| 8718 | /* 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] | 8719 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8720 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8721 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8722 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8723 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8724 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8725 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8726 | *vptr = ptr_hist[hist_ptr]; |
| 8727 | *vlen = len_hist[hist_ptr]; |
| 8728 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8729 | } |
| 8730 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8731 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8732 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8733 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8734 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8735 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8736 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8737 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8738 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8739 | * line header values and does not stop at commas. |
| 8740 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8741 | */ |
| 8742 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8743 | struct hdr_idx *idx, int occ, |
| 8744 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8745 | { |
| 8746 | struct hdr_ctx local_ctx; |
| 8747 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8748 | int len_hist[MAX_HDR_HISTORY]; |
| 8749 | unsigned int hist_ptr; |
| 8750 | int found; |
| 8751 | |
| 8752 | if (!ctx) { |
| 8753 | local_ctx.idx = 0; |
| 8754 | ctx = &local_ctx; |
| 8755 | } |
| 8756 | |
| 8757 | if (occ >= 0) { |
| 8758 | /* search from the beginning */ |
| 8759 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8760 | occ--; |
| 8761 | if (occ <= 0) { |
| 8762 | *vptr = ctx->line + ctx->val; |
| 8763 | *vlen = ctx->vlen; |
| 8764 | return 1; |
| 8765 | } |
| 8766 | } |
| 8767 | return 0; |
| 8768 | } |
| 8769 | |
| 8770 | /* negative occurrence, we scan all the list then walk back */ |
| 8771 | if (-occ > MAX_HDR_HISTORY) |
| 8772 | return 0; |
| 8773 | |
| 8774 | found = hist_ptr = 0; |
| 8775 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8776 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8777 | len_hist[hist_ptr] = ctx->vlen; |
| 8778 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8779 | hist_ptr = 0; |
| 8780 | found++; |
| 8781 | } |
| 8782 | if (-occ > found) |
| 8783 | return 0; |
| 8784 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8785 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8786 | */ |
| 8787 | hist_ptr += occ; |
| 8788 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8789 | hist_ptr -= MAX_HDR_HISTORY; |
| 8790 | *vptr = ptr_hist[hist_ptr]; |
| 8791 | *vlen = len_hist[hist_ptr]; |
| 8792 | return 1; |
| 8793 | } |
| 8794 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8795 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8796 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8797 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8798 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8799 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8800 | 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] | 8801 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8802 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8803 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8804 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8805 | 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] | 8806 | dir, |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8807 | 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] | 8808 | 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] | 8809 | |
| 8810 | for (max = 0; start + max < end; max++) |
| 8811 | if (start[max] == '\r' || start[max] == '\n') |
| 8812 | break; |
| 8813 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8814 | UBOUND(max, trash.size - trash.len - 3); |
| 8815 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8816 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8817 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8818 | } |
| 8819 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8820 | |
| 8821 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 8822 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 8823 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 8824 | * assigned to the stream and returned. |
| 8825 | */ |
| 8826 | struct http_txn *http_alloc_txn(struct stream *s) |
| 8827 | { |
| 8828 | struct http_txn *txn = s->txn; |
| 8829 | |
| 8830 | if (txn) |
| 8831 | return txn; |
| 8832 | |
| 8833 | txn = pool_alloc2(pool2_http_txn); |
| 8834 | if (!txn) |
| 8835 | return txn; |
| 8836 | |
| 8837 | txn->hdr_idx.size = global.tune.max_http_hdr; |
| 8838 | txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx); |
| 8839 | if (!txn->hdr_idx.v) { |
| 8840 | pool_free2(pool2_http_txn, txn); |
| 8841 | return NULL; |
| 8842 | } |
| 8843 | |
| 8844 | s->txn = txn; |
| 8845 | return txn; |
| 8846 | } |
| 8847 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8848 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8849 | * 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] | 8850 | * the required fields are properly allocated and that we only need to (re)init |
| 8851 | * them. This should be used before processing any new request. |
| 8852 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8853 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8854 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8855 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8856 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8857 | |
| 8858 | txn->flags = 0; |
| 8859 | txn->status = -1; |
| 8860 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8861 | txn->cookie_first_date = 0; |
| 8862 | txn->cookie_last_date = 0; |
| 8863 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8864 | txn->srv_cookie = NULL; |
| 8865 | txn->cli_cookie = NULL; |
| 8866 | txn->uri = NULL; |
| 8867 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8868 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8869 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 8870 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8871 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8872 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 8873 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 8874 | txn->req.chunk_len = 0LL; |
| 8875 | txn->req.body_len = 0LL; |
| 8876 | txn->rsp.chunk_len = 0LL; |
| 8877 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8878 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8879 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8880 | txn->req.chn = &s->req; |
| 8881 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8882 | |
| 8883 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8884 | |
| 8885 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8886 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8887 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8888 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8889 | if (txn->hdr_idx.v) |
| 8890 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 8891 | |
| 8892 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 8893 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8894 | } |
| 8895 | |
| 8896 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8897 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8898 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8899 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8900 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8901 | |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8902 | /* release any possible compression context */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8903 | if (s->flags & SF_COMP_READY) |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8904 | s->comp_algo->end(&s->comp_ctx); |
| 8905 | s->comp_algo = NULL; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8906 | s->flags &= ~SF_COMP_READY; |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8907 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8908 | /* these ones will have been dynamically allocated */ |
| 8909 | pool_free2(pool2_requri, txn->uri); |
| 8910 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8911 | pool_free2(pool2_capture, txn->srv_cookie); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8912 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8913 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8914 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8915 | txn->uri = NULL; |
| 8916 | txn->srv_cookie = NULL; |
| 8917 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8918 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8919 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8920 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8921 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8922 | pool_free2(h->pool, s->req_cap[h->index]); |
| 8923 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8924 | } |
| 8925 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8926 | if (s->res_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->rsp_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8929 | pool_free2(h->pool, s->res_cap[h->index]); |
| 8930 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8931 | } |
| 8932 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 8933 | vars_prune(&s->vars_txn, s); |
| 8934 | vars_prune(&s->vars_reqres, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8935 | } |
| 8936 | |
| 8937 | /* 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] | 8938 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8939 | { |
| 8940 | http_end_txn(s); |
| 8941 | http_init_txn(s); |
| 8942 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 8943 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 8944 | * any rulelist match the NULL pointer. |
| 8945 | */ |
| 8946 | s->current_rule_list = NULL; |
| 8947 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8948 | s->be = strm_fe(s); |
| 8949 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8950 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8951 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8952 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8953 | /* re-init store persistence */ |
| 8954 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8955 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8956 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8957 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8958 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8959 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8960 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8961 | /* We must trim any excess data from the response buffer, because we |
| 8962 | * may have blocked an invalid response from a server that we don't |
| 8963 | * want to accidentely forward once we disable the analysers, nor do |
| 8964 | * we want those data to come along with next response. A typical |
| 8965 | * example of such data would be from a buggy server responding to |
| 8966 | * a HEAD with some data, or sending more than the advertised |
| 8967 | * content-length. |
| 8968 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8969 | if (unlikely(s->res.buf->i)) |
| 8970 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8971 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8972 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8973 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8974 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8975 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8976 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8977 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8978 | s->req.rex = TICK_ETERNITY; |
| 8979 | s->req.wex = TICK_ETERNITY; |
| 8980 | s->req.analyse_exp = TICK_ETERNITY; |
| 8981 | s->res.rex = TICK_ETERNITY; |
| 8982 | s->res.wex = TICK_ETERNITY; |
| 8983 | s->res.analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8984 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8985 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8986 | void free_http_res_rules(struct list *r) |
| 8987 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8988 | struct act_rule *tr, *pr; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8989 | |
| 8990 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8991 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8992 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8993 | free(pr); |
| 8994 | } |
| 8995 | } |
| 8996 | |
| 8997 | void free_http_req_rules(struct list *r) |
| 8998 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8999 | struct act_rule *tr, *pr; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9000 | |
| 9001 | list_for_each_entry_safe(pr, tr, r, list) { |
| 9002 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9003 | if (pr->action == ACT_HTTP_REQ_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9004 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9005 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9006 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9007 | free(pr); |
| 9008 | } |
| 9009 | } |
| 9010 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9011 | /* parse an "http-request" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9012 | 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] | 9013 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9014 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 9015 | struct action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9016 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9017 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9018 | |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9019 | rule = (struct act_rule*)calloc(1, sizeof(struct act_rule)); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9020 | if (!rule) { |
| 9021 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9022 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9023 | } |
| 9024 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9025 | rule->deny_status = HTTP_ERR_403; |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9026 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9027 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9028 | cur_arg = 1; |
Willy Tarreau | 5bd6759 | 2014-04-28 22:00:46 +0200 | [diff] [blame] | 9029 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) { |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9030 | int code; |
| 9031 | int hc; |
| 9032 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9033 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9034 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9035 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 9036 | cur_arg++; |
| 9037 | if (!args[cur_arg]) { |
| 9038 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 9039 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9040 | goto out_err; |
| 9041 | } |
| 9042 | |
| 9043 | code = atol(args[cur_arg]); |
| 9044 | cur_arg++; |
| 9045 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 9046 | if (http_err_codes[hc] == code) { |
| 9047 | rule->deny_status = hc; |
| 9048 | break; |
| 9049 | } |
| 9050 | } |
| 9051 | |
| 9052 | if (hc >= HTTP_ERR_SIZE) { |
| 9053 | Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n", |
| 9054 | file, linenum, code); |
| 9055 | } |
| 9056 | } |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 9057 | } else if (!strcmp(args[0], "tarpit")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9058 | rule->action = ACT_HTTP_REQ_TARPIT; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 9059 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9060 | } else if (!strcmp(args[0], "auth")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9061 | rule->action = ACT_HTTP_REQ_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9062 | cur_arg = 1; |
| 9063 | |
| 9064 | while(*args[cur_arg]) { |
| 9065 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9066 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9067 | cur_arg+=2; |
| 9068 | continue; |
| 9069 | } else |
| 9070 | break; |
| 9071 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9072 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9073 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9074 | cur_arg = 1; |
| 9075 | |
| 9076 | if (!*args[cur_arg] || |
| 9077 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9078 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 9079 | file, linenum, args[0]); |
| 9080 | goto out_err; |
| 9081 | } |
| 9082 | rule->arg.nice = atoi(args[cur_arg]); |
| 9083 | if (rule->arg.nice < -1024) |
| 9084 | rule->arg.nice = -1024; |
| 9085 | else if (rule->arg.nice > 1024) |
| 9086 | rule->arg.nice = 1024; |
| 9087 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9088 | } else if (!strcmp(args[0], "set-tos")) { |
| 9089 | #ifdef IP_TOS |
| 9090 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9091 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9092 | cur_arg = 1; |
| 9093 | |
| 9094 | if (!*args[cur_arg] || |
| 9095 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9096 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9097 | file, linenum, args[0]); |
| 9098 | goto out_err; |
| 9099 | } |
| 9100 | |
| 9101 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9102 | if (err && *err != '\0') { |
| 9103 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9104 | file, linenum, err, args[0]); |
| 9105 | goto out_err; |
| 9106 | } |
| 9107 | cur_arg++; |
| 9108 | #else |
| 9109 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9110 | goto out_err; |
| 9111 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9112 | } else if (!strcmp(args[0], "set-mark")) { |
| 9113 | #ifdef SO_MARK |
| 9114 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9115 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9116 | cur_arg = 1; |
| 9117 | |
| 9118 | if (!*args[cur_arg] || |
| 9119 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9120 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9121 | file, linenum, args[0]); |
| 9122 | goto out_err; |
| 9123 | } |
| 9124 | |
| 9125 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9126 | if (err && *err != '\0') { |
| 9127 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9128 | file, linenum, err, args[0]); |
| 9129 | goto out_err; |
| 9130 | } |
| 9131 | cur_arg++; |
| 9132 | global.last_checks |= LSTCHK_NETADM; |
| 9133 | #else |
| 9134 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9135 | goto out_err; |
| 9136 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9137 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9138 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9139 | cur_arg = 1; |
| 9140 | |
| 9141 | if (!*args[cur_arg] || |
| 9142 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9143 | bad_log_level: |
| 9144 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9145 | file, linenum, args[0]); |
| 9146 | goto out_err; |
| 9147 | } |
| 9148 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9149 | rule->arg.loglevel = -1; |
| 9150 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 9151 | goto bad_log_level; |
| 9152 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9153 | } 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] | 9154 | 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] | 9155 | cur_arg = 1; |
| 9156 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 9157 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9158 | (*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] | 9159 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9160 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9161 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9162 | } |
| 9163 | |
| 9164 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9165 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9166 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 9167 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9168 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9169 | 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] | 9170 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9171 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9172 | free(proxy->conf.lfs_file); |
| 9173 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9174 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9175 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9176 | } 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] | 9177 | 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] | 9178 | cur_arg = 1; |
| 9179 | |
| 9180 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 9181 | (*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] | 9182 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 9183 | file, linenum, args[0]); |
| 9184 | goto out_err; |
| 9185 | } |
| 9186 | |
| 9187 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9188 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9189 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9190 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9191 | error = NULL; |
| 9192 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9193 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9194 | args[cur_arg + 1], error); |
| 9195 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9196 | goto out_err; |
| 9197 | } |
| 9198 | |
| 9199 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9200 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9201 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9202 | file, linenum); |
| 9203 | |
| 9204 | free(proxy->conf.lfs_file); |
| 9205 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9206 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9207 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9208 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9209 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9210 | cur_arg = 1; |
| 9211 | |
| 9212 | if (!*args[cur_arg] || |
| 9213 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9214 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9215 | file, linenum, args[0]); |
| 9216 | goto out_err; |
| 9217 | } |
| 9218 | |
| 9219 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9220 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9221 | |
| 9222 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9223 | free(proxy->conf.lfs_file); |
| 9224 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9225 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9226 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9227 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9228 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 9229 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9230 | struct sample_expr *expr; |
| 9231 | unsigned int where; |
| 9232 | char *err = NULL; |
| 9233 | |
| 9234 | cur_arg = 1; |
| 9235 | proxy->conf.args.ctx = ARGC_TRK; |
| 9236 | |
| 9237 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9238 | if (!expr) { |
| 9239 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9240 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9241 | free(err); |
| 9242 | goto out_err; |
| 9243 | } |
| 9244 | |
| 9245 | where = 0; |
| 9246 | if (proxy->cap & PR_CAP_FE) |
| 9247 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9248 | if (proxy->cap & PR_CAP_BE) |
| 9249 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9250 | |
| 9251 | if (!(expr->fetch->val & where)) { |
| 9252 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9253 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9254 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9255 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9256 | free(expr); |
| 9257 | goto out_err; |
| 9258 | } |
| 9259 | |
| 9260 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9261 | cur_arg++; |
| 9262 | if (!args[cur_arg]) { |
| 9263 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 9264 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9265 | free(expr); |
| 9266 | goto out_err; |
| 9267 | } |
| 9268 | /* we copy the table name for now, it will be resolved later */ |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9269 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9270 | cur_arg++; |
| 9271 | } |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9272 | rule->arg.trk_ctr.expr = expr; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9273 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9274 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9275 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9276 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9277 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9278 | 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] | 9279 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9280 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9281 | goto out_err; |
| 9282 | } |
| 9283 | |
| 9284 | /* this redirect rule might already contain a parsed condition which |
| 9285 | * we'll pass to the http-request rule. |
| 9286 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9287 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9288 | rule->arg.redir = redir; |
| 9289 | rule->cond = redir->cond; |
| 9290 | redir->cond = NULL; |
| 9291 | cur_arg = 2; |
| 9292 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9293 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9294 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9295 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9296 | /* |
| 9297 | * '+ 8' for 'add-acl(' |
| 9298 | * '- 9' for 'add-acl(' + trailing ')' |
| 9299 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9300 | 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] | 9301 | |
| 9302 | cur_arg = 1; |
| 9303 | |
| 9304 | if (!*args[cur_arg] || |
| 9305 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9306 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9307 | file, linenum, args[0]); |
| 9308 | goto out_err; |
| 9309 | } |
| 9310 | |
| 9311 | LIST_INIT(&rule->arg.map.key); |
| 9312 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9313 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9314 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9315 | file, linenum); |
| 9316 | free(proxy->conf.lfs_file); |
| 9317 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9318 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9319 | cur_arg += 1; |
| 9320 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9321 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9322 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9323 | /* |
| 9324 | * '+ 8' for 'del-acl(' |
| 9325 | * '- 9' for 'del-acl(' + trailing ')' |
| 9326 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9327 | 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] | 9328 | |
| 9329 | cur_arg = 1; |
| 9330 | |
| 9331 | if (!*args[cur_arg] || |
| 9332 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9333 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9334 | file, linenum, args[0]); |
| 9335 | goto out_err; |
| 9336 | } |
| 9337 | |
| 9338 | LIST_INIT(&rule->arg.map.key); |
| 9339 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9340 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9341 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9342 | file, linenum); |
| 9343 | free(proxy->conf.lfs_file); |
| 9344 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9345 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9346 | cur_arg += 1; |
| 9347 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9348 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9349 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9350 | /* |
| 9351 | * '+ 8' for 'del-map(' |
| 9352 | * '- 9' for 'del-map(' + trailing ')' |
| 9353 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9354 | 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] | 9355 | |
| 9356 | cur_arg = 1; |
| 9357 | |
| 9358 | if (!*args[cur_arg] || |
| 9359 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9360 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9361 | file, linenum, args[0]); |
| 9362 | goto out_err; |
| 9363 | } |
| 9364 | |
| 9365 | LIST_INIT(&rule->arg.map.key); |
| 9366 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9367 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9368 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9369 | file, linenum); |
| 9370 | free(proxy->conf.lfs_file); |
| 9371 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9372 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9373 | cur_arg += 1; |
| 9374 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9375 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9376 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9377 | /* |
| 9378 | * '+ 8' for 'set-map(' |
| 9379 | * '- 9' for 'set-map(' + trailing ')' |
| 9380 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9381 | 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] | 9382 | |
| 9383 | cur_arg = 1; |
| 9384 | |
| 9385 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9386 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9387 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9388 | file, linenum, args[0]); |
| 9389 | goto out_err; |
| 9390 | } |
| 9391 | |
| 9392 | LIST_INIT(&rule->arg.map.key); |
| 9393 | LIST_INIT(&rule->arg.map.value); |
| 9394 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9395 | |
| 9396 | /* key pattern */ |
| 9397 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9398 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9399 | file, linenum); |
| 9400 | |
| 9401 | /* value pattern */ |
| 9402 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9403 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9404 | file, linenum); |
| 9405 | free(proxy->conf.lfs_file); |
| 9406 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9407 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9408 | |
| 9409 | cur_arg += 2; |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 9410 | } else if (strncmp(args[0], "set-src", 7) == 0) { |
| 9411 | struct sample_expr *expr; |
| 9412 | unsigned int where; |
| 9413 | char *err = NULL; |
| 9414 | |
| 9415 | cur_arg = 1; |
| 9416 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9417 | |
| 9418 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9419 | if (!expr) { |
| 9420 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9421 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9422 | free(err); |
| 9423 | goto out_err; |
| 9424 | } |
| 9425 | |
| 9426 | where = 0; |
| 9427 | if (proxy->cap & PR_CAP_FE) |
| 9428 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9429 | if (proxy->cap & PR_CAP_BE) |
| 9430 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9431 | |
| 9432 | if (!(expr->fetch->val & where)) { |
| 9433 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9434 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9435 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9436 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9437 | free(expr); |
| 9438 | goto out_err; |
| 9439 | } |
| 9440 | |
Thierry FOURNIER | a002dc9 | 2015-07-31 08:50:51 +0200 | [diff] [blame] | 9441 | rule->arg.expr = expr; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9442 | rule->action = ACT_HTTP_REQ_SET_SRC; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9443 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9444 | char *errmsg = NULL; |
| 9445 | cur_arg = 1; |
| 9446 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9447 | rule->from = ACT_F_HTTP_REQ; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9448 | 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] | 9449 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9450 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9451 | free(errmsg); |
| 9452 | goto out_err; |
| 9453 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9454 | } else { |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 9455 | 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] | 9456 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9457 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9458 | } |
| 9459 | |
| 9460 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9461 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9462 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9463 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9464 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9465 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9466 | file, linenum, args[0], errmsg); |
| 9467 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9468 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9469 | } |
| 9470 | rule->cond = cond; |
| 9471 | } |
| 9472 | else if (*args[cur_arg]) { |
| 9473 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9474 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9475 | file, linenum, args[0], args[cur_arg]); |
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 | |
| 9479 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9480 | out_err: |
| 9481 | free(rule); |
| 9482 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9483 | } |
| 9484 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9485 | /* parse an "http-respose" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9486 | 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] | 9487 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9488 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 9489 | struct action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9490 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9491 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9492 | |
| 9493 | rule = calloc(1, sizeof(*rule)); |
| 9494 | if (!rule) { |
| 9495 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9496 | goto out_err; |
| 9497 | } |
| 9498 | |
| 9499 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9500 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9501 | cur_arg = 1; |
| 9502 | } else if (!strcmp(args[0], "deny")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9503 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9504 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9505 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9506 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9507 | cur_arg = 1; |
| 9508 | |
| 9509 | if (!*args[cur_arg] || |
| 9510 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9511 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9512 | file, linenum, args[0]); |
| 9513 | goto out_err; |
| 9514 | } |
| 9515 | rule->arg.nice = atoi(args[cur_arg]); |
| 9516 | if (rule->arg.nice < -1024) |
| 9517 | rule->arg.nice = -1024; |
| 9518 | else if (rule->arg.nice > 1024) |
| 9519 | rule->arg.nice = 1024; |
| 9520 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9521 | } else if (!strcmp(args[0], "set-tos")) { |
| 9522 | #ifdef IP_TOS |
| 9523 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9524 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9525 | cur_arg = 1; |
| 9526 | |
| 9527 | if (!*args[cur_arg] || |
| 9528 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9529 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9530 | file, linenum, args[0]); |
| 9531 | goto out_err; |
| 9532 | } |
| 9533 | |
| 9534 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9535 | if (err && *err != '\0') { |
| 9536 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9537 | file, linenum, err, args[0]); |
| 9538 | goto out_err; |
| 9539 | } |
| 9540 | cur_arg++; |
| 9541 | #else |
| 9542 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9543 | goto out_err; |
| 9544 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9545 | } else if (!strcmp(args[0], "set-mark")) { |
| 9546 | #ifdef SO_MARK |
| 9547 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9548 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9549 | cur_arg = 1; |
| 9550 | |
| 9551 | if (!*args[cur_arg] || |
| 9552 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9553 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9554 | file, linenum, args[0]); |
| 9555 | goto out_err; |
| 9556 | } |
| 9557 | |
| 9558 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9559 | if (err && *err != '\0') { |
| 9560 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9561 | file, linenum, err, args[0]); |
| 9562 | goto out_err; |
| 9563 | } |
| 9564 | cur_arg++; |
| 9565 | global.last_checks |= LSTCHK_NETADM; |
| 9566 | #else |
| 9567 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9568 | goto out_err; |
| 9569 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9570 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9571 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9572 | cur_arg = 1; |
| 9573 | |
| 9574 | if (!*args[cur_arg] || |
| 9575 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9576 | bad_log_level: |
| 9577 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9578 | file, linenum, args[0]); |
| 9579 | goto out_err; |
| 9580 | } |
| 9581 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9582 | rule->arg.loglevel = -1; |
| 9583 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 9584 | goto bad_log_level; |
| 9585 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9586 | } 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] | 9587 | 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] | 9588 | cur_arg = 1; |
| 9589 | |
| 9590 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9591 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9592 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9593 | file, linenum, args[0]); |
| 9594 | goto out_err; |
| 9595 | } |
| 9596 | |
| 9597 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9598 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9599 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9600 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9601 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9602 | 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] | 9603 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9604 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9605 | free(proxy->conf.lfs_file); |
| 9606 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9607 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9608 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9609 | } 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] | 9610 | 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] | 9611 | cur_arg = 1; |
| 9612 | |
| 9613 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9614 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9615 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9616 | file, linenum, args[0]); |
| 9617 | goto out_err; |
| 9618 | } |
| 9619 | |
| 9620 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9621 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9622 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9623 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9624 | error = NULL; |
| 9625 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9626 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9627 | args[cur_arg + 1], error); |
| 9628 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9629 | goto out_err; |
| 9630 | } |
| 9631 | |
| 9632 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9633 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9634 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9635 | file, linenum); |
| 9636 | |
| 9637 | free(proxy->conf.lfs_file); |
| 9638 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9639 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9640 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9641 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9642 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9643 | cur_arg = 1; |
| 9644 | |
| 9645 | if (!*args[cur_arg] || |
| 9646 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9647 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9648 | file, linenum, args[0]); |
| 9649 | goto out_err; |
| 9650 | } |
| 9651 | |
| 9652 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9653 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9654 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9655 | proxy->conf.args.ctx = ARGC_HRS; |
| 9656 | free(proxy->conf.lfs_file); |
| 9657 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9658 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9659 | cur_arg += 1; |
| 9660 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9661 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9662 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9663 | /* |
| 9664 | * '+ 8' for 'add-acl(' |
| 9665 | * '- 9' for 'add-acl(' + trailing ')' |
| 9666 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9667 | 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] | 9668 | |
| 9669 | cur_arg = 1; |
| 9670 | |
| 9671 | if (!*args[cur_arg] || |
| 9672 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9673 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9674 | file, linenum, args[0]); |
| 9675 | goto out_err; |
| 9676 | } |
| 9677 | |
| 9678 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9679 | proxy->conf.args.ctx = ARGC_HRS; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9680 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9681 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9682 | file, linenum); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9683 | free(proxy->conf.lfs_file); |
| 9684 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9685 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9686 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9687 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9688 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9689 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9690 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9691 | /* |
| 9692 | * '+ 8' for 'del-acl(' |
| 9693 | * '- 9' for 'del-acl(' + trailing ')' |
| 9694 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9695 | 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] | 9696 | |
| 9697 | cur_arg = 1; |
| 9698 | |
| 9699 | if (!*args[cur_arg] || |
| 9700 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9701 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9702 | file, linenum, args[0]); |
| 9703 | goto out_err; |
| 9704 | } |
| 9705 | |
| 9706 | LIST_INIT(&rule->arg.map.key); |
| 9707 | proxy->conf.args.ctx = ARGC_HRS; |
| 9708 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9709 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9710 | file, linenum); |
| 9711 | free(proxy->conf.lfs_file); |
| 9712 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9713 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9714 | cur_arg += 1; |
| 9715 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9716 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9717 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9718 | /* |
| 9719 | * '+ 8' for 'del-map(' |
| 9720 | * '- 9' for 'del-map(' + trailing ')' |
| 9721 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9722 | 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] | 9723 | |
| 9724 | cur_arg = 1; |
| 9725 | |
| 9726 | if (!*args[cur_arg] || |
| 9727 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9728 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9729 | file, linenum, args[0]); |
| 9730 | goto out_err; |
| 9731 | } |
| 9732 | |
| 9733 | LIST_INIT(&rule->arg.map.key); |
| 9734 | proxy->conf.args.ctx = ARGC_HRS; |
| 9735 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9736 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9737 | file, linenum); |
| 9738 | free(proxy->conf.lfs_file); |
| 9739 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9740 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9741 | cur_arg += 1; |
| 9742 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9743 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9744 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9745 | /* |
| 9746 | * '+ 8' for 'set-map(' |
| 9747 | * '- 9' for 'set-map(' + trailing ')' |
| 9748 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9749 | 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] | 9750 | |
| 9751 | cur_arg = 1; |
| 9752 | |
| 9753 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9754 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9755 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9756 | file, linenum, args[0]); |
| 9757 | goto out_err; |
| 9758 | } |
| 9759 | |
| 9760 | LIST_INIT(&rule->arg.map.key); |
| 9761 | LIST_INIT(&rule->arg.map.value); |
| 9762 | |
| 9763 | proxy->conf.args.ctx = ARGC_HRS; |
| 9764 | |
| 9765 | /* key pattern */ |
| 9766 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9767 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9768 | file, linenum); |
| 9769 | |
| 9770 | /* value pattern */ |
| 9771 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9772 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9773 | file, linenum); |
| 9774 | |
| 9775 | free(proxy->conf.lfs_file); |
| 9776 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9777 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9778 | |
| 9779 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9780 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9781 | struct redirect_rule *redir; |
| 9782 | char *errmsg = NULL; |
| 9783 | |
| 9784 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) { |
| 9785 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9786 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9787 | goto out_err; |
| 9788 | } |
| 9789 | |
| 9790 | /* this redirect rule might already contain a parsed condition which |
| 9791 | * we'll pass to the http-request rule. |
| 9792 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9793 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9794 | rule->arg.redir = redir; |
| 9795 | rule->cond = redir->cond; |
| 9796 | redir->cond = NULL; |
| 9797 | cur_arg = 2; |
| 9798 | return rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9799 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9800 | char *errmsg = NULL; |
| 9801 | cur_arg = 1; |
| 9802 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9803 | rule->from = ACT_F_HTTP_RES; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9804 | 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] | 9805 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9806 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9807 | free(errmsg); |
| 9808 | goto out_err; |
| 9809 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9810 | } else { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 9811 | 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] | 9812 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 9813 | goto out_err; |
| 9814 | } |
| 9815 | |
| 9816 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9817 | struct acl_cond *cond; |
| 9818 | char *errmsg = NULL; |
| 9819 | |
| 9820 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9821 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9822 | file, linenum, args[0], errmsg); |
| 9823 | free(errmsg); |
| 9824 | goto out_err; |
| 9825 | } |
| 9826 | rule->cond = cond; |
| 9827 | } |
| 9828 | else if (*args[cur_arg]) { |
| 9829 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9830 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9831 | file, linenum, args[0], args[cur_arg]); |
| 9832 | goto out_err; |
| 9833 | } |
| 9834 | |
| 9835 | return rule; |
| 9836 | out_err: |
| 9837 | free(rule); |
| 9838 | return NULL; |
| 9839 | } |
| 9840 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9841 | /* 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] | 9842 | * 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] | 9843 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 9844 | * 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] | 9845 | */ |
| 9846 | 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] | 9847 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9848 | { |
| 9849 | struct redirect_rule *rule; |
| 9850 | int cur_arg; |
| 9851 | int type = REDIRECT_TYPE_NONE; |
| 9852 | int code = 302; |
| 9853 | const char *destination = NULL; |
| 9854 | const char *cookie = NULL; |
| 9855 | int cookie_set = 0; |
| 9856 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9857 | struct acl_cond *cond = NULL; |
| 9858 | |
| 9859 | cur_arg = 0; |
| 9860 | while (*(args[cur_arg])) { |
| 9861 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9862 | if (!*args[cur_arg + 1]) |
| 9863 | goto missing_arg; |
| 9864 | |
| 9865 | type = REDIRECT_TYPE_LOCATION; |
| 9866 | cur_arg++; |
| 9867 | destination = args[cur_arg]; |
| 9868 | } |
| 9869 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9870 | if (!*args[cur_arg + 1]) |
| 9871 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9872 | type = REDIRECT_TYPE_PREFIX; |
| 9873 | cur_arg++; |
| 9874 | destination = args[cur_arg]; |
| 9875 | } |
| 9876 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9877 | if (!*args[cur_arg + 1]) |
| 9878 | goto missing_arg; |
| 9879 | |
| 9880 | type = REDIRECT_TYPE_SCHEME; |
| 9881 | cur_arg++; |
| 9882 | destination = args[cur_arg]; |
| 9883 | } |
| 9884 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9885 | if (!*args[cur_arg + 1]) |
| 9886 | goto missing_arg; |
| 9887 | |
| 9888 | cur_arg++; |
| 9889 | cookie = args[cur_arg]; |
| 9890 | cookie_set = 1; |
| 9891 | } |
| 9892 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9893 | if (!*args[cur_arg + 1]) |
| 9894 | goto missing_arg; |
| 9895 | |
| 9896 | cur_arg++; |
| 9897 | cookie = args[cur_arg]; |
| 9898 | cookie_set = 0; |
| 9899 | } |
| 9900 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9901 | if (!*args[cur_arg + 1]) |
| 9902 | goto missing_arg; |
| 9903 | |
| 9904 | cur_arg++; |
| 9905 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9906 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9907 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9908 | "'%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] | 9909 | args[cur_arg - 1], args[cur_arg]); |
| 9910 | return NULL; |
| 9911 | } |
| 9912 | } |
| 9913 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9914 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9915 | } |
| 9916 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9917 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9918 | } |
| 9919 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9920 | strcmp(args[cur_arg], "unless") == 0) { |
| 9921 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 9922 | if (!cond) { |
| 9923 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9924 | return NULL; |
| 9925 | } |
| 9926 | break; |
| 9927 | } |
| 9928 | else { |
| 9929 | memprintf(errmsg, |
| 9930 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9931 | args[cur_arg]); |
| 9932 | return NULL; |
| 9933 | } |
| 9934 | cur_arg++; |
| 9935 | } |
| 9936 | |
| 9937 | if (type == REDIRECT_TYPE_NONE) { |
| 9938 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9939 | return NULL; |
| 9940 | } |
| 9941 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9942 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 9943 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 9944 | return NULL; |
| 9945 | } |
| 9946 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9947 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 9948 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9949 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9950 | |
| 9951 | if (!use_fmt) { |
| 9952 | /* old-style static redirect rule */ |
| 9953 | rule->rdr_str = strdup(destination); |
| 9954 | rule->rdr_len = strlen(destination); |
| 9955 | } |
| 9956 | else { |
| 9957 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9958 | |
| 9959 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9960 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9961 | * makes it hard for the user to configure a self-redirection. |
| 9962 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9963 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9964 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9965 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9966 | dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR |
| 9967 | : (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] | 9968 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9969 | free(curproxy->conf.lfs_file); |
| 9970 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9971 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9972 | } |
| 9973 | } |
| 9974 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9975 | if (cookie) { |
| 9976 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9977 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9978 | */ |
| 9979 | rule->cookie_len = strlen(cookie); |
| 9980 | if (cookie_set) { |
| 9981 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9982 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9983 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9984 | rule->cookie_len += 9; |
| 9985 | } else { |
| 9986 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9987 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9988 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9989 | rule->cookie_len += 20; |
| 9990 | } |
| 9991 | } |
| 9992 | rule->type = type; |
| 9993 | rule->code = code; |
| 9994 | rule->flags = flags; |
| 9995 | LIST_INIT(&rule->list); |
| 9996 | return rule; |
| 9997 | |
| 9998 | missing_arg: |
| 9999 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 10000 | return NULL; |
| 10001 | } |
| 10002 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10003 | /************************************************************************/ |
| 10004 | /* The code below is dedicated to ACL parsing and matching */ |
| 10005 | /************************************************************************/ |
| 10006 | |
| 10007 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10008 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 10009 | * which means that a request or response is ready. If some data is missing, |
| 10010 | * 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] | 10011 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 10012 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10013 | * |
| 10014 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10015 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 10016 | * decide whether or not an HTTP message is present ; |
| 10017 | * 0 if the requested data cannot be fetched or if it is certain that |
| 10018 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10019 | * 1 if an HTTP message is ready |
| 10020 | */ |
| 10021 | static int |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10022 | smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10023 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10024 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10025 | struct http_txn *txn; |
| 10026 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10027 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 10028 | /* Note: this function may only be used from places where |
| 10029 | * http_init_txn() has already been done, and implies that <s>, |
| 10030 | * <txn>, and <hdr_idx.v> are properly set. An extra check protects |
| 10031 | * against an eventual mistake in the fetch capability matrix. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10032 | */ |
| 10033 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10034 | if (!s) |
| 10035 | return 0; |
| 10036 | txn = s->txn; |
| 10037 | |
| 10038 | if (!txn) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10039 | return 0; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10040 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10041 | |
| 10042 | /* Check for a dependency on a request */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10043 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10044 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10045 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10046 | /* If the buffer does not leave enough free space at the end, |
| 10047 | * we must first realign it. |
| 10048 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10049 | if (s->req.buf->p > s->req.buf->data && |
| 10050 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 10051 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10052 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10053 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 10054 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10055 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10056 | |
| 10057 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10058 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10059 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 10060 | |
| 10061 | /* Still no valid request ? */ |
| 10062 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 10063 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10064 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
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 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10068 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10069 | return 0; |
| 10070 | } |
| 10071 | |
| 10072 | /* OK we just got a valid HTTP request. We have some minor |
| 10073 | * preparation to perform so that further checks can rely |
| 10074 | * on HTTP tests. |
| 10075 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10076 | |
| 10077 | /* If the request was parsed but was too large, we must absolutely |
| 10078 | * return an error so that it is not processed. At the moment this |
| 10079 | * cannot happen, but if the parsers are to change in the future, |
| 10080 | * we want this check to be maintained. |
| 10081 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10082 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 10083 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10084 | msg->msg_state = HTTP_MSG_ERROR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10085 | smp->data.u.sint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10086 | return 1; |
| 10087 | } |
| 10088 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10089 | 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] | 10090 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 10091 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10092 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10093 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 10094 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10095 | } |
| 10096 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10097 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10098 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10099 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10100 | |
| 10101 | /* otherwise everything's ready for the request */ |
| 10102 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10103 | else { |
| 10104 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10105 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 10106 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10107 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10108 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10109 | } |
| 10110 | |
| 10111 | /* everything's OK */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10112 | smp->data.u.sint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10113 | return 1; |
| 10114 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10115 | |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 10116 | /* Note: these functinos *do* modify the sample. Even in case of success, at |
| 10117 | * least the type and uint value are modified. |
| 10118 | */ |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10119 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10120 | do { int r = smp_prefetch_http(smp->px, smp->strm, smp->opt, args, smp, 1); if (r <= 0) return r; } while (0) |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10121 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10122 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10123 | do { int r = smp_prefetch_http(smp->px, smp->strm, smp->opt, args, smp, 0); if (r <= 0) return r; } while (0) |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10124 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10125 | |
| 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 | */ |