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 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/appsession.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 29 | #include <common/base64.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 30 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 31 | #include <common/compat.h> |
| 32 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 33 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/memory.h> |
| 35 | #include <common/mini-clist.h> |
| 36 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 37 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 38 | #include <common/time.h> |
| 39 | #include <common/uri_auth.h> |
| 40 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
| 42 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 45 | #include <proto/acl.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 | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | #include <proto/session.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> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 69 | const char HTTP_100[] = |
| 70 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 71 | |
| 72 | const struct chunk http_100_chunk = { |
| 73 | .str = (char *)&HTTP_100, |
| 74 | .len = sizeof(HTTP_100)-1 |
| 75 | }; |
| 76 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 77 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 78 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 79 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 80 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 81 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 82 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 83 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 84 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 85 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 86 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 87 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 88 | |
| 89 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 90 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 91 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 92 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 93 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 94 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 95 | |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 96 | |
| 97 | /* same as 302 except that the browser MUST retry with the same method */ |
| 98 | const char *HTTP_307 = |
| 99 | "HTTP/1.1 307 Temporary Redirect\r\n" |
| 100 | "Cache-Control: no-cache\r\n" |
| 101 | "Content-length: 0\r\n" |
| 102 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 103 | |
| 104 | /* same as 301 except that the browser MUST retry with the same method */ |
| 105 | const char *HTTP_308 = |
| 106 | "HTTP/1.1 308 Permanent Redirect\r\n" |
| 107 | "Content-length: 0\r\n" |
| 108 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 109 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 111 | const char *HTTP_401_fmt = |
| 112 | "HTTP/1.0 401 Unauthorized\r\n" |
| 113 | "Cache-Control: no-cache\r\n" |
| 114 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 115 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 116 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 117 | "\r\n" |
| 118 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 119 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 120 | const char *HTTP_407_fmt = |
| 121 | "HTTP/1.0 407 Unauthorized\r\n" |
| 122 | "Cache-Control: no-cache\r\n" |
| 123 | "Connection: close\r\n" |
| 124 | "Content-Type: text/html\r\n" |
| 125 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 126 | "\r\n" |
Godbach | b301654 | 2014-12-17 16:32:05 +0800 | [diff] [blame] | 127 | "<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] | 128 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 129 | |
| 130 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 131 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 132 | [HTTP_ERR_400] = 400, |
| 133 | [HTTP_ERR_403] = 403, |
| 134 | [HTTP_ERR_408] = 408, |
| 135 | [HTTP_ERR_500] = 500, |
| 136 | [HTTP_ERR_502] = 502, |
| 137 | [HTTP_ERR_503] = 503, |
| 138 | [HTTP_ERR_504] = 504, |
| 139 | }; |
| 140 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 141 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 142 | [HTTP_ERR_200] = |
| 143 | "HTTP/1.0 200 OK\r\n" |
| 144 | "Cache-Control: no-cache\r\n" |
| 145 | "Connection: close\r\n" |
| 146 | "Content-Type: text/html\r\n" |
| 147 | "\r\n" |
| 148 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 149 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 150 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 151 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 152 | "Cache-Control: no-cache\r\n" |
| 153 | "Connection: close\r\n" |
| 154 | "Content-Type: text/html\r\n" |
| 155 | "\r\n" |
| 156 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 157 | |
| 158 | [HTTP_ERR_403] = |
| 159 | "HTTP/1.0 403 Forbidden\r\n" |
| 160 | "Cache-Control: no-cache\r\n" |
| 161 | "Connection: close\r\n" |
| 162 | "Content-Type: text/html\r\n" |
| 163 | "\r\n" |
| 164 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 165 | |
| 166 | [HTTP_ERR_408] = |
| 167 | "HTTP/1.0 408 Request Time-out\r\n" |
| 168 | "Cache-Control: no-cache\r\n" |
| 169 | "Connection: close\r\n" |
| 170 | "Content-Type: text/html\r\n" |
| 171 | "\r\n" |
| 172 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 173 | |
| 174 | [HTTP_ERR_500] = |
| 175 | "HTTP/1.0 500 Server Error\r\n" |
| 176 | "Cache-Control: no-cache\r\n" |
| 177 | "Connection: close\r\n" |
| 178 | "Content-Type: text/html\r\n" |
| 179 | "\r\n" |
| 180 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 181 | |
| 182 | [HTTP_ERR_502] = |
| 183 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 184 | "Cache-Control: no-cache\r\n" |
| 185 | "Connection: close\r\n" |
| 186 | "Content-Type: text/html\r\n" |
| 187 | "\r\n" |
| 188 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 189 | |
| 190 | [HTTP_ERR_503] = |
| 191 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 192 | "Cache-Control: no-cache\r\n" |
| 193 | "Connection: close\r\n" |
| 194 | "Content-Type: text/html\r\n" |
| 195 | "\r\n" |
| 196 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 197 | |
| 198 | [HTTP_ERR_504] = |
| 199 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 200 | "Cache-Control: no-cache\r\n" |
| 201 | "Connection: close\r\n" |
| 202 | "Content-Type: text/html\r\n" |
| 203 | "\r\n" |
| 204 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 205 | |
| 206 | }; |
| 207 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 208 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 209 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 210 | [STAT_STATUS_DENY] = "DENY", |
| 211 | [STAT_STATUS_DONE] = "DONE", |
| 212 | [STAT_STATUS_ERRP] = "ERRP", |
| 213 | [STAT_STATUS_EXCD] = "EXCD", |
| 214 | [STAT_STATUS_NONE] = "NONE", |
| 215 | [STAT_STATUS_PART] = "PART", |
| 216 | [STAT_STATUS_UNKN] = "UNKN", |
| 217 | }; |
| 218 | |
| 219 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 220 | /* List head of all known action keywords for "http-request" */ |
| 221 | struct http_req_action_kw_list http_req_keywords = { |
| 222 | .list = LIST_HEAD_INIT(http_req_keywords.list) |
| 223 | }; |
| 224 | |
| 225 | /* List head of all known action keywords for "http-response" */ |
| 226 | struct http_res_action_kw_list http_res_keywords = { |
| 227 | .list = LIST_HEAD_INIT(http_res_keywords.list) |
| 228 | }; |
| 229 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 230 | /* We must put the messages here since GCC cannot initialize consts depending |
| 231 | * on strlen(). |
| 232 | */ |
| 233 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 234 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 235 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
| 236 | static struct hdr_ctx static_hdr_ctx; |
| 237 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 238 | #define FD_SETS_ARE_BITFIELDS |
| 239 | #ifdef FD_SETS_ARE_BITFIELDS |
| 240 | /* |
| 241 | * This map is used with all the FD_* macros to check whether a particular bit |
| 242 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 243 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 244 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 245 | * exclusively to the macros. |
| 246 | */ |
| 247 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 248 | 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] | 249 | 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] | 250 | |
| 251 | #else |
| 252 | #error "Check if your OS uses bitfields for fd_sets" |
| 253 | #endif |
| 254 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 255 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn); |
| 256 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 257 | void init_proto_http() |
| 258 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 259 | int i; |
| 260 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 261 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 262 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 263 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 264 | if (!http_err_msgs[msg]) { |
| 265 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 266 | abort(); |
| 267 | } |
| 268 | |
| 269 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 270 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 271 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 272 | |
| 273 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 274 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 275 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 276 | * printable characters above. |
| 277 | */ |
| 278 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 279 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 280 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 281 | for (i = 0; i < 32; i++) { |
| 282 | FD_SET(i, hdr_encode_map); |
| 283 | FD_SET(i, url_encode_map); |
| 284 | } |
| 285 | for (i = 127; i < 256; i++) { |
| 286 | FD_SET(i, hdr_encode_map); |
| 287 | FD_SET(i, url_encode_map); |
| 288 | } |
| 289 | |
| 290 | tmp = "\"#{|}"; |
| 291 | while (*tmp) { |
| 292 | FD_SET(*tmp, hdr_encode_map); |
| 293 | tmp++; |
| 294 | } |
| 295 | |
| 296 | tmp = "\"#"; |
| 297 | while (*tmp) { |
| 298 | FD_SET(*tmp, url_encode_map); |
| 299 | tmp++; |
| 300 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 301 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 302 | /* initialize the http header encoding map. The draft httpbis define the |
| 303 | * header content as: |
| 304 | * |
| 305 | * HTTP-message = start-line |
| 306 | * *( header-field CRLF ) |
| 307 | * CRLF |
| 308 | * [ message-body ] |
| 309 | * header-field = field-name ":" OWS field-value OWS |
| 310 | * field-value = *( field-content / obs-fold ) |
| 311 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 312 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 313 | * field-vchar = VCHAR / obs-text |
| 314 | * VCHAR = %x21-7E |
| 315 | * obs-text = %x80-FF |
| 316 | * |
| 317 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 318 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 319 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 320 | */ |
| 321 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 322 | for (i = 0x00; i <= 0x08; i++) |
| 323 | FD_SET(i, http_encode_map); |
| 324 | for (i = 0x0a; i <= 0x1f; i++) |
| 325 | FD_SET(i, http_encode_map); |
| 326 | FD_SET(0x7f, http_encode_map); |
| 327 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 328 | /* memory allocations */ |
| 329 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 330 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 331 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 332 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 333 | /* |
| 334 | * We have 26 list of methods (1 per first letter), each of which can have |
| 335 | * up to 3 entries (2 valid, 1 null). |
| 336 | */ |
| 337 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 338 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 339 | int len; |
| 340 | const char text[8]; |
| 341 | }; |
| 342 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 343 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 344 | ['C' - 'A'] = { |
| 345 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 346 | }, |
| 347 | ['D' - 'A'] = { |
| 348 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 349 | }, |
| 350 | ['G' - 'A'] = { |
| 351 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 352 | }, |
| 353 | ['H' - 'A'] = { |
| 354 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 355 | }, |
Christopher Faulet | a591ef2 | 2015-07-31 14:26:57 +0200 | [diff] [blame] | 356 | ['O' - 'A'] = { |
| 357 | [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" }, |
| 358 | }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 359 | ['P' - 'A'] = { |
| 360 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 361 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 362 | }, |
| 363 | ['T' - 'A'] = { |
| 364 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 365 | }, |
| 366 | /* rest is empty like this : |
Willy Tarreau | bcd0336 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 367 | * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 368 | */ |
| 369 | }; |
| 370 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 371 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 372 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 373 | [HTTP_METH_GET] = { "GET", 3 }, |
| 374 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 375 | [HTTP_METH_POST] = { "POST", 4 }, |
| 376 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 377 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 378 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 379 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 380 | }; |
| 381 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 382 | /* 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] | 383 | * 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] | 384 | * RFC2616 for those chars. |
| 385 | */ |
| 386 | |
| 387 | const char http_is_spht[256] = { |
| 388 | [' '] = 1, ['\t'] = 1, |
| 389 | }; |
| 390 | |
| 391 | const char http_is_crlf[256] = { |
| 392 | ['\r'] = 1, ['\n'] = 1, |
| 393 | }; |
| 394 | |
| 395 | const char http_is_lws[256] = { |
| 396 | [' '] = 1, ['\t'] = 1, |
| 397 | ['\r'] = 1, ['\n'] = 1, |
| 398 | }; |
| 399 | |
| 400 | const char http_is_sep[256] = { |
| 401 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 402 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 403 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 404 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 405 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 406 | }; |
| 407 | |
| 408 | const char http_is_ctl[256] = { |
| 409 | [0 ... 31] = 1, |
| 410 | [127] = 1, |
| 411 | }; |
| 412 | |
| 413 | /* |
| 414 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 415 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 416 | * them correctly. Instead, define every non-CTL char's status. |
| 417 | */ |
| 418 | const char http_is_token[256] = { |
| 419 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 420 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 421 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 422 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 423 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 424 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 425 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 426 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 427 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 428 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 429 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 430 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 431 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 432 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 433 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 434 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 435 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 436 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 437 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 438 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 439 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 440 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 441 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 442 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 443 | }; |
| 444 | |
| 445 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 446 | /* |
| 447 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 448 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 449 | */ |
| 450 | const char http_is_ver_token[256] = { |
| 451 | ['.'] = 1, ['/'] = 1, |
| 452 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 453 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 454 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 455 | }; |
| 456 | |
| 457 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 458 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 459 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 460 | * 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] | 461 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 462 | * of headers is automatically adjusted. The number of bytes added is returned |
| 463 | * on success, otherwise <0 is returned indicating an error. |
| 464 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 465 | 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] | 466 | { |
| 467 | int bytes, len; |
| 468 | |
| 469 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 470 | 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] | 471 | if (!bytes) |
| 472 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 473 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 474 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 475 | } |
| 476 | |
| 477 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 478 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 479 | * 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] | 480 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 481 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 482 | * of headers is automatically adjusted. The number of bytes added is returned |
| 483 | * on success, otherwise <0 is returned indicating an error. |
| 484 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 485 | int http_header_add_tail2(struct http_msg *msg, |
| 486 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 487 | { |
| 488 | int bytes; |
| 489 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 490 | 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] | 491 | if (!bytes) |
| 492 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 493 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 494 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 495 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 496 | |
| 497 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 498 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 499 | * If so, returns the position of the first non-space character relative to |
| 500 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 501 | * to return a pointer to the place after the first space. Returns 0 if the |
| 502 | * header name does not match. Checks are case-insensitive. |
| 503 | */ |
| 504 | int http_header_match2(const char *hdr, const char *end, |
| 505 | const char *name, int len) |
| 506 | { |
| 507 | const char *val; |
| 508 | |
| 509 | if (hdr + len >= end) |
| 510 | return 0; |
| 511 | if (hdr[len] != ':') |
| 512 | return 0; |
| 513 | if (strncasecmp(hdr, name, len) != 0) |
| 514 | return 0; |
| 515 | val = hdr + len + 1; |
| 516 | while (val < end && HTTP_IS_SPHT(*val)) |
| 517 | val++; |
| 518 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 519 | return len + 2; /* we may replace starting from second space */ |
| 520 | return val - hdr; |
| 521 | } |
| 522 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 523 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 524 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 525 | * structure holds everything necessary to use the header and find next |
| 526 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 527 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 528 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 529 | * http_find_header2() except that it is designed to work with full-line headers |
| 530 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 531 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 532 | * header is rescanned. This allows rescanning after a header deletion. |
| 533 | */ |
| 534 | int http_find_full_header2(const char *name, int len, |
| 535 | char *sol, struct hdr_idx *idx, |
| 536 | struct hdr_ctx *ctx) |
| 537 | { |
| 538 | char *eol, *sov; |
| 539 | int cur_idx, old_idx; |
| 540 | |
| 541 | cur_idx = ctx->idx; |
| 542 | if (cur_idx) { |
| 543 | /* We have previously returned a header, let's search another one */ |
| 544 | sol = ctx->line; |
| 545 | eol = sol + idx->v[cur_idx].len; |
| 546 | goto next_hdr; |
| 547 | } |
| 548 | |
| 549 | /* first request for this header */ |
| 550 | sol += hdr_idx_first_pos(idx); |
| 551 | old_idx = 0; |
| 552 | cur_idx = hdr_idx_first_idx(idx); |
| 553 | while (cur_idx) { |
| 554 | eol = sol + idx->v[cur_idx].len; |
| 555 | |
| 556 | if (len == 0) { |
| 557 | /* No argument was passed, we want any header. |
| 558 | * To achieve this, we simply build a fake request. */ |
| 559 | while (sol + len < eol && sol[len] != ':') |
| 560 | len++; |
| 561 | name = sol; |
| 562 | } |
| 563 | |
| 564 | if ((len < eol - sol) && |
| 565 | (sol[len] == ':') && |
| 566 | (strncasecmp(sol, name, len) == 0)) { |
| 567 | ctx->del = len; |
| 568 | sov = sol + len + 1; |
| 569 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 570 | sov++; |
| 571 | |
| 572 | ctx->line = sol; |
| 573 | ctx->prev = old_idx; |
| 574 | ctx->idx = cur_idx; |
| 575 | ctx->val = sov - sol; |
| 576 | ctx->tws = 0; |
| 577 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 578 | eol--; |
| 579 | ctx->tws++; |
| 580 | } |
| 581 | ctx->vlen = eol - sov; |
| 582 | return 1; |
| 583 | } |
| 584 | next_hdr: |
| 585 | sol = eol + idx->v[cur_idx].cr + 1; |
| 586 | old_idx = cur_idx; |
| 587 | cur_idx = idx->v[cur_idx].next; |
| 588 | } |
| 589 | return 0; |
| 590 | } |
| 591 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 592 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 593 | * par 2.2 for more information. Note that it requires a valid header to return |
| 594 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 595 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 596 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 597 | { |
| 598 | int quoted, qdpair; |
| 599 | |
| 600 | quoted = qdpair = 0; |
| 601 | for (; s < e; s++) { |
| 602 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 603 | else if (quoted) { |
| 604 | if (*s == '\\') qdpair = 1; |
| 605 | else if (*s == '"') quoted = 0; |
| 606 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 607 | else if (*s == '"') quoted = 1; |
| 608 | else if (*s == ',') return s; |
| 609 | } |
| 610 | return s; |
| 611 | } |
| 612 | |
| 613 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 614 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 615 | * structure holds everything necessary to use the header and find next |
| 616 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 617 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 618 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 619 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 620 | * is NULL when searching for a new values of a header, the current header is |
| 621 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 622 | */ |
| 623 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 624 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 625 | struct hdr_ctx *ctx) |
| 626 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 627 | char *eol, *sov; |
| 628 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 629 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 630 | cur_idx = ctx->idx; |
| 631 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 632 | /* We have previously returned a value, let's search |
| 633 | * another one on the same line. |
| 634 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 635 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 636 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 637 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 638 | eol = sol + idx->v[cur_idx].len; |
| 639 | |
| 640 | if (sov >= eol) |
| 641 | /* no more values in this header */ |
| 642 | goto next_hdr; |
| 643 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 644 | /* values remaining for this header, skip the comma but save it |
| 645 | * for later use (eg: for header deletion). |
| 646 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 647 | sov++; |
| 648 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 649 | sov++; |
| 650 | |
| 651 | goto return_hdr; |
| 652 | } |
| 653 | |
| 654 | /* first request for this header */ |
| 655 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 656 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 657 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 658 | while (cur_idx) { |
| 659 | eol = sol + idx->v[cur_idx].len; |
| 660 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 661 | if (len == 0) { |
| 662 | /* No argument was passed, we want any header. |
| 663 | * To achieve this, we simply build a fake request. */ |
| 664 | while (sol + len < eol && sol[len] != ':') |
| 665 | len++; |
| 666 | name = sol; |
| 667 | } |
| 668 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 669 | if ((len < eol - sol) && |
| 670 | (sol[len] == ':') && |
| 671 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 672 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 673 | sov = sol + len + 1; |
| 674 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 675 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 676 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 677 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 678 | ctx->prev = old_idx; |
| 679 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 680 | ctx->idx = cur_idx; |
| 681 | ctx->val = sov - sol; |
| 682 | |
| 683 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 684 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 685 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 686 | eol--; |
| 687 | ctx->tws++; |
| 688 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 689 | ctx->vlen = eol - sov; |
| 690 | return 1; |
| 691 | } |
| 692 | next_hdr: |
| 693 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 694 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 695 | cur_idx = idx->v[cur_idx].next; |
| 696 | } |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 701 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 702 | struct hdr_ctx *ctx) |
| 703 | { |
| 704 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 705 | } |
| 706 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 707 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 708 | * the http_find_header functions. The value is removed, as well as surrounding |
| 709 | * 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] | 710 | * 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] | 711 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 712 | * no more header, so any processing may stop. The ctx is always left in a form |
| 713 | * that can be handled by http_find_header2() to find next occurrence. |
| 714 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 715 | 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] | 716 | { |
| 717 | int cur_idx = ctx->idx; |
| 718 | char *sol = ctx->line; |
| 719 | struct hdr_idx_elem *hdr; |
| 720 | int delta, skip_comma; |
| 721 | |
| 722 | if (!cur_idx) |
| 723 | return 0; |
| 724 | |
| 725 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 726 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 727 | /* 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] | 728 | 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] | 729 | http_msg_move_end(msg, delta); |
| 730 | idx->used--; |
| 731 | hdr->len = 0; /* unused entry */ |
| 732 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 733 | if (idx->tail == ctx->idx) |
| 734 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 735 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 14acc65 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 736 | 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] | 737 | 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] | 738 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 739 | return ctx->idx; |
| 740 | } |
| 741 | |
| 742 | /* 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] | 743 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 744 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 745 | */ |
| 746 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 747 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 748 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 749 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 750 | NULL, 0); |
| 751 | hdr->len += delta; |
| 752 | http_msg_move_end(msg, delta); |
| 753 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 754 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 755 | return ctx->idx; |
| 756 | } |
| 757 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 758 | /* This function handles a server error at the stream interface level. The |
| 759 | * stream interface is assumed to be already in a closed state. An optional |
| 760 | * message is copied into the input buffer, and an HTTP status code stored. |
| 761 | * 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] | 762 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 763 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 764 | static void http_server_error(struct session *s, struct stream_interface *si, |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 765 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 766 | { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 767 | channel_auto_read(si->ob); |
| 768 | channel_abort(si->ob); |
| 769 | channel_auto_close(si->ob); |
| 770 | channel_erase(si->ob); |
| 771 | channel_auto_close(si->ib); |
| 772 | channel_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 773 | if (status > 0 && msg) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 774 | s->txn.status = status; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 775 | bo_inject(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 776 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 777 | if (!(s->flags & SN_ERR_MASK)) |
| 778 | s->flags |= err; |
| 779 | if (!(s->flags & SN_FINST_MASK)) |
| 780 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 781 | } |
| 782 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 783 | /* This function returns the appropriate error location for the given session |
| 784 | * and message. |
| 785 | */ |
| 786 | |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 787 | struct chunk *http_error_message(struct session *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 788 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 789 | if (s->be->errmsg[msgnum].str) |
| 790 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 791 | else if (s->fe->errmsg[msgnum].str) |
| 792 | return &s->fe->errmsg[msgnum]; |
| 793 | else |
| 794 | return &http_err_chunks[msgnum]; |
| 795 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 796 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 797 | /* |
Willy Tarreau | bcd0336 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 798 | * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown |
| 799 | * ones. |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 800 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 801 | 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] | 802 | { |
| 803 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 804 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 805 | |
| 806 | m = ((unsigned)*str - 'A'); |
| 807 | |
| 808 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 809 | for (h = http_methods[m]; h->len > 0; h++) { |
| 810 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 811 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 812 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 813 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 814 | }; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 815 | } |
Willy Tarreau | bcd0336 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 816 | return HTTP_METH_OTHER; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 817 | } |
| 818 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 819 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 820 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 821 | * It is returned otherwise. |
| 822 | */ |
| 823 | static char * |
| 824 | http_get_path(struct http_txn *txn) |
| 825 | { |
| 826 | char *ptr, *end; |
| 827 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 828 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 829 | end = ptr + txn->req.sl.rq.u_l; |
| 830 | |
| 831 | if (ptr >= end) |
| 832 | return NULL; |
| 833 | |
| 834 | /* RFC2616, par. 5.1.2 : |
| 835 | * Request-URI = "*" | absuri | abspath | authority |
| 836 | */ |
| 837 | |
| 838 | if (*ptr == '*') |
| 839 | return NULL; |
| 840 | |
| 841 | if (isalpha((unsigned char)*ptr)) { |
| 842 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 843 | ptr++; |
| 844 | while (ptr < end && |
| 845 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 846 | ptr++; |
| 847 | /* skip '://' */ |
| 848 | if (ptr == end || *ptr++ != ':') |
| 849 | return NULL; |
| 850 | if (ptr == end || *ptr++ != '/') |
| 851 | return NULL; |
| 852 | if (ptr == end || *ptr++ != '/') |
| 853 | return NULL; |
| 854 | } |
| 855 | /* skip [user[:passwd]@]host[:[port]] */ |
| 856 | |
| 857 | while (ptr < end && *ptr != '/') |
| 858 | ptr++; |
| 859 | |
| 860 | if (ptr == end) |
| 861 | return NULL; |
| 862 | |
| 863 | /* OK, we got the '/' ! */ |
| 864 | return ptr; |
| 865 | } |
| 866 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 867 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 868 | * If not found, return NULL. It is returned otherwise. |
| 869 | */ |
| 870 | static char * |
| 871 | http_get_path_from_string(char *str) |
| 872 | { |
| 873 | char *ptr = str; |
| 874 | |
| 875 | /* RFC2616, par. 5.1.2 : |
| 876 | * Request-URI = "*" | absuri | abspath | authority |
| 877 | */ |
| 878 | |
| 879 | if (*ptr == '*') |
| 880 | return NULL; |
| 881 | |
| 882 | if (isalpha((unsigned char)*ptr)) { |
| 883 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 884 | ptr++; |
| 885 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 886 | ptr++; |
| 887 | /* skip '://' */ |
| 888 | if (*ptr == '\0' || *ptr++ != ':') |
| 889 | return NULL; |
| 890 | if (*ptr == '\0' || *ptr++ != '/') |
| 891 | return NULL; |
| 892 | if (*ptr == '\0' || *ptr++ != '/') |
| 893 | return NULL; |
| 894 | } |
| 895 | /* skip [user[:passwd]@]host[:[port]] */ |
| 896 | |
| 897 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 898 | ptr++; |
| 899 | |
| 900 | if (*ptr == '\0' || *ptr == ' ') |
| 901 | return NULL; |
| 902 | |
| 903 | /* OK, we got the '/' ! */ |
| 904 | return ptr; |
| 905 | } |
| 906 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 907 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 908 | * in redirect mode. This may only be called just after the stream interface |
| 909 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 910 | * follow normal proxy processing. NOTE: this function is designed to support |
| 911 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 912 | */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 913 | void http_perform_server_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 914 | { |
| 915 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 916 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 917 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 918 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 919 | |
| 920 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 921 | trash.len = strlen(HTTP_302); |
| 922 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 923 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 924 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 925 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 926 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 927 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 928 | return; |
| 929 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 930 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 931 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 932 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 933 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 934 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 935 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 936 | /* 3: add the request URI. Since it was already forwarded, we need |
| 937 | * to temporarily rewind the buffer. |
| 938 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 939 | txn = &s->txn; |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 940 | b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 941 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 942 | path = http_get_path(txn); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 943 | 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] | 944 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 945 | b_adv(s->req->buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 946 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 947 | if (!path) |
| 948 | return; |
| 949 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 950 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 951 | return; |
| 952 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 953 | memcpy(trash.str + trash.len, path, len); |
| 954 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 955 | |
| 956 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 957 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 958 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 959 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 960 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 961 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 962 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 963 | |
| 964 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 965 | si_shutr(si); |
| 966 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 967 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 968 | si->state = SI_ST_CLO; |
| 969 | |
| 970 | /* send the message */ |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 971 | http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 972 | |
| 973 | /* 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] | 974 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 975 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 976 | } |
| 977 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 978 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 979 | * that the server side is closed. Note that err_type is actually a |
| 980 | * bitmask, where almost only aborts may be cumulated with other |
| 981 | * values. We consider that aborted operations are more important |
| 982 | * than timeouts or errors due to the fact that nobody else in the |
| 983 | * logs might explain incomplete retries. All others should avoid |
| 984 | * being cumulated. It should normally not be possible to have multiple |
| 985 | * 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] | 986 | * Note that connection errors appearing on the second request of a keep-alive |
| 987 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 988 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 989 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 990 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 991 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 992 | |
| 993 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 994 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 995 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 996 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 997 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 998 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 999 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1000 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1001 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1002 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1003 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1004 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1005 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1006 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1007 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1008 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 1009 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1010 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1011 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
Willy Tarreau | 3634624 | 2014-02-24 18:26:30 +0100 | [diff] [blame] | 1012 | 503, (s->flags & SN_SRV_REUSED) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1013 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1014 | else if (err_type & SI_ET_CONN_RES) |
| 1015 | http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1016 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 1017 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1018 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1019 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1020 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1021 | } |
| 1022 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1023 | extern const char sess_term_cond[8]; |
| 1024 | extern const char sess_fin_state[8]; |
| 1025 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1026 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1027 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1028 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1029 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1030 | /* |
| 1031 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1032 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1033 | */ |
| 1034 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1035 | char **cap, struct cap_hdr *cap_hdr) |
| 1036 | { |
| 1037 | char *eol, *sol, *col, *sov; |
| 1038 | int cur_idx; |
| 1039 | struct cap_hdr *h; |
| 1040 | int len; |
| 1041 | |
| 1042 | sol = som + hdr_idx_first_pos(idx); |
| 1043 | cur_idx = hdr_idx_first_idx(idx); |
| 1044 | |
| 1045 | while (cur_idx) { |
| 1046 | eol = sol + idx->v[cur_idx].len; |
| 1047 | |
| 1048 | col = sol; |
| 1049 | while (col < eol && *col != ':') |
| 1050 | col++; |
| 1051 | |
| 1052 | sov = col + 1; |
| 1053 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1054 | sov++; |
| 1055 | |
| 1056 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1057 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1058 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1059 | if (cap[h->index] == NULL) |
| 1060 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1061 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1062 | |
| 1063 | if (cap[h->index] == NULL) { |
| 1064 | Alert("HTTP capture : out of memory.\n"); |
| 1065 | continue; |
| 1066 | } |
| 1067 | |
| 1068 | len = eol - sov; |
| 1069 | if (len > h->len) |
| 1070 | len = h->len; |
| 1071 | |
| 1072 | memcpy(cap[h->index], sov, len); |
| 1073 | cap[h->index][len]=0; |
| 1074 | } |
| 1075 | } |
| 1076 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1077 | cur_idx = idx->v[cur_idx].next; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1082 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1083 | */ |
| 1084 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1085 | |
| 1086 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1087 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1088 | */ |
| 1089 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1090 | ptr++; \ |
| 1091 | if (likely(ptr < end)) \ |
| 1092 | goto good; \ |
| 1093 | else { \ |
| 1094 | state = (st); \ |
| 1095 | goto http_msg_ood; \ |
| 1096 | } \ |
| 1097 | } while (0) |
| 1098 | |
| 1099 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1100 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1101 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1102 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1103 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1104 | * will give undefined results. |
| 1105 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1106 | * and that msg->sol points to the beginning of the response. |
| 1107 | * If a complete line is found (which implies that at least one CR or LF is |
| 1108 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1109 | * returned indicating an incomplete line (which does not mean that parts have |
| 1110 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1111 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1112 | * upon next call. |
| 1113 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1114 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1115 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1116 | * 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] | 1117 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1118 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1119 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1120 | enum ht_state state, const char *ptr, const char *end, |
| 1121 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1122 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1123 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1124 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1125 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1126 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1127 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1128 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1129 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1130 | |
| 1131 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1132 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1133 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1134 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1135 | state = HTTP_MSG_ERROR; |
| 1136 | break; |
| 1137 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1138 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1139 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1140 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1141 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1142 | goto http_msg_rpcode; |
| 1143 | } |
| 1144 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1145 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1146 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1147 | state = HTTP_MSG_ERROR; |
| 1148 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1149 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1150 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1151 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1152 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1153 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1154 | |
| 1155 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1156 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1157 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1158 | } |
| 1159 | |
| 1160 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1161 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1162 | http_msg_rsp_reason: |
| 1163 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1164 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1165 | msg->sl.st.r_l = 0; |
| 1166 | goto http_msg_rpline_eol; |
| 1167 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1168 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1169 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1170 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1171 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1172 | goto http_msg_rpreason; |
| 1173 | } |
| 1174 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1175 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1176 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1177 | goto http_msg_rsp_reason; |
| 1178 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1179 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1180 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1181 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1182 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1183 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1184 | http_msg_rpline_eol: |
| 1185 | /* We have seen the end of line. Note that we do not |
| 1186 | * necessarily have the \n yet, but at least we know that we |
| 1187 | * have EITHER \r OR \n, otherwise the response would not be |
| 1188 | * complete. We can then record the response length and return |
| 1189 | * to the caller which will be able to register it. |
| 1190 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1191 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1192 | return ptr; |
| 1193 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1194 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1195 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1196 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1197 | exit(1); |
| 1198 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1199 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1203 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1204 | if (ret_state) |
| 1205 | *ret_state = state; |
| 1206 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1207 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1208 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1209 | } |
| 1210 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1211 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1212 | * This function parses a request line between <ptr> and <end>, starting with |
| 1213 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1214 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1215 | * will give undefined results. |
| 1216 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1217 | * and that msg->sol points to the beginning of the request. |
| 1218 | * If a complete line is found (which implies that at least one CR or LF is |
| 1219 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1220 | * returned indicating an incomplete line (which does not mean that parts have |
| 1221 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1222 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1223 | * upon next call. |
| 1224 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1225 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1226 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1227 | * 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] | 1228 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1229 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1230 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1231 | enum ht_state state, const char *ptr, const char *end, |
| 1232 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1233 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1234 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1235 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1236 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1237 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1238 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1239 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1240 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1241 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1242 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1243 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1244 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1245 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1246 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1247 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1248 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1249 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1250 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1251 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1252 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1253 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1254 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1255 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1256 | msg->sl.rq.v_l = 0; |
| 1257 | goto http_msg_rqline_eol; |
| 1258 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1259 | state = HTTP_MSG_ERROR; |
| 1260 | break; |
| 1261 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1262 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1263 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1264 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1265 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1266 | goto http_msg_rquri; |
| 1267 | } |
| 1268 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1269 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1270 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1271 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1272 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1273 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1274 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1275 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1276 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1277 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1278 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1279 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1280 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1281 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1282 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1283 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1284 | /* non-ASCII chars are forbidden unless option |
| 1285 | * accept-invalid-http-request is enabled in the frontend. |
| 1286 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1287 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1288 | if (msg->err_pos < -1) |
| 1289 | goto invalid_char; |
| 1290 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1291 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1292 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1293 | } |
| 1294 | |
| 1295 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1296 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1297 | goto http_msg_req09_uri_e; |
| 1298 | } |
| 1299 | |
| 1300 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1301 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1302 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1303 | state = HTTP_MSG_ERROR; |
| 1304 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1305 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1306 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1307 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1308 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1309 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1310 | goto http_msg_rqver; |
| 1311 | } |
| 1312 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1313 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1314 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1315 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1316 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1317 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1318 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1319 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1320 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1321 | |
| 1322 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1323 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1324 | http_msg_rqline_eol: |
| 1325 | /* We have seen the end of line. Note that we do not |
| 1326 | * necessarily have the \n yet, but at least we know that we |
| 1327 | * have EITHER \r OR \n, otherwise the request would not be |
| 1328 | * complete. We can then record the request length and return |
| 1329 | * to the caller which will be able to register it. |
| 1330 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1331 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1332 | return ptr; |
| 1333 | } |
| 1334 | |
| 1335 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1336 | state = HTTP_MSG_ERROR; |
| 1337 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1338 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1339 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1340 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1341 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1342 | exit(1); |
| 1343 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1344 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1345 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1346 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1347 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1348 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1349 | if (ret_state) |
| 1350 | *ret_state = state; |
| 1351 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1352 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1353 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1354 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1355 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1356 | /* |
| 1357 | * Returns the data from Authorization header. Function may be called more |
| 1358 | * than once so data is stored in txn->auth_data. When no header is found |
| 1359 | * 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] | 1360 | * searching again for something we are unable to find anyway. However, if |
| 1361 | * the result if valid, the cache is not reused because we would risk to |
| 1362 | * have the credentials overwritten by another session in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1363 | */ |
| 1364 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 1365 | /* This bufffer is initialized in the file 'src/haproxy.c'. This length is |
| 1366 | * set according to global.tune.bufsize. |
| 1367 | */ |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1368 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1369 | |
| 1370 | int |
| 1371 | get_http_auth(struct session *s) |
| 1372 | { |
| 1373 | |
| 1374 | struct http_txn *txn = &s->txn; |
| 1375 | struct chunk auth_method; |
| 1376 | struct hdr_ctx ctx; |
| 1377 | char *h, *p; |
| 1378 | int len; |
| 1379 | |
| 1380 | #ifdef DEBUG_AUTH |
| 1381 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1382 | #endif |
| 1383 | |
| 1384 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1385 | return 0; |
| 1386 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1387 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1388 | |
| 1389 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1390 | |
| 1391 | if (txn->flags & TX_USE_PX_CONN) { |
| 1392 | h = "Proxy-Authorization"; |
| 1393 | len = strlen(h); |
| 1394 | } else { |
| 1395 | h = "Authorization"; |
| 1396 | len = strlen(h); |
| 1397 | } |
| 1398 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1399 | 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] | 1400 | return 0; |
| 1401 | |
| 1402 | h = ctx.line + ctx.val; |
| 1403 | |
| 1404 | p = memchr(h, ' ', ctx.vlen); |
Willy Tarreau | 2b9b25b | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1405 | len = p - h; |
| 1406 | if (!p || len <= 0) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1407 | return 0; |
| 1408 | |
Willy Tarreau | 2b9b25b | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1409 | chunk_initlen(&auth_method, h, 0, len); |
| 1410 | chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1411 | |
| 1412 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1413 | |
| 1414 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1415 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1416 | |
| 1417 | if (len < 0) |
| 1418 | return 0; |
| 1419 | |
| 1420 | |
| 1421 | get_http_auth_buff[len] = '\0'; |
| 1422 | |
| 1423 | p = strchr(get_http_auth_buff, ':'); |
| 1424 | |
| 1425 | if (!p) |
| 1426 | return 0; |
| 1427 | |
| 1428 | txn->auth.user = get_http_auth_buff; |
| 1429 | *p = '\0'; |
| 1430 | txn->auth.pass = p+1; |
| 1431 | |
| 1432 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1433 | return 1; |
| 1434 | } |
| 1435 | |
| 1436 | return 0; |
| 1437 | } |
| 1438 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1439 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1440 | /* |
| 1441 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1442 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1443 | * ensuring that the message does not wrap. The function can be preempted |
| 1444 | * everywhere when data are missing and recalled at the exact same location |
| 1445 | * with no information loss. The message may even be realigned between two |
| 1446 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1447 | * 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] | 1448 | * fields. Note that msg->sol will be initialized after completing the first |
| 1449 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1450 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1451 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1452 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1453 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1454 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1455 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1456 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1457 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1458 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1459 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1460 | ptr = buf->p + msg->next; |
| 1461 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1462 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1463 | if (unlikely(ptr >= end)) |
| 1464 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1465 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1466 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1467 | /* |
| 1468 | * First, states that are specific to the response only. |
| 1469 | * We check them first so that request and headers are |
| 1470 | * closer to each other (accessed more often). |
| 1471 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1472 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1473 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1474 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1475 | /* we have a start of message, but we have to check |
| 1476 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1477 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1478 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1479 | if (unlikely(ptr != buf->p)) { |
| 1480 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1481 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1482 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1483 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1484 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1485 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1486 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1487 | hdr_idx_init(idx); |
| 1488 | state = HTTP_MSG_RPVER; |
| 1489 | goto http_msg_rpver; |
| 1490 | } |
| 1491 | |
| 1492 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1493 | goto http_msg_invalid; |
| 1494 | |
| 1495 | if (unlikely(*ptr == '\n')) |
| 1496 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1497 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1498 | /* stop here */ |
| 1499 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1500 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1501 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1502 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1503 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1504 | /* stop here */ |
| 1505 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1506 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1507 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1508 | case HTTP_MSG_RPVER_SP: |
| 1509 | case HTTP_MSG_RPCODE: |
| 1510 | case HTTP_MSG_RPCODE_SP: |
| 1511 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1512 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1513 | state, ptr, end, |
| 1514 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1515 | if (unlikely(!ptr)) |
| 1516 | return; |
| 1517 | |
| 1518 | /* we have a full response and we know that we have either a CR |
| 1519 | * or an LF at <ptr>. |
| 1520 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1521 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1522 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1523 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1524 | if (likely(*ptr == '\r')) |
| 1525 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1526 | goto http_msg_rpline_end; |
| 1527 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1528 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1529 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1530 | /* msg->sol must point to the first of CR or LF. */ |
| 1531 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1532 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1533 | /* stop here */ |
| 1534 | |
| 1535 | /* |
| 1536 | * Second, states that are specific to the request only |
| 1537 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1538 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1539 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1540 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1541 | /* we have a start of message, but we have to check |
| 1542 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1543 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1544 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1545 | if (likely(ptr != buf->p)) { |
| 1546 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1547 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1548 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1549 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1550 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1551 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1552 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1553 | state = HTTP_MSG_RQMETH; |
| 1554 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1555 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1556 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1557 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1558 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1559 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1560 | if (unlikely(*ptr == '\n')) |
| 1561 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1562 | 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] | 1563 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1564 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1565 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1566 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1567 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1568 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1569 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1570 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1571 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1572 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1573 | case HTTP_MSG_RQMETH_SP: |
| 1574 | case HTTP_MSG_RQURI: |
| 1575 | case HTTP_MSG_RQURI_SP: |
| 1576 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1577 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1578 | state, ptr, end, |
| 1579 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1580 | if (unlikely(!ptr)) |
| 1581 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1582 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1583 | /* we have a full request and we know that we have either a CR |
| 1584 | * or an LF at <ptr>. |
| 1585 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1586 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1587 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1588 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1589 | if (likely(*ptr == '\r')) |
| 1590 | 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] | 1591 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1592 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1593 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1594 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1595 | /* check for HTTP/0.9 request : no version information available. |
| 1596 | * msg->sol must point to the first of CR or LF. |
| 1597 | */ |
| 1598 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1599 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1600 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1601 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1602 | 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] | 1603 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1604 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1605 | /* |
| 1606 | * Common states below |
| 1607 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1608 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1609 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1610 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1611 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1612 | goto http_msg_hdr_name; |
| 1613 | } |
| 1614 | |
| 1615 | if (likely(*ptr == '\r')) |
| 1616 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1617 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1618 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1619 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1620 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1621 | /* assumes msg->sol points to the first char */ |
| 1622 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1623 | 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] | 1624 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1625 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1626 | 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] | 1627 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1628 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1629 | goto http_msg_invalid; |
| 1630 | |
| 1631 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1632 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1633 | |
| 1634 | /* and we still accept this non-token character */ |
| 1635 | 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] | 1636 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1637 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1638 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1639 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1640 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1641 | 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] | 1642 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1643 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1644 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1645 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1646 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1647 | goto http_msg_hdr_val; |
| 1648 | } |
| 1649 | |
| 1650 | if (likely(*ptr == '\r')) |
| 1651 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1652 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1653 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1654 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1655 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1656 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1657 | 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] | 1658 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1659 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1660 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1661 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1662 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1663 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1664 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1665 | goto http_msg_hdr_l1_sp; |
| 1666 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1667 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1668 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1669 | goto http_msg_complete_header; |
| 1670 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1671 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1672 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1673 | /* assumes msg->sol points to the first char, and msg->sov |
| 1674 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1675 | */ |
| 1676 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1677 | 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] | 1678 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1679 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1680 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1681 | * real header end in case it ends with lots of LWS, but is this |
| 1682 | * really needed ? |
| 1683 | */ |
| 1684 | if (likely(*ptr == '\r')) |
| 1685 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1686 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1687 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1688 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1689 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1690 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1691 | 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] | 1692 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1693 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1694 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1695 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1696 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1697 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1698 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1699 | goto http_msg_hdr_val; |
| 1700 | } |
| 1701 | http_msg_complete_header: |
| 1702 | /* |
| 1703 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1704 | * Assumes msg->sol points to the first char, msg->sov points |
| 1705 | * to the first character of the value and msg->eol to the |
| 1706 | * first CR or LF so we know how the line ends. We insert last |
| 1707 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1708 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1709 | 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] | 1710 | idx, idx->tail) < 0)) |
| 1711 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1712 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1713 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1714 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1715 | goto http_msg_hdr_name; |
| 1716 | } |
| 1717 | |
| 1718 | if (likely(*ptr == '\r')) |
| 1719 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1720 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1721 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1722 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1723 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1724 | /* Assumes msg->sol points to the first of either CR or LF. |
| 1725 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 1726 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 1727 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1728 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1729 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1730 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1731 | msg->eoh = msg->sol; |
| 1732 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1733 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1734 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1735 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1736 | |
| 1737 | case HTTP_MSG_ERROR: |
| 1738 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1739 | break; |
| 1740 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1741 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1742 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1743 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1744 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1745 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1746 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1747 | } |
| 1748 | http_msg_ood: |
| 1749 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1750 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1751 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1752 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1753 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1754 | http_msg_invalid: |
| 1755 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1756 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1757 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1758 | return; |
| 1759 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1760 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1761 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1762 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1763 | * nothing is done and 1 is returned. |
| 1764 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1765 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1766 | { |
| 1767 | int delta; |
| 1768 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1769 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1770 | |
| 1771 | if (msg->sl.rq.v_l != 0) |
| 1772 | return 1; |
| 1773 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1774 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1775 | if (txn->meth != HTTP_METH_GET) |
| 1776 | return 0; |
| 1777 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1778 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1779 | delta = 0; |
| 1780 | |
| 1781 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1782 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1783 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1784 | } |
| 1785 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1786 | 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] | 1787 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1788 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1789 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1790 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1791 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1792 | NULL, NULL); |
| 1793 | if (unlikely(!cur_end)) |
| 1794 | return 0; |
| 1795 | |
| 1796 | /* we have a full HTTP/1.0 request now and we know that |
| 1797 | * we have either a CR or an LF at <ptr>. |
| 1798 | */ |
| 1799 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1800 | return 1; |
| 1801 | } |
| 1802 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1803 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1804 | * and "keep-alive" values. If we already know that some headers may safely |
| 1805 | * 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] | 1806 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1807 | * - 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] | 1808 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1809 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1810 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1811 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1812 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1813 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1814 | 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] | 1815 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1816 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1817 | const char *hdr_val = "Connection"; |
| 1818 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1819 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1820 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1821 | return; |
| 1822 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1823 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1824 | hdr_val = "Proxy-Connection"; |
| 1825 | hdr_len = 16; |
| 1826 | } |
| 1827 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1828 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1829 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1830 | 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] | 1831 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1832 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1833 | if (to_del & 2) |
| 1834 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1835 | else |
| 1836 | txn->flags |= TX_CON_KAL_SET; |
| 1837 | } |
| 1838 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1839 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1840 | if (to_del & 1) |
| 1841 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1842 | else |
| 1843 | txn->flags |= TX_CON_CLO_SET; |
| 1844 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1845 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1846 | txn->flags |= TX_HDR_CONN_UPG; |
| 1847 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1848 | } |
| 1849 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1850 | txn->flags |= TX_HDR_CONN_PRS; |
| 1851 | return; |
| 1852 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1853 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1854 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1855 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1856 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1857 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1858 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1859 | 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] | 1860 | { |
| 1861 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1862 | const char *hdr_val = "Connection"; |
| 1863 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1864 | |
| 1865 | ctx.idx = 0; |
| 1866 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1867 | |
| 1868 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1869 | hdr_val = "Proxy-Connection"; |
| 1870 | hdr_len = 16; |
| 1871 | } |
| 1872 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1873 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1874 | 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] | 1875 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1876 | if (wanted & TX_CON_KAL_SET) |
| 1877 | txn->flags |= TX_CON_KAL_SET; |
| 1878 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1879 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1880 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1881 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1882 | if (wanted & TX_CON_CLO_SET) |
| 1883 | txn->flags |= TX_CON_CLO_SET; |
| 1884 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1885 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1886 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1887 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1888 | |
| 1889 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1890 | return; |
| 1891 | |
| 1892 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1893 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1894 | hdr_val = "Connection: close"; |
| 1895 | hdr_len = 17; |
| 1896 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1897 | hdr_val = "Proxy-Connection: close"; |
| 1898 | hdr_len = 23; |
| 1899 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1900 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1904 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1905 | hdr_val = "Connection: keep-alive"; |
| 1906 | hdr_len = 22; |
| 1907 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1908 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1909 | hdr_len = 28; |
| 1910 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1911 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1912 | } |
| 1913 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1914 | } |
| 1915 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 1916 | /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to |
| 1917 | * the first byte of data after the chunk size, so that we know we can forward |
| 1918 | * exactly msg->next bytes. msg->sol contains the exact number of bytes forming |
| 1919 | * the chunk size. That way it is always possible to differentiate between the |
| 1920 | * start of the body and the start of the data. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1921 | * 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] | 1922 | * 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] | 1923 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1924 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1925 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1926 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1927 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1928 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1929 | const char *end = buf->data + buf->size; |
| 1930 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1931 | unsigned int chunk = 0; |
| 1932 | |
| 1933 | /* The chunk size is in the following form, though we are only |
| 1934 | * interested in the size and CRLF : |
| 1935 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1936 | */ |
| 1937 | while (1) { |
| 1938 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1939 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1940 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1941 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1942 | if (c < 0) /* not a hex digit anymore */ |
| 1943 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1944 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1945 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1946 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1947 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1948 | chunk = (chunk << 4) + c; |
| 1949 | } |
| 1950 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1951 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1952 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1953 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1954 | |
| 1955 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1956 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1957 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1958 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1959 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1960 | } |
| 1961 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1962 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1963 | * for the end of chunk size. |
| 1964 | */ |
| 1965 | while (1) { |
| 1966 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1967 | /* we now have a CR or an LF at ptr */ |
| 1968 | if (likely(*ptr == '\r')) { |
| 1969 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1970 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1971 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1972 | return 0; |
| 1973 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1974 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1975 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1976 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1977 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1978 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1979 | /* done */ |
| 1980 | break; |
| 1981 | } |
| 1982 | else if (*ptr == ';') { |
| 1983 | /* chunk extension, ends at next CRLF */ |
| 1984 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1985 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1986 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1987 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1988 | |
| 1989 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1990 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1991 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1992 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1993 | return 0; |
| 1994 | } |
| 1995 | /* we have a CRLF now, loop above */ |
| 1996 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1997 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1998 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1999 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2000 | } |
| 2001 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2002 | /* 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] | 2003 | * which may or may not be present. We save that into ->next, |
| 2004 | * and the number of bytes parsed into msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2005 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2006 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2007 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2008 | msg->sol += buf->size; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2009 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2010 | msg->chunk_len = chunk; |
| 2011 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2012 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2013 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2014 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2015 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2016 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2017 | } |
| 2018 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2019 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2020 | * 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] | 2021 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2022 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2023 | * If not enough data are available, the function does not change anything |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2024 | * except maybe msg->next if it could parse some lines, and returns zero. |
| 2025 | * If a parse error is encountered, the function returns < 0 and does not |
| 2026 | * change anything except maybe msg->next. Note that the message must |
| 2027 | * already be in HTTP_MSG_TRAILERS state before calling this function, |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2028 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2029 | * forwarding, and that msg->next exactly matches the length of trailers |
| 2030 | * already parsed and not forwarded. It is also important to note that this |
| 2031 | * 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] | 2032 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2033 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2034 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2035 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2036 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2037 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2038 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2039 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2040 | const char *ptr = b_ptr(buf, msg->next); |
| 2041 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2042 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2043 | |
| 2044 | /* scan current line and stop at LF or CRLF */ |
| 2045 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2046 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2047 | return 0; |
| 2048 | |
| 2049 | if (*ptr == '\n') { |
| 2050 | if (!p1) |
| 2051 | p1 = ptr; |
| 2052 | p2 = ptr; |
| 2053 | break; |
| 2054 | } |
| 2055 | |
| 2056 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2057 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2058 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2059 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2060 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2061 | p1 = ptr; |
| 2062 | } |
| 2063 | |
| 2064 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2065 | if (ptr >= buf->data + buf->size) |
| 2066 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | /* after LF; point to beginning of next line */ |
| 2070 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2071 | if (p2 >= buf->data + buf->size) |
| 2072 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2073 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2074 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2075 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2076 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2077 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2078 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2079 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2080 | * Everything was scheduled for forwarding, there's nothing |
| 2081 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2082 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2083 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2084 | msg->msg_state = HTTP_MSG_DONE; |
| 2085 | return 1; |
| 2086 | } |
| 2087 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2088 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2089 | } |
| 2090 | } |
| 2091 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2092 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF |
| 2093 | * or a possible LF alone at the end of a chunk. It automatically adjusts |
| 2094 | * 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] | 2095 | * 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] | 2096 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2097 | * not enough data are available, the function does not change anything and |
| 2098 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2099 | * does not change anything. Note: this function is designed to parse wrapped |
| 2100 | * CRLF at the end of the buffer. |
| 2101 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2102 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2103 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2104 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2105 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2106 | int bytes; |
| 2107 | |
| 2108 | /* NB: we'll check data availabilty at the end. It's not a |
| 2109 | * problem because whatever we match first will be checked |
| 2110 | * against the correct length. |
| 2111 | */ |
| 2112 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2113 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2114 | if (*ptr == '\r') { |
| 2115 | bytes++; |
| 2116 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2117 | if (ptr >= buf->data + buf->size) |
| 2118 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2119 | } |
| 2120 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2121 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2122 | return 0; |
| 2123 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2124 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2125 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2126 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2127 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2128 | |
| 2129 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2130 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2131 | ptr = buf->data; |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2132 | /* Advance ->next to allow the CRLF to be forwarded */ |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2133 | msg->next += bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2134 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2135 | return 1; |
| 2136 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2137 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2138 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2139 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2140 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2141 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2142 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2143 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2144 | { |
| 2145 | int q = 1000; |
| 2146 | |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2147 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2148 | goto out; |
| 2149 | q = (*qvalue++ - '0') * 1000; |
| 2150 | |
| 2151 | if (*qvalue++ != '.') |
| 2152 | goto out; |
| 2153 | |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2154 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2155 | goto out; |
| 2156 | q += (*qvalue++ - '0') * 100; |
| 2157 | |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2158 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2159 | goto out; |
| 2160 | q += (*qvalue++ - '0') * 10; |
| 2161 | |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2162 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2163 | goto out; |
| 2164 | q += (*qvalue++ - '0') * 1; |
| 2165 | out: |
| 2166 | if (q > 1000) |
| 2167 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2168 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2169 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2170 | return q; |
| 2171 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2172 | |
| 2173 | /* |
| 2174 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2175 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2176 | int select_compression_request_header(struct session *s, struct buffer *req) |
| 2177 | { |
| 2178 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2179 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2180 | struct hdr_ctx ctx; |
| 2181 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2182 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2183 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2184 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 2185 | * 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] | 2186 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 2187 | */ |
| 2188 | ctx.idx = 0; |
| 2189 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2190 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2191 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2192 | (ctx.vlen < 31 || |
| 2193 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2194 | ctx.line[ctx.val + 30] < '6' || |
| 2195 | (ctx.line[ctx.val + 30] == '6' && |
| 2196 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2197 | s->comp_algo = NULL; |
| 2198 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2199 | } |
| 2200 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2201 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2202 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2203 | int best_q = 0; |
| 2204 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2205 | ctx.idx = 0; |
| 2206 | 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] | 2207 | const char *qval; |
| 2208 | int q; |
| 2209 | int toklen; |
| 2210 | |
| 2211 | /* try to isolate the token from the optional q-value */ |
| 2212 | toklen = 0; |
| 2213 | while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)]) |
| 2214 | toklen++; |
| 2215 | |
| 2216 | qval = ctx.line + ctx.val + toklen; |
| 2217 | while (1) { |
| 2218 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2219 | qval++; |
| 2220 | |
| 2221 | if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') { |
| 2222 | qval = NULL; |
| 2223 | break; |
| 2224 | } |
| 2225 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2226 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2227 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2228 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2229 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2230 | if (qval >= ctx.line + ctx.val + ctx.vlen) { |
| 2231 | qval = NULL; |
| 2232 | break; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2233 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2234 | if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0) |
| 2235 | break; |
| 2236 | |
| 2237 | while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';') |
| 2238 | qval++; |
| 2239 | } |
| 2240 | |
| 2241 | /* 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] | 2242 | q = qval ? parse_qvalue(qval + 2, NULL) : 1000; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2243 | |
| 2244 | if (q <= best_q) |
| 2245 | continue; |
| 2246 | |
| 2247 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 2248 | if (*(ctx.line + ctx.val) == '*' || |
| 2249 | word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) { |
| 2250 | s->comp_algo = comp_algo; |
| 2251 | best_q = q; |
| 2252 | break; |
| 2253 | } |
| 2254 | } |
| 2255 | } |
| 2256 | } |
| 2257 | |
| 2258 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2259 | if (s->comp_algo) { |
| 2260 | if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) { |
| 2261 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2262 | ctx.idx = 0; |
| 2263 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2264 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2265 | } |
| 2266 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2267 | return 1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | /* identity is implicit does not require headers */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2271 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
| 2272 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2273 | if (comp_algo->add_data == identity_add_data) { |
| 2274 | s->comp_algo = comp_algo; |
| 2275 | return 1; |
| 2276 | } |
| 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2281 | return 0; |
| 2282 | } |
| 2283 | |
| 2284 | /* |
| 2285 | * Selects a comression algorithm depending of the server response. |
| 2286 | */ |
| 2287 | int select_compression_response_header(struct session *s, struct buffer *res) |
| 2288 | { |
| 2289 | struct http_txn *txn = &s->txn; |
| 2290 | struct http_msg *msg = &txn->rsp; |
| 2291 | struct hdr_ctx ctx; |
| 2292 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2293 | |
| 2294 | /* no common compression algorithm was found in request header */ |
| 2295 | if (s->comp_algo == NULL) |
| 2296 | goto fail; |
| 2297 | |
| 2298 | /* HTTP < 1.1 should not be compressed */ |
Willy Tarreau | 7257550 | 2013-12-24 14:41:35 +0100 | [diff] [blame] | 2299 | 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] | 2300 | goto fail; |
| 2301 | |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2302 | /* 200 only */ |
| 2303 | if (txn->status != 200) |
| 2304 | goto fail; |
| 2305 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2306 | /* Content-Length is null */ |
| 2307 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2308 | goto fail; |
| 2309 | |
| 2310 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2311 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2312 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2313 | goto fail; |
| 2314 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2315 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2316 | ctx.idx = 0; |
| 2317 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2318 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2319 | goto fail; |
| 2320 | } |
| 2321 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2322 | comp_type = NULL; |
| 2323 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2324 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2325 | * not listed in the "compression type" directive if any. If no content-type was |
| 2326 | * found but configuration requires one, we don't compress either. Backend has |
| 2327 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2328 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2329 | ctx.idx = 0; |
| 2330 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2331 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2332 | goto fail; |
| 2333 | |
| 2334 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 2335 | (s->fe->comp && (comp_type = s->fe->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2336 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2337 | if (ctx.vlen >= comp_type->name_len && |
| 2338 | 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] | 2339 | /* this Content-Type should be compressed */ |
| 2340 | break; |
| 2341 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2342 | /* this Content-Type should not be compressed */ |
| 2343 | if (comp_type == NULL) |
| 2344 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2345 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2346 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2347 | else { /* no content-type header */ |
| 2348 | if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types)) |
| 2349 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2350 | } |
| 2351 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2352 | /* limit compression rate */ |
| 2353 | if (global.comp_rate_lim > 0) |
| 2354 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2355 | goto fail; |
| 2356 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2357 | /* limit cpu usage */ |
| 2358 | if (idle_pct < compress_min_idle) |
| 2359 | goto fail; |
| 2360 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2361 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2362 | 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] | 2363 | goto fail; |
| 2364 | |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2365 | s->flags |= SN_COMP_READY; |
| 2366 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2367 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2368 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2369 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2370 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2371 | |
| 2372 | /* add Transfer-Encoding header */ |
| 2373 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2374 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2375 | |
| 2376 | /* |
| 2377 | * Add Content-Encoding header when it's not identity encoding. |
| 2378 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2379 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2380 | * header. |
| 2381 | */ |
| 2382 | if (s->comp_algo->add_data != identity_add_data) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2383 | trash.len = 18; |
| 2384 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
| 2385 | memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len); |
| 2386 | trash.len += s->comp_algo->name_len; |
| 2387 | trash.str[trash.len] = '\0'; |
| 2388 | 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] | 2389 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2390 | return 1; |
| 2391 | |
| 2392 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2393 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2394 | return 0; |
| 2395 | } |
| 2396 | |
Willy Tarreau | 2e47a3a | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2397 | void http_adjust_conn_mode(struct session *s, struct http_txn *txn, struct http_msg *msg) |
| 2398 | { |
| 2399 | int tmp = TX_CON_WANT_KAL; |
| 2400 | |
| 2401 | if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 2402 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
| 2403 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2404 | tmp = TX_CON_WANT_TUN; |
| 2405 | |
| 2406 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 2407 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2408 | tmp = TX_CON_WANT_TUN; |
| 2409 | } |
| 2410 | |
| 2411 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 2412 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 2413 | /* option httpclose + server_close => forceclose */ |
| 2414 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 2415 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2416 | tmp = TX_CON_WANT_CLO; |
| 2417 | else |
| 2418 | tmp = TX_CON_WANT_SCL; |
| 2419 | } |
| 2420 | |
| 2421 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
| 2422 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 2423 | tmp = TX_CON_WANT_CLO; |
| 2424 | |
| 2425 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2426 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2427 | |
| 2428 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 2429 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2430 | /* parse the Connection header and possibly clean it */ |
| 2431 | int to_del = 0; |
| 2432 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 2433 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 2434 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
| 2435 | to_del |= 2; /* remove "keep-alive" */ |
| 2436 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2437 | to_del |= 1; /* remove "close" */ |
| 2438 | http_parse_connection_header(txn, msg, to_del); |
| 2439 | } |
| 2440 | |
| 2441 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2442 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2443 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2444 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2445 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 2446 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
| 2447 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
| 2448 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2449 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2450 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2451 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2452 | * processing can continue on next analysers, or zero if it either needs more |
| 2453 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2454 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2455 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2456 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2457 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2458 | int http_wait_for_request(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2459 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2460 | /* |
| 2461 | * We will parse the partial (or complete) lines. |
| 2462 | * We will check the request syntax, and also join multi-line |
| 2463 | * headers. An index of all the lines will be elaborated while |
| 2464 | * parsing. |
| 2465 | * |
| 2466 | * For the parsing, we use a 28 states FSM. |
| 2467 | * |
| 2468 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2469 | * req->buf->p = beginning of request |
| 2470 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2471 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2472 | * msg->eol = end of current header or line (LF or CRLF) |
| 2473 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2474 | * |
| 2475 | * At end of parsing, we may perform a capture of the error (if any), and |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 2476 | * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2477 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2478 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2479 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2480 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2481 | int cur_idx; |
| 2482 | struct http_txn *txn = &s->txn; |
| 2483 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2484 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2485 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2486 | DPRINTF(stderr,"[%u] %s: session=%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] | 2487 | now_ms, __FUNCTION__, |
| 2488 | s, |
| 2489 | req, |
| 2490 | req->rex, req->wex, |
| 2491 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2492 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2493 | req->analysers); |
| 2494 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2495 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2496 | s->srv_error = http_return_srv_error; |
| 2497 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2498 | /* There's a protected area at the end of the buffer for rewriting |
| 2499 | * purposes. We don't want to start to parse the request if the |
| 2500 | * protected area is affected, because we may have to move processed |
| 2501 | * data later, which is much more complicated. |
| 2502 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2503 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2504 | if (txn->flags & TX_NOT_FIRST) { |
| 2505 | if (unlikely(!channel_reserved(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2506 | 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] | 2507 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2508 | /* 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] | 2509 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2510 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2511 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2512 | return 0; |
| 2513 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2514 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2515 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2516 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2517 | } |
| 2518 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2519 | /* Note that we have the same problem with the response ; we |
| 2520 | * may want to send a redirect, error or anything which requires |
| 2521 | * some spare space. So we'll ensure that we have at least |
| 2522 | * maxrewrite bytes available in the response buffer before |
| 2523 | * processing that one. This will only affect pipelined |
| 2524 | * keep-alive requests. |
| 2525 | */ |
| 2526 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2527 | unlikely(!channel_reserved(s->rep) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2528 | bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) || |
| 2529 | bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) { |
| 2530 | if (s->rep->buf->o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2531 | if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2532 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2533 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2534 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2535 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2536 | s->rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2537 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2538 | return 0; |
| 2539 | } |
| 2540 | } |
| 2541 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2542 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2543 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2544 | } |
| 2545 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2546 | /* 1: we might have to print this header in debug mode */ |
| 2547 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2548 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 8767b13 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2549 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2550 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2551 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2552 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2553 | /* this is a bit complex : in case of error on the request line, |
| 2554 | * we know that rq.l is still zero, so we display only the part |
| 2555 | * up to the end of the line (truncated by debug_hdr). |
| 2556 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2557 | 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] | 2558 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2559 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2560 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2561 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2562 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2563 | while (cur_idx) { |
| 2564 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2565 | debug_hdr("clihdr", s, sol, eol); |
| 2566 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2567 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2568 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2569 | } |
| 2570 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2571 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2572 | /* |
| 2573 | * Now we quickly check if we have found a full valid request. |
| 2574 | * If not so, we check the FD and buffer states before leaving. |
| 2575 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2576 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2577 | * requests are checked first. When waiting for a second request |
| 2578 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2579 | * we note the error in the session flags but don't set any state. |
| 2580 | * Since the error will be noted there, it will not be counted by |
| 2581 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2582 | * Last, we may increase some tracked counters' http request errors on |
| 2583 | * the cases that are deliberately the client's fault. For instance, |
| 2584 | * a timeout or connection reset is not counted as an error. However |
| 2585 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2586 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2587 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2588 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2589 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2590 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2591 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2592 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2593 | session_inc_http_req_ctr(s); |
| 2594 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2595 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2596 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2597 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2598 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2599 | /* 1: Since we are in header mode, if there's no space |
| 2600 | * left for headers, we won't be able to free more |
| 2601 | * later, so the session will never terminate. We |
| 2602 | * must terminate it now. |
| 2603 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2604 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2605 | /* FIXME: check if URI is set and return Status |
| 2606 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2607 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2608 | session_inc_http_req_ctr(s); |
| 2609 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2610 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2611 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2612 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2613 | goto return_bad_req; |
| 2614 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2615 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2616 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2617 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2618 | if (!(s->flags & SN_ERR_MASK)) |
| 2619 | s->flags |= SN_ERR_CLICL; |
| 2620 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2621 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2622 | goto failed_keep_alive; |
| 2623 | |
Willy Tarreau | 1c3a612 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2624 | if (s->fe->options & PR_O_IGNORE_PRB) |
| 2625 | goto failed_keep_alive; |
| 2626 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2627 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2628 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2629 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2630 | session_inc_http_err_ctr(s); |
| 2631 | } |
| 2632 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2633 | txn->status = 400; |
| 2634 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2635 | msg->msg_state = HTTP_MSG_ERROR; |
| 2636 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2637 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2638 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2639 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2640 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2641 | if (s->listener->counters) |
| 2642 | s->listener->counters->failed_req++; |
| 2643 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2644 | if (!(s->flags & SN_FINST_MASK)) |
| 2645 | s->flags |= SN_FINST_R; |
| 2646 | return 0; |
| 2647 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2648 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2649 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2650 | else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2651 | if (!(s->flags & SN_ERR_MASK)) |
| 2652 | s->flags |= SN_ERR_CLITO; |
| 2653 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2654 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2655 | goto failed_keep_alive; |
| 2656 | |
Willy Tarreau | 1c3a612 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2657 | if (s->fe->options & PR_O_IGNORE_PRB) |
| 2658 | goto failed_keep_alive; |
| 2659 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2660 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2661 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2662 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2663 | session_inc_http_err_ctr(s); |
| 2664 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2665 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2666 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2667 | msg->msg_state = HTTP_MSG_ERROR; |
| 2668 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2669 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2670 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2671 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2672 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2673 | if (s->listener->counters) |
| 2674 | s->listener->counters->failed_req++; |
| 2675 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2676 | if (!(s->flags & SN_FINST_MASK)) |
| 2677 | s->flags |= SN_FINST_R; |
| 2678 | return 0; |
| 2679 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2680 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2681 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2682 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2683 | if (!(s->flags & SN_ERR_MASK)) |
| 2684 | s->flags |= SN_ERR_CLICL; |
| 2685 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2686 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2687 | goto failed_keep_alive; |
| 2688 | |
Willy Tarreau | 1c3a612 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2689 | if (s->fe->options & PR_O_IGNORE_PRB) |
| 2690 | goto failed_keep_alive; |
| 2691 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2692 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2693 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2694 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2695 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2696 | msg->msg_state = HTTP_MSG_ERROR; |
| 2697 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2698 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2699 | session_inc_http_err_ctr(s); |
| 2700 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2701 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2702 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2703 | if (s->listener->counters) |
| 2704 | s->listener->counters->failed_req++; |
| 2705 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2706 | if (!(s->flags & SN_FINST_MASK)) |
| 2707 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2708 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2709 | } |
| 2710 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2711 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2712 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2713 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2714 | #ifdef TCP_QUICKACK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 2715 | if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && conn_ctrl_ready(__objt_conn(s->req->prod->end))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2716 | /* We need more data, we have to re-enable quick-ack in case we |
| 2717 | * previously disabled it, otherwise we might cause the client |
| 2718 | * to delay next data. |
| 2719 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2720 | setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2721 | } |
| 2722 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2723 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2724 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2725 | /* If the client starts to talk, let's fall back to |
| 2726 | * request timeout processing. |
| 2727 | */ |
| 2728 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2729 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2730 | } |
| 2731 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2732 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2733 | if (!tick_isset(req->analyse_exp)) { |
| 2734 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2735 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2736 | tick_isset(s->be->timeout.httpka)) |
| 2737 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2738 | else |
| 2739 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2740 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2741 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2742 | /* we're not ready yet */ |
| 2743 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2744 | |
| 2745 | failed_keep_alive: |
| 2746 | /* Here we process low-level errors for keep-alive requests. In |
| 2747 | * short, if the request is not the first one and it experiences |
| 2748 | * a timeout, read error or shutdown, we just silently close so |
| 2749 | * that the client can try again. |
| 2750 | */ |
| 2751 | txn->status = 0; |
| 2752 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2753 | req->analysers = 0; |
| 2754 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2755 | s->logs.level = 0; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2756 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2757 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2758 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2759 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2760 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2761 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2762 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2763 | * 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] | 2764 | * 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] | 2765 | * 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] | 2766 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2767 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2768 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2769 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2770 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2771 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2772 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2773 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2774 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2775 | /* kill the pending keep-alive timeout */ |
| 2776 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2777 | req->analyse_exp = TICK_ETERNITY; |
| 2778 | } |
| 2779 | |
| 2780 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2781 | /* Maybe we found in invalid header name while we were configured not |
| 2782 | * to block on that, so we have to capture it now. |
| 2783 | */ |
| 2784 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2785 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2786 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2787 | /* |
| 2788 | * 1: identify the method |
| 2789 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2790 | 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] | 2791 | |
| 2792 | /* we can make use of server redirect on GET and HEAD */ |
| 2793 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2794 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2795 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2796 | /* |
| 2797 | * 2: check if the URI matches the monitor_uri. |
| 2798 | * We have to do this for every request which gets in, because |
| 2799 | * the monitor-uri is defined by the frontend. |
| 2800 | */ |
| 2801 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2802 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2803 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2804 | s->fe->monitor_uri, |
| 2805 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2806 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2807 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2808 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2809 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2810 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2811 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2812 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2813 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2814 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2815 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2816 | int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2817 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2818 | ret = acl_pass(ret); |
| 2819 | if (cond->pol == ACL_COND_UNLESS) |
| 2820 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2821 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2822 | if (ret) { |
| 2823 | /* we fail this request, let's return 503 service unavail */ |
| 2824 | txn->status = 503; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2825 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2826 | if (!(s->flags & SN_ERR_MASK)) |
| 2827 | s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2828 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2829 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2830 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2831 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2832 | /* nothing to fail, let's reply normaly */ |
| 2833 | txn->status = 200; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2834 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2835 | if (!(s->flags & SN_ERR_MASK)) |
| 2836 | s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2837 | goto return_prx_cond; |
| 2838 | } |
| 2839 | |
| 2840 | /* |
| 2841 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2842 | * Note: we cannot log anymore if the request has been |
| 2843 | * classified as invalid. |
| 2844 | */ |
| 2845 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2846 | /* we have a complete HTTP request that we must log */ |
| 2847 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2848 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2849 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2850 | if (urilen >= REQURI_LEN) |
| 2851 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2852 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2853 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2854 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2855 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2856 | s->do_log(s); |
| 2857 | } else { |
| 2858 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2859 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2860 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2861 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2862 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 2863 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2864 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2865 | |
Willy Tarreau | 5564555 | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2866 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 2867 | * exactly one digit "." one digit. This check may be disabled using |
| 2868 | * option accept-invalid-http-request. |
| 2869 | */ |
| 2870 | if (!(s->fe->options2 & PR_O2_REQBUG_OK)) { |
| 2871 | if (msg->sl.rq.v_l != 8) { |
| 2872 | msg->err_pos = msg->sl.rq.v; |
| 2873 | goto return_bad_req; |
| 2874 | } |
| 2875 | |
| 2876 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 2877 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 2878 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 2879 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 2880 | msg->err_pos = msg->sl.rq.v + 4; |
| 2881 | goto return_bad_req; |
| 2882 | } |
| 2883 | } |
| 2884 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2885 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2886 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2887 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2888 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2889 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2890 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2891 | |
| 2892 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2893 | 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] | 2894 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2895 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2896 | * we have what looks like a proxied connection instead of a connection, |
| 2897 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2898 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2899 | * happen to do that despite being non-standard :-( |
| 2900 | * We consider that a request not beginning with either '/' or '*' is |
| 2901 | * a proxied connection, which covers both "scheme://location" and |
| 2902 | * CONNECT ip:port. |
| 2903 | */ |
| 2904 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2905 | 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] | 2906 | txn->flags |= TX_USE_PX_CONN; |
| 2907 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2908 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2909 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2910 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2911 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2912 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2913 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2914 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2915 | |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2916 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 2917 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2918 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2919 | * The length of a message body is determined by one of the following |
| 2920 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2921 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2922 | * 1. Any response to a HEAD request and any response with a 1xx |
| 2923 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 2924 | * code is always terminated by the first empty line after the |
| 2925 | * header fields, regardless of the header fields present in the |
| 2926 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2927 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2928 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 2929 | * the connection will become a tunnel immediately after the empty |
| 2930 | * line that concludes the header fields. A client MUST ignore any |
| 2931 | * Content-Length or Transfer-Encoding header fields received in |
| 2932 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2933 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2934 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 2935 | * transfer coding (Section 4.1) is the final encoding, the message |
| 2936 | * body length is determined by reading and decoding the chunked |
| 2937 | * data until the transfer coding indicates the data is complete. |
| 2938 | * |
| 2939 | * If a Transfer-Encoding header field is present in a response and |
| 2940 | * the chunked transfer coding is not the final encoding, the |
| 2941 | * message body length is determined by reading the connection until |
| 2942 | * it is closed by the server. If a Transfer-Encoding header field |
| 2943 | * is present in a request and the chunked transfer coding is not |
| 2944 | * the final encoding, the message body length cannot be determined |
| 2945 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 2946 | * status code and then close the connection. |
| 2947 | * |
| 2948 | * If a message is received with both a Transfer-Encoding and a |
| 2949 | * Content-Length header field, the Transfer-Encoding overrides the |
| 2950 | * Content-Length. Such a message might indicate an attempt to |
| 2951 | * perform request smuggling (Section 9.5) or response splitting |
| 2952 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 2953 | * remove the received Content-Length field prior to forwarding such |
| 2954 | * a message downstream. |
| 2955 | * |
| 2956 | * 4. If a message is received without Transfer-Encoding and with |
| 2957 | * either multiple Content-Length header fields having differing |
| 2958 | * field-values or a single Content-Length header field having an |
| 2959 | * invalid value, then the message framing is invalid and the |
| 2960 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 2961 | * request message, the server MUST respond with a 400 (Bad Request) |
| 2962 | * status code and then close the connection. If this is a response |
| 2963 | * message received by a proxy, the proxy MUST close the connection |
| 2964 | * to the server, discard the received response, and send a 502 (Bad |
| 2965 | * Gateway) response to the client. If this is a response message |
| 2966 | * received by a user agent, the user agent MUST close the |
| 2967 | * connection to the server and discard the received response. |
| 2968 | * |
| 2969 | * 5. If a valid Content-Length header field is present without |
| 2970 | * Transfer-Encoding, its decimal value defines the expected message |
| 2971 | * body length in octets. If the sender closes the connection or |
| 2972 | * the recipient times out before the indicated number of octets are |
| 2973 | * received, the recipient MUST consider the message to be |
| 2974 | * incomplete and close the connection. |
| 2975 | * |
| 2976 | * 6. If this is a request message and none of the above are true, then |
| 2977 | * the message body length is zero (no message body is present). |
| 2978 | * |
| 2979 | * 7. Otherwise, this is a response message without a declared message |
| 2980 | * body length, so the message body length is determined by the |
| 2981 | * number of octets received prior to the server closing the |
| 2982 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2983 | */ |
| 2984 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2985 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2986 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | e77bc17 | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 2987 | 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] | 2988 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2989 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2990 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 49efa26 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2991 | /* chunked not last, return badreq */ |
| 2992 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2993 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2994 | } |
| 2995 | |
Willy Tarreau | dfa3d92 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2996 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2997 | ctx.idx = 0; |
Willy Tarreau | dfa3d92 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2998 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 2999 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 3000 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 3001 | } |
Willy Tarreau | 49efa26 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3002 | 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] | 3003 | signed long long cl; |
| 3004 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3005 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3006 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3007 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3008 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3009 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3010 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3011 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3012 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3013 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3014 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3015 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3016 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3017 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3018 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3019 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3020 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3021 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3022 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3023 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3024 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3025 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3026 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3027 | } |
| 3028 | |
Willy Tarreau | 49efa26 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3029 | /* even bodyless requests have a known length */ |
| 3030 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3031 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3032 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 3033 | * only change if both the request and the config reference something else. |
| 3034 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3035 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3036 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3037 | * if FE and BE have the same settings (common). The method consists in |
| 3038 | * checking if options changed between the two calls (implying that either |
| 3039 | * one is non-null, or one of them is non-null and we are there for the first |
| 3040 | * time. |
| 3041 | */ |
| 3042 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | 2e47a3a | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 3043 | ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) |
| 3044 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3045 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3046 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3047 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3048 | req->analyse_exp = TICK_ETERNITY; |
| 3049 | return 1; |
| 3050 | |
| 3051 | return_bad_req: |
| 3052 | /* We centralize bad requests processing here */ |
| 3053 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3054 | /* we detected a parsing error. We want to archive this request |
| 3055 | * in the dedicated proxy area for later troubleshooting. |
| 3056 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3057 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3061 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3062 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3063 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3064 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3065 | if (s->listener->counters) |
| 3066 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3067 | |
| 3068 | return_prx_cond: |
| 3069 | if (!(s->flags & SN_ERR_MASK)) |
| 3070 | s->flags |= SN_ERR_PRXCOND; |
| 3071 | if (!(s->flags & SN_FINST_MASK)) |
| 3072 | s->flags |= SN_FINST_R; |
| 3073 | |
| 3074 | req->analysers = 0; |
| 3075 | req->analyse_exp = TICK_ETERNITY; |
| 3076 | return 0; |
| 3077 | } |
| 3078 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3079 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3080 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3081 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3082 | * and program a response message if something was unexpected. It cannot fail |
| 3083 | * 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] | 3084 | * 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] | 3085 | * s->target which is supposed to already point to the stats applet. The caller |
| 3086 | * is expected to have already assigned an appctx to the session. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3087 | */ |
| 3088 | int http_handle_stats(struct session *s, struct channel *req) |
| 3089 | { |
| 3090 | struct stats_admin_rule *stats_admin_rule; |
| 3091 | struct stream_interface *si = s->rep->prod; |
| 3092 | struct http_txn *txn = &s->txn; |
| 3093 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3094 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3095 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3096 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3097 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3098 | appctx = si_appctx(si); |
| 3099 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3100 | appctx->st1 = appctx->st2 = 0; |
| 3101 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3102 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | af3cf70 | 2014-04-22 22:19:53 +0200 | [diff] [blame] | 3103 | if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD)) |
| 3104 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3105 | |
| 3106 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3107 | lookup = uri + uri_auth->uri_len; |
| 3108 | |
| 3109 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3110 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3111 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3112 | break; |
| 3113 | } |
| 3114 | } |
| 3115 | |
| 3116 | if (uri_auth->refresh) { |
| 3117 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3118 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3119 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3120 | break; |
| 3121 | } |
| 3122 | } |
| 3123 | } |
| 3124 | |
| 3125 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3126 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3127 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3128 | break; |
| 3129 | } |
| 3130 | } |
| 3131 | |
| 3132 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3133 | if (memcmp(h, ";st=", 4) == 0) { |
| 3134 | int i; |
| 3135 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3136 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3137 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3138 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3139 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3140 | break; |
| 3141 | } |
| 3142 | } |
| 3143 | break; |
| 3144 | } |
| 3145 | } |
| 3146 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3147 | appctx->ctx.stats.scope_str = 0; |
| 3148 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3149 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3150 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3151 | int itx = 0; |
| 3152 | const char *h2; |
| 3153 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3154 | const char *err; |
| 3155 | |
| 3156 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3157 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3158 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3159 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3160 | itx++; |
| 3161 | h++; |
| 3162 | } |
| 3163 | |
| 3164 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3165 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3166 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3167 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3168 | /* 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] | 3169 | memcpy(scope_txt, h2, itx); |
| 3170 | scope_txt[itx] = '\0'; |
| 3171 | err = invalid_char(scope_txt); |
| 3172 | if (err) { |
| 3173 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3174 | appctx->ctx.stats.scope_str = 0; |
| 3175 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3176 | } |
| 3177 | break; |
| 3178 | } |
| 3179 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3180 | |
| 3181 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3182 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3183 | int ret = 1; |
| 3184 | |
| 3185 | if (stats_admin_rule->cond) { |
| 3186 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 3187 | ret = acl_pass(ret); |
| 3188 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3189 | ret = !ret; |
| 3190 | } |
| 3191 | |
| 3192 | if (ret) { |
| 3193 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3194 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3195 | break; |
| 3196 | } |
| 3197 | } |
| 3198 | |
| 3199 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3200 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3201 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3202 | /* we'll need the request body, possibly after sending 100-continue */ |
| 3203 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3204 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3205 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3206 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3207 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3208 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3209 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3210 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3211 | else { |
| 3212 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3213 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3214 | } |
| 3215 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3216 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3217 | return 1; |
| 3218 | } |
| 3219 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3220 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3221 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3222 | */ |
Vincent Bernat | a72d390 | 2016-05-19 11:15:51 +0200 | [diff] [blame] | 3223 | static inline void inet_set_tos(int fd, struct sockaddr_storage *from, int tos) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3224 | { |
| 3225 | #ifdef IP_TOS |
Vincent Bernat | a72d390 | 2016-05-19 11:15:51 +0200 | [diff] [blame] | 3226 | if (from->ss_family == AF_INET) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3227 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3228 | #endif |
| 3229 | #ifdef IPV6_TCLASS |
Vincent Bernat | a72d390 | 2016-05-19 11:15:51 +0200 | [diff] [blame] | 3230 | if (from->ss_family == AF_INET6) { |
| 3231 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr)) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3232 | /* v4-mapped addresses need IP_TOS */ |
| 3233 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3234 | else |
| 3235 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3236 | } |
| 3237 | #endif |
| 3238 | } |
| 3239 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3240 | static int http_transform_header(struct session* s, struct http_msg *msg, const char* name, uint name_len, |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 3241 | char* buf, struct hdr_idx* idx, struct list *fmt, struct my_regex *re, |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3242 | struct hdr_ctx* ctx, int action) |
| 3243 | { |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3244 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 3245 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
| 3246 | struct chunk *replace = get_trash_chunk(); |
| 3247 | struct chunk *output = get_trash_chunk(); |
| 3248 | |
| 3249 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 3250 | if (replace->len >= replace->size - 1) |
| 3251 | return -1; |
| 3252 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3253 | ctx->idx = 0; |
| 3254 | |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3255 | /* Choose the header browsing function. */ |
| 3256 | switch (action) { |
| 3257 | case HTTP_REQ_ACT_REPLACE_VAL: |
| 3258 | case HTTP_RES_ACT_REPLACE_VAL: |
| 3259 | http_find_hdr_func = http_find_header2; |
| 3260 | break; |
| 3261 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3262 | case HTTP_RES_ACT_REPLACE_HDR: |
| 3263 | http_find_hdr_func = http_find_full_header2; |
| 3264 | break; |
| 3265 | default: /* impossible */ |
| 3266 | return -1; |
| 3267 | } |
| 3268 | |
| 3269 | while (http_find_hdr_func(name, name_len, buf, idx, ctx)) { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3270 | struct hdr_idx_elem *hdr = idx->v + ctx->idx; |
| 3271 | int delta; |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3272 | char *val = ctx->line + ctx->val; |
| 3273 | char* val_end = val + ctx->vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3274 | |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3275 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch)) |
| 3276 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3277 | |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3278 | output->len = exp_replace(output->str, output->size, val, replace->str, pmatch); |
| 3279 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3280 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3281 | |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3282 | 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] | 3283 | |
| 3284 | hdr->len += delta; |
| 3285 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3286 | |
| 3287 | /* Adjust the length of the current value of the index. */ |
| 3288 | ctx->vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | return 0; |
| 3292 | } |
| 3293 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3294 | /* Executes the http-request rules <rules> for session <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3295 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3296 | * further processing of the request (auth, deny, ...), and defaults to |
| 3297 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3298 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 3299 | * on txn->flags if it encounters a tarpit rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3300 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3301 | enum rule_result |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3302 | http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3303 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3304 | struct connection *cli_conn; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3305 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3306 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3307 | const char *auth_realm; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3308 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3309 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3310 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3311 | continue; |
| 3312 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3313 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3314 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3315 | int ret; |
| 3316 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3317 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3318 | ret = acl_pass(ret); |
| 3319 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3320 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3321 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3322 | |
| 3323 | if (!ret) /* condition not matched */ |
| 3324 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3325 | } |
| 3326 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3327 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3328 | switch (rule->action) { |
| 3329 | case HTTP_REQ_ACT_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3330 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3331 | |
| 3332 | case HTTP_REQ_ACT_DENY: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3333 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3334 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3335 | case HTTP_REQ_ACT_TARPIT: |
| 3336 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3337 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3338 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3339 | case HTTP_REQ_ACT_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3340 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3341 | auth_realm = rule->arg.auth.realm; |
| 3342 | if (!auth_realm) { |
| 3343 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3344 | auth_realm = STATS_DEFAULT_REALM; |
| 3345 | else |
| 3346 | auth_realm = px->id; |
| 3347 | } |
| 3348 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3349 | * count one error, because normal browsing won't significantly |
| 3350 | * increase the counter but brute force attempts will. |
| 3351 | */ |
| 3352 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3353 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
| 3354 | stream_int_retnclose(&s->si[0], &trash); |
| 3355 | session_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3356 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3357 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3358 | case HTTP_REQ_ACT_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3359 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3360 | return HTTP_RULE_RES_BADREQ; |
| 3361 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3362 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3363 | case HTTP_REQ_ACT_SET_NICE: |
| 3364 | s->task->nice = rule->arg.nice; |
| 3365 | break; |
| 3366 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3367 | case HTTP_REQ_ACT_SET_TOS: |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3368 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Vincent Bernat | a72d390 | 2016-05-19 11:15:51 +0200 | [diff] [blame] | 3369 | 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] | 3370 | break; |
| 3371 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3372 | case HTTP_REQ_ACT_SET_MARK: |
| 3373 | #ifdef SO_MARK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3374 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3375 | 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] | 3376 | #endif |
| 3377 | break; |
| 3378 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3379 | case HTTP_REQ_ACT_SET_LOGL: |
| 3380 | s->logs.level = rule->arg.loglevel; |
| 3381 | break; |
| 3382 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3383 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3384 | case HTTP_REQ_ACT_REPLACE_VAL: |
| 3385 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3386 | txn->req.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt, |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 3387 | &rule->arg.hdr_add.re, &ctx, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3388 | return HTTP_RULE_RES_BADREQ; |
| 3389 | break; |
| 3390 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3391 | case HTTP_REQ_ACT_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3392 | ctx.idx = 0; |
| 3393 | /* remove all occurrences of the header */ |
| 3394 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3395 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3396 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3397 | } |
Willy Tarreau | fcf75d2 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3398 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3399 | |
Willy Tarreau | fcf75d2 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3400 | case HTTP_REQ_ACT_SET_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3401 | case HTTP_REQ_ACT_ADD_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3402 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3403 | trash.len = rule->arg.hdr_add.name_len; |
| 3404 | trash.str[trash.len++] = ':'; |
| 3405 | trash.str[trash.len++] = ' '; |
| 3406 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
Willy Tarreau | fcf75d2 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3407 | |
| 3408 | if (rule->action == HTTP_REQ_ACT_SET_HDR) { |
| 3409 | /* remove all occurrences of the header */ |
| 3410 | ctx.idx = 0; |
| 3411 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3412 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3413 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3414 | } |
| 3415 | } |
| 3416 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3417 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3418 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3419 | |
| 3420 | case HTTP_REQ_ACT_DEL_ACL: |
| 3421 | case HTTP_REQ_ACT_DEL_MAP: { |
| 3422 | struct pat_ref *ref; |
| 3423 | char *key; |
| 3424 | int len; |
| 3425 | |
| 3426 | /* collect reference */ |
| 3427 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3428 | if (!ref) |
| 3429 | continue; |
| 3430 | |
| 3431 | /* collect key */ |
| 3432 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3433 | key = trash.str; |
| 3434 | key[len] = '\0'; |
| 3435 | |
| 3436 | /* perform update */ |
| 3437 | /* returned code: 1=ok, 0=ko */ |
| 3438 | pat_ref_delete(ref, key); |
| 3439 | |
| 3440 | break; |
| 3441 | } |
| 3442 | |
| 3443 | case HTTP_REQ_ACT_ADD_ACL: { |
| 3444 | struct pat_ref *ref; |
| 3445 | char *key; |
| 3446 | struct chunk *trash_key; |
| 3447 | int len; |
| 3448 | |
| 3449 | trash_key = get_trash_chunk(); |
| 3450 | |
| 3451 | /* collect reference */ |
| 3452 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3453 | if (!ref) |
| 3454 | continue; |
| 3455 | |
| 3456 | /* collect key */ |
| 3457 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3458 | key = trash_key->str; |
| 3459 | key[len] = '\0'; |
| 3460 | |
| 3461 | /* perform update */ |
| 3462 | /* add entry only if it does not already exist */ |
| 3463 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3464 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3465 | |
| 3466 | break; |
| 3467 | } |
| 3468 | |
| 3469 | case HTTP_REQ_ACT_SET_MAP: { |
| 3470 | struct pat_ref *ref; |
| 3471 | char *key, *value; |
| 3472 | struct chunk *trash_key, *trash_value; |
| 3473 | int len; |
| 3474 | |
| 3475 | trash_key = get_trash_chunk(); |
| 3476 | trash_value = get_trash_chunk(); |
| 3477 | |
| 3478 | /* collect reference */ |
| 3479 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3480 | if (!ref) |
| 3481 | continue; |
| 3482 | |
| 3483 | /* collect key */ |
| 3484 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3485 | key = trash_key->str; |
| 3486 | key[len] = '\0'; |
| 3487 | |
| 3488 | /* collect value */ |
| 3489 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3490 | value = trash_value->str; |
| 3491 | value[len] = '\0'; |
| 3492 | |
| 3493 | /* perform update */ |
| 3494 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3495 | /* update entry if it exists */ |
| 3496 | pat_ref_set(ref, key, value, NULL); |
| 3497 | else |
| 3498 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3499 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3500 | |
| 3501 | break; |
| 3502 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3503 | |
| 3504 | case HTTP_REQ_ACT_CUSTOM_CONT: |
| 3505 | rule->action_ptr(rule, px, s, txn); |
| 3506 | break; |
| 3507 | |
| 3508 | case HTTP_REQ_ACT_CUSTOM_STOP: |
| 3509 | rule->action_ptr(rule, px, s, txn); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3510 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3511 | } |
| 3512 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3513 | |
| 3514 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3515 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3516 | } |
| 3517 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3518 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3519 | /* Executes the http-response rules <rules> for session <s>, proxy <px> and |
| 3520 | * transaction <txn>. Returns the first rule that prevents further processing |
| 3521 | * of the response (deny, ...) or NULL if it executed all rules or stopped |
| 3522 | * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny |
| 3523 | * rule. |
| 3524 | */ |
| 3525 | static struct http_res_rule * |
| 3526 | http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 3527 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3528 | struct connection *cli_conn; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3529 | struct http_res_rule *rule; |
| 3530 | struct hdr_ctx ctx; |
| 3531 | |
| 3532 | list_for_each_entry(rule, rules, list) { |
| 3533 | if (rule->action >= HTTP_RES_ACT_MAX) |
| 3534 | continue; |
| 3535 | |
| 3536 | /* check optional condition */ |
| 3537 | if (rule->cond) { |
| 3538 | int ret; |
| 3539 | |
| 3540 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 3541 | ret = acl_pass(ret); |
| 3542 | |
| 3543 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3544 | ret = !ret; |
| 3545 | |
| 3546 | if (!ret) /* condition not matched */ |
| 3547 | continue; |
| 3548 | } |
| 3549 | |
| 3550 | |
| 3551 | switch (rule->action) { |
| 3552 | case HTTP_RES_ACT_ALLOW: |
| 3553 | return NULL; /* "allow" rules are OK */ |
| 3554 | |
| 3555 | case HTTP_RES_ACT_DENY: |
| 3556 | txn->flags |= TX_SVDENY; |
| 3557 | return rule; |
| 3558 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3559 | case HTTP_RES_ACT_SET_NICE: |
| 3560 | s->task->nice = rule->arg.nice; |
| 3561 | break; |
| 3562 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3563 | case HTTP_RES_ACT_SET_TOS: |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3564 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Vincent Bernat | a72d390 | 2016-05-19 11:15:51 +0200 | [diff] [blame] | 3565 | 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] | 3566 | break; |
| 3567 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3568 | case HTTP_RES_ACT_SET_MARK: |
| 3569 | #ifdef SO_MARK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3570 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3571 | 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] | 3572 | #endif |
| 3573 | break; |
| 3574 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3575 | case HTTP_RES_ACT_SET_LOGL: |
| 3576 | s->logs.level = rule->arg.loglevel; |
| 3577 | break; |
| 3578 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3579 | case HTTP_RES_ACT_REPLACE_HDR: |
| 3580 | case HTTP_RES_ACT_REPLACE_VAL: |
| 3581 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3582 | txn->rsp.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt, |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 3583 | &rule->arg.hdr_add.re, &ctx, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3584 | return NULL; /* note: we should report an error here */ |
| 3585 | break; |
| 3586 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3587 | case HTTP_RES_ACT_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3588 | ctx.idx = 0; |
| 3589 | /* remove all occurrences of the header */ |
| 3590 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3591 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3592 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3593 | } |
Willy Tarreau | fcf75d2 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3594 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3595 | |
Willy Tarreau | fcf75d2 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3596 | case HTTP_RES_ACT_SET_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3597 | case HTTP_RES_ACT_ADD_HDR: |
| 3598 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3599 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3600 | trash.len = rule->arg.hdr_add.name_len; |
| 3601 | trash.str[trash.len++] = ':'; |
| 3602 | trash.str[trash.len++] = ' '; |
| 3603 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
Willy Tarreau | fcf75d2 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3604 | |
| 3605 | if (rule->action == HTTP_RES_ACT_SET_HDR) { |
| 3606 | /* remove all occurrences of the header */ |
| 3607 | ctx.idx = 0; |
| 3608 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3609 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3610 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3611 | } |
| 3612 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3613 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3614 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3615 | |
| 3616 | case HTTP_RES_ACT_DEL_ACL: |
| 3617 | case HTTP_RES_ACT_DEL_MAP: { |
| 3618 | struct pat_ref *ref; |
| 3619 | char *key; |
| 3620 | int len; |
| 3621 | |
| 3622 | /* collect reference */ |
| 3623 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3624 | if (!ref) |
| 3625 | continue; |
| 3626 | |
| 3627 | /* collect key */ |
| 3628 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3629 | key = trash.str; |
| 3630 | key[len] = '\0'; |
| 3631 | |
| 3632 | /* perform update */ |
| 3633 | /* returned code: 1=ok, 0=ko */ |
| 3634 | pat_ref_delete(ref, key); |
| 3635 | |
| 3636 | break; |
| 3637 | } |
| 3638 | |
| 3639 | case HTTP_RES_ACT_ADD_ACL: { |
| 3640 | struct pat_ref *ref; |
| 3641 | char *key; |
| 3642 | struct chunk *trash_key; |
| 3643 | int len; |
| 3644 | |
| 3645 | trash_key = get_trash_chunk(); |
| 3646 | |
| 3647 | /* collect reference */ |
| 3648 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3649 | if (!ref) |
| 3650 | continue; |
| 3651 | |
| 3652 | /* collect key */ |
| 3653 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3654 | key = trash_key->str; |
| 3655 | key[len] = '\0'; |
| 3656 | |
| 3657 | /* perform update */ |
| 3658 | /* check if the entry already exists */ |
| 3659 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3660 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3661 | |
| 3662 | break; |
| 3663 | } |
| 3664 | |
| 3665 | case HTTP_RES_ACT_SET_MAP: { |
| 3666 | struct pat_ref *ref; |
| 3667 | char *key, *value; |
| 3668 | struct chunk *trash_key, *trash_value; |
| 3669 | int len; |
| 3670 | |
| 3671 | trash_key = get_trash_chunk(); |
| 3672 | trash_value = get_trash_chunk(); |
| 3673 | |
| 3674 | /* collect reference */ |
| 3675 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3676 | if (!ref) |
| 3677 | continue; |
| 3678 | |
| 3679 | /* collect key */ |
| 3680 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3681 | key = trash_key->str; |
| 3682 | key[len] = '\0'; |
| 3683 | |
| 3684 | /* collect value */ |
| 3685 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3686 | value = trash_value->str; |
| 3687 | value[len] = '\0'; |
| 3688 | |
| 3689 | /* perform update */ |
| 3690 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3691 | /* update entry if it exists */ |
| 3692 | pat_ref_set(ref, key, value, NULL); |
| 3693 | else |
| 3694 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3695 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3696 | |
| 3697 | break; |
| 3698 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3699 | |
| 3700 | case HTTP_RES_ACT_CUSTOM_CONT: |
| 3701 | rule->action_ptr(rule, px, s, txn); |
| 3702 | break; |
| 3703 | |
| 3704 | case HTTP_RES_ACT_CUSTOM_STOP: |
| 3705 | rule->action_ptr(rule, px, s, txn); |
| 3706 | return rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3707 | } |
| 3708 | } |
| 3709 | |
| 3710 | /* we reached the end of the rules, nothing to report */ |
| 3711 | return NULL; |
| 3712 | } |
| 3713 | |
| 3714 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3715 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3716 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3717 | * as too large a request to build a valid response. |
| 3718 | */ |
| 3719 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn) |
| 3720 | { |
| 3721 | struct http_msg *msg = &txn->req; |
| 3722 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3723 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3724 | |
| 3725 | /* build redirect message */ |
| 3726 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3727 | case 308: |
| 3728 | msg_fmt = HTTP_308; |
| 3729 | break; |
| 3730 | case 307: |
| 3731 | msg_fmt = HTTP_307; |
| 3732 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3733 | case 303: |
| 3734 | msg_fmt = HTTP_303; |
| 3735 | break; |
| 3736 | case 301: |
| 3737 | msg_fmt = HTTP_301; |
| 3738 | break; |
| 3739 | case 302: |
| 3740 | default: |
| 3741 | msg_fmt = HTTP_302; |
| 3742 | break; |
| 3743 | } |
| 3744 | |
| 3745 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3746 | return 0; |
| 3747 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3748 | location = trash.str + trash.len; |
| 3749 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3750 | switch(rule->type) { |
| 3751 | case REDIRECT_TYPE_SCHEME: { |
| 3752 | const char *path; |
| 3753 | const char *host; |
| 3754 | struct hdr_ctx ctx; |
| 3755 | int pathlen; |
| 3756 | int hostlen; |
| 3757 | |
| 3758 | host = ""; |
| 3759 | hostlen = 0; |
| 3760 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3761 | if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3762 | host = ctx.line + ctx.val; |
| 3763 | hostlen = ctx.vlen; |
| 3764 | } |
| 3765 | |
| 3766 | path = http_get_path(txn); |
| 3767 | /* build message using path */ |
| 3768 | if (path) { |
| 3769 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3770 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3771 | int qs = 0; |
| 3772 | while (qs < pathlen) { |
| 3773 | if (path[qs] == '?') { |
| 3774 | pathlen = qs; |
| 3775 | break; |
| 3776 | } |
| 3777 | qs++; |
| 3778 | } |
| 3779 | } |
| 3780 | } else { |
| 3781 | path = "/"; |
| 3782 | pathlen = 1; |
| 3783 | } |
| 3784 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3785 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3786 | /* check if we can add scheme + "://" + host + path */ |
| 3787 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3788 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3789 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3790 | /* add scheme */ |
| 3791 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3792 | trash.len += rule->rdr_len; |
| 3793 | } |
| 3794 | else { |
| 3795 | /* add scheme with executing log format */ |
| 3796 | 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] | 3797 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3798 | /* check if we can add scheme + "://" + host + path */ |
| 3799 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 3800 | return 0; |
| 3801 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3802 | /* add "://" */ |
| 3803 | memcpy(trash.str + trash.len, "://", 3); |
| 3804 | trash.len += 3; |
| 3805 | |
| 3806 | /* add host */ |
| 3807 | memcpy(trash.str + trash.len, host, hostlen); |
| 3808 | trash.len += hostlen; |
| 3809 | |
| 3810 | /* add path */ |
| 3811 | memcpy(trash.str + trash.len, path, pathlen); |
| 3812 | trash.len += pathlen; |
| 3813 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3814 | /* 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] | 3815 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3816 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3817 | if (trash.len > trash.size - 5) |
| 3818 | return 0; |
| 3819 | trash.str[trash.len] = '/'; |
| 3820 | trash.len++; |
| 3821 | } |
| 3822 | |
| 3823 | break; |
| 3824 | } |
| 3825 | case REDIRECT_TYPE_PREFIX: { |
| 3826 | const char *path; |
| 3827 | int pathlen; |
| 3828 | |
| 3829 | path = http_get_path(txn); |
| 3830 | /* build message using path */ |
| 3831 | if (path) { |
| 3832 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3833 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3834 | int qs = 0; |
| 3835 | while (qs < pathlen) { |
| 3836 | if (path[qs] == '?') { |
| 3837 | pathlen = qs; |
| 3838 | break; |
| 3839 | } |
| 3840 | qs++; |
| 3841 | } |
| 3842 | } |
| 3843 | } else { |
| 3844 | path = "/"; |
| 3845 | pathlen = 1; |
| 3846 | } |
| 3847 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3848 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3849 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 3850 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3851 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3852 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3853 | * add anything, otherwise it makes it hard for the user to |
| 3854 | * configure a self-redirection. |
| 3855 | */ |
| 3856 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 3857 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3858 | trash.len += rule->rdr_len; |
| 3859 | } |
| 3860 | } |
| 3861 | else { |
| 3862 | /* add prefix with executing log format */ |
| 3863 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 3864 | |
| 3865 | /* Check length */ |
| 3866 | if (trash.len + pathlen > trash.size - 4) |
| 3867 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | /* add path */ |
| 3871 | memcpy(trash.str + trash.len, path, pathlen); |
| 3872 | trash.len += pathlen; |
| 3873 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3874 | /* 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] | 3875 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3876 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3877 | if (trash.len > trash.size - 5) |
| 3878 | return 0; |
| 3879 | trash.str[trash.len] = '/'; |
| 3880 | trash.len++; |
| 3881 | } |
| 3882 | |
| 3883 | break; |
| 3884 | } |
| 3885 | case REDIRECT_TYPE_LOCATION: |
| 3886 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3887 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3888 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 3889 | return 0; |
| 3890 | |
| 3891 | /* add location */ |
| 3892 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3893 | trash.len += rule->rdr_len; |
| 3894 | } |
| 3895 | else { |
| 3896 | /* add location with executing log format */ |
| 3897 | 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] | 3898 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3899 | /* Check left length */ |
| 3900 | if (trash.len > trash.size - 4) |
| 3901 | return 0; |
| 3902 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3903 | break; |
| 3904 | } |
| 3905 | |
| 3906 | if (rule->cookie_len) { |
| 3907 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 3908 | trash.len += 14; |
| 3909 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 3910 | trash.len += rule->cookie_len; |
| 3911 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 3912 | trash.len += 2; |
| 3913 | } |
| 3914 | |
| 3915 | /* add end of headers and the keep-alive/close status. |
| 3916 | * We may choose to set keep-alive if the Location begins |
| 3917 | * with a slash, because the client will come back to the |
| 3918 | * same server. |
| 3919 | */ |
| 3920 | txn->status = rule->code; |
| 3921 | /* let's log the request time */ |
| 3922 | s->logs.tv_request = now; |
| 3923 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3924 | if (*location == '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3925 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3926 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
| 3927 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3928 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3929 | /* keep-alive possible */ |
| 3930 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
| 3931 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3932 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3933 | trash.len += 30; |
| 3934 | } else { |
| 3935 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 3936 | trash.len += 24; |
| 3937 | } |
| 3938 | } |
| 3939 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 3940 | trash.len += 4; |
| 3941 | bo_inject(txn->rsp.chn, trash.str, trash.len); |
| 3942 | /* "eat" the request */ |
| 3943 | bi_fast_delete(txn->req.chn->buf, msg->sov); |
Willy Tarreau | 6d8bac7 | 2014-04-25 12:19:32 +0200 | [diff] [blame] | 3944 | msg->next -= msg->sov; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3945 | msg->sov = 0; |
| 3946 | txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY; |
| 3947 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3948 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3949 | txn->rsp.msg_state = HTTP_MSG_DONE; |
| 3950 | } else { |
| 3951 | /* keep-alive not possible */ |
| 3952 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3953 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3954 | trash.len += 29; |
| 3955 | } else { |
| 3956 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3957 | trash.len += 23; |
| 3958 | } |
| 3959 | stream_int_retnclose(txn->req.chn->prod, &trash); |
| 3960 | txn->req.chn->analysers = 0; |
| 3961 | } |
| 3962 | |
| 3963 | if (!(s->flags & SN_ERR_MASK)) |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 3964 | s->flags |= SN_ERR_LOCAL; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3965 | if (!(s->flags & SN_FINST_MASK)) |
| 3966 | s->flags |= SN_FINST_R; |
| 3967 | |
| 3968 | return 1; |
| 3969 | } |
| 3970 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3971 | /* This stream analyser runs all HTTP request processing which is common to |
| 3972 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3973 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3974 | * 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] | 3975 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3976 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3977 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3978 | int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px) |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3979 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3980 | struct http_txn *txn = &s->txn; |
| 3981 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3982 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3983 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3984 | enum rule_result verdict; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3985 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3986 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3987 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3988 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3989 | return 0; |
| 3990 | } |
| 3991 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3992 | DPRINTF(stderr,"[%u] %s: session=%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] | 3993 | now_ms, __FUNCTION__, |
| 3994 | s, |
| 3995 | req, |
| 3996 | req->rex, req->wex, |
| 3997 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3998 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3999 | req->analysers); |
| 4000 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4001 | /* just in case we have some per-backend tracking */ |
| 4002 | session_inc_be_http_req_ctr(s); |
| 4003 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4004 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4005 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
| 4006 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 4007 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4008 | switch (verdict) { |
| 4009 | case HTTP_RULE_RES_CONT: |
| 4010 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4011 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4012 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4013 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4014 | if (txn->flags & TX_CLTARPIT) |
| 4015 | goto tarpit; |
| 4016 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4017 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4018 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4019 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4020 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4021 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4022 | goto done; |
| 4023 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4024 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4025 | goto return_bad_req; |
| 4026 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4027 | } |
| 4028 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4029 | /* OK at this stage, we know that the request was accepted according to |
| 4030 | * the http-request rules, we can check for the stats. Note that the |
| 4031 | * URI is detected *before* the req* rules in order not to be affected |
| 4032 | * by a possible reqrep, while they are processed *after* so that a |
| 4033 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4034 | */ |
| 4035 | if (stats_check_uri(s->rep->prod, txn, px)) { |
| 4036 | s->target = &http_stats_applet.obj_type; |
| 4037 | if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) { |
| 4038 | txn->status = 500; |
| 4039 | s->logs.tv_request = now; |
| 4040 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 4041 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4042 | if (!(s->flags & SN_ERR_MASK)) |
| 4043 | s->flags |= SN_ERR_RESOURCE; |
| 4044 | goto return_prx_cond; |
| 4045 | } |
| 4046 | |
| 4047 | /* parse the whole stats request and extract the relevant information */ |
| 4048 | http_handle_stats(s, req); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4049 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn); |
| 4050 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4051 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4052 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4053 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4054 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4055 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4056 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4057 | } |
| 4058 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4059 | /* evaluate the req* rules except reqadd */ |
| 4060 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4061 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4062 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4063 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4064 | if (txn->flags & TX_CLDENY) |
| 4065 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4066 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4067 | if (txn->flags & TX_CLTARPIT) |
| 4068 | goto tarpit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4069 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4070 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4071 | /* add request headers from the rule sets in the same order */ |
| 4072 | list_for_each_entry(wl, &px->req_add, list) { |
| 4073 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 4074 | int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4075 | ret = acl_pass(ret); |
| 4076 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4077 | ret = !ret; |
| 4078 | if (!ret) |
| 4079 | continue; |
| 4080 | } |
| 4081 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4082 | 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] | 4083 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4084 | } |
| 4085 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4086 | |
| 4087 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4088 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4089 | /* process the stats request now */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4090 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4091 | s->fe->fe_counters.intercepted_req++; |
| 4092 | |
| 4093 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 4094 | s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy |
| 4095 | if (!(s->flags & SN_FINST_MASK)) |
| 4096 | s->flags |= SN_FINST_R; |
| 4097 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4098 | /* we may want to compress the stats page */ |
| 4099 | if (s->fe->comp || s->be->comp) |
| 4100 | select_compression_request_header(s, req->buf); |
| 4101 | |
| 4102 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
Willy Tarreau | de8ca96 | 2014-11-20 22:23:10 +0100 | [diff] [blame] | 4103 | 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] | 4104 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4105 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4106 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4107 | /* check whether we have some ACLs set to redirect this request */ |
| 4108 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4109 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4110 | int ret; |
| 4111 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 4112 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4113 | ret = acl_pass(ret); |
| 4114 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4115 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4116 | if (!ret) |
| 4117 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4118 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4119 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4120 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4121 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4122 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4123 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4124 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4125 | * If this happens, then the data will not come immediately, so we must |
| 4126 | * send all what we have without waiting. Note that due to the small gain |
| 4127 | * 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] | 4128 | * 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] | 4129 | * itself once used. |
| 4130 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4131 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4132 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4133 | done: /* done with this analyser, continue with next ones that the calling |
| 4134 | * points will have set, if any. |
| 4135 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4136 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | fc566b5 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4137 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4138 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4139 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4140 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4141 | tarpit: |
| 4142 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4143 | * which may be the same as the connect timeout if unspecified. |
| 4144 | * If unset, then set it to zero because we really want it to |
| 4145 | * eventually expire. We build the tarpit as an analyser. |
| 4146 | */ |
| 4147 | channel_erase(s->req); |
| 4148 | |
| 4149 | /* wipe the request out so that we can drop the connection early |
| 4150 | * if the client closes first. |
| 4151 | */ |
| 4152 | channel_dont_connect(req); |
| 4153 | req->analysers = 0; /* remove switching rules etc... */ |
Bertrand Paquet | c89b09b | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 4154 | |
| 4155 | /* Allow cookie logging |
| 4156 | */ |
| 4157 | if (s->be->cookie_name || s->fe->capture_name) |
| 4158 | manage_client_side_cookies(s, req); |
| 4159 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4160 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4161 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4162 | if (!req->analyse_exp) |
| 4163 | req->analyse_exp = tick_add(now_ms, 0); |
| 4164 | session_inc_http_err_ctr(s); |
| 4165 | s->fe->fe_counters.denied_req++; |
| 4166 | if (s->fe != s->be) |
| 4167 | s->be->be_counters.denied_req++; |
| 4168 | if (s->listener->counters) |
| 4169 | s->listener->counters->denied_req++; |
Thierry FOURNIER | fc566b5 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4170 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4171 | |
| 4172 | deny: /* this request was blocked (denied) */ |
Bertrand Paquet | c89b09b | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 4173 | |
| 4174 | /* Allow cookie logging |
| 4175 | */ |
| 4176 | if (s->be->cookie_name || s->fe->capture_name) |
| 4177 | manage_client_side_cookies(s, req); |
| 4178 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4179 | txn->flags |= TX_CLDENY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4180 | txn->status = 403; |
| 4181 | s->logs.tv_request = now; |
| 4182 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
| 4183 | session_inc_http_err_ctr(s); |
| 4184 | s->fe->fe_counters.denied_req++; |
| 4185 | if (s->fe != s->be) |
| 4186 | s->be->be_counters.denied_req++; |
| 4187 | if (s->listener->counters) |
| 4188 | s->listener->counters->denied_req++; |
| 4189 | goto return_prx_cond; |
| 4190 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4191 | return_bad_req: |
| 4192 | /* We centralize bad requests processing here */ |
| 4193 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4194 | /* we detected a parsing error. We want to archive this request |
| 4195 | * in the dedicated proxy area for later troubleshooting. |
| 4196 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4197 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4198 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4199 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4200 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4201 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4202 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4203 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4204 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4205 | if (s->listener->counters) |
| 4206 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4207 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4208 | return_prx_cond: |
| 4209 | if (!(s->flags & SN_ERR_MASK)) |
| 4210 | s->flags |= SN_ERR_PRXCOND; |
| 4211 | if (!(s->flags & SN_FINST_MASK)) |
| 4212 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4213 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4214 | req->analysers = 0; |
| 4215 | req->analyse_exp = TICK_ETERNITY; |
| 4216 | return 0; |
| 4217 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4218 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4219 | /* This function performs all the processing enabled for the current request. |
| 4220 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4221 | * needs more data, encounters an error, or wants to immediately abort the |
| 4222 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 4223 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4224 | int http_process_request(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4225 | { |
| 4226 | struct http_txn *txn = &s->txn; |
| 4227 | struct http_msg *msg = &txn->req; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4228 | struct connection *cli_conn = objt_conn(req->prod->end); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4229 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4230 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4231 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4232 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4233 | return 0; |
| 4234 | } |
| 4235 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4236 | DPRINTF(stderr,"[%u] %s: session=%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] | 4237 | now_ms, __FUNCTION__, |
| 4238 | s, |
| 4239 | req, |
| 4240 | req->rex, req->wex, |
| 4241 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4242 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4243 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4244 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 4245 | if (s->fe->comp || s->be->comp) |
| 4246 | select_compression_request_header(s, req->buf); |
| 4247 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4248 | /* |
| 4249 | * Right now, we know that we have processed the entire headers |
| 4250 | * and that unwanted requests have been filtered out. We can do |
| 4251 | * whatever we want with the remaining request. Also, now we |
| 4252 | * may have separate values for ->fe, ->be. |
| 4253 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4254 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4255 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4256 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4257 | * incoming request. Note that this requires that a connection is |
| 4258 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4259 | */ |
| 4260 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4261 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4262 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4263 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4264 | /* Note that for now we don't reuse existing proxy connections */ |
| 4265 | if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4266 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4267 | txn->status = 500; |
| 4268 | req->analysers = 0; |
| 4269 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
| 4270 | |
| 4271 | if (!(s->flags & SN_ERR_MASK)) |
| 4272 | s->flags |= SN_ERR_RESOURCE; |
| 4273 | if (!(s->flags & SN_FINST_MASK)) |
| 4274 | s->flags |= SN_FINST_R; |
| 4275 | |
| 4276 | return 0; |
| 4277 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4278 | |
| 4279 | path = http_get_path(txn); |
| 4280 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4281 | 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] | 4282 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4283 | /* if the path was found, we have to remove everything between |
| 4284 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4285 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4286 | * u_l characters by a single "/". |
| 4287 | */ |
| 4288 | if (path) { |
| 4289 | char *cur_ptr = req->buf->p; |
| 4290 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4291 | int delta; |
| 4292 | |
| 4293 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4294 | http_msg_move_end(&txn->req, delta); |
| 4295 | cur_end += delta; |
| 4296 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4297 | goto return_bad_req; |
| 4298 | } |
| 4299 | else { |
| 4300 | char *cur_ptr = req->buf->p; |
| 4301 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4302 | int delta; |
| 4303 | |
| 4304 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4305 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4306 | http_msg_move_end(&txn->req, delta); |
| 4307 | cur_end += delta; |
| 4308 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4309 | goto return_bad_req; |
| 4310 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4311 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4312 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4313 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4314 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4315 | * Note that doing so might move headers in the request, but |
| 4316 | * the fields will stay coherent and the URI will not move. |
| 4317 | * This should only be performed in the backend. |
| 4318 | */ |
Bertrand Paquet | c89b09b | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 4319 | if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4320 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4321 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4322 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4323 | * 8: the appsession cookie was looked up very early in 1.2, |
| 4324 | * so let's do the same now. |
| 4325 | */ |
| 4326 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4327 | /* It needs to look into the URI unless persistence must be ignored */ |
| 4328 | if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4329 | get_srv_from_appsession(s, req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4330 | } |
| 4331 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4332 | /* add unique-id if "header-unique-id" is specified */ |
| 4333 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4334 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 4335 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4336 | goto return_bad_req; |
| 4337 | s->unique_id[0] = '\0'; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4338 | build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id); |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4339 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4340 | |
| 4341 | if (s->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4342 | chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 4343 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4344 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4345 | 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] | 4346 | goto return_bad_req; |
| 4347 | } |
| 4348 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4349 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4350 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4351 | * asks for it. |
| 4352 | */ |
| 4353 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4354 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4355 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Cyril Bonté | a32d275 | 2012-05-29 23:27:41 +0200 | [diff] [blame] | 4356 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name, |
| 4357 | s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4358 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4359 | /* The header is set to be added only if none is present |
| 4360 | * and we found it, so don't do anything. |
| 4361 | */ |
| 4362 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4363 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4364 | /* Add an X-Forwarded-For header unless the source IP is |
| 4365 | * in the 'except' network range. |
| 4366 | */ |
| 4367 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4368 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4369 | != s->fe->except_net.s_addr) && |
| 4370 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4371 | (((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] | 4372 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4373 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4374 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4375 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4376 | |
| 4377 | /* Note: we rely on the backend to get the header name to be used for |
| 4378 | * x-forwarded-for, because the header is really meant for the backends. |
| 4379 | * However, if the backend did not specify any option, we have to rely |
| 4380 | * on the frontend's header name. |
| 4381 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4382 | if (s->be->fwdfor_hdr_len) { |
| 4383 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4384 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4385 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4386 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4387 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4388 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4389 | 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] | 4390 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4391 | 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] | 4392 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4393 | } |
| 4394 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4395 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4396 | /* FIXME: for the sake of completeness, we should also support |
| 4397 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4398 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4399 | int len; |
| 4400 | char pn[INET6_ADDRSTRLEN]; |
| 4401 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4402 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4403 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4404 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4405 | /* Note: we rely on the backend to get the header name to be used for |
| 4406 | * x-forwarded-for, because the header is really meant for the backends. |
| 4407 | * However, if the backend did not specify any option, we have to rely |
| 4408 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4409 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4410 | if (s->be->fwdfor_hdr_len) { |
| 4411 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4412 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4413 | } else { |
| 4414 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4415 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4416 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4417 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4418 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4419 | 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] | 4420 | goto return_bad_req; |
| 4421 | } |
| 4422 | } |
| 4423 | |
| 4424 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4425 | * 10: add X-Original-To if either the frontend or the backend |
| 4426 | * asks for it. |
| 4427 | */ |
| 4428 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 4429 | |
| 4430 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4431 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4432 | /* Add an X-Original-To header unless the destination IP is |
| 4433 | * in the 'except' network range. |
| 4434 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4435 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4436 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4437 | if (cli_conn->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4438 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4439 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4440 | != s->fe->except_to.s_addr) && |
| 4441 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4442 | (((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] | 4443 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4444 | int len; |
| 4445 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4446 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4447 | |
| 4448 | /* Note: we rely on the backend to get the header name to be used for |
| 4449 | * x-original-to, because the header is really meant for the backends. |
| 4450 | * However, if the backend did not specify any option, we have to rely |
| 4451 | * on the frontend's header name. |
| 4452 | */ |
| 4453 | if (s->be->orgto_hdr_len) { |
| 4454 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4455 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4456 | } else { |
| 4457 | len = s->fe->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4458 | memcpy(trash.str, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4459 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4460 | 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] | 4461 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4462 | 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] | 4463 | goto return_bad_req; |
| 4464 | } |
| 4465 | } |
| 4466 | } |
| 4467 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4468 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4469 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4470 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4471 | * "Upgrade" is present in the Connection header. |
| 4472 | */ |
| 4473 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4474 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4475 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4476 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4477 | unsigned int want_flags = 0; |
| 4478 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4479 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4480 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4481 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4482 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4483 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4484 | want_flags |= TX_CON_CLO_SET; |
| 4485 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4486 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4487 | ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
| 4488 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4489 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4490 | want_flags |= TX_CON_KAL_SET; |
| 4491 | } |
| 4492 | |
| 4493 | 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] | 4494 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4495 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4496 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4497 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4498 | /* If we have no server assigned yet and we're balancing on url_param |
| 4499 | * with a POST request, we may be interested in checking the body for |
| 4500 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4501 | */ |
| 4502 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 4503 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4504 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4505 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4506 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4507 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4508 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4509 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4510 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4511 | #ifdef TCP_QUICKACK |
| 4512 | /* We expect some data from the client. Unless we know for sure |
| 4513 | * we already have a full request, we have to re-enable quick-ack |
| 4514 | * in case we previously disabled it, otherwise we might cause |
| 4515 | * the client to delay further data. |
| 4516 | */ |
| 4517 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4518 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4519 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4520 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4521 | 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] | 4522 | #endif |
| 4523 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4524 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4525 | /************************************************************* |
| 4526 | * OK, that's finished for the headers. We have done what we * |
| 4527 | * could. Let's switch to the DATA state. * |
| 4528 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4529 | req->analyse_exp = TICK_ETERNITY; |
| 4530 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4531 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4532 | /* if the server closes the connection, we want to immediately react |
| 4533 | * and close the socket to save packets and syscalls. |
| 4534 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4535 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
| 4536 | req->cons->flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4537 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4538 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4539 | /* OK let's go on with the BODY now */ |
| 4540 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4541 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4542 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4543 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4544 | /* we detected a parsing error. We want to archive this request |
| 4545 | * in the dedicated proxy area for later troubleshooting. |
| 4546 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4547 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4548 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4549 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4550 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4551 | txn->status = 400; |
| 4552 | req->analysers = 0; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4553 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4554 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4555 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4556 | if (s->listener->counters) |
| 4557 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4558 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4559 | if (!(s->flags & SN_ERR_MASK)) |
| 4560 | s->flags |= SN_ERR_PRXCOND; |
| 4561 | if (!(s->flags & SN_FINST_MASK)) |
| 4562 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4563 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4564 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4565 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4566 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4567 | * returns zero, at the beginning because it prevents any other processing |
| 4568 | * from occurring, and at the end because it terminates the request. |
| 4569 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4570 | int http_process_tarpit(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4571 | { |
| 4572 | struct http_txn *txn = &s->txn; |
| 4573 | |
| 4574 | /* This connection is being tarpitted. The CLIENT side has |
| 4575 | * already set the connect expiration date to the right |
| 4576 | * timeout. We just have to check that the client is still |
| 4577 | * there and that the timeout has not expired. |
| 4578 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4579 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4580 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4581 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4582 | return 0; |
| 4583 | |
| 4584 | /* We will set the queue timer to the time spent, just for |
| 4585 | * logging purposes. We fake a 500 server error, so that the |
| 4586 | * attacker will not suspect his connection has been tarpitted. |
| 4587 | * It will not cause trouble to the logs because we can exclude |
| 4588 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4589 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4590 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4591 | |
| 4592 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4593 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4594 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4595 | |
| 4596 | req->analysers = 0; |
| 4597 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4598 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4599 | if (!(s->flags & SN_ERR_MASK)) |
| 4600 | s->flags |= SN_ERR_PRXCOND; |
| 4601 | if (!(s->flags & SN_FINST_MASK)) |
| 4602 | s->flags |= SN_FINST_T; |
| 4603 | return 0; |
| 4604 | } |
| 4605 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4606 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4607 | * for either the buffer to be full, or the full advertised contents to have |
| 4608 | * reached the buffer. It must only be called after the standard HTTP request |
| 4609 | * processing has occurred, because it expects the request to be parsed and will |
| 4610 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4611 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4612 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4613 | * 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] | 4614 | */ |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4615 | int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4616 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4617 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4618 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4619 | |
| 4620 | /* We have to parse the HTTP request body to find any required data. |
| 4621 | * "balance url_param check_post" should have been the only way to get |
| 4622 | * into this. We were brought here after HTTP header analysis, so all |
| 4623 | * related structures are ready. |
| 4624 | */ |
| 4625 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4626 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4627 | /* This is the first call */ |
| 4628 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4629 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4630 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4631 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4632 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4633 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4634 | */ |
| 4635 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4636 | struct hdr_ctx ctx; |
| 4637 | ctx.idx = 0; |
| 4638 | /* Expect is allowed in 1.1, look for it */ |
| 4639 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4640 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 4641 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 4642 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4643 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4644 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4645 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4646 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4647 | /* 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] | 4648 | * req->buf->p still points to the beginning of the message. We |
| 4649 | * must save the body in msg->next because it survives buffer |
| 4650 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4651 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4652 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4653 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4654 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4655 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4656 | else |
| 4657 | msg->msg_state = HTTP_MSG_DATA; |
| 4658 | } |
| 4659 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4660 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4661 | /* We're in content-length mode, we just have to wait for enough data. */ |
Willy Tarreau | 5b7f924 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4662 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4663 | goto missing_data; |
| 4664 | |
| 4665 | /* OK we have everything we need now */ |
| 4666 | goto http_end; |
| 4667 | } |
| 4668 | |
| 4669 | /* OK here we're parsing a chunked-encoded message */ |
| 4670 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4671 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4672 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4673 | * 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] | 4674 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4675 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4676 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4677 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4678 | if (!ret) |
| 4679 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4680 | else if (ret < 0) { |
| 4681 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4682 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4683 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4684 | } |
| 4685 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4686 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 5b7f924 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4687 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 4688 | * for at least a whole chunk or the whole content length bytes after |
| 4689 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4690 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4691 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4692 | goto http_end; |
| 4693 | |
Willy Tarreau | 5b7f924 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4694 | 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] | 4695 | goto http_end; |
| 4696 | |
| 4697 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4698 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4699 | * we have the maximum we can expect. |
| 4700 | */ |
| 4701 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4702 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4703 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4704 | 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] | 4705 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4706 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4707 | |
| 4708 | if (!(s->flags & SN_ERR_MASK)) |
| 4709 | s->flags |= SN_ERR_CLITO; |
| 4710 | if (!(s->flags & SN_FINST_MASK)) |
| 4711 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4712 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4713 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4714 | |
| 4715 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4716 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4717 | /* Not enough data. We'll re-use the http-request |
| 4718 | * timeout here. Ideally, we should set the timeout |
| 4719 | * relative to the accept() date. We just set the |
| 4720 | * request timeout once at the beginning of the |
| 4721 | * request. |
| 4722 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4723 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4724 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4725 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4726 | return 0; |
| 4727 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4728 | |
| 4729 | http_end: |
| 4730 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4731 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4732 | req->analysers &= ~an_bit; |
| 4733 | req->analyse_exp = TICK_ETERNITY; |
| 4734 | return 1; |
| 4735 | |
| 4736 | return_bad_req: /* let's centralize all bad requests */ |
| 4737 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4738 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4739 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4740 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4741 | if (!(s->flags & SN_ERR_MASK)) |
| 4742 | s->flags |= SN_ERR_PRXCOND; |
| 4743 | if (!(s->flags & SN_FINST_MASK)) |
| 4744 | s->flags |= SN_FINST_R; |
| 4745 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4746 | return_err_msg: |
| 4747 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4748 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4749 | if (s->listener->counters) |
| 4750 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4751 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4752 | } |
| 4753 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4754 | /* send a server's name with an outgoing request over an established connection. |
| 4755 | * Note: this function is designed to be called once the request has been scheduled |
| 4756 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4757 | * proceeding. |
| 4758 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4759 | 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] | 4760 | |
| 4761 | struct hdr_ctx ctx; |
| 4762 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4763 | char *hdr_name = be->server_id_hdr_name; |
| 4764 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4765 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4766 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4767 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4768 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4769 | ctx.idx = 0; |
| 4770 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4771 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4772 | if (old_o) { |
| 4773 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4774 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4775 | txn->req.next += old_o; |
| 4776 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4777 | } |
| 4778 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4779 | old_i = chn->buf->i; |
| 4780 | 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] | 4781 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4782 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4783 | } |
| 4784 | |
| 4785 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4786 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4787 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4788 | hdr_val += hdr_name_len; |
| 4789 | *hdr_val++ = ':'; |
| 4790 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4791 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4792 | 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] | 4793 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4794 | if (old_o) { |
| 4795 | /* If this was a forwarded request, we must readjust the amount of |
| 4796 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4797 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4798 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4799 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4800 | old_o += chn->buf->i - old_i; |
| 4801 | b_adv(chn->buf, old_o); |
| 4802 | txn->req.next -= old_o; |
| 4803 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4804 | } |
| 4805 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4806 | return 0; |
| 4807 | } |
| 4808 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4809 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4810 | * right now but it works. |
| 4811 | */ |
| 4812 | void http_end_txn_clean_session(struct session *s) |
| 4813 | { |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4814 | int prev_status = s->txn.status; |
| 4815 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4816 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4817 | * server's connections. We need a safer way to reinitialize buffer |
| 4818 | * flags. We also need a more accurate method for computing per-request |
| 4819 | * data. |
| 4820 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4821 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4822 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4823 | * to the server. |
| 4824 | */ |
| 4825 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4826 | !si_conn_ready(s->req->cons)) { |
| 4827 | s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4828 | si_shutr(s->req->cons); |
| 4829 | si_shutw(s->req->cons); |
| 4830 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4831 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4832 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4833 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4834 | if (unlikely(s->srv_conn)) |
| 4835 | sess_change_server(s, NULL); |
| 4836 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4837 | |
| 4838 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4839 | session_process_counters(s); |
| 4840 | |
| 4841 | if (s->txn.status) { |
| 4842 | int n; |
| 4843 | |
| 4844 | n = s->txn.status / 100; |
| 4845 | if (n < 1 || n > 5) |
| 4846 | n = 0; |
| 4847 | |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4848 | if (s->fe->mode == PR_MODE_HTTP) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4849 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4850 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4851 | s->fe->fe_counters.p.http.comp_rsp++; |
| 4852 | } |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 4853 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4854 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4855 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4856 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4857 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4858 | s->be->be_counters.p.http.comp_rsp++; |
| 4859 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4860 | } |
| 4861 | |
| 4862 | /* don't count other requests' data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4863 | s->logs.bytes_in -= s->req->buf->i; |
| 4864 | s->logs.bytes_out -= s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4865 | |
| 4866 | /* let's do a final log if we need it */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 4867 | if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4868 | !(s->flags & SN_MONITOR) && |
| 4869 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 4870 | s->do_log(s); |
| 4871 | } |
| 4872 | |
Willy Tarreau | c177ea7 | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 4873 | /* stop tracking content-based counters */ |
| 4874 | session_stop_content_counters(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 4875 | session_update_time_stats(s); |
| 4876 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4877 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4878 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4879 | tv_zero(&s->logs.tv_request); |
| 4880 | s->logs.t_queue = -1; |
| 4881 | s->logs.t_connect = -1; |
| 4882 | s->logs.t_data = -1; |
| 4883 | s->logs.t_close = 0; |
| 4884 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4885 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4886 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4887 | s->logs.bytes_in = s->req->total = s->req->buf->i; |
| 4888 | s->logs.bytes_out = s->rep->total = s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4889 | |
| 4890 | if (s->pend_pos) |
| 4891 | pendconn_free(s->pend_pos); |
| 4892 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4893 | if (objt_server(s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4894 | if (s->flags & SN_CURR_SESS) { |
| 4895 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4896 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4897 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4898 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 4899 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4900 | } |
| 4901 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4902 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4903 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4904 | /* only release our endpoint if we don't intend to reuse the |
| 4905 | * connection. |
| 4906 | */ |
| 4907 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4908 | !si_conn_ready(s->req->cons)) { |
| 4909 | si_release_endpoint(s->req->cons); |
| 4910 | } |
| 4911 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4912 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4913 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 4914 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4915 | s->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | c920096 | 2013-12-31 23:03:09 +0100 | [diff] [blame] | 4916 | s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */ |
Willy Tarreau | b4d0509 | 2014-09-01 20:35:55 +0200 | [diff] [blame] | 4917 | 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); |
| 4918 | s->rep->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); |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4919 | s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST); |
Willy Tarreau | 3634624 | 2014-02-24 18:26:30 +0100 | [diff] [blame] | 4920 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED); |
Cyril Bonté | 17f7707 | 2014-10-22 22:30:13 +0200 | [diff] [blame] | 4921 | s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4922 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4923 | s->txn.meth = 0; |
| 4924 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4925 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4926 | |
| 4927 | if (prev_status == 401 || prev_status == 407) { |
| 4928 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 4929 | * a chance of being able to send the visitor again to the same |
| 4930 | * server over the same connection. This is required by some |
| 4931 | * broken protocols such as NTLM, and anyway whenever there is |
| 4932 | * an opportunity for sending the challenge to the proper place, |
| 4933 | * it's better to do it (at least it helps with debugging). |
| 4934 | */ |
| 4935 | s->txn.flags |= TX_PREFER_LAST; |
| 4936 | } |
| 4937 | |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 4938 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4939 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 4940 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4941 | if (s->fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4942 | s->req->flags |= CF_NEVER_WAIT; |
| 4943 | s->rep->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4944 | } |
| 4945 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4946 | /* if the request buffer is not empty, it means we're |
| 4947 | * about to process another request, so send pending |
| 4948 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4949 | * Just don't do this if the buffer is close to be full, |
| 4950 | * because the request will wait for it to flush a little |
| 4951 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4952 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4953 | if (s->req->buf->i) { |
| 4954 | if (s->rep->buf->o && |
| 4955 | !buffer_full(s->rep->buf, global.tune.maxrewrite) && |
| 4956 | bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4957 | s->rep->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4958 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4959 | |
Willy Tarreau | 3de8e7a | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 4960 | /* we're removing the analysers, we MUST re-enable events detection. |
| 4961 | * We don't enable close on the response channel since it's either |
| 4962 | * already closed, or in keep-alive with an idle connection handler. |
| 4963 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4964 | channel_auto_read(s->req); |
| 4965 | channel_auto_close(s->req); |
| 4966 | channel_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4967 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 4968 | /* we're in keep-alive with an idle connection, monitor it */ |
| 4969 | si_idle_conn(s->req->cons); |
| 4970 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 4971 | s->req->analysers = s->listener->analysers; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4972 | s->rep->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4973 | } |
| 4974 | |
| 4975 | |
| 4976 | /* This function updates the request state machine according to the response |
| 4977 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4978 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4979 | * it is only used to find when a request/response couple is complete. Both |
| 4980 | * this function and its equivalent should loop until both return zero. It |
| 4981 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4982 | */ |
| 4983 | int http_sync_req_state(struct session *s) |
| 4984 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4985 | struct channel *chn = s->req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4986 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4987 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4988 | unsigned int old_state = txn->req.msg_state; |
| 4989 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4990 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 4991 | return 0; |
| 4992 | |
| 4993 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4994 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4995 | * We can shut the read side unless we want to abort_on_close, |
| 4996 | * or we have a POST request. The issue with POST requests is |
| 4997 | * that some browsers still send a CRLF after the request, and |
| 4998 | * this CRLF must be read so that it does not remain in the kernel |
| 4999 | * buffers, otherwise a close could cause an RST on some systems |
| 5000 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5001 | * Note that if we're using keep-alive on the client side, we'd |
| 5002 | * rather poll now and keep the polling enabled for the whole |
| 5003 | * session's life than enabling/disabling it between each |
| 5004 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5005 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5006 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5007 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5008 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 5009 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5010 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5011 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5012 | /* if the server closes the connection, we want to immediately react |
| 5013 | * and close the socket to save packets and syscalls. |
| 5014 | */ |
| 5015 | chn->cons->flags |= SI_FL_NOHALF; |
| 5016 | |
Willy Tarreau | c0d5613 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 5017 | /* In any case we've finished parsing the request so we must |
| 5018 | * disable Nagle when sending data because 1) we're not going |
| 5019 | * to shut this side, and 2) the server is waiting for us to |
| 5020 | * send pending data. |
| 5021 | */ |
| 5022 | chn->flags |= CF_NEVER_WAIT; |
| 5023 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5024 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5025 | goto wait_other_side; |
| 5026 | |
| 5027 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5028 | /* The server has not finished to respond, so we |
| 5029 | * don't want to move in order not to upset it. |
| 5030 | */ |
| 5031 | goto wait_other_side; |
| 5032 | } |
| 5033 | |
| 5034 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5035 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5036 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5037 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5038 | goto wait_other_side; |
| 5039 | } |
| 5040 | |
| 5041 | /* When we get here, it means that both the request and the |
| 5042 | * response have finished receiving. Depending on the connection |
| 5043 | * mode, we'll have to wait for the last bytes to leave in either |
| 5044 | * direction, and sometimes for a close to be effective. |
| 5045 | */ |
| 5046 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5047 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5048 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5049 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5050 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5051 | } |
| 5052 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5053 | /* Option forceclose is set, or either side wants to close, |
| 5054 | * let's enforce it now that we're not expecting any new |
| 5055 | * data to come. The caller knows the session is complete |
| 5056 | * once both states are CLOSED. |
| 5057 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5058 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5059 | channel_shutr_now(chn); |
| 5060 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5061 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5062 | } |
| 5063 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5064 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5065 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5066 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5067 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5068 | channel_auto_read(chn); |
| 5069 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5070 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5071 | } |
| 5072 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5073 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5074 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5075 | chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5076 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5077 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5078 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5079 | goto http_msg_closing; |
| 5080 | } |
| 5081 | else { |
| 5082 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5083 | goto http_msg_closed; |
| 5084 | } |
| 5085 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5086 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5087 | } |
| 5088 | |
| 5089 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5090 | http_msg_closing: |
| 5091 | /* nothing else to forward, just waiting for the output buffer |
| 5092 | * to be empty and for the shutw_now to take effect. |
| 5093 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5094 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5095 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5096 | goto http_msg_closed; |
| 5097 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5098 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5099 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5100 | goto wait_other_side; |
| 5101 | } |
| 5102 | } |
| 5103 | |
| 5104 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5105 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5106 | /* see above in MSG_DONE why we only do this in these states */ |
| 5107 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5108 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5109 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5110 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5111 | goto wait_other_side; |
| 5112 | } |
| 5113 | |
| 5114 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5115 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5116 | } |
| 5117 | |
| 5118 | |
| 5119 | /* This function updates the response state machine according to the request |
| 5120 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5121 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5122 | * it is only used to find when a request/response couple is complete. Both |
| 5123 | * this function and its equivalent should loop until both return zero. It |
| 5124 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5125 | */ |
| 5126 | int http_sync_res_state(struct session *s) |
| 5127 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5128 | struct channel *chn = s->rep; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5129 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5130 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5131 | unsigned int old_state = txn->rsp.msg_state; |
| 5132 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5133 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5134 | return 0; |
| 5135 | |
| 5136 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5137 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5138 | * still monitor the server connection for a possible close |
| 5139 | * while the request is being uploaded, so we don't disable |
| 5140 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5141 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5142 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5143 | |
| 5144 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5145 | goto wait_other_side; |
| 5146 | |
| 5147 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5148 | /* The client seems to still be sending data, probably |
| 5149 | * because we got an error response during an upload. |
| 5150 | * We have the choice of either breaking the connection |
| 5151 | * or letting it pass through. Let's do the later. |
| 5152 | */ |
| 5153 | goto wait_other_side; |
| 5154 | } |
| 5155 | |
| 5156 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5157 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5158 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5159 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5160 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5161 | goto wait_other_side; |
| 5162 | } |
| 5163 | |
| 5164 | /* When we get here, it means that both the request and the |
| 5165 | * response have finished receiving. Depending on the connection |
| 5166 | * mode, we'll have to wait for the last bytes to leave in either |
| 5167 | * direction, and sometimes for a close to be effective. |
| 5168 | */ |
| 5169 | |
| 5170 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5171 | /* Server-close mode : shut read and wait for the request |
| 5172 | * side to close its output buffer. The caller will detect |
| 5173 | * when we're in DONE and the other is in CLOSED and will |
| 5174 | * catch that for the final cleanup. |
| 5175 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5176 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5177 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5178 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5179 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5180 | /* Option forceclose is set, or either side wants to close, |
| 5181 | * let's enforce it now that we're not expecting any new |
| 5182 | * data to come. The caller knows the session is complete |
| 5183 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5184 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5185 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5186 | channel_shutr_now(chn); |
| 5187 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5188 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5189 | } |
| 5190 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5191 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5192 | * need to forward remaining data. Keep-alive will need to monitor |
| 5193 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5194 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5195 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5196 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5197 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5198 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5199 | } |
| 5200 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5201 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5202 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5203 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5204 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5205 | goto http_msg_closing; |
| 5206 | } |
| 5207 | else { |
| 5208 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5209 | goto http_msg_closed; |
| 5210 | } |
| 5211 | } |
| 5212 | goto wait_other_side; |
| 5213 | } |
| 5214 | |
| 5215 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5216 | http_msg_closing: |
| 5217 | /* nothing else to forward, just waiting for the output buffer |
| 5218 | * to be empty and for the shutw_now to take effect. |
| 5219 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5220 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5221 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5222 | goto http_msg_closed; |
| 5223 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5224 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5225 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5226 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5227 | if (objt_server(s->target)) |
| 5228 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5229 | goto wait_other_side; |
| 5230 | } |
| 5231 | } |
| 5232 | |
| 5233 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5234 | http_msg_closed: |
| 5235 | /* drop any pending data */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5236 | bi_erase(chn); |
| 5237 | channel_auto_close(chn); |
| 5238 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5239 | goto wait_other_side; |
| 5240 | } |
| 5241 | |
| 5242 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5243 | /* We force the response to leave immediately if we're waiting for the |
| 5244 | * other side, since there is no pending shutdown to push it out. |
| 5245 | */ |
| 5246 | if (!channel_is_empty(chn)) |
| 5247 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5248 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5249 | } |
| 5250 | |
| 5251 | |
| 5252 | /* Resync the request and response state machines. Return 1 if either state |
| 5253 | * changes. |
| 5254 | */ |
| 5255 | int http_resync_states(struct session *s) |
| 5256 | { |
| 5257 | struct http_txn *txn = &s->txn; |
| 5258 | int old_req_state = txn->req.msg_state; |
| 5259 | int old_res_state = txn->rsp.msg_state; |
| 5260 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5261 | http_sync_req_state(s); |
| 5262 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5263 | if (!http_sync_res_state(s)) |
| 5264 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5265 | if (!http_sync_req_state(s)) |
| 5266 | break; |
| 5267 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5268 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5269 | /* OK, both state machines agree on a compatible state. |
| 5270 | * There are a few cases we're interested in : |
| 5271 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5272 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5273 | * directions, so let's simply disable both analysers. |
| 5274 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5275 | * request. |
| 5276 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5277 | * with server-close mode means we've completed one request and we |
| 5278 | * must re-initialize the server connection. |
| 5279 | */ |
| 5280 | |
| 5281 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5282 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5283 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5284 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 5285 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5286 | channel_auto_close(s->req); |
| 5287 | channel_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5288 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5289 | channel_auto_close(s->rep); |
| 5290 | channel_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5291 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5292 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
| 5293 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5294 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5295 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5296 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5297 | channel_auto_close(s->rep); |
| 5298 | channel_auto_read(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5299 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5300 | channel_abort(s->req); |
| 5301 | channel_auto_close(s->req); |
| 5302 | channel_auto_read(s->req); |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5303 | bi_erase(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5304 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5305 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5306 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5307 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5308 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5309 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5310 | /* server-close/keep-alive: terminate this transaction, |
| 5311 | * possibly killing the server connection and reinitialize |
Willy Tarreau | c72d85b | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 5312 | * a fresh-new transaction, but only once we're sure there's |
| 5313 | * enough room in the request and response buffer to process |
| 5314 | * another request. The request buffer must not hold any |
| 5315 | * pending output data and the request buffer must not have |
| 5316 | * output data occupying the reserve. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5317 | */ |
Willy Tarreau | c72d85b | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 5318 | if (s->req->buf->o) |
| 5319 | s->req->flags |= CF_WAKE_WRITE; |
| 5320 | else if (channel_congested(s->rep)) |
| 5321 | s->rep->flags |= CF_WAKE_WRITE; |
| 5322 | else |
| 5323 | http_end_txn_clean_session(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5324 | } |
| 5325 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5326 | return txn->req.msg_state != old_req_state || |
| 5327 | txn->rsp.msg_state != old_res_state; |
| 5328 | } |
| 5329 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5330 | /* This function is an analyser which forwards request body (including chunk |
| 5331 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5332 | * zero byte. The only situation where it must not be called is when we're in |
| 5333 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5334 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5335 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5336 | * read more data, or 1 once we can go on with next request or end the session. |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5337 | * 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] | 5338 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5339 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5340 | int http_request_forward_body(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5341 | { |
| 5342 | struct http_txn *txn = &s->txn; |
| 5343 | struct http_msg *msg = &s->txn.req; |
| 5344 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5345 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5346 | return 0; |
| 5347 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5348 | 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] | 5349 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5350 | /* Output closed while we were sending data. We must abort and |
| 5351 | * wake the other side up. |
| 5352 | */ |
| 5353 | msg->msg_state = HTTP_MSG_ERROR; |
| 5354 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5355 | return 1; |
| 5356 | } |
| 5357 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5358 | /* Note that we don't have to send 100-continue back because we don't |
| 5359 | * need the data to complete our job, and it's up to the server to |
| 5360 | * decide whether to return 100, 417 or anything else in return of |
| 5361 | * an "Expect: 100-continue" header. |
| 5362 | */ |
| 5363 | |
Willy Tarreau | 5bebcd0 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5364 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5365 | /* we have msg->sov which points to the first byte of message |
| 5366 | * body, and req->buf.p still points to the beginning of the |
| 5367 | * message. We forward the headers now, as we don't need them |
| 5368 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5369 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5370 | b_adv(req->buf, msg->sov); |
| 5371 | msg->next -= msg->sov; |
| 5372 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5373 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5374 | /* The previous analysers guarantee that the state is somewhere |
| 5375 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 5376 | * msg->next are always correct. |
| 5377 | */ |
| 5378 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5379 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 5380 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5381 | else |
| 5382 | msg->msg_state = HTTP_MSG_DATA; |
| 5383 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5384 | } |
| 5385 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5386 | /* Some post-connect processing might want us to refrain from starting to |
| 5387 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5388 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5389 | */ |
| 5390 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
| 5391 | if (!(s->rep->flags & CF_READ_ATTACHED)) { |
| 5392 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5393 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5394 | goto missing_data; |
| 5395 | } |
| 5396 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5397 | } |
| 5398 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5399 | /* in most states, we should abort in case of early close */ |
| 5400 | channel_auto_close(req); |
| 5401 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5402 | if (req->to_forward) { |
| 5403 | /* We can't process the buffer's contents yet */ |
| 5404 | req->flags |= CF_WAKE_WRITE; |
| 5405 | goto missing_data; |
| 5406 | } |
| 5407 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5408 | while (1) { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5409 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5410 | /* must still forward */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5411 | /* we may have some pending data starting at req->buf->p */ |
| 5412 | if (msg->chunk_len > req->buf->i - msg->next) { |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5413 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5414 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5415 | } |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5416 | msg->next += msg->chunk_len; |
| 5417 | msg->chunk_len = 0; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5418 | |
| 5419 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5420 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5421 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5422 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5423 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5424 | } |
| 5425 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5426 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 5427 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5428 | * TRAILERS state. |
| 5429 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5430 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5431 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5432 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5433 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5434 | else if (ret < 0) { |
| 5435 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5436 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5437 | http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5438 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5439 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5440 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5441 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5442 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5443 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5444 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5445 | |
| 5446 | if (ret == 0) |
| 5447 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5448 | else if (ret < 0) { |
| 5449 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5450 | if (msg->err_pos >= 0) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5451 | http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5452 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5453 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5454 | /* we're in MSG_CHUNK_SIZE now */ |
| 5455 | } |
| 5456 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5457 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5458 | |
| 5459 | if (ret == 0) |
| 5460 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5461 | else if (ret < 0) { |
| 5462 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5463 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5464 | http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5465 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5466 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5467 | /* we're in HTTP_MSG_DONE now */ |
| 5468 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5469 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5470 | int old_state = msg->msg_state; |
| 5471 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5472 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5473 | |
| 5474 | /* we may have some pending data starting at req->buf->p |
| 5475 | * such as last chunk of data or trailers. |
| 5476 | */ |
| 5477 | b_adv(req->buf, msg->next); |
Willy Tarreau | b4d0509 | 2014-09-01 20:35:55 +0200 | [diff] [blame] | 5478 | if (unlikely(!(s->req->flags & CF_WROTE_DATA))) |
Willy Tarreau | 5bebcd0 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5479 | msg->sov -= msg->next; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5480 | msg->next = 0; |
| 5481 | |
Willy Tarreau | 294e467 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5482 | /* we don't want to forward closes on DONE except in |
| 5483 | * tunnel mode. |
| 5484 | */ |
| 5485 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5486 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5487 | if (http_resync_states(s)) { |
| 5488 | /* some state changes occurred, maybe the analyser |
| 5489 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 5490 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5491 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5492 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5493 | /* request errors are most likely due to |
| 5494 | * the server aborting the transfer. |
| 5495 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5496 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5497 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5498 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5499 | http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5500 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5501 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5502 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5503 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5504 | |
| 5505 | /* If "option abortonclose" is set on the backend, we |
| 5506 | * want to monitor the client's connection and forward |
| 5507 | * any shutdown notification to the server, which will |
| 5508 | * decide whether to close or to go on processing the |
Willy Tarreau | 294e467 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5509 | * request. We only do that in tunnel mode, and not in |
| 5510 | * other modes since it can be abused to exhaust source |
| 5511 | * ports. |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5512 | */ |
| 5513 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5514 | channel_auto_read(req); |
Willy Tarreau | 294e467 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5515 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 5516 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 5517 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5518 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5519 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5520 | else if (s->txn.meth == HTTP_METH_POST) { |
| 5521 | /* POST requests may require to read extra CRLF |
| 5522 | * sent by broken browsers and which could cause |
| 5523 | * an RST to be sent upon close on some systems |
| 5524 | * (eg: Linux). |
| 5525 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5526 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5527 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5528 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5529 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5530 | } |
| 5531 | } |
| 5532 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5533 | missing_data: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5534 | /* we may have some pending data starting at req->buf->p */ |
| 5535 | b_adv(req->buf, msg->next); |
Willy Tarreau | b4d0509 | 2014-09-01 20:35:55 +0200 | [diff] [blame] | 5536 | if (unlikely(!(s->req->flags & CF_WROTE_DATA))) |
Willy Tarreau | 5bebcd0 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5537 | msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i); |
| 5538 | |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5539 | msg->next = 0; |
| 5540 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
| 5541 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5542 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5543 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5544 | if (!(s->flags & SN_ERR_MASK)) |
| 5545 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5546 | if (!(s->flags & SN_FINST_MASK)) { |
| 5547 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5548 | s->flags |= SN_FINST_H; |
| 5549 | else |
| 5550 | s->flags |= SN_FINST_D; |
| 5551 | } |
| 5552 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5553 | s->fe->fe_counters.cli_aborts++; |
| 5554 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5555 | if (objt_server(s->target)) |
| 5556 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5557 | |
| 5558 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5559 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5560 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5561 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5562 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5563 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5564 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5565 | /* 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] | 5566 | * 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] | 5567 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5568 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5569 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5570 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5571 | /* 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] | 5572 | * 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] | 5573 | * 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] | 5574 | * modes are already handled by the stream sock layer. We must not do |
| 5575 | * this in content-length mode because it could present the MSG_MORE |
| 5576 | * flag with the last block of forwarded data, which would cause an |
| 5577 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5578 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5579 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5580 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5581 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5582 | return 0; |
| 5583 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5584 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5585 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5586 | if (s->listener->counters) |
| 5587 | s->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5588 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5589 | return_bad_req_stats_ok: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5590 | /* we may have some pending data starting at req->buf->p */ |
| 5591 | b_adv(req->buf, msg->next); |
| 5592 | msg->next = 0; |
| 5593 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5594 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5595 | if (txn->status) { |
| 5596 | /* Note: we don't send any error if some data were already sent */ |
| 5597 | stream_int_retnclose(req->prod, NULL); |
| 5598 | } else { |
| 5599 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5600 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5601 | } |
| 5602 | req->analysers = 0; |
| 5603 | s->rep->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] | 5604 | |
| 5605 | if (!(s->flags & SN_ERR_MASK)) |
| 5606 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5607 | if (!(s->flags & SN_FINST_MASK)) { |
| 5608 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5609 | s->flags |= SN_FINST_H; |
| 5610 | else |
| 5611 | s->flags |= SN_FINST_D; |
| 5612 | } |
| 5613 | return 0; |
| 5614 | |
| 5615 | aborted_xfer: |
| 5616 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5617 | if (txn->status) { |
| 5618 | /* Note: we don't send any error if some data were already sent */ |
| 5619 | stream_int_retnclose(req->prod, NULL); |
| 5620 | } else { |
| 5621 | txn->status = 502; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5622 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5623 | } |
| 5624 | req->analysers = 0; |
| 5625 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5626 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5627 | s->fe->fe_counters.srv_aborts++; |
| 5628 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5629 | if (objt_server(s->target)) |
| 5630 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5631 | |
| 5632 | if (!(s->flags & SN_ERR_MASK)) |
| 5633 | s->flags |= SN_ERR_SRVCL; |
| 5634 | if (!(s->flags & SN_FINST_MASK)) { |
| 5635 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5636 | s->flags |= SN_FINST_H; |
| 5637 | else |
| 5638 | s->flags |= SN_FINST_D; |
| 5639 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5640 | return 0; |
| 5641 | } |
| 5642 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5643 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5644 | * processing can continue on next analysers, or zero if it either needs more |
| 5645 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 5646 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 5647 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5648 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5649 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5650 | int http_wait_for_response(struct session *s, struct channel *rep, int an_bit) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5651 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5652 | struct http_txn *txn = &s->txn; |
| 5653 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5654 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5655 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5656 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5657 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5658 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5659 | DPRINTF(stderr,"[%u] %s: session=%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] | 5660 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5661 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5662 | rep, |
| 5663 | rep->rex, rep->wex, |
| 5664 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5665 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5666 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5667 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5668 | /* |
| 5669 | * Now parse the partial (or complete) lines. |
| 5670 | * We will check the response syntax, and also join multi-line |
| 5671 | * headers. An index of all the lines will be elaborated while |
| 5672 | * parsing. |
| 5673 | * |
| 5674 | * For the parsing, we use a 28 states FSM. |
| 5675 | * |
| 5676 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5677 | * rep->buf->p = beginning of response |
| 5678 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5679 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5680 | * msg->eol = end of current header or line (LF or CRLF) |
| 5681 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5682 | */ |
| 5683 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5684 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5685 | /* There's a protected area at the end of the buffer for rewriting |
| 5686 | * purposes. We don't want to start to parse the request if the |
| 5687 | * protected area is affected, because we may have to move processed |
| 5688 | * data later, which is much more complicated. |
| 5689 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5690 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5691 | if (unlikely(!channel_reserved(rep))) { |
| 5692 | /* some data has still not left the buffer, wake us once that's done */ |
| 5693 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5694 | goto abort_response; |
| 5695 | channel_dont_close(rep); |
| 5696 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5697 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5698 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5699 | } |
| 5700 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5701 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5702 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5703 | buffer_slow_realign(rep->buf); |
| 5704 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5705 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5706 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5707 | } |
| 5708 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5709 | /* 1: we might have to print this header in debug mode */ |
| 5710 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5711 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 8767b13 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5712 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5713 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5714 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5715 | sol = rep->buf->p; |
| 5716 | 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] | 5717 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5718 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5719 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5720 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5721 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5722 | while (cur_idx) { |
| 5723 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5724 | debug_hdr("srvhdr", s, sol, eol); |
| 5725 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5726 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5727 | } |
| 5728 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5729 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5730 | /* |
| 5731 | * Now we quickly check if we have found a full valid response. |
| 5732 | * If not so, we check the FD and buffer states before leaving. |
| 5733 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5734 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5735 | * responses are checked first. |
| 5736 | * |
| 5737 | * Depending on whether the client is still there or not, we |
| 5738 | * may send an error response back or not. Note that normally |
| 5739 | * we should only check for HTTP status there, and check I/O |
| 5740 | * errors somewhere else. |
| 5741 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5742 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5743 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5744 | /* Invalid response */ |
| 5745 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5746 | /* we detected a parsing error. We want to archive this response |
| 5747 | * in the dedicated proxy area for later troubleshooting. |
| 5748 | */ |
| 5749 | hdr_response_bad: |
| 5750 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5751 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5752 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5753 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5754 | if (objt_server(s->target)) { |
| 5755 | objt_server(s->target)->counters.failed_resp++; |
| 5756 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5757 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5758 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5759 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5760 | rep->analysers = 0; |
| 5761 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5762 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5763 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5764 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5765 | |
| 5766 | if (!(s->flags & SN_ERR_MASK)) |
| 5767 | s->flags |= SN_ERR_PRXCOND; |
| 5768 | if (!(s->flags & SN_FINST_MASK)) |
| 5769 | s->flags |= SN_FINST_H; |
| 5770 | |
| 5771 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5772 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5773 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5774 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5775 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5776 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5777 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5778 | goto hdr_response_bad; |
| 5779 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5780 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5781 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5782 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5783 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5784 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5785 | else if (txn->flags & TX_NOT_FIRST) |
| 5786 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5787 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5788 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5789 | if (objt_server(s->target)) { |
| 5790 | objt_server(s->target)->counters.failed_resp++; |
| 5791 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5792 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5793 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5794 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5795 | rep->analysers = 0; |
| 5796 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5797 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5798 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5799 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5800 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5801 | if (!(s->flags & SN_ERR_MASK)) |
| 5802 | s->flags |= SN_ERR_SRVCL; |
| 5803 | if (!(s->flags & SN_FINST_MASK)) |
| 5804 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5805 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5806 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5807 | |
Willy Tarreau | 2a4f511 | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 5808 | /* read timeout : return a 504 to the client. */ |
| 5809 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5810 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5811 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5812 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5813 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5814 | if (objt_server(s->target)) { |
| 5815 | objt_server(s->target)->counters.failed_resp++; |
| 5816 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5817 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5818 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5819 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5820 | rep->analysers = 0; |
| 5821 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5822 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5823 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5824 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5825 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5826 | if (!(s->flags & SN_ERR_MASK)) |
| 5827 | s->flags |= SN_ERR_SRVTO; |
| 5828 | if (!(s->flags & SN_FINST_MASK)) |
| 5829 | s->flags |= SN_FINST_H; |
| 5830 | return 0; |
| 5831 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5832 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5833 | /* client abort with an abortonclose */ |
| 5834 | else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
| 5835 | s->fe->fe_counters.cli_aborts++; |
| 5836 | s->be->be_counters.cli_aborts++; |
| 5837 | if (objt_server(s->target)) |
| 5838 | objt_server(s->target)->counters.cli_aborts++; |
| 5839 | |
| 5840 | rep->analysers = 0; |
| 5841 | channel_auto_close(rep); |
| 5842 | |
| 5843 | txn->status = 400; |
| 5844 | bi_erase(rep); |
| 5845 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400)); |
| 5846 | |
| 5847 | if (!(s->flags & SN_ERR_MASK)) |
| 5848 | s->flags |= SN_ERR_CLICL; |
| 5849 | if (!(s->flags & SN_FINST_MASK)) |
| 5850 | s->flags |= SN_FINST_H; |
| 5851 | |
| 5852 | /* process_session() will take care of the error */ |
| 5853 | return 0; |
| 5854 | } |
| 5855 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5856 | /* 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] | 5857 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5858 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5859 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5860 | else if (txn->flags & TX_NOT_FIRST) |
| 5861 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5862 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5863 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5864 | if (objt_server(s->target)) { |
| 5865 | objt_server(s->target)->counters.failed_resp++; |
| 5866 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5867 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5868 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5869 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5870 | rep->analysers = 0; |
| 5871 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5872 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5873 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5874 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5875 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5876 | if (!(s->flags & SN_ERR_MASK)) |
| 5877 | s->flags |= SN_ERR_SRVCL; |
| 5878 | if (!(s->flags & SN_FINST_MASK)) |
| 5879 | s->flags |= SN_FINST_H; |
| 5880 | return 0; |
| 5881 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5882 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5883 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5884 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5885 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5886 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5887 | else if (txn->flags & TX_NOT_FIRST) |
| 5888 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5889 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5890 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5891 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5892 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5893 | |
| 5894 | if (!(s->flags & SN_ERR_MASK)) |
| 5895 | s->flags |= SN_ERR_CLICL; |
| 5896 | if (!(s->flags & SN_FINST_MASK)) |
| 5897 | s->flags |= SN_FINST_H; |
| 5898 | |
| 5899 | /* process_session() will take care of the error */ |
| 5900 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5901 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5902 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5903 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5904 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5905 | return 0; |
| 5906 | } |
| 5907 | |
| 5908 | /* More interesting part now : we know that we have a complete |
| 5909 | * response which at least looks like HTTP. We have an indicator |
| 5910 | * of each header's length, so we can parse them quickly. |
| 5911 | */ |
| 5912 | |
| 5913 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5914 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5915 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5916 | /* |
| 5917 | * 1: get the status code |
| 5918 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5919 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5920 | if (n < 1 || n > 5) |
| 5921 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5922 | /* when the client triggers a 4xx from the server, it's most often due |
| 5923 | * to a missing object or permission. These events should be tracked |
| 5924 | * because if they happen often, it may indicate a brute force or a |
| 5925 | * vulnerability scan. |
| 5926 | */ |
| 5927 | if (n == 4) |
| 5928 | session_inc_http_err_ctr(s); |
| 5929 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5930 | if (objt_server(s->target)) |
| 5931 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5932 | |
Willy Tarreau | 5564555 | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 5933 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 5934 | * exactly one digit "." one digit. This check may be disabled using |
| 5935 | * option accept-invalid-http-response. |
| 5936 | */ |
| 5937 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 5938 | if (msg->sl.st.v_l != 8) { |
| 5939 | msg->err_pos = 0; |
| 5940 | goto hdr_response_bad; |
| 5941 | } |
| 5942 | |
| 5943 | if (rep->buf->p[4] != '/' || |
| 5944 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 5945 | rep->buf->p[6] != '.' || |
| 5946 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 5947 | msg->err_pos = 4; |
| 5948 | goto hdr_response_bad; |
| 5949 | } |
| 5950 | } |
| 5951 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5952 | /* check if the response is HTTP/1.1 or above */ |
| 5953 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5954 | ((rep->buf->p[5] > '1') || |
| 5955 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5956 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5957 | |
| 5958 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5959 | 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] | 5960 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5961 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5962 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5963 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5964 | 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] | 5965 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5966 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5967 | * and 505 are triggered on demand by client request, so we must not |
| 5968 | * count them as server failures. |
| 5969 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5970 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5971 | 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] | 5972 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5973 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5974 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5975 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5976 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5977 | /* |
| 5978 | * 2: check for cacheability. |
| 5979 | */ |
| 5980 | |
| 5981 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5982 | case 100: |
| 5983 | /* |
| 5984 | * We may be facing a 100-continue response, in which case this |
| 5985 | * is not the right response, and we're waiting for the next one. |
| 5986 | * Let's allow this response to go to the client and wait for the |
| 5987 | * next one. |
| 5988 | */ |
| 5989 | hdr_idx_init(&txn->hdr_idx); |
| 5990 | msg->next -= channel_forward(rep, msg->next); |
| 5991 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5992 | txn->status = 0; |
| 5993 | s->logs.t_data = -1; /* was not a response yet */ |
| 5994 | goto next_one; |
| 5995 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5996 | case 200: |
| 5997 | case 203: |
| 5998 | case 206: |
| 5999 | case 300: |
| 6000 | case 301: |
| 6001 | case 410: |
| 6002 | /* RFC2616 @13.4: |
| 6003 | * "A response received with a status code of |
| 6004 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 6005 | * by a cache (...) unless a cache-control |
| 6006 | * directive prohibits caching." |
| 6007 | * |
| 6008 | * RFC2616 @9.5: POST method : |
| 6009 | * "Responses to this method are not cacheable, |
| 6010 | * unless the response includes appropriate |
| 6011 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6012 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6013 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6014 | ((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] | 6015 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 6016 | break; |
| 6017 | default: |
| 6018 | break; |
| 6019 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6020 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6021 | /* |
| 6022 | * 3: we may need to capture headers |
| 6023 | */ |
| 6024 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 6025 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6026 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6027 | txn->rsp.cap, s->fe->rsp_cap); |
| 6028 | |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6029 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 6030 | * by RFC7230#3.3.3 : |
| 6031 | * |
| 6032 | * The length of a message body is determined by one of the following |
| 6033 | * (in order of precedence): |
| 6034 | * |
| 6035 | * 1. Any response to a HEAD request and any response with a 1xx |
| 6036 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 6037 | * code is always terminated by the first empty line after the |
| 6038 | * header fields, regardless of the header fields present in the |
| 6039 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6040 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6041 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 6042 | * the connection will become a tunnel immediately after the empty |
| 6043 | * line that concludes the header fields. A client MUST ignore any |
| 6044 | * Content-Length or Transfer-Encoding header fields received in |
| 6045 | * such a message. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6046 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6047 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 6048 | * transfer coding (Section 4.1) is the final encoding, the message |
| 6049 | * body length is determined by reading and decoding the chunked |
| 6050 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6051 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6052 | * If a Transfer-Encoding header field is present in a response and |
| 6053 | * the chunked transfer coding is not the final encoding, the |
| 6054 | * message body length is determined by reading the connection until |
| 6055 | * it is closed by the server. If a Transfer-Encoding header field |
| 6056 | * is present in a request and the chunked transfer coding is not |
| 6057 | * the final encoding, the message body length cannot be determined |
| 6058 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 6059 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6060 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6061 | * If a message is received with both a Transfer-Encoding and a |
| 6062 | * Content-Length header field, the Transfer-Encoding overrides the |
| 6063 | * Content-Length. Such a message might indicate an attempt to |
| 6064 | * perform request smuggling (Section 9.5) or response splitting |
| 6065 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 6066 | * remove the received Content-Length field prior to forwarding such |
| 6067 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6068 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6069 | * 4. If a message is received without Transfer-Encoding and with |
| 6070 | * either multiple Content-Length header fields having differing |
| 6071 | * field-values or a single Content-Length header field having an |
| 6072 | * invalid value, then the message framing is invalid and the |
| 6073 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 6074 | * request message, the server MUST respond with a 400 (Bad Request) |
| 6075 | * status code and then close the connection. If this is a response |
| 6076 | * message received by a proxy, the proxy MUST close the connection |
| 6077 | * to the server, discard the received response, and send a 502 (Bad |
| 6078 | * Gateway) response to the client. If this is a response message |
| 6079 | * received by a user agent, the user agent MUST close the |
| 6080 | * connection to the server and discard the received response. |
| 6081 | * |
| 6082 | * 5. If a valid Content-Length header field is present without |
| 6083 | * Transfer-Encoding, its decimal value defines the expected message |
| 6084 | * body length in octets. If the sender closes the connection or |
| 6085 | * the recipient times out before the indicated number of octets are |
| 6086 | * received, the recipient MUST consider the message to be |
| 6087 | * incomplete and close the connection. |
| 6088 | * |
| 6089 | * 6. If this is a request message and none of the above are true, then |
| 6090 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6091 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6092 | * 7. Otherwise, this is a response message without a declared message |
| 6093 | * body length, so the message body length is determined by the |
| 6094 | * number of octets received prior to the server closing the |
| 6095 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6096 | */ |
| 6097 | |
| 6098 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6099 | * 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] | 6100 | * 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] | 6101 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6102 | */ |
| 6103 | if (txn->meth == HTTP_METH_HEAD || |
| 6104 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6105 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6106 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 6107 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6108 | goto skip_content_length; |
| 6109 | } |
| 6110 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6111 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6112 | ctx.idx = 0; |
Willy Tarreau | e77bc17 | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 6113 | 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] | 6114 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6115 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6116 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6117 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6118 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6119 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6120 | break; |
| 6121 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6122 | } |
| 6123 | |
Willy Tarreau | dfa3d92 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6124 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6125 | ctx.idx = 0; |
Willy Tarreau | 660418d | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6126 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | dfa3d92 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6127 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 6128 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 6129 | } |
Willy Tarreau | 660418d | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6130 | 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] | 6131 | signed long long cl; |
| 6132 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6133 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6134 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6135 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6136 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6137 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6138 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6139 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6140 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6141 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6142 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6143 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6144 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6145 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6146 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6147 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6148 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6149 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6150 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6151 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6152 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6153 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6154 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6155 | } |
| 6156 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6157 | if (s->fe->comp || s->be->comp) |
| 6158 | select_compression_response_header(s, rep->buf); |
| 6159 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6160 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6161 | /* Now we have to check if we need to modify the Connection header. |
| 6162 | * This is more difficult on the response than it is on the request, |
| 6163 | * because we can have two different HTTP versions and we don't know |
| 6164 | * how the client will interprete a response. For instance, let's say |
| 6165 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6166 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6167 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6168 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6169 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6170 | * indicating how a request MAY be understood by the client. In case |
| 6171 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6172 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6173 | * will fall back to explicit close. Note that we won't take risks with |
| 6174 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6175 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6176 | */ |
| 6177 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6178 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6179 | txn->status == 101)) { |
| 6180 | /* Either we've established an explicit tunnel, or we're |
| 6181 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6182 | * to understand the next protocols. We have to switch to tunnel |
| 6183 | * mode, so that we transfer the request and responses then let |
| 6184 | * this protocol pass unmodified. When we later implement specific |
| 6185 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6186 | * header which contains information about that protocol for |
| 6187 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6188 | */ |
| 6189 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6190 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6191 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6192 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6193 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6194 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6195 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6196 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6197 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6198 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6199 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6200 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6201 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6202 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6203 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6204 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6205 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6206 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6207 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6208 | /* now adjust header transformations depending on current state */ |
| 6209 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6210 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6211 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6212 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6213 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6214 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6215 | else { /* SCL / KAL */ |
| 6216 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6217 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6218 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6219 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6220 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6221 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6222 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6223 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6224 | /* Some keep-alive responses are converted to Server-close if |
| 6225 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6226 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6227 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6228 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6229 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6230 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6231 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6232 | } |
| 6233 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6234 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6235 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6236 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6237 | /* end of job, return OK */ |
| 6238 | rep->analysers &= ~an_bit; |
| 6239 | rep->analyse_exp = TICK_ETERNITY; |
| 6240 | channel_auto_close(rep); |
| 6241 | return 1; |
| 6242 | |
| 6243 | abort_keep_alive: |
| 6244 | /* A keep-alive request to the server failed on a network error. |
| 6245 | * The client is required to retry. We need to close without returning |
| 6246 | * any other information so that the client retries. |
| 6247 | */ |
| 6248 | txn->status = 0; |
| 6249 | rep->analysers = 0; |
| 6250 | s->req->analysers = 0; |
| 6251 | channel_auto_close(rep); |
| 6252 | s->logs.logwait = 0; |
| 6253 | s->logs.level = 0; |
| 6254 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 6255 | bi_erase(rep); |
| 6256 | stream_int_retnclose(rep->cons, NULL); |
| 6257 | return 0; |
| 6258 | } |
| 6259 | |
| 6260 | /* This function performs all the processing enabled for the current response. |
| 6261 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 6262 | * and updates s->rep->analysers. It might make sense to explode it into several |
| 6263 | * other functions. It works like process_request (see indications above). |
| 6264 | */ |
| 6265 | int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px) |
| 6266 | { |
| 6267 | struct http_txn *txn = &s->txn; |
| 6268 | struct http_msg *msg = &txn->rsp; |
| 6269 | struct proxy *cur_proxy; |
| 6270 | struct cond_wordlist *wl; |
| 6271 | struct http_res_rule *http_res_last_rule = NULL; |
| 6272 | |
| 6273 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 6274 | now_ms, __FUNCTION__, |
| 6275 | s, |
| 6276 | rep, |
| 6277 | rep->rex, rep->wex, |
| 6278 | rep->flags, |
| 6279 | rep->buf->i, |
| 6280 | rep->analysers); |
| 6281 | |
| 6282 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6283 | return 0; |
| 6284 | |
| 6285 | rep->analysers &= ~an_bit; |
| 6286 | rep->analyse_exp = TICK_ETERNITY; |
| 6287 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6288 | /* The stats applet needs to adjust the Connection header but we don't |
| 6289 | * apply any filter there. |
| 6290 | */ |
| 6291 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) |
| 6292 | goto skip_filters; |
| 6293 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6294 | /* |
| 6295 | * We will have to evaluate the filters. |
| 6296 | * As opposed to version 1.2, now they will be evaluated in the |
| 6297 | * filters order and not in the header order. This means that |
| 6298 | * each filter has to be validated among all headers. |
| 6299 | * |
| 6300 | * Filters are tried with ->be first, then with ->fe if it is |
| 6301 | * different from ->be. |
| 6302 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6303 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6304 | cur_proxy = s->be; |
| 6305 | while (1) { |
| 6306 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6307 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6308 | /* evaluate http-response rules */ |
| 6309 | if (!http_res_last_rule) |
| 6310 | http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s, txn); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 6311 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6312 | /* try headers filters */ |
| 6313 | if (rule_set->rsp_exp != NULL) { |
| 6314 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6315 | return_bad_resp: |
| 6316 | if (objt_server(s->target)) { |
| 6317 | objt_server(s->target)->counters.failed_resp++; |
| 6318 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6319 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6320 | s->be->be_counters.failed_resp++; |
| 6321 | return_srv_prx_502: |
| 6322 | rep->analysers = 0; |
| 6323 | txn->status = 502; |
| 6324 | s->logs.t_data = -1; /* was not a valid response */ |
| 6325 | rep->prod->flags |= SI_FL_NOLINGER; |
| 6326 | bi_erase(rep); |
| 6327 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
| 6328 | if (!(s->flags & SN_ERR_MASK)) |
| 6329 | s->flags |= SN_ERR_PRXCOND; |
| 6330 | if (!(s->flags & SN_FINST_MASK)) |
| 6331 | s->flags |= SN_FINST_H; |
| 6332 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6333 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6334 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6335 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6336 | /* has the response been denied ? */ |
| 6337 | if (txn->flags & TX_SVDENY) { |
| 6338 | if (objt_server(s->target)) |
| 6339 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6340 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6341 | s->be->be_counters.denied_resp++; |
| 6342 | s->fe->fe_counters.denied_resp++; |
| 6343 | if (s->listener->counters) |
| 6344 | s->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6345 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6346 | goto return_srv_prx_502; |
| 6347 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6348 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6349 | /* add response headers from the rule sets in the same order */ |
| 6350 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 0cb4b89 | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6351 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6352 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6353 | if (wl->cond) { |
| 6354 | int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 6355 | ret = acl_pass(ret); |
| 6356 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6357 | ret = !ret; |
| 6358 | if (!ret) |
| 6359 | continue; |
| 6360 | } |
| 6361 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6362 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6363 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6364 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6365 | /* check whether we're already working on the frontend */ |
| 6366 | if (cur_proxy == s->fe) |
| 6367 | break; |
| 6368 | cur_proxy = s->fe; |
| 6369 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6370 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6371 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | 0cb4b89 | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6372 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6373 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6374 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6375 | /* |
| 6376 | * Now check for a server cookie. |
| 6377 | */ |
| 6378 | if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name || |
| 6379 | (s->be->options & PR_O_CHK_CACHE)) |
| 6380 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6381 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6382 | /* |
| 6383 | * Check for cache-control or pragma headers if required. |
| 6384 | */ |
Willy Tarreau | 0cb4b89 | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6385 | 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] | 6386 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6387 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6388 | /* |
| 6389 | * Add server cookie in the response if needed |
| 6390 | */ |
| 6391 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6392 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 6393 | (!(s->flags & SN_DIRECT) || |
| 6394 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6395 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6396 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6397 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6398 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 6399 | !(s->flags & SN_IGNORE_PRST)) { |
| 6400 | /* the server is known, it's not the one the client requested, or the |
| 6401 | * cookie's last seen date needs to be refreshed. We have to |
| 6402 | * insert a set-cookie here, except if we want to insert only on POST |
| 6403 | * requests and this one isn't. Note that servers which don't have cookies |
| 6404 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6405 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6406 | if (!objt_server(s->target)->cookie) { |
| 6407 | chunk_printf(&trash, |
| 6408 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6409 | s->be->cookie_name); |
| 6410 | } |
| 6411 | else { |
| 6412 | 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] | 6413 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6414 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6415 | /* emit last_date, which is mandatory */ |
| 6416 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6417 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6418 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6419 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6420 | if (s->be->cookie_maxlife) { |
| 6421 | /* emit first_date, which is either the original one or |
| 6422 | * the current date. |
| 6423 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6424 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6425 | s30tob64(txn->cookie_first_date ? |
| 6426 | txn->cookie_first_date >> 2 : |
| 6427 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6428 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6429 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6430 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6431 | chunk_appendf(&trash, "; path=/"); |
| 6432 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6433 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6434 | if (s->be->cookie_domain) |
| 6435 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6436 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6437 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6438 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6439 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6440 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6441 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6442 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6443 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6444 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6445 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6446 | txn->flags &= ~TX_SCK_MASK; |
| 6447 | if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT)) |
| 6448 | /* the server did not change, only the date was updated */ |
| 6449 | txn->flags |= TX_SCK_UPDATED; |
| 6450 | else |
| 6451 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6452 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6453 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6454 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6455 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6456 | * 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] | 6457 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6458 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6459 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6460 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6461 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6462 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6463 | "Cache-control: private", 22) < 0)) |
| 6464 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6465 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6466 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6467 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6468 | /* |
| 6469 | * Check if result will be cacheable with a cookie. |
| 6470 | * We'll block the response if security checks have caught |
| 6471 | * nasty things such as a cacheable cookie. |
| 6472 | */ |
| 6473 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6474 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6475 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6476 | /* we're in presence of a cacheable response containing |
| 6477 | * a set-cookie header. We'll block it as requested by |
| 6478 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6479 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6480 | if (objt_server(s->target)) |
| 6481 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6482 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6483 | s->be->be_counters.denied_resp++; |
| 6484 | s->fe->fe_counters.denied_resp++; |
| 6485 | if (s->listener->counters) |
| 6486 | s->listener->counters->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6487 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6488 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6489 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6490 | send_log(s->be, LOG_ALERT, |
| 6491 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6492 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6493 | goto return_srv_prx_502; |
| 6494 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6495 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6496 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6497 | /* |
| 6498 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6499 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6500 | * not to have to deal with some client bugs : some of them fail an upgrade |
Willy Tarreau | 0cb4b89 | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6501 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6502 | * want to touch any 101 response either since it's switching to another |
| 6503 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6504 | */ |
Willy Tarreau | 0cb4b89 | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6505 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6506 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 6507 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6508 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6509 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6510 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6511 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6512 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6513 | /* we want a keep-alive response here. Keep-alive header |
| 6514 | * required if either side is not 1.1. |
| 6515 | */ |
| 6516 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6517 | want_flags |= TX_CON_KAL_SET; |
| 6518 | } |
| 6519 | else { |
| 6520 | /* we want a close response here. Close header required if |
| 6521 | * the server is 1.1, regardless of the client. |
| 6522 | */ |
| 6523 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6524 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6525 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6526 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6527 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6528 | http_change_connection_header(txn, msg, want_flags); |
| 6529 | } |
| 6530 | |
| 6531 | skip_header_mangling: |
| 6532 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
| 6533 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 6534 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6535 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6536 | /* if the user wants to log as soon as possible, without counting |
| 6537 | * bytes from the server, then this is the right moment. We have |
| 6538 | * to temporarily assign bytes_out to log what we currently have. |
| 6539 | */ |
| 6540 | if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 6541 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6542 | s->logs.bytes_out = txn->rsp.eoh; |
| 6543 | s->do_log(s); |
| 6544 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6545 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6546 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6547 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6548 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6549 | /* This function is an analyser which forwards response body (including chunk |
| 6550 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6551 | * zero byte. The only situation where it must not be called is when we're in |
| 6552 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6553 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6554 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 6555 | * read more data, or 1 once we can go on with next request or end the session. |
Willy Tarreau | d351021 | 2014-04-21 11:24:13 +0200 | [diff] [blame] | 6556 | * |
| 6557 | * It is capable of compressing response data both in content-length mode and |
| 6558 | * in chunked mode. The state machines follows different flows depending on |
| 6559 | * whether content-length and chunked modes are used, since there are no |
| 6560 | * trailers in content-length : |
| 6561 | * |
| 6562 | * chk-mode cl-mode |
| 6563 | * ,----- BODY -----. |
| 6564 | * / \ |
| 6565 | * V size > 0 V chk-mode |
| 6566 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6567 | * | | size == 0 | last byte | |
| 6568 | * | v final crlf v inspected | |
| 6569 | * | TRAILERS -----------> DONE | |
| 6570 | * | | |
| 6571 | * `----------------------------------------------' |
| 6572 | * |
| 6573 | * Compression only happens in the DATA state, and must be flushed in final |
| 6574 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6575 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6576 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6577 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6578 | int http_response_forward_body(struct session *s, struct channel *res, int an_bit) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6579 | { |
| 6580 | struct http_txn *txn = &s->txn; |
| 6581 | struct http_msg *msg = &s->txn.rsp; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6582 | static struct buffer *tmpbuf = NULL; |
| 6583 | int compressing = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6584 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6585 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6586 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6587 | return 0; |
| 6588 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6589 | 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] | 6590 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 6591 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6592 | /* Output closed while we were sending data. We must abort and |
| 6593 | * wake the other side up. |
| 6594 | */ |
| 6595 | msg->msg_state = HTTP_MSG_ERROR; |
| 6596 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6597 | return 1; |
| 6598 | } |
| 6599 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6600 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6601 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6602 | |
Willy Tarreau | 5bebcd0 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 6603 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6604 | /* we have msg->sov which points to the first byte of message |
| 6605 | * body, and res->buf.p still points to the beginning of the |
| 6606 | * message. We forward the headers now, as we don't need them |
| 6607 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6608 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6609 | b_adv(res->buf, msg->sov); |
| 6610 | msg->next -= msg->sov; |
| 6611 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6612 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6613 | /* The previous analysers guarantee that the state is somewhere |
| 6614 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 6615 | * msg->next are always correct. |
| 6616 | */ |
| 6617 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 6618 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 6619 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6620 | else |
| 6621 | msg->msg_state = HTTP_MSG_DATA; |
| 6622 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6623 | } |
| 6624 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6625 | if (res->to_forward) { |
| 6626 | /* We can't process the buffer's contents yet */ |
| 6627 | res->flags |= CF_WAKE_WRITE; |
| 6628 | goto missing_data; |
| 6629 | } |
| 6630 | |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6631 | if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) { |
| 6632 | /* We need a compression buffer in the DATA state to put the |
| 6633 | * output of compressed data, and in CRLF state to let the |
| 6634 | * TRAILERS state finish the job of removing the trailing CRLF. |
| 6635 | */ |
| 6636 | if (unlikely(tmpbuf == NULL)) { |
| 6637 | /* this is the first time we need the compression buffer */ |
| 6638 | tmpbuf = pool_alloc2(pool2_buffer); |
| 6639 | if (tmpbuf == NULL) |
| 6640 | goto aborted_xfer; /* no memory */ |
| 6641 | } |
| 6642 | |
| 6643 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6644 | if (ret < 0) { |
| 6645 | res->flags |= CF_WAKE_WRITE; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6646 | goto missing_data; /* not enough spaces in buffers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6647 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6648 | compressing = 1; |
| 6649 | } |
| 6650 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6651 | while (1) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6652 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6653 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6654 | /* we may have some pending data starting at res->buf->p */ |
| 6655 | if (unlikely(s->comp_algo)) { |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 6656 | ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6657 | if (ret < 0) |
| 6658 | goto aborted_xfer; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6659 | |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6660 | if (msg->chunk_len) { |
| 6661 | /* input empty or output full */ |
| 6662 | if (res->buf->i > msg->next) |
| 6663 | res->flags |= CF_WAKE_WRITE; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6664 | goto missing_data; |
| 6665 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6666 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6667 | else { |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6668 | if (msg->chunk_len > res->buf->i - msg->next) { |
| 6669 | /* output full */ |
| 6670 | res->flags |= CF_WAKE_WRITE; |
| 6671 | goto missing_data; |
| 6672 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6673 | msg->next += msg->chunk_len; |
| 6674 | msg->chunk_len = 0; |
| 6675 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6676 | |
| 6677 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6678 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6679 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6680 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6681 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6682 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6683 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6684 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6685 | |
| 6686 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 6687 | /* we want the CRLF after the data */ |
| 6688 | |
| 6689 | ret = http_skip_chunk_crlf(msg); |
| 6690 | if (ret == 0) |
| 6691 | goto missing_data; |
| 6692 | else if (ret < 0) { |
| 6693 | if (msg->err_pos >= 0) |
| 6694 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe); |
| 6695 | goto return_bad_res; |
| 6696 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6697 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 6698 | |
| 6699 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6700 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 6701 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6702 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6703 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6704 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6705 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6706 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6707 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6708 | else if (ret < 0) { |
| 6709 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6710 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6711 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6712 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 6713 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6714 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6715 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6716 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6717 | if (unlikely(compressing)) { |
| 6718 | /* we need to flush output contents before syncing FSMs */ |
| 6719 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6720 | compressing = 0; |
| 6721 | } |
| 6722 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6723 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6724 | if (ret == 0) |
| 6725 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6726 | else if (ret < 0) { |
| 6727 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6728 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6729 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6730 | } |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6731 | /* we're in HTTP_MSG_DONE now, fall through */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6732 | |
| 6733 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6734 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6735 | if (unlikely(compressing)) { |
| 6736 | /* we need to flush output contents before syncing FSMs */ |
| 6737 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6738 | compressing = 0; |
| 6739 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6740 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6741 | /* we may have some pending data starting at res->buf->p |
| 6742 | * such as a last chunk of data or trailers. |
| 6743 | */ |
| 6744 | b_adv(res->buf, msg->next); |
| 6745 | msg->next = 0; |
| 6746 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6747 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6748 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6749 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6750 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6751 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6752 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6753 | if (http_resync_states(s)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6754 | /* some state changes occurred, maybe the analyser |
| 6755 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6756 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6757 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6758 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6759 | /* response errors are most likely due to |
| 6760 | * the client aborting the transfer. |
| 6761 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6762 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6763 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6764 | if (msg->err_pos >= 0) |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6765 | http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6766 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6767 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6768 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6769 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6770 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6771 | } |
| 6772 | } |
| 6773 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6774 | missing_data: |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6775 | /* we may have some pending data starting at res->buf->p */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6776 | if (unlikely(compressing)) { |
| 6777 | 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] | 6778 | compressing = 0; |
| 6779 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6780 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6781 | if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) { |
| 6782 | b_adv(res->buf, msg->next); |
| 6783 | msg->next = 0; |
| 6784 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6785 | } |
| 6786 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6787 | if (res->flags & CF_SHUTW) |
| 6788 | goto aborted_xfer; |
| 6789 | |
| 6790 | /* stop waiting for data if the input is closed before the end. If the |
| 6791 | * client side was already closed, it means that the client has aborted, |
| 6792 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6793 | * server abort. |
| 6794 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6795 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | b2c6a78 | 2014-04-23 20:29:01 +0200 | [diff] [blame] | 6796 | if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6797 | goto aborted_xfer; |
Christopher Faulet | be80167 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6798 | /* If we have some pending data, we continue the processing */ |
| 6799 | if (!buffer_pending(res->buf)) { |
| 6800 | if (!(s->flags & SN_ERR_MASK)) |
| 6801 | s->flags |= SN_ERR_SRVCL; |
| 6802 | s->be->be_counters.srv_aborts++; |
| 6803 | if (objt_server(s->target)) |
| 6804 | objt_server(s->target)->counters.srv_aborts++; |
| 6805 | goto return_bad_res_stats_ok; |
| 6806 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6807 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6808 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6809 | /* we need to obey the req analyser, so if it leaves, we must too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6810 | if (!s->req->analysers) |
| 6811 | goto return_bad_res; |
| 6812 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6813 | /* 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] | 6814 | * 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] | 6815 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 6816 | * close. |
| 6817 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6818 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6819 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6820 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6821 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6822 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6823 | /* 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] | 6824 | * 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] | 6825 | * 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] | 6826 | * modes are already handled by the stream sock layer. We must not do |
| 6827 | * this in content-length mode because it could present the MSG_MORE |
| 6828 | * flag with the last block of forwarded data, which would cause an |
| 6829 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6830 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6831 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6832 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6833 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6834 | /* the session handler will take care of timeouts and errors */ |
| 6835 | return 0; |
| 6836 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6837 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6838 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6839 | if (objt_server(s->target)) |
| 6840 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6841 | |
| 6842 | return_bad_res_stats_ok: |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 6843 | if (unlikely(compressing)) { |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6844 | 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] | 6845 | compressing = 0; |
| 6846 | } |
| 6847 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6848 | /* we may have some pending data starting at res->buf->p */ |
| 6849 | if (s->comp_algo == NULL) { |
| 6850 | b_adv(res->buf, msg->next); |
| 6851 | msg->next = 0; |
| 6852 | } |
| 6853 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6854 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6855 | /* don't send any error message as we're in the body */ |
| 6856 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6857 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6858 | 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] | 6859 | if (objt_server(s->target)) |
| 6860 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6861 | |
| 6862 | if (!(s->flags & SN_ERR_MASK)) |
| 6863 | s->flags |= SN_ERR_PRXCOND; |
| 6864 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6865 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6866 | return 0; |
| 6867 | |
| 6868 | aborted_xfer: |
Willy Tarreau | 6fef8ae | 2014-04-22 21:22:06 +0200 | [diff] [blame] | 6869 | if (unlikely(compressing)) { |
| 6870 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
| 6871 | compressing = 0; |
| 6872 | } |
| 6873 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6874 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6875 | /* don't send any error message as we're in the body */ |
| 6876 | stream_int_retnclose(res->cons, NULL); |
| 6877 | res->analysers = 0; |
| 6878 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 6879 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6880 | s->fe->fe_counters.cli_aborts++; |
| 6881 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6882 | if (objt_server(s->target)) |
| 6883 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6884 | |
| 6885 | if (!(s->flags & SN_ERR_MASK)) |
| 6886 | s->flags |= SN_ERR_CLICL; |
| 6887 | if (!(s->flags & SN_FINST_MASK)) |
| 6888 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6889 | return 0; |
| 6890 | } |
| 6891 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6892 | /* Iterate the same filter through all request headers. |
| 6893 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6894 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6895 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6896 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6897 | int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6898 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6899 | char *cur_ptr, *cur_end, *cur_next; |
| 6900 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6901 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6902 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6903 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6904 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6905 | last_hdr = 0; |
| 6906 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6907 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6908 | old_idx = 0; |
| 6909 | |
| 6910 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6911 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6912 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6913 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6914 | (exp->action == ACT_ALLOW || |
| 6915 | exp->action == ACT_DENY || |
| 6916 | exp->action == ACT_TARPIT)) |
| 6917 | return 0; |
| 6918 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6919 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6920 | if (!cur_idx) |
| 6921 | break; |
| 6922 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6923 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6924 | cur_ptr = cur_next; |
| 6925 | cur_end = cur_ptr + cur_hdr->len; |
| 6926 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6927 | |
| 6928 | /* Now we have one header between cur_ptr and cur_end, |
| 6929 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6930 | */ |
| 6931 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 6932 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6933 | switch (exp->action) { |
| 6934 | case ACT_SETBE: |
| 6935 | /* It is not possible to jump a second time. |
| 6936 | * FIXME: should we return an HTTP/500 here so that |
| 6937 | * the admin knows there's a problem ? |
| 6938 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6939 | if (s->be != s->fe) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6940 | break; |
| 6941 | |
| 6942 | /* Swithing Proxy */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6943 | session_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6944 | last_hdr = 1; |
| 6945 | break; |
| 6946 | |
| 6947 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6948 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6949 | last_hdr = 1; |
| 6950 | break; |
| 6951 | |
| 6952 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6953 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6954 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6955 | break; |
| 6956 | |
| 6957 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6958 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6959 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6960 | break; |
| 6961 | |
| 6962 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6963 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6964 | if (trash.len < 0) |
| 6965 | return -1; |
| 6966 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6967 | 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] | 6968 | /* FIXME: if the user adds a newline in the replacement, the |
| 6969 | * index will not be recalculated for now, and the new line |
| 6970 | * will not be counted as a new header. |
| 6971 | */ |
| 6972 | |
| 6973 | cur_end += delta; |
| 6974 | cur_next += delta; |
| 6975 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6976 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6977 | break; |
| 6978 | |
| 6979 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6980 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6981 | cur_next += delta; |
| 6982 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6983 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6984 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6985 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6986 | cur_hdr->len = 0; |
| 6987 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6988 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6989 | break; |
| 6990 | |
| 6991 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6992 | } |
| 6993 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6994 | /* keep the link from this header to next one in case of later |
| 6995 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6996 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6997 | old_idx = cur_idx; |
| 6998 | } |
| 6999 | return 0; |
| 7000 | } |
| 7001 | |
| 7002 | |
| 7003 | /* Apply the filter to the request line. |
| 7004 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7005 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7006 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7007 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7008 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7009 | int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7010 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7011 | char *cur_ptr, *cur_end; |
| 7012 | int done; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7013 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7014 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7015 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7016 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7017 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7018 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7019 | (exp->action == ACT_ALLOW || |
| 7020 | exp->action == ACT_DENY || |
| 7021 | exp->action == ACT_TARPIT)) |
| 7022 | return 0; |
| 7023 | else if (exp->action == ACT_REMOVE) |
| 7024 | return 0; |
| 7025 | |
| 7026 | done = 0; |
| 7027 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7028 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7029 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7030 | |
| 7031 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7032 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 7033 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7034 | switch (exp->action) { |
| 7035 | case ACT_SETBE: |
| 7036 | /* It is not possible to jump a second time. |
| 7037 | * FIXME: should we return an HTTP/500 here so that |
| 7038 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7039 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7040 | if (s->be != s->fe) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7041 | break; |
| 7042 | |
| 7043 | /* Swithing Proxy */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7044 | session_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7045 | done = 1; |
| 7046 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7047 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7048 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7049 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7050 | done = 1; |
| 7051 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7052 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7053 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7054 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7055 | done = 1; |
| 7056 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7057 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7058 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7059 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7060 | done = 1; |
| 7061 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7062 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7063 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7064 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7065 | if (trash.len < 0) |
| 7066 | return -1; |
| 7067 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7068 | 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] | 7069 | /* FIXME: if the user adds a newline in the replacement, the |
| 7070 | * index will not be recalculated for now, and the new line |
| 7071 | * will not be counted as a new header. |
| 7072 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7073 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7074 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7075 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7076 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7077 | HTTP_MSG_RQMETH, |
| 7078 | cur_ptr, cur_end + 1, |
| 7079 | NULL, NULL); |
| 7080 | if (unlikely(!cur_end)) |
| 7081 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7082 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7083 | /* we have a full request and we know that we have either a CR |
| 7084 | * or an LF at <ptr>. |
| 7085 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7086 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7087 | 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] | 7088 | /* there is no point trying this regex on headers */ |
| 7089 | return 1; |
| 7090 | } |
| 7091 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7092 | return done; |
| 7093 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7094 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7095 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7096 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7097 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7098 | * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7099 | * 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] | 7100 | * unparsable request. Since it can manage the switch to another backend, it |
| 7101 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7102 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7103 | int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7104 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7105 | struct http_txn *txn = &s->txn; |
| 7106 | struct hdr_exp *exp; |
| 7107 | |
| 7108 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7109 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7110 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7111 | /* |
| 7112 | * The interleaving of transformations and verdicts |
| 7113 | * makes it difficult to decide to continue or stop |
| 7114 | * the evaluation. |
| 7115 | */ |
| 7116 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7117 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7118 | break; |
| 7119 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7120 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7121 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7122 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7123 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7124 | |
| 7125 | /* if this filter had a condition, evaluate it now and skip to |
| 7126 | * next filter if the condition does not match. |
| 7127 | */ |
| 7128 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7129 | ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7130 | ret = acl_pass(ret); |
| 7131 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7132 | ret = !ret; |
| 7133 | |
| 7134 | if (!ret) |
| 7135 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7136 | } |
| 7137 | |
| 7138 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7139 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7140 | if (unlikely(ret < 0)) |
| 7141 | return -1; |
| 7142 | |
| 7143 | if (likely(ret == 0)) { |
| 7144 | /* The filter did not match the request, it can be |
| 7145 | * iterated through all headers. |
| 7146 | */ |
Willy Tarreau | a256fae | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7147 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7148 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7149 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7150 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7151 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7152 | } |
| 7153 | |
| 7154 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7155 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7156 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7157 | * Try to retrieve the server associated to the appsession. |
| 7158 | * If the server is found, it's assigned to the session. |
| 7159 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7160 | void manage_client_side_appsession(struct session *s, const char *buf, int len) { |
| 7161 | struct http_txn *txn = &s->txn; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7162 | appsess *asession = NULL; |
| 7163 | char *sessid_temp = NULL; |
| 7164 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7165 | if (len > s->be->appsession_len) { |
| 7166 | len = s->be->appsession_len; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7167 | } |
| 7168 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7169 | if (s->be->options2 & PR_O2_AS_REQL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7170 | /* request-learn option is enabled : store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7171 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7172 | /* free previously allocated memory as we don't need the session id found in the URL anymore */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7173 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7174 | } |
| 7175 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7176 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7177 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7178 | send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7179 | return; |
| 7180 | } |
| 7181 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7182 | memcpy(txn->sessid, buf, len); |
| 7183 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7184 | } |
| 7185 | |
| 7186 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 7187 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7188 | send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7189 | return; |
| 7190 | } |
| 7191 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7192 | memcpy(sessid_temp, buf, len); |
| 7193 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7194 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7195 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7196 | /* free previously allocated memory */ |
| 7197 | pool_free2(apools.sessid, sessid_temp); |
| 7198 | |
| 7199 | if (asession != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7200 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
| 7201 | if (!(s->be->options2 & PR_O2_AS_REQL)) |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7202 | asession->request_count++; |
| 7203 | |
| 7204 | if (asession->serverid != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7205 | struct server *srv = s->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7206 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7207 | while (srv) { |
| 7208 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7209 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7210 | (s->be->options & PR_O_PERSIST) || |
| 7211 | (s->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7212 | /* we found the server and it's usable */ |
| 7213 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7214 | txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7215 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 7216 | s->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 7217 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7218 | break; |
| 7219 | } else { |
| 7220 | txn->flags &= ~TX_CK_MASK; |
| 7221 | txn->flags |= TX_CK_DOWN; |
| 7222 | } |
| 7223 | } |
| 7224 | srv = srv->next; |
| 7225 | } |
| 7226 | } |
| 7227 | } |
| 7228 | } |
| 7229 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7230 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7231 | * same way as with headers above except that the semi-colon also ends a token. |
| 7232 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7233 | * return a valid result. |
| 7234 | */ |
| 7235 | char *find_cookie_value_end(char *s, const char *e) |
| 7236 | { |
| 7237 | int quoted, qdpair; |
| 7238 | |
| 7239 | quoted = qdpair = 0; |
| 7240 | for (; s < e; s++) { |
| 7241 | if (qdpair) qdpair = 0; |
| 7242 | else if (quoted) { |
| 7243 | if (*s == '\\') qdpair = 1; |
| 7244 | else if (*s == '"') quoted = 0; |
| 7245 | } |
| 7246 | else if (*s == '"') quoted = 1; |
| 7247 | else if (*s == ',' || *s == ';') return s; |
| 7248 | } |
| 7249 | return s; |
| 7250 | } |
| 7251 | |
| 7252 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7253 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7254 | * the first delimiter is updated if required. The function tries as much as |
| 7255 | * possible to respect the following principles : |
| 7256 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7257 | * colon, in which case <next> is simply removed |
| 7258 | * - set exactly one space character after the new first delimiter, unless |
| 7259 | * there are not enough characters in the block being moved to do so. |
| 7260 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7261 | * one. |
| 7262 | * |
| 7263 | * It is the caller's responsibility to ensure that : |
| 7264 | * - <from> points to a valid delimiter or the colon ; |
| 7265 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7266 | * - there are non-space chars before <from> ; |
| 7267 | * - there is a CR/LF at or after <next>. |
| 7268 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7269 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7270 | { |
| 7271 | char *prev = *from; |
| 7272 | |
| 7273 | if (*prev == ':') { |
| 7274 | /* We're removing the first value, preserve the colon and add a |
| 7275 | * space if possible. |
| 7276 | */ |
| 7277 | if (!http_is_crlf[(unsigned char)*next]) |
| 7278 | next++; |
| 7279 | prev++; |
| 7280 | if (prev < next) |
| 7281 | *prev++ = ' '; |
| 7282 | |
| 7283 | while (http_is_spht[(unsigned char)*next]) |
| 7284 | next++; |
| 7285 | } else { |
| 7286 | /* Remove useless spaces before the old delimiter. */ |
| 7287 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 7288 | prev--; |
| 7289 | *from = prev; |
| 7290 | |
| 7291 | /* copy the delimiter and if possible a space if we're |
| 7292 | * not at the end of the line. |
| 7293 | */ |
| 7294 | if (!http_is_crlf[(unsigned char)*next]) { |
| 7295 | *prev++ = *next++; |
| 7296 | if (prev + 1 < next) |
| 7297 | *prev++ = ' '; |
| 7298 | while (http_is_spht[(unsigned char)*next]) |
| 7299 | next++; |
| 7300 | } |
| 7301 | } |
| 7302 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7303 | } |
| 7304 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7305 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7306 | * 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] | 7307 | * desirable to call it only when needed. This code is quite complex because |
| 7308 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7309 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7310 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7311 | void manage_client_side_cookies(struct session *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7312 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7313 | struct http_txn *txn = &s->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7314 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7315 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7316 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7317 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7318 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7319 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7320 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7321 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7322 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7323 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7324 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7325 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7326 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7327 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7328 | hdr_beg = hdr_next; |
| 7329 | hdr_end = hdr_beg + cur_hdr->len; |
| 7330 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7331 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7332 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7333 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7334 | * "Cookie:" headers. |
| 7335 | */ |
| 7336 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7337 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7338 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7339 | old_idx = cur_idx; |
| 7340 | continue; |
| 7341 | } |
| 7342 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7343 | del_from = NULL; /* nothing to be deleted */ |
| 7344 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7345 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7346 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7347 | * attributes whose name begin with a '$', and associate them with |
| 7348 | * the right cookie, if we want to delete this cookie. |
| 7349 | * So there are 3 cases for each cookie read : |
| 7350 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7351 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7352 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7353 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7354 | * "special" cookie. |
| 7355 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7356 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7357 | * *MUST* delete it. |
| 7358 | * |
| 7359 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7360 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7361 | * the RFC explicitly allows spaces before it, and not within the |
| 7362 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7363 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7364 | * after the equal sign too, resulting in some (rare) buggy |
| 7365 | * implementations trying to do that. So let's do what servers do. |
| 7366 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7367 | * allowed quoted strings in values, with any possible character |
| 7368 | * after a backslash, including control chars and delimitors, which |
| 7369 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7370 | * within values even without quotes. |
| 7371 | * |
| 7372 | * We have to keep multiple pointers in order to support cookie |
| 7373 | * removal at the beginning, middle or end of header without |
| 7374 | * corrupting the header. All of these headers are valid : |
| 7375 | * |
| 7376 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7377 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7378 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7379 | * | | | | | | | | | |
| 7380 | * | | | | | | | | hdr_end <--+ |
| 7381 | * | | | | | | | +--> next |
| 7382 | * | | | | | | +----> val_end |
| 7383 | * | | | | | +-----------> val_beg |
| 7384 | * | | | | +--------------> equal |
| 7385 | * | | | +----------------> att_end |
| 7386 | * | | +---------------------> att_beg |
| 7387 | * | +--------------------------> prev |
| 7388 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7389 | */ |
| 7390 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7391 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7392 | /* Iterate through all cookies on this line */ |
| 7393 | |
| 7394 | /* find att_beg */ |
| 7395 | att_beg = prev + 1; |
| 7396 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7397 | att_beg++; |
| 7398 | |
| 7399 | /* find att_end : this is the first character after the last non |
| 7400 | * space before the equal. It may be equal to hdr_end. |
| 7401 | */ |
| 7402 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7403 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7404 | while (equal < hdr_end) { |
| 7405 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7406 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7407 | if (http_is_spht[(unsigned char)*equal++]) |
| 7408 | continue; |
| 7409 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7410 | } |
| 7411 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7412 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7413 | * is between <att_beg> and <equal>, both may be identical. |
| 7414 | */ |
| 7415 | |
| 7416 | /* look for end of cookie if there is an equal sign */ |
| 7417 | if (equal < hdr_end && *equal == '=') { |
| 7418 | /* look for the beginning of the value */ |
| 7419 | val_beg = equal + 1; |
| 7420 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7421 | val_beg++; |
| 7422 | |
| 7423 | /* find the end of the value, respecting quotes */ |
| 7424 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7425 | |
| 7426 | /* make val_end point to the first white space or delimitor after the value */ |
| 7427 | val_end = next; |
| 7428 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7429 | val_end--; |
| 7430 | } else { |
| 7431 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7432 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7433 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7434 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7435 | * they will automatically be removed if a header before them is removed, |
| 7436 | * since they're supposed to be linked together. |
| 7437 | */ |
| 7438 | if (*att_beg == '$') |
| 7439 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7440 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7441 | /* Ignore cookies with no equal sign */ |
| 7442 | if (equal == next) { |
| 7443 | /* This is not our cookie, so we must preserve it. But if we already |
| 7444 | * scheduled another cookie for removal, we cannot remove the |
| 7445 | * complete header, but we can remove the previous block itself. |
| 7446 | */ |
| 7447 | preserve_hdr = 1; |
| 7448 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7449 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7450 | val_end += delta; |
| 7451 | next += delta; |
| 7452 | hdr_end += delta; |
| 7453 | hdr_next += delta; |
| 7454 | cur_hdr->len += delta; |
| 7455 | http_msg_move_end(&txn->req, delta); |
| 7456 | prev = del_from; |
| 7457 | del_from = NULL; |
| 7458 | } |
| 7459 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7460 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7461 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7462 | /* if there are spaces around the equal sign, we need to |
| 7463 | * strip them otherwise we'll get trouble for cookie captures, |
| 7464 | * or even for rewrites. Since this happens extremely rarely, |
| 7465 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7466 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7467 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7468 | int stripped_before = 0; |
| 7469 | int stripped_after = 0; |
| 7470 | |
| 7471 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7472 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7473 | equal += stripped_before; |
| 7474 | val_beg += stripped_before; |
| 7475 | } |
| 7476 | |
| 7477 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7478 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7479 | val_beg += stripped_after; |
| 7480 | stripped_before += stripped_after; |
| 7481 | } |
| 7482 | |
| 7483 | val_end += stripped_before; |
| 7484 | next += stripped_before; |
| 7485 | hdr_end += stripped_before; |
| 7486 | hdr_next += stripped_before; |
| 7487 | cur_hdr->len += stripped_before; |
| 7488 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7489 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7490 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7491 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7492 | /* First, let's see if we want to capture this cookie. We check |
| 7493 | * that we don't already have a client side cookie, because we |
| 7494 | * can only capture one. Also as an optimisation, we ignore |
| 7495 | * cookies shorter than the declared name. |
| 7496 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7497 | if (s->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7498 | (val_end - att_beg >= s->fe->capture_namelen) && |
| 7499 | memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7500 | int log_len = val_end - att_beg; |
| 7501 | |
| 7502 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7503 | Alert("HTTP logging : out of memory.\n"); |
| 7504 | } else { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7505 | if (log_len > s->fe->capture_len) |
| 7506 | log_len = s->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7507 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7508 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7509 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7510 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7511 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7512 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7513 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7514 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7515 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7516 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7517 | * For cookies in prefix mode, the form is : |
| 7518 | * |
| 7519 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7520 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7521 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7522 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7523 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7524 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7525 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7526 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7527 | * have the server ID between val_beg and delim, and the original cookie between |
| 7528 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7529 | * |
| 7530 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7531 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7532 | * | || || | |+-> next |
| 7533 | * | || || | +--> val_end |
| 7534 | * | || || +---------> delim |
| 7535 | * | || |+------------> val_beg |
| 7536 | * | || +-------------> att_end = equal |
| 7537 | * | |+-----------------> att_beg |
| 7538 | * | +------------------> prev |
| 7539 | * +-------------------------> hdr_beg |
| 7540 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7541 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7542 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7543 | for (delim = val_beg; delim < val_end; delim++) |
| 7544 | if (*delim == COOKIE_DELIM) |
| 7545 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7546 | } else { |
| 7547 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7548 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7549 | /* Now check if the cookie contains a date field, which would |
| 7550 | * appear after a vertical bar ('|') just after the server name |
| 7551 | * and before the delimiter. |
| 7552 | */ |
| 7553 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7554 | if (vbar1) { |
| 7555 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7556 | * right is the last seen date. It is a base64 encoded |
| 7557 | * 30-bit value representing the UNIX date since the |
| 7558 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7559 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7560 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7561 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7562 | if (val_end - vbar1 >= 5) { |
| 7563 | val = b64tos30(vbar1); |
| 7564 | if (val > 0) |
| 7565 | txn->cookie_last_date = val << 2; |
| 7566 | } |
| 7567 | /* look for a second vertical bar */ |
| 7568 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7569 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7570 | val = b64tos30(vbar1 + 1); |
| 7571 | if (val > 0) |
| 7572 | txn->cookie_first_date = val << 2; |
| 7573 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7574 | } |
| 7575 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7576 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7577 | /* if the cookie has an expiration date and the proxy wants to check |
| 7578 | * it, then we do that now. We first check if the cookie is too old, |
| 7579 | * then only if it has expired. We detect strict overflow because the |
| 7580 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7581 | * in the future are ignored if their offset is beyond one day. This |
| 7582 | * allows an admin to fix timezone issues without expiring everyone |
| 7583 | * and at the same time avoids keeping unwanted side effects for too |
| 7584 | * long. |
| 7585 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7586 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7587 | (((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] | 7588 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7589 | txn->flags &= ~TX_CK_MASK; |
| 7590 | txn->flags |= TX_CK_OLD; |
| 7591 | delim = val_beg; // let's pretend we have not found the cookie |
| 7592 | txn->cookie_first_date = 0; |
| 7593 | txn->cookie_last_date = 0; |
| 7594 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7595 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7596 | (((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] | 7597 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7598 | txn->flags &= ~TX_CK_MASK; |
| 7599 | txn->flags |= TX_CK_EXPIRED; |
| 7600 | delim = val_beg; // let's pretend we have not found the cookie |
| 7601 | txn->cookie_first_date = 0; |
| 7602 | txn->cookie_last_date = 0; |
| 7603 | } |
| 7604 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7605 | /* Here, we'll look for the first running server which supports the cookie. |
| 7606 | * This allows to share a same cookie between several servers, for example |
| 7607 | * to dedicate backup servers to specific servers only. |
| 7608 | * However, to prevent clients from sticking to cookie-less backup server |
| 7609 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7610 | * empty cookies and mark them as invalid. |
| 7611 | * The same behaviour is applied when persistence must be ignored. |
| 7612 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7613 | if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7614 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7615 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7616 | while (srv) { |
| 7617 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7618 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7619 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7620 | (s->be->options & PR_O_PERSIST) || |
| 7621 | (s->flags & SN_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7622 | /* we found the server and we can use it */ |
| 7623 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7624 | txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7625 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 7626 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7627 | break; |
| 7628 | } else { |
| 7629 | /* we found a server, but it's down, |
| 7630 | * mark it as such and go on in case |
| 7631 | * another one is available. |
| 7632 | */ |
| 7633 | txn->flags &= ~TX_CK_MASK; |
| 7634 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7635 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7636 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7637 | srv = srv->next; |
| 7638 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7639 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7640 | 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] | 7641 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7642 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7643 | if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7644 | txn->flags |= TX_CK_UNUSED; |
| 7645 | else |
| 7646 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7647 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7648 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7649 | /* depending on the cookie mode, we may have to either : |
| 7650 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7651 | * the server never sees it ; |
| 7652 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7653 | * application cookie so that it does not get accidentely removed later, |
| 7654 | * if we're in cookie prefix mode |
| 7655 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7656 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7657 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7658 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7659 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7660 | val_end += delta; |
| 7661 | next += delta; |
| 7662 | hdr_end += delta; |
| 7663 | hdr_next += delta; |
| 7664 | cur_hdr->len += delta; |
| 7665 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7666 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7667 | del_from = NULL; |
| 7668 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7669 | } |
| 7670 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7671 | (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] | 7672 | del_from = prev; |
| 7673 | } |
| 7674 | } else { |
| 7675 | /* This is not our cookie, so we must preserve it. But if we already |
| 7676 | * scheduled another cookie for removal, we cannot remove the |
| 7677 | * complete header, but we can remove the previous block itself. |
| 7678 | */ |
| 7679 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7680 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7681 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7682 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7683 | if (att_beg >= del_from) |
| 7684 | att_beg += delta; |
| 7685 | if (att_end >= del_from) |
| 7686 | att_end += delta; |
| 7687 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7688 | val_end += delta; |
| 7689 | next += delta; |
| 7690 | hdr_end += delta; |
| 7691 | hdr_next += delta; |
| 7692 | cur_hdr->len += delta; |
| 7693 | http_msg_move_end(&txn->req, delta); |
| 7694 | prev = del_from; |
| 7695 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7696 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7697 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7698 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7699 | /* Look for the appsession cookie unless persistence must be ignored */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7700 | if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7701 | int cmp_len, value_len; |
| 7702 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7703 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7704 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 7705 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 7706 | value_begin = att_beg + s->be->appsession_name_len; |
| 7707 | value_len = val_end - att_beg - s->be->appsession_name_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7708 | } else { |
| 7709 | cmp_len = att_end - att_beg; |
| 7710 | value_begin = val_beg; |
| 7711 | value_len = val_end - val_beg; |
| 7712 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7713 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7714 | /* let's see if the cookie is our appcookie */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7715 | if (cmp_len == s->be->appsession_name_len && |
| 7716 | memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) { |
| 7717 | manage_client_side_appsession(s, value_begin, value_len); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7718 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7719 | } |
| 7720 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7721 | /* continue with next cookie on this header line */ |
| 7722 | att_beg = next; |
| 7723 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7724 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7725 | /* There are no more cookies on this line. |
| 7726 | * We may still have one (or several) marked for deletion at the |
| 7727 | * end of the line. We must do this now in two ways : |
| 7728 | * - if some cookies must be preserved, we only delete from the |
| 7729 | * mark to the end of line ; |
| 7730 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7731 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7732 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7733 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7734 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7735 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7736 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7737 | cur_hdr->len += delta; |
| 7738 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7739 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7740 | |
| 7741 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7742 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7743 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7744 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7745 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7746 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7747 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7748 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7749 | } |
| 7750 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7751 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7752 | old_idx = cur_idx; |
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 | } |
| 7755 | |
| 7756 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7757 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7758 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7759 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7760 | int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7761 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7762 | char *cur_ptr, *cur_end, *cur_next; |
| 7763 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7764 | struct http_txn *txn = &s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7765 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7766 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7767 | |
| 7768 | last_hdr = 0; |
| 7769 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7770 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7771 | old_idx = 0; |
| 7772 | |
| 7773 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7774 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7775 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7776 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7777 | (exp->action == ACT_ALLOW || |
| 7778 | exp->action == ACT_DENY)) |
| 7779 | return 0; |
| 7780 | |
| 7781 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7782 | if (!cur_idx) |
| 7783 | break; |
| 7784 | |
| 7785 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7786 | cur_ptr = cur_next; |
| 7787 | cur_end = cur_ptr + cur_hdr->len; |
| 7788 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7789 | |
| 7790 | /* Now we have one header between cur_ptr and cur_end, |
| 7791 | * and the next header starts at cur_next. |
| 7792 | */ |
| 7793 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 7794 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7795 | switch (exp->action) { |
| 7796 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7797 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7798 | last_hdr = 1; |
| 7799 | break; |
| 7800 | |
| 7801 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7802 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7803 | last_hdr = 1; |
| 7804 | break; |
| 7805 | |
| 7806 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7807 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7808 | if (trash.len < 0) |
| 7809 | return -1; |
| 7810 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7811 | 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] | 7812 | /* FIXME: if the user adds a newline in the replacement, the |
| 7813 | * index will not be recalculated for now, and the new line |
| 7814 | * will not be counted as a new header. |
| 7815 | */ |
| 7816 | |
| 7817 | cur_end += delta; |
| 7818 | cur_next += delta; |
| 7819 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7820 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7821 | break; |
| 7822 | |
| 7823 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7824 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7825 | cur_next += delta; |
| 7826 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7827 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7828 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7829 | txn->hdr_idx.used--; |
| 7830 | cur_hdr->len = 0; |
| 7831 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7832 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7833 | break; |
| 7834 | |
| 7835 | } |
| 7836 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7837 | |
| 7838 | /* keep the link from this header to next one in case of later |
| 7839 | * removal of next header. |
| 7840 | */ |
| 7841 | old_idx = cur_idx; |
| 7842 | } |
| 7843 | return 0; |
| 7844 | } |
| 7845 | |
| 7846 | |
| 7847 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7848 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7849 | * or -1 if a replacement resulted in an invalid status line. |
| 7850 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7851 | int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7852 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7853 | char *cur_ptr, *cur_end; |
| 7854 | int done; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7855 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7856 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7857 | |
| 7858 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7859 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7860 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7861 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7862 | (exp->action == ACT_ALLOW || |
| 7863 | exp->action == ACT_DENY)) |
| 7864 | return 0; |
| 7865 | else if (exp->action == ACT_REMOVE) |
| 7866 | return 0; |
| 7867 | |
| 7868 | done = 0; |
| 7869 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7870 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7871 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7872 | |
| 7873 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7874 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 7875 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7876 | switch (exp->action) { |
| 7877 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7878 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7879 | done = 1; |
| 7880 | break; |
| 7881 | |
| 7882 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7883 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7884 | done = 1; |
| 7885 | break; |
| 7886 | |
| 7887 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7888 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7889 | if (trash.len < 0) |
| 7890 | return -1; |
| 7891 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7892 | 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] | 7893 | /* FIXME: if the user adds a newline in the replacement, the |
| 7894 | * index will not be recalculated for now, and the new line |
| 7895 | * will not be counted as a new header. |
| 7896 | */ |
| 7897 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7898 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7899 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7900 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7901 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7902 | cur_ptr, cur_end + 1, |
| 7903 | NULL, NULL); |
| 7904 | if (unlikely(!cur_end)) |
| 7905 | return -1; |
| 7906 | |
| 7907 | /* we have a full respnse and we know that we have either a CR |
| 7908 | * or an LF at <ptr>. |
| 7909 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7910 | 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] | 7911 | 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] | 7912 | /* there is no point trying this regex on headers */ |
| 7913 | return 1; |
| 7914 | } |
| 7915 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7916 | return done; |
| 7917 | } |
| 7918 | |
| 7919 | |
| 7920 | |
| 7921 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7922 | * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7923 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7924 | * unparsable response. |
| 7925 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7926 | int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7927 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7928 | struct http_txn *txn = &s->txn; |
| 7929 | struct hdr_exp *exp; |
| 7930 | |
| 7931 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7932 | int ret; |
| 7933 | |
| 7934 | /* |
| 7935 | * The interleaving of transformations and verdicts |
| 7936 | * makes it difficult to decide to continue or stop |
| 7937 | * the evaluation. |
| 7938 | */ |
| 7939 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7940 | if (txn->flags & TX_SVDENY) |
| 7941 | break; |
| 7942 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7943 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7944 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7945 | exp->action == ACT_PASS)) { |
| 7946 | exp = exp->next; |
| 7947 | continue; |
| 7948 | } |
| 7949 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7950 | /* if this filter had a condition, evaluate it now and skip to |
| 7951 | * next filter if the condition does not match. |
| 7952 | */ |
| 7953 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7954 | ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7955 | ret = acl_pass(ret); |
| 7956 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7957 | ret = !ret; |
| 7958 | if (!ret) |
| 7959 | continue; |
| 7960 | } |
| 7961 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7962 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7963 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7964 | if (unlikely(ret < 0)) |
| 7965 | return -1; |
| 7966 | |
| 7967 | if (likely(ret == 0)) { |
| 7968 | /* The filter did not match the response, it can be |
| 7969 | * iterated through all headers. |
| 7970 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7971 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 7972 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7973 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7974 | } |
| 7975 | return 0; |
| 7976 | } |
| 7977 | |
| 7978 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7979 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7980 | * 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] | 7981 | * desirable to call it only when needed. This function is also used when we |
| 7982 | * 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] | 7983 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7984 | void manage_server_side_cookies(struct session *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7985 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7986 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7987 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7988 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7989 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7990 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7991 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7992 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7993 | /* Iterate through the headers. |
| 7994 | * we start with the start line. |
| 7995 | */ |
| 7996 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7997 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7998 | |
| 7999 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 8000 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8001 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8002 | |
| 8003 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8004 | hdr_beg = hdr_next; |
| 8005 | hdr_end = hdr_beg + cur_hdr->len; |
| 8006 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8007 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8008 | /* We have one full header between hdr_beg and hdr_end, and the |
| 8009 | * next header starts at hdr_next. We're only interested in |
| 8010 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8011 | */ |
| 8012 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8013 | is_cookie2 = 0; |
| 8014 | prev = hdr_beg + 10; |
| 8015 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8016 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8017 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 8018 | if (!val) { |
| 8019 | old_idx = cur_idx; |
| 8020 | continue; |
| 8021 | } |
| 8022 | is_cookie2 = 1; |
| 8023 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8024 | } |
| 8025 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8026 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 8027 | * <prev> points to the colon. |
| 8028 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8029 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8030 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8031 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8032 | * check-cache is enabled) and we are not interested in checking |
| 8033 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8034 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8035 | if (s->be->cookie_name == NULL && |
| 8036 | s->be->appsession_name == NULL && |
| 8037 | s->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8038 | return; |
| 8039 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8040 | /* OK so now we know we have to process this response cookie. |
| 8041 | * The format of the Set-Cookie header is slightly different |
| 8042 | * from the format of the Cookie header in that it does not |
| 8043 | * support the comma as a cookie delimiter (thus the header |
| 8044 | * cannot be folded) because the Expires attribute described in |
| 8045 | * the original Netscape's spec may contain an unquoted date |
| 8046 | * with a comma inside. We have to live with this because |
| 8047 | * many browsers don't support Max-Age and some browsers don't |
| 8048 | * support quoted strings. However the Set-Cookie2 header is |
| 8049 | * clean. |
| 8050 | * |
| 8051 | * We have to keep multiple pointers in order to support cookie |
| 8052 | * removal at the beginning, middle or end of header without |
| 8053 | * corrupting the header (in case of set-cookie2). A special |
| 8054 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8055 | * fields after the first semi-colon. The <next> pointer points |
| 8056 | * either to the end of line (set-cookie) or next unquoted comma |
| 8057 | * (set-cookie2). All of these headers are valid : |
| 8058 | * |
| 8059 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8060 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8061 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8062 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8063 | * | | | | | | | | | | |
| 8064 | * | | | | | | | | +-> next hdr_end <--+ |
| 8065 | * | | | | | | | +------------> scav |
| 8066 | * | | | | | | +--------------> val_end |
| 8067 | * | | | | | +--------------------> val_beg |
| 8068 | * | | | | +----------------------> equal |
| 8069 | * | | | +------------------------> att_end |
| 8070 | * | | +----------------------------> att_beg |
| 8071 | * | +------------------------------> prev |
| 8072 | * +-----------------------------------------> hdr_beg |
| 8073 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8074 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8075 | for (; prev < hdr_end; prev = next) { |
| 8076 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8077 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8078 | /* find att_beg */ |
| 8079 | att_beg = prev + 1; |
| 8080 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8081 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8082 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8083 | /* find att_end : this is the first character after the last non |
| 8084 | * space before the equal. It may be equal to hdr_end. |
| 8085 | */ |
| 8086 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8087 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8088 | while (equal < hdr_end) { |
| 8089 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8090 | break; |
| 8091 | if (http_is_spht[(unsigned char)*equal++]) |
| 8092 | continue; |
| 8093 | att_end = equal; |
| 8094 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8095 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8096 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8097 | * is between <att_beg> and <equal>, both may be identical. |
| 8098 | */ |
| 8099 | |
| 8100 | /* look for end of cookie if there is an equal sign */ |
| 8101 | if (equal < hdr_end && *equal == '=') { |
| 8102 | /* look for the beginning of the value */ |
| 8103 | val_beg = equal + 1; |
| 8104 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8105 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8106 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8107 | /* find the end of the value, respecting quotes */ |
| 8108 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8109 | |
| 8110 | /* make val_end point to the first white space or delimitor after the value */ |
| 8111 | val_end = next; |
| 8112 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8113 | val_end--; |
| 8114 | } else { |
| 8115 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8116 | val_beg = val_end = next = equal; |
| 8117 | } |
| 8118 | |
| 8119 | if (next < hdr_end) { |
| 8120 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8121 | * a colon or semi-colon before the end. So skip all attr-value |
| 8122 | * pairs and look for the next comma. For Set-Cookie, since |
| 8123 | * commas are permitted in values, skip to the end. |
| 8124 | */ |
| 8125 | if (is_cookie2) |
| 8126 | next = find_hdr_value_end(next, hdr_end); |
| 8127 | else |
| 8128 | next = hdr_end; |
| 8129 | } |
| 8130 | |
| 8131 | /* Now everything is as on the diagram above */ |
| 8132 | |
| 8133 | /* Ignore cookies with no equal sign */ |
| 8134 | if (equal == val_end) |
| 8135 | continue; |
| 8136 | |
| 8137 | /* If there are spaces around the equal sign, we need to |
| 8138 | * strip them otherwise we'll get trouble for cookie captures, |
| 8139 | * or even for rewrites. Since this happens extremely rarely, |
| 8140 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8141 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8142 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8143 | int stripped_before = 0; |
| 8144 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8145 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8146 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8147 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8148 | equal += stripped_before; |
| 8149 | val_beg += stripped_before; |
| 8150 | } |
| 8151 | |
| 8152 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8153 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8154 | val_beg += stripped_after; |
| 8155 | stripped_before += stripped_after; |
| 8156 | } |
| 8157 | |
| 8158 | val_end += stripped_before; |
| 8159 | next += stripped_before; |
| 8160 | hdr_end += stripped_before; |
| 8161 | hdr_next += stripped_before; |
| 8162 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8163 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8164 | } |
| 8165 | |
| 8166 | /* First, let's see if we want to capture this cookie. We check |
| 8167 | * that we don't already have a server side cookie, because we |
| 8168 | * can only capture one. Also as an optimisation, we ignore |
| 8169 | * cookies shorter than the declared name. |
| 8170 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8171 | if (s->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8172 | txn->srv_cookie == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8173 | (val_end - att_beg >= s->fe->capture_namelen) && |
| 8174 | memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8175 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8176 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8177 | Alert("HTTP logging : out of memory.\n"); |
| 8178 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8179 | else { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8180 | if (log_len > s->fe->capture_len) |
| 8181 | log_len = s->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8182 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8183 | txn->srv_cookie[log_len] = 0; |
| 8184 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8185 | } |
| 8186 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8187 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8188 | /* now check if we need to process it for persistence */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8189 | if (!(s->flags & SN_IGNORE_PRST) && |
| 8190 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8191 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8192 | /* assume passive cookie by default */ |
| 8193 | txn->flags &= ~TX_SCK_MASK; |
| 8194 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8195 | |
| 8196 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8197 | * this occurrence because we'll insert another one later. |
| 8198 | * 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] | 8199 | * a direct access. |
| 8200 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8201 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8202 | /* The "preserve" flag was set, we don't want to touch the |
| 8203 | * server's cookie. |
| 8204 | */ |
| 8205 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8206 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 8207 | ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8208 | /* this cookie must be deleted */ |
| 8209 | if (*prev == ':' && next == hdr_end) { |
| 8210 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8211 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8212 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8213 | txn->hdr_idx.used--; |
| 8214 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8215 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8216 | hdr_next += delta; |
| 8217 | http_msg_move_end(&txn->rsp, delta); |
| 8218 | /* note: while both invalid now, <next> and <hdr_end> |
| 8219 | * are still equal, so the for() will stop as expected. |
| 8220 | */ |
| 8221 | } else { |
| 8222 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8223 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8224 | next = prev; |
| 8225 | hdr_end += delta; |
| 8226 | hdr_next += delta; |
| 8227 | cur_hdr->len += delta; |
| 8228 | http_msg_move_end(&txn->rsp, delta); |
| 8229 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8230 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8231 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8232 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8233 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8234 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8235 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8236 | * with this server since we know it. |
| 8237 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8238 | 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] | 8239 | next += delta; |
| 8240 | hdr_end += delta; |
| 8241 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8242 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8243 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8244 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8245 | txn->flags &= ~TX_SCK_MASK; |
| 8246 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8247 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8248 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8249 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8250 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8251 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8252 | 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] | 8253 | next += delta; |
| 8254 | hdr_end += delta; |
| 8255 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8256 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8257 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8258 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8259 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8260 | txn->flags &= ~TX_SCK_MASK; |
| 8261 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8262 | } |
| 8263 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 8264 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8265 | else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8266 | int cmp_len, value_len; |
| 8267 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8268 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8269 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 8270 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 8271 | value_begin = att_beg + s->be->appsession_name_len; |
| 8272 | value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8273 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8274 | cmp_len = att_end - att_beg; |
| 8275 | value_begin = val_beg; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8276 | value_len = MIN(s->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8277 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8278 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8279 | if ((cmp_len == s->be->appsession_name_len) && |
| 8280 | (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8281 | /* free a possibly previously allocated memory */ |
| 8282 | pool_free2(apools.sessid, txn->sessid); |
| 8283 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8284 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8285 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8286 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8287 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8288 | return; |
| 8289 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8290 | memcpy(txn->sessid, value_begin, value_len); |
| 8291 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8292 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8293 | } |
| 8294 | /* that's done for this cookie, check the next one on the same |
| 8295 | * line when next != hdr_end (only if is_cookie2). |
| 8296 | */ |
| 8297 | } |
| 8298 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8299 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8300 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8301 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8302 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8303 | appsess *asession = NULL; |
| 8304 | /* only do insert, if lookup fails */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8305 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8306 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 8307 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8308 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 8309 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8310 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8311 | return; |
| 8312 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8313 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 8314 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8315 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 8316 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8317 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8318 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8319 | return; |
| 8320 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8321 | memcpy(asession->sessid, txn->sessid, s->be->appsession_len); |
| 8322 | asession->sessid[s->be->appsession_len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8323 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8324 | server_id_len = strlen(objt_server(s->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8325 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8326 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8327 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8328 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8329 | return; |
| 8330 | } |
| 8331 | asession->serverid[0] = '\0'; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8332 | memcpy(asession->serverid, objt_server(s->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8333 | |
| 8334 | asession->request_count = 0; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8335 | appsession_hash_insert(&(s->be->htbl_proxy), asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8336 | } |
| 8337 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8338 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8339 | asession->request_count++; |
| 8340 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8341 | } |
| 8342 | |
| 8343 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8344 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8345 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8346 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8347 | void check_response_for_cacheability(struct session *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8348 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8349 | struct http_txn *txn = &s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8350 | char *p1, *p2; |
| 8351 | |
| 8352 | char *cur_ptr, *cur_end, *cur_next; |
| 8353 | int cur_idx; |
| 8354 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8355 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8356 | return; |
| 8357 | |
| 8358 | /* Iterate through the headers. |
| 8359 | * we start with the start line. |
| 8360 | */ |
| 8361 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8362 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8363 | |
| 8364 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8365 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8366 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8367 | |
| 8368 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8369 | cur_ptr = cur_next; |
| 8370 | cur_end = cur_ptr + cur_hdr->len; |
| 8371 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8372 | |
| 8373 | /* We have one full header between cur_ptr and cur_end, and the |
| 8374 | * next header starts at cur_next. We're only interested in |
| 8375 | * "Cookie:" headers. |
| 8376 | */ |
| 8377 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8378 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8379 | if (val) { |
| 8380 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8381 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8382 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8383 | return; |
| 8384 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8385 | } |
| 8386 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8387 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8388 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8389 | continue; |
| 8390 | |
| 8391 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8392 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8393 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8394 | |
| 8395 | if (p1 >= cur_end) /* no more info */ |
| 8396 | continue; |
| 8397 | |
| 8398 | /* p1 is at the beginning of the value */ |
| 8399 | p2 = p1; |
| 8400 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8401 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8402 | p2++; |
| 8403 | |
| 8404 | /* we have a complete value between p1 and p2 */ |
| 8405 | if (p2 < cur_end && *p2 == '=') { |
| 8406 | /* we have something of the form no-cache="set-cookie" */ |
| 8407 | if ((cur_end - p1 >= 21) && |
| 8408 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8409 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8410 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8411 | continue; |
| 8412 | } |
| 8413 | |
| 8414 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8415 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8416 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8417 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8418 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8419 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8420 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8421 | return; |
| 8422 | } |
| 8423 | |
| 8424 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8425 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8426 | continue; |
| 8427 | } |
| 8428 | } |
| 8429 | } |
| 8430 | |
| 8431 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8432 | /* |
| 8433 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 8434 | * If the server is found, it's assigned to the session. |
| 8435 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8436 | void get_srv_from_appsession(struct session *s, const char *begin, int len) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8437 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8438 | char *end_params, *first_param, *cur_param, *next_param; |
| 8439 | char separator; |
| 8440 | int value_len; |
| 8441 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8442 | int mode = s->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8443 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8444 | if (s->be->appsession_name == NULL || |
| 8445 | (s->txn.meth != HTTP_METH_GET && s->txn.meth != HTTP_METH_POST && s->txn.meth != HTTP_METH_HEAD)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8446 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8447 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8448 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8449 | first_param = NULL; |
| 8450 | switch (mode) { |
| 8451 | case PR_O2_AS_M_PP: |
| 8452 | first_param = memchr(begin, ';', len); |
| 8453 | break; |
| 8454 | case PR_O2_AS_M_QS: |
| 8455 | first_param = memchr(begin, '?', len); |
| 8456 | break; |
| 8457 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8458 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8459 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8460 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8461 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8462 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8463 | switch (mode) { |
| 8464 | case PR_O2_AS_M_PP: |
| 8465 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 8466 | end_params = (char *) begin + len; |
| 8467 | } |
| 8468 | separator = ';'; |
| 8469 | break; |
| 8470 | case PR_O2_AS_M_QS: |
| 8471 | end_params = (char *) begin + len; |
| 8472 | separator = '&'; |
| 8473 | break; |
| 8474 | default: |
| 8475 | /* unknown mode, shouldn't happen */ |
| 8476 | return; |
| 8477 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8478 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8479 | cur_param = next_param = end_params; |
| 8480 | while (cur_param > first_param) { |
| 8481 | cur_param--; |
| 8482 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 8483 | /* let's see if this is the appsession parameter */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8484 | if ((cur_param + s->be->appsession_name_len + 1 < next_param) && |
| 8485 | ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') && |
| 8486 | (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8487 | /* Cool... it's the right one */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8488 | cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 8489 | value_len = MIN(s->be->appsession_len, next_param - cur_param); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8490 | if (value_len > 0) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8491 | manage_client_side_appsession(s, cur_param, value_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8492 | } |
| 8493 | break; |
| 8494 | } |
| 8495 | next_param = cur_param; |
| 8496 | } |
| 8497 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8498 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 8499 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8500 | appsession_hash_dump(&(s->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8501 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8502 | } |
| 8503 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8504 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8505 | * 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] | 8506 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8507 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8508 | * 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] | 8509 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8510 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8511 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8512 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8513 | 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] | 8514 | { |
| 8515 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8516 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8517 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8518 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8519 | if (!uri_auth) |
| 8520 | return 0; |
| 8521 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8522 | 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] | 8523 | return 0; |
| 8524 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8525 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8526 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8527 | return 0; |
| 8528 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8529 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8530 | return 0; |
| 8531 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8532 | return 1; |
| 8533 | } |
| 8534 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8535 | /* |
| 8536 | * 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] | 8537 | * By default it tries to report the error position as msg->err_pos. However if |
| 8538 | * this one is not set, it will then report msg->next, which is the last known |
| 8539 | * 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] | 8540 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8541 | */ |
| 8542 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8543 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8544 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8545 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8546 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8547 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8548 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8549 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8550 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8551 | len1 = MIN(len1, es->len); |
| 8552 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8553 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8554 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8555 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8556 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8557 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8558 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8559 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8560 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8561 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8562 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8563 | es->when = date; // user-visible date |
| 8564 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8565 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8566 | es->oe = other_end; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8567 | if (objt_conn(s->req->prod->end)) |
| 8568 | es->src = __objt_conn(s->req->prod->end)->addr.from; |
| 8569 | else |
| 8570 | memset(&es->src, 0, sizeof(es->src)); |
| 8571 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8572 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8573 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8574 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8575 | es->s_flags = s->flags; |
| 8576 | es->t_flags = s->txn.flags; |
| 8577 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8578 | es->b_out = chn->buf->o; |
| 8579 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8580 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8581 | es->m_clen = msg->chunk_len; |
| 8582 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8583 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8584 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8585 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8586 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8587 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8588 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8589 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8590 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8591 | * 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] | 8592 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8593 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8594 | * 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] | 8595 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8596 | 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] | 8597 | struct hdr_idx *idx, int occ, |
| 8598 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8599 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8600 | struct hdr_ctx local_ctx; |
| 8601 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8602 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8603 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8604 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8605 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8606 | if (!ctx) { |
| 8607 | local_ctx.idx = 0; |
| 8608 | ctx = &local_ctx; |
| 8609 | } |
| 8610 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8611 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8612 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8613 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8614 | occ--; |
| 8615 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8616 | *vptr = ctx->line + ctx->val; |
| 8617 | *vlen = ctx->vlen; |
| 8618 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8619 | } |
| 8620 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8621 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8622 | } |
| 8623 | |
| 8624 | /* negative occurrence, we scan all the list then walk back */ |
| 8625 | if (-occ > MAX_HDR_HISTORY) |
| 8626 | return 0; |
| 8627 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8628 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8629 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8630 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8631 | len_hist[hist_ptr] = ctx->vlen; |
| 8632 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8633 | hist_ptr = 0; |
| 8634 | found++; |
| 8635 | } |
| 8636 | if (-occ > found) |
| 8637 | return 0; |
| 8638 | /* 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] | 8639 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8640 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8641 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8642 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8643 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8644 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8645 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8646 | *vptr = ptr_hist[hist_ptr]; |
| 8647 | *vlen = len_hist[hist_ptr]; |
| 8648 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8649 | } |
| 8650 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8651 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8652 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8653 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8654 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8655 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8656 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8657 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8658 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8659 | * line header values and does not stop at commas. |
| 8660 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8661 | */ |
| 8662 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8663 | struct hdr_idx *idx, int occ, |
| 8664 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8665 | { |
| 8666 | struct hdr_ctx local_ctx; |
| 8667 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8668 | int len_hist[MAX_HDR_HISTORY]; |
| 8669 | unsigned int hist_ptr; |
| 8670 | int found; |
| 8671 | |
| 8672 | if (!ctx) { |
| 8673 | local_ctx.idx = 0; |
| 8674 | ctx = &local_ctx; |
| 8675 | } |
| 8676 | |
| 8677 | if (occ >= 0) { |
| 8678 | /* search from the beginning */ |
| 8679 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8680 | occ--; |
| 8681 | if (occ <= 0) { |
| 8682 | *vptr = ctx->line + ctx->val; |
| 8683 | *vlen = ctx->vlen; |
| 8684 | return 1; |
| 8685 | } |
| 8686 | } |
| 8687 | return 0; |
| 8688 | } |
| 8689 | |
| 8690 | /* negative occurrence, we scan all the list then walk back */ |
| 8691 | if (-occ > MAX_HDR_HISTORY) |
| 8692 | return 0; |
| 8693 | |
| 8694 | found = hist_ptr = 0; |
| 8695 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8696 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8697 | len_hist[hist_ptr] = ctx->vlen; |
| 8698 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8699 | hist_ptr = 0; |
| 8700 | found++; |
| 8701 | } |
| 8702 | if (-occ > found) |
| 8703 | return 0; |
Nenad Merdanovic | bb65643 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8704 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8705 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
Nenad Merdanovic | bb65643 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8706 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8707 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8708 | * to remain in the 0..9 range. |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8709 | */ |
Nenad Merdanovic | bb65643 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8710 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8711 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8712 | hist_ptr -= MAX_HDR_HISTORY; |
| 8713 | *vptr = ptr_hist[hist_ptr]; |
| 8714 | *vlen = len_hist[hist_ptr]; |
| 8715 | return 1; |
| 8716 | } |
| 8717 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8718 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8719 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8720 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8721 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8722 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8723 | void debug_hdr(const char *dir, struct session *s, const char *start, const char *end) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8724 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8725 | int max; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8726 | 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] | 8727 | dir, |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8728 | objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1, |
| 8729 | objt_conn(s->req->cons->end) ? (unsigned short)objt_conn(s->req->cons->end)->t.sock.fd : -1); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8730 | |
| 8731 | for (max = 0; start + max < end; max++) |
| 8732 | if (start[max] == '\r' || start[max] == '\n') |
| 8733 | break; |
| 8734 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8735 | UBOUND(max, trash.size - trash.len - 3); |
| 8736 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8737 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8738 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8739 | } |
| 8740 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8741 | /* |
| 8742 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 8743 | * the required fields are properly allocated and that we only need to (re)init |
| 8744 | * them. This should be used before processing any new request. |
| 8745 | */ |
| 8746 | void http_init_txn(struct session *s) |
| 8747 | { |
| 8748 | struct http_txn *txn = &s->txn; |
| 8749 | struct proxy *fe = s->fe; |
| 8750 | |
| 8751 | txn->flags = 0; |
| 8752 | txn->status = -1; |
| 8753 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8754 | txn->cookie_first_date = 0; |
| 8755 | txn->cookie_last_date = 0; |
| 8756 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8757 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8758 | 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] | 8759 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8760 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8761 | 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] | 8762 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 8763 | txn->req.chunk_len = 0LL; |
| 8764 | txn->req.body_len = 0LL; |
| 8765 | txn->rsp.chunk_len = 0LL; |
| 8766 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8767 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8768 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 8769 | txn->req.chn = s->req; |
| 8770 | txn->rsp.chn = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8771 | |
| 8772 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8773 | |
| 8774 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8775 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8776 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8777 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8778 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8779 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 8780 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8781 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8782 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 8783 | |
| 8784 | if (txn->hdr_idx.v) |
| 8785 | hdr_idx_init(&txn->hdr_idx); |
| 8786 | } |
| 8787 | |
| 8788 | /* to be used at the end of a transaction */ |
| 8789 | void http_end_txn(struct session *s) |
| 8790 | { |
| 8791 | struct http_txn *txn = &s->txn; |
| 8792 | |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8793 | /* release any possible compression context */ |
| 8794 | if (s->flags & SN_COMP_READY) |
| 8795 | s->comp_algo->end(&s->comp_ctx); |
| 8796 | s->comp_algo = NULL; |
| 8797 | s->flags &= ~SN_COMP_READY; |
| 8798 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8799 | /* these ones will have been dynamically allocated */ |
| 8800 | pool_free2(pool2_requri, txn->uri); |
| 8801 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8802 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8803 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8804 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8805 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8806 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8807 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8808 | txn->uri = NULL; |
| 8809 | txn->srv_cookie = NULL; |
| 8810 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8811 | |
| 8812 | if (txn->req.cap) { |
| 8813 | struct cap_hdr *h; |
| 8814 | for (h = s->fe->req_cap; h; h = h->next) |
| 8815 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 8816 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 8817 | } |
| 8818 | |
| 8819 | if (txn->rsp.cap) { |
| 8820 | struct cap_hdr *h; |
| 8821 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 8822 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 8823 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 8824 | } |
| 8825 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8826 | } |
| 8827 | |
| 8828 | /* to be used at the end of a transaction to prepare a new one */ |
| 8829 | void http_reset_txn(struct session *s) |
| 8830 | { |
| 8831 | http_end_txn(s); |
| 8832 | http_init_txn(s); |
| 8833 | |
| 8834 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8835 | s->logs.logwait = s->fe->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8836 | s->logs.level = 0; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 8837 | session_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8838 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8839 | /* re-init store persistence */ |
| 8840 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8841 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8842 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8843 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8844 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 8845 | s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8846 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8847 | /* We must trim any excess data from the response buffer, because we |
| 8848 | * may have blocked an invalid response from a server that we don't |
| 8849 | * want to accidentely forward once we disable the analysers, nor do |
| 8850 | * we want those data to come along with next response. A typical |
| 8851 | * example of such data would be from a buggy server responding to |
| 8852 | * a HEAD with some data, or sending more than the advertised |
| 8853 | * content-length. |
| 8854 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8855 | if (unlikely(s->rep->buf->i)) |
| 8856 | s->rep->buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8857 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8858 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8859 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8860 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8861 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8862 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8863 | |
| 8864 | s->req->rex = TICK_ETERNITY; |
| 8865 | s->req->wex = TICK_ETERNITY; |
| 8866 | s->req->analyse_exp = TICK_ETERNITY; |
| 8867 | s->rep->rex = TICK_ETERNITY; |
| 8868 | s->rep->wex = TICK_ETERNITY; |
| 8869 | s->rep->analyse_exp = TICK_ETERNITY; |
| 8870 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8871 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8872 | void free_http_res_rules(struct list *r) |
| 8873 | { |
| 8874 | struct http_res_rule *tr, *pr; |
| 8875 | |
| 8876 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8877 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8878 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8879 | free(pr); |
| 8880 | } |
| 8881 | } |
| 8882 | |
| 8883 | void free_http_req_rules(struct list *r) |
| 8884 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8885 | struct http_req_rule *tr, *pr; |
| 8886 | |
| 8887 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8888 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8889 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8890 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8891 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8892 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8893 | free(pr); |
| 8894 | } |
| 8895 | } |
| 8896 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8897 | /* parse an "http-request" rule */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8898 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8899 | { |
| 8900 | struct http_req_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8901 | struct http_req_action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8902 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8903 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8904 | |
| 8905 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 8906 | if (!rule) { |
| 8907 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8908 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8909 | } |
| 8910 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8911 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8912 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 8913 | cur_arg = 1; |
Willy Tarreau | 5bd6759 | 2014-04-28 22:00:46 +0200 | [diff] [blame] | 8914 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8915 | rule->action = HTTP_REQ_ACT_DENY; |
| 8916 | cur_arg = 1; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8917 | } else if (!strcmp(args[0], "tarpit")) { |
| 8918 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 8919 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8920 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8921 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8922 | cur_arg = 1; |
| 8923 | |
| 8924 | while(*args[cur_arg]) { |
| 8925 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8926 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8927 | cur_arg+=2; |
| 8928 | continue; |
| 8929 | } else |
| 8930 | break; |
| 8931 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8932 | } else if (!strcmp(args[0], "set-nice")) { |
| 8933 | rule->action = HTTP_REQ_ACT_SET_NICE; |
| 8934 | cur_arg = 1; |
| 8935 | |
| 8936 | if (!*args[cur_arg] || |
| 8937 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8938 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8939 | file, linenum, args[0]); |
| 8940 | goto out_err; |
| 8941 | } |
| 8942 | rule->arg.nice = atoi(args[cur_arg]); |
| 8943 | if (rule->arg.nice < -1024) |
| 8944 | rule->arg.nice = -1024; |
| 8945 | else if (rule->arg.nice > 1024) |
| 8946 | rule->arg.nice = 1024; |
| 8947 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8948 | } else if (!strcmp(args[0], "set-tos")) { |
| 8949 | #ifdef IP_TOS |
| 8950 | char *err; |
| 8951 | rule->action = HTTP_REQ_ACT_SET_TOS; |
| 8952 | cur_arg = 1; |
| 8953 | |
| 8954 | if (!*args[cur_arg] || |
| 8955 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8956 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8957 | file, linenum, args[0]); |
| 8958 | goto out_err; |
| 8959 | } |
| 8960 | |
| 8961 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8962 | if (err && *err != '\0') { |
| 8963 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8964 | file, linenum, err, args[0]); |
| 8965 | goto out_err; |
| 8966 | } |
| 8967 | cur_arg++; |
| 8968 | #else |
| 8969 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8970 | goto out_err; |
| 8971 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8972 | } else if (!strcmp(args[0], "set-mark")) { |
| 8973 | #ifdef SO_MARK |
| 8974 | char *err; |
| 8975 | rule->action = HTTP_REQ_ACT_SET_MARK; |
| 8976 | cur_arg = 1; |
| 8977 | |
| 8978 | if (!*args[cur_arg] || |
| 8979 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8980 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8981 | file, linenum, args[0]); |
| 8982 | goto out_err; |
| 8983 | } |
| 8984 | |
| 8985 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8986 | if (err && *err != '\0') { |
| 8987 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8988 | file, linenum, err, args[0]); |
| 8989 | goto out_err; |
| 8990 | } |
| 8991 | cur_arg++; |
| 8992 | global.last_checks |= LSTCHK_NETADM; |
| 8993 | #else |
| 8994 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8995 | goto out_err; |
| 8996 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8997 | } else if (!strcmp(args[0], "set-log-level")) { |
| 8998 | rule->action = HTTP_REQ_ACT_SET_LOGL; |
| 8999 | cur_arg = 1; |
| 9000 | |
| 9001 | if (!*args[cur_arg] || |
| 9002 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9003 | bad_log_level: |
| 9004 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9005 | file, linenum, args[0]); |
| 9006 | goto out_err; |
| 9007 | } |
| 9008 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9009 | rule->arg.loglevel = -1; |
| 9010 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 9011 | goto bad_log_level; |
| 9012 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9013 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9014 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 9015 | cur_arg = 1; |
| 9016 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 9017 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9018 | (*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] | 9019 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9020 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9021 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9022 | } |
| 9023 | |
| 9024 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9025 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9026 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 9027 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9028 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9029 | 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] | 9030 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9031 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9032 | free(proxy->conf.lfs_file); |
| 9033 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9034 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9035 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9036 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
| 9037 | rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9038 | cur_arg = 1; |
| 9039 | |
| 9040 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 8f249f4 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 9041 | (*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] | 9042 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 9043 | file, linenum, args[0]); |
| 9044 | goto out_err; |
| 9045 | } |
| 9046 | |
| 9047 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9048 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9049 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9050 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9051 | error = NULL; |
| 9052 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9053 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9054 | args[cur_arg + 1], error); |
| 9055 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9056 | goto out_err; |
| 9057 | } |
| 9058 | |
| 9059 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9060 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9061 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9062 | file, linenum); |
| 9063 | |
| 9064 | free(proxy->conf.lfs_file); |
| 9065 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9066 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9067 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9068 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9069 | rule->action = HTTP_REQ_ACT_DEL_HDR; |
| 9070 | cur_arg = 1; |
| 9071 | |
| 9072 | if (!*args[cur_arg] || |
| 9073 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9074 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9075 | file, linenum, args[0]); |
| 9076 | goto out_err; |
| 9077 | } |
| 9078 | |
| 9079 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9080 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9081 | |
| 9082 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9083 | free(proxy->conf.lfs_file); |
| 9084 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9085 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9086 | cur_arg += 1; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9087 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9088 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9089 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9090 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9091 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) { |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9092 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9093 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9094 | goto out_err; |
| 9095 | } |
| 9096 | |
| 9097 | /* this redirect rule might already contain a parsed condition which |
| 9098 | * we'll pass to the http-request rule. |
| 9099 | */ |
| 9100 | rule->action = HTTP_REQ_ACT_REDIR; |
| 9101 | rule->arg.redir = redir; |
| 9102 | rule->cond = redir->cond; |
| 9103 | redir->cond = NULL; |
| 9104 | cur_arg = 2; |
| 9105 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9106 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9107 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9108 | rule->action = HTTP_REQ_ACT_ADD_ACL; |
| 9109 | /* |
| 9110 | * '+ 8' for 'add-acl(' |
| 9111 | * '- 9' for 'add-acl(' + trailing ')' |
| 9112 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9113 | 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] | 9114 | |
| 9115 | cur_arg = 1; |
| 9116 | |
| 9117 | if (!*args[cur_arg] || |
| 9118 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9119 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9120 | file, linenum, args[0]); |
| 9121 | goto out_err; |
| 9122 | } |
| 9123 | |
| 9124 | LIST_INIT(&rule->arg.map.key); |
| 9125 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9126 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9127 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9128 | file, linenum); |
| 9129 | free(proxy->conf.lfs_file); |
| 9130 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9131 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9132 | cur_arg += 1; |
| 9133 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9134 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
| 9135 | rule->action = HTTP_REQ_ACT_DEL_ACL; |
| 9136 | /* |
| 9137 | * '+ 8' for 'del-acl(' |
| 9138 | * '- 9' for 'del-acl(' + trailing ')' |
| 9139 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9140 | 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] | 9141 | |
| 9142 | cur_arg = 1; |
| 9143 | |
| 9144 | if (!*args[cur_arg] || |
| 9145 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9146 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9147 | file, linenum, args[0]); |
| 9148 | goto out_err; |
| 9149 | } |
| 9150 | |
| 9151 | LIST_INIT(&rule->arg.map.key); |
| 9152 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9153 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9154 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9155 | file, linenum); |
| 9156 | free(proxy->conf.lfs_file); |
| 9157 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9158 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9159 | cur_arg += 1; |
| 9160 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9161 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
| 9162 | rule->action = HTTP_REQ_ACT_DEL_MAP; |
| 9163 | /* |
| 9164 | * '+ 8' for 'del-map(' |
| 9165 | * '- 9' for 'del-map(' + trailing ')' |
| 9166 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9167 | 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] | 9168 | |
| 9169 | cur_arg = 1; |
| 9170 | |
| 9171 | if (!*args[cur_arg] || |
| 9172 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9173 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9174 | file, linenum, args[0]); |
| 9175 | goto out_err; |
| 9176 | } |
| 9177 | |
| 9178 | LIST_INIT(&rule->arg.map.key); |
| 9179 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9180 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9181 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9182 | file, linenum); |
| 9183 | free(proxy->conf.lfs_file); |
| 9184 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9185 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9186 | cur_arg += 1; |
| 9187 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9188 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9189 | rule->action = HTTP_REQ_ACT_SET_MAP; |
| 9190 | /* |
| 9191 | * '+ 8' for 'set-map(' |
| 9192 | * '- 9' for 'set-map(' + trailing ')' |
| 9193 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9194 | 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] | 9195 | |
| 9196 | cur_arg = 1; |
| 9197 | |
| 9198 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9199 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9200 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9201 | file, linenum, args[0]); |
| 9202 | goto out_err; |
| 9203 | } |
| 9204 | |
| 9205 | LIST_INIT(&rule->arg.map.key); |
| 9206 | LIST_INIT(&rule->arg.map.value); |
| 9207 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9208 | |
| 9209 | /* key pattern */ |
| 9210 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9211 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9212 | file, linenum); |
| 9213 | |
| 9214 | /* value pattern */ |
| 9215 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9216 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9217 | file, linenum); |
| 9218 | free(proxy->conf.lfs_file); |
| 9219 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9220 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9221 | |
| 9222 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9223 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9224 | char *errmsg = NULL; |
| 9225 | cur_arg = 1; |
| 9226 | /* try in the module list */ |
| 9227 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9228 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9229 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9230 | free(errmsg); |
| 9231 | goto out_err; |
| 9232 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9233 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9234 | 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', but got '%s'%s.\n", |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9235 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9236 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9237 | } |
| 9238 | |
| 9239 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9240 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9241 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9242 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9243 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9244 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9245 | file, linenum, args[0], errmsg); |
| 9246 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9247 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9248 | } |
| 9249 | rule->cond = cond; |
| 9250 | } |
| 9251 | else if (*args[cur_arg]) { |
| 9252 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9253 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9254 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9255 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9256 | } |
| 9257 | |
| 9258 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9259 | out_err: |
| 9260 | free(rule); |
| 9261 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9262 | } |
| 9263 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9264 | /* parse an "http-respose" rule */ |
| 9265 | struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 9266 | { |
| 9267 | struct http_res_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9268 | struct http_res_action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9269 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9270 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9271 | |
| 9272 | rule = calloc(1, sizeof(*rule)); |
| 9273 | if (!rule) { |
| 9274 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9275 | goto out_err; |
| 9276 | } |
| 9277 | |
| 9278 | if (!strcmp(args[0], "allow")) { |
| 9279 | rule->action = HTTP_RES_ACT_ALLOW; |
| 9280 | cur_arg = 1; |
| 9281 | } else if (!strcmp(args[0], "deny")) { |
| 9282 | rule->action = HTTP_RES_ACT_DENY; |
| 9283 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9284 | } else if (!strcmp(args[0], "set-nice")) { |
| 9285 | rule->action = HTTP_RES_ACT_SET_NICE; |
| 9286 | cur_arg = 1; |
| 9287 | |
| 9288 | if (!*args[cur_arg] || |
| 9289 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9290 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9291 | file, linenum, args[0]); |
| 9292 | goto out_err; |
| 9293 | } |
| 9294 | rule->arg.nice = atoi(args[cur_arg]); |
| 9295 | if (rule->arg.nice < -1024) |
| 9296 | rule->arg.nice = -1024; |
| 9297 | else if (rule->arg.nice > 1024) |
| 9298 | rule->arg.nice = 1024; |
| 9299 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9300 | } else if (!strcmp(args[0], "set-tos")) { |
| 9301 | #ifdef IP_TOS |
| 9302 | char *err; |
| 9303 | rule->action = HTTP_RES_ACT_SET_TOS; |
| 9304 | cur_arg = 1; |
| 9305 | |
| 9306 | if (!*args[cur_arg] || |
| 9307 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9308 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9309 | file, linenum, args[0]); |
| 9310 | goto out_err; |
| 9311 | } |
| 9312 | |
| 9313 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9314 | if (err && *err != '\0') { |
| 9315 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9316 | file, linenum, err, args[0]); |
| 9317 | goto out_err; |
| 9318 | } |
| 9319 | cur_arg++; |
| 9320 | #else |
| 9321 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9322 | goto out_err; |
| 9323 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9324 | } else if (!strcmp(args[0], "set-mark")) { |
| 9325 | #ifdef SO_MARK |
| 9326 | char *err; |
| 9327 | rule->action = HTTP_RES_ACT_SET_MARK; |
| 9328 | cur_arg = 1; |
| 9329 | |
| 9330 | if (!*args[cur_arg] || |
| 9331 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9332 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9333 | file, linenum, args[0]); |
| 9334 | goto out_err; |
| 9335 | } |
| 9336 | |
| 9337 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9338 | if (err && *err != '\0') { |
| 9339 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9340 | file, linenum, err, args[0]); |
| 9341 | goto out_err; |
| 9342 | } |
| 9343 | cur_arg++; |
| 9344 | global.last_checks |= LSTCHK_NETADM; |
| 9345 | #else |
| 9346 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9347 | goto out_err; |
| 9348 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9349 | } else if (!strcmp(args[0], "set-log-level")) { |
| 9350 | rule->action = HTTP_RES_ACT_SET_LOGL; |
| 9351 | cur_arg = 1; |
| 9352 | |
| 9353 | if (!*args[cur_arg] || |
| 9354 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9355 | bad_log_level: |
| 9356 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9357 | file, linenum, args[0]); |
| 9358 | goto out_err; |
| 9359 | } |
| 9360 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9361 | rule->arg.loglevel = -1; |
| 9362 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 9363 | goto bad_log_level; |
| 9364 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9365 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9366 | rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR; |
| 9367 | cur_arg = 1; |
| 9368 | |
| 9369 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9370 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9371 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9372 | file, linenum, args[0]); |
| 9373 | goto out_err; |
| 9374 | } |
| 9375 | |
| 9376 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9377 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9378 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9379 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9380 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9381 | 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] | 9382 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9383 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9384 | free(proxy->conf.lfs_file); |
| 9385 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9386 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9387 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9388 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9389 | rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9390 | cur_arg = 1; |
| 9391 | |
| 9392 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | a772b94 | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9393 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9394 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9395 | file, linenum, args[0]); |
| 9396 | goto out_err; |
| 9397 | } |
| 9398 | |
| 9399 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9400 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9401 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9402 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9403 | error = NULL; |
| 9404 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9405 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9406 | args[cur_arg + 1], error); |
| 9407 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9408 | goto out_err; |
| 9409 | } |
| 9410 | |
| 9411 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9412 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9413 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9414 | file, linenum); |
| 9415 | |
| 9416 | free(proxy->conf.lfs_file); |
| 9417 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9418 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9419 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9420 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9421 | rule->action = HTTP_RES_ACT_DEL_HDR; |
| 9422 | cur_arg = 1; |
| 9423 | |
| 9424 | if (!*args[cur_arg] || |
| 9425 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9426 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9427 | file, linenum, args[0]); |
| 9428 | goto out_err; |
| 9429 | } |
| 9430 | |
| 9431 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9432 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9433 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9434 | proxy->conf.args.ctx = ARGC_HRS; |
| 9435 | free(proxy->conf.lfs_file); |
| 9436 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9437 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9438 | cur_arg += 1; |
| 9439 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9440 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9441 | rule->action = HTTP_RES_ACT_ADD_ACL; |
| 9442 | /* |
| 9443 | * '+ 8' for 'add-acl(' |
| 9444 | * '- 9' for 'add-acl(' + trailing ')' |
| 9445 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9446 | 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] | 9447 | |
| 9448 | cur_arg = 1; |
| 9449 | |
| 9450 | if (!*args[cur_arg] || |
| 9451 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9452 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9453 | file, linenum, args[0]); |
| 9454 | goto out_err; |
| 9455 | } |
| 9456 | |
| 9457 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9458 | proxy->conf.args.ctx = ARGC_HRS; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9459 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9460 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9461 | file, linenum); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9462 | free(proxy->conf.lfs_file); |
| 9463 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9464 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9465 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9466 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9467 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9468 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
| 9469 | rule->action = HTTP_RES_ACT_DEL_ACL; |
| 9470 | /* |
| 9471 | * '+ 8' for 'del-acl(' |
| 9472 | * '- 9' for 'del-acl(' + trailing ')' |
| 9473 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9474 | 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] | 9475 | |
| 9476 | cur_arg = 1; |
| 9477 | |
| 9478 | if (!*args[cur_arg] || |
| 9479 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9480 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9481 | file, linenum, args[0]); |
| 9482 | goto out_err; |
| 9483 | } |
| 9484 | |
| 9485 | LIST_INIT(&rule->arg.map.key); |
| 9486 | proxy->conf.args.ctx = ARGC_HRS; |
| 9487 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9488 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9489 | file, linenum); |
| 9490 | free(proxy->conf.lfs_file); |
| 9491 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9492 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9493 | cur_arg += 1; |
| 9494 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9495 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
| 9496 | rule->action = HTTP_RES_ACT_DEL_MAP; |
| 9497 | /* |
| 9498 | * '+ 8' for 'del-map(' |
| 9499 | * '- 9' for 'del-map(' + trailing ')' |
| 9500 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9501 | 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] | 9502 | |
| 9503 | cur_arg = 1; |
| 9504 | |
| 9505 | if (!*args[cur_arg] || |
| 9506 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9507 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9508 | file, linenum, args[0]); |
| 9509 | goto out_err; |
| 9510 | } |
| 9511 | |
| 9512 | LIST_INIT(&rule->arg.map.key); |
| 9513 | proxy->conf.args.ctx = ARGC_HRS; |
| 9514 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9515 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9516 | file, linenum); |
| 9517 | free(proxy->conf.lfs_file); |
| 9518 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9519 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9520 | cur_arg += 1; |
| 9521 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9522 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9523 | rule->action = HTTP_RES_ACT_SET_MAP; |
| 9524 | /* |
| 9525 | * '+ 8' for 'set-map(' |
| 9526 | * '- 9' for 'set-map(' + trailing ')' |
| 9527 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9528 | 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] | 9529 | |
| 9530 | cur_arg = 1; |
| 9531 | |
| 9532 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9533 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9534 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9535 | file, linenum, args[0]); |
| 9536 | goto out_err; |
| 9537 | } |
| 9538 | |
| 9539 | LIST_INIT(&rule->arg.map.key); |
| 9540 | LIST_INIT(&rule->arg.map.value); |
| 9541 | |
| 9542 | proxy->conf.args.ctx = ARGC_HRS; |
| 9543 | |
| 9544 | /* key pattern */ |
| 9545 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9546 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9547 | file, linenum); |
| 9548 | |
| 9549 | /* value pattern */ |
| 9550 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9551 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9552 | file, linenum); |
| 9553 | |
| 9554 | free(proxy->conf.lfs_file); |
| 9555 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9556 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9557 | |
| 9558 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9559 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9560 | char *errmsg = NULL; |
| 9561 | cur_arg = 1; |
| 9562 | /* try in the module list */ |
| 9563 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9564 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9565 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9566 | free(errmsg); |
| 9567 | goto out_err; |
| 9568 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9569 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9570 | 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', but got '%s'%s.\n", |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9571 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 9572 | goto out_err; |
| 9573 | } |
| 9574 | |
| 9575 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9576 | struct acl_cond *cond; |
| 9577 | char *errmsg = NULL; |
| 9578 | |
| 9579 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9580 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9581 | file, linenum, args[0], errmsg); |
| 9582 | free(errmsg); |
| 9583 | goto out_err; |
| 9584 | } |
| 9585 | rule->cond = cond; |
| 9586 | } |
| 9587 | else if (*args[cur_arg]) { |
| 9588 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9589 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9590 | file, linenum, args[0], args[cur_arg]); |
| 9591 | goto out_err; |
| 9592 | } |
| 9593 | |
| 9594 | return rule; |
| 9595 | out_err: |
| 9596 | free(rule); |
| 9597 | return NULL; |
| 9598 | } |
| 9599 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9600 | /* 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] | 9601 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
| 9602 | * dynamic log-format rule instead of a static string. |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9603 | */ |
| 9604 | struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy, |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9605 | const char **args, char **errmsg, int use_fmt) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9606 | { |
| 9607 | struct redirect_rule *rule; |
| 9608 | int cur_arg; |
| 9609 | int type = REDIRECT_TYPE_NONE; |
| 9610 | int code = 302; |
| 9611 | const char *destination = NULL; |
| 9612 | const char *cookie = NULL; |
| 9613 | int cookie_set = 0; |
| 9614 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9615 | struct acl_cond *cond = NULL; |
| 9616 | |
| 9617 | cur_arg = 0; |
| 9618 | while (*(args[cur_arg])) { |
| 9619 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9620 | if (!*args[cur_arg + 1]) |
| 9621 | goto missing_arg; |
| 9622 | |
| 9623 | type = REDIRECT_TYPE_LOCATION; |
| 9624 | cur_arg++; |
| 9625 | destination = args[cur_arg]; |
| 9626 | } |
| 9627 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9628 | if (!*args[cur_arg + 1]) |
| 9629 | goto missing_arg; |
| 9630 | |
| 9631 | type = REDIRECT_TYPE_PREFIX; |
| 9632 | cur_arg++; |
| 9633 | destination = args[cur_arg]; |
| 9634 | } |
| 9635 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9636 | if (!*args[cur_arg + 1]) |
| 9637 | goto missing_arg; |
| 9638 | |
| 9639 | type = REDIRECT_TYPE_SCHEME; |
| 9640 | cur_arg++; |
| 9641 | destination = args[cur_arg]; |
| 9642 | } |
| 9643 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9644 | if (!*args[cur_arg + 1]) |
| 9645 | goto missing_arg; |
| 9646 | |
| 9647 | cur_arg++; |
| 9648 | cookie = args[cur_arg]; |
| 9649 | cookie_set = 1; |
| 9650 | } |
| 9651 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9652 | if (!*args[cur_arg + 1]) |
| 9653 | goto missing_arg; |
| 9654 | |
| 9655 | cur_arg++; |
| 9656 | cookie = args[cur_arg]; |
| 9657 | cookie_set = 0; |
| 9658 | } |
| 9659 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9660 | if (!*args[cur_arg + 1]) |
| 9661 | goto missing_arg; |
| 9662 | |
| 9663 | cur_arg++; |
| 9664 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9665 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9666 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9667 | "'%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] | 9668 | args[cur_arg - 1], args[cur_arg]); |
| 9669 | return NULL; |
| 9670 | } |
| 9671 | } |
| 9672 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9673 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9674 | } |
| 9675 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9676 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9677 | } |
| 9678 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9679 | strcmp(args[cur_arg], "unless") == 0) { |
| 9680 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 9681 | if (!cond) { |
| 9682 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9683 | return NULL; |
| 9684 | } |
| 9685 | break; |
| 9686 | } |
| 9687 | else { |
| 9688 | memprintf(errmsg, |
| 9689 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9690 | args[cur_arg]); |
| 9691 | return NULL; |
| 9692 | } |
| 9693 | cur_arg++; |
| 9694 | } |
| 9695 | |
| 9696 | if (type == REDIRECT_TYPE_NONE) { |
| 9697 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9698 | return NULL; |
| 9699 | } |
| 9700 | |
| 9701 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 9702 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9703 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9704 | |
| 9705 | if (!use_fmt) { |
| 9706 | /* old-style static redirect rule */ |
| 9707 | rule->rdr_str = strdup(destination); |
| 9708 | rule->rdr_len = strlen(destination); |
| 9709 | } |
| 9710 | else { |
| 9711 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9712 | |
| 9713 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9714 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9715 | * makes it hard for the user to configure a self-redirection. |
| 9716 | */ |
Godbach | 543b978 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9717 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9718 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9719 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9720 | (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9721 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9722 | free(curproxy->conf.lfs_file); |
| 9723 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9724 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9725 | } |
| 9726 | } |
| 9727 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9728 | if (cookie) { |
| 9729 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9730 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9731 | */ |
| 9732 | rule->cookie_len = strlen(cookie); |
| 9733 | if (cookie_set) { |
| 9734 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9735 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9736 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9737 | rule->cookie_len += 9; |
| 9738 | } else { |
| 9739 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9740 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9741 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9742 | rule->cookie_len += 20; |
| 9743 | } |
| 9744 | } |
| 9745 | rule->type = type; |
| 9746 | rule->code = code; |
| 9747 | rule->flags = flags; |
| 9748 | LIST_INIT(&rule->list); |
| 9749 | return rule; |
| 9750 | |
| 9751 | missing_arg: |
| 9752 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9753 | return NULL; |
| 9754 | } |
| 9755 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9756 | /************************************************************************/ |
| 9757 | /* The code below is dedicated to ACL parsing and matching */ |
| 9758 | /************************************************************************/ |
| 9759 | |
| 9760 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9761 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9762 | * which means that a request or response is ready. If some data is missing, |
| 9763 | * 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] | 9764 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9765 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9766 | * |
| 9767 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9768 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9769 | * decide whether or not an HTTP message is present ; |
| 9770 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9771 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9772 | * 1 if an HTTP message is ready |
| 9773 | */ |
| 9774 | static int |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9775 | smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9776 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9777 | { |
| 9778 | struct http_txn *txn = l7; |
| 9779 | struct http_msg *msg = &txn->req; |
| 9780 | |
| 9781 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 9782 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 9783 | */ |
| 9784 | |
| 9785 | if (unlikely(!s || !txn)) |
| 9786 | return 0; |
| 9787 | |
| 9788 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9789 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9790 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9791 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9792 | if (unlikely(!s->req)) |
| 9793 | return 0; |
| 9794 | |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9795 | /* If the buffer does not leave enough free space at the end, |
| 9796 | * we must first realign it. |
| 9797 | */ |
| 9798 | if (s->req->buf->p > s->req->buf->data && |
| 9799 | s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite) |
| 9800 | buffer_slow_realign(s->req->buf); |
| 9801 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9802 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9803 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9804 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9805 | |
| 9806 | /* Try to decode HTTP request */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9807 | if (likely(msg->next < s->req->buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9808 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9809 | |
| 9810 | /* Still no valid request ? */ |
| 9811 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9812 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9813 | buffer_full(s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9814 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9815 | } |
| 9816 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9817 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9818 | return 0; |
| 9819 | } |
| 9820 | |
| 9821 | /* OK we just got a valid HTTP request. We have some minor |
| 9822 | * preparation to perform so that further checks can rely |
| 9823 | * on HTTP tests. |
| 9824 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9825 | |
| 9826 | /* If the request was parsed but was too large, we must absolutely |
| 9827 | * return an error so that it is not processed. At the moment this |
| 9828 | * cannot happen, but if the parsers are to change in the future, |
| 9829 | * we want this check to be maintained. |
| 9830 | */ |
| 9831 | if (unlikely(s->req->buf->i + s->req->buf->p > |
| 9832 | s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) { |
| 9833 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9834 | smp->data.uint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9835 | return 1; |
| 9836 | } |
| 9837 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9838 | 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] | 9839 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 9840 | s->flags |= SN_REDIRECTABLE; |
| 9841 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9842 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9843 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9844 | } |
| 9845 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9846 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9847 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9848 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9849 | |
| 9850 | /* otherwise everything's ready for the request */ |
| 9851 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9852 | else { |
| 9853 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9854 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9855 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9856 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9857 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9858 | } |
| 9859 | |
| 9860 | /* everything's OK */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9861 | smp->data.uint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9862 | return 1; |
| 9863 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9864 | |
Willy Tarreau | a4ba9db | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 9865 | /* Note: these functinos *do* modify the sample. Even in case of success, at |
| 9866 | * least the type and uint value are modified. |
| 9867 | */ |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9868 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9869 | do { int r = smp_prefetch_http(px, l4, l7, opt, args, smp, 1); if (r <= 0) return r; } while (0) |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9870 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9871 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9872 | do { int r = smp_prefetch_http(px, l4, l7, opt, args, smp, 0); if (r <= 0) return r; } while (0) |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9873 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9874 | |
| 9875 | /* 1. Check on METHOD |
| 9876 | * We use the pre-parsed method if it is known, and store its number as an |
| 9877 | * integer. If it is unknown, we use the pointer and the length. |
| 9878 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9879 | 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] | 9880 | { |
| 9881 | int len, meth; |
| 9882 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9883 | len = strlen(text); |
| 9884 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9885 | |
| 9886 | pattern->val.i = meth; |
| 9887 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 71196f3 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 9888 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9889 | pattern->len = len; |
| 9890 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9891 | else { |
| 9892 | pattern->ptr.str = NULL; |
| 9893 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9894 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9895 | return 1; |
| 9896 | } |
| 9897 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9898 | /* This function fetches the method of current HTTP request and stores |
| 9899 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 9900 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 9901 | * in <len> and <ptr> is NULL ; |
| 9902 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 9903 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9904 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9905 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9906 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9907 | smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9908 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9909 | { |
| 9910 | int meth; |
| 9911 | struct http_txn *txn = l7; |
| 9912 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9913 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9914 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9915 | meth = txn->meth; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9916 | smp->type = SMP_T_METH; |
| 9917 | smp->data.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9918 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9919 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9920 | /* ensure the indexes are not affected */ |
| 9921 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9922 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9923 | smp->data.meth.str.len = txn->req.sl.rq.m_l; |
| 9924 | smp->data.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9925 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9926 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9927 | return 1; |
| 9928 | } |
| 9929 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9930 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9931 | 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] | 9932 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9933 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9934 | struct pattern_list *lst; |
| 9935 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9936 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9937 | list_for_each_entry(lst, &expr->patterns, list) { |
| 9938 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9939 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9940 | /* well-known method */ |
| 9941 | if (pattern->val.i != HTTP_METH_OTHER) { |
| 9942 | if (smp->data.meth.meth == pattern->val.i) |
| 9943 | return pattern; |
| 9944 | else |
| 9945 | continue; |
| 9946 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9947 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9948 | /* Other method, we must compare the strings */ |
| 9949 | if (pattern->len != smp->data.meth.str.len) |
| 9950 | continue; |
| 9951 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9952 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | c62a0c6 | 2014-08-28 20:42:57 +0200 | [diff] [blame] | 9953 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) || |
| 9954 | (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0)) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9955 | return pattern; |
| 9956 | } |
| 9957 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9958 | } |
| 9959 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9960 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9961 | smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9962 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9963 | { |
| 9964 | struct http_txn *txn = l7; |
| 9965 | char *ptr; |
| 9966 | int len; |
| 9967 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9968 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9969 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9970 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9971 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9972 | |
| 9973 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9974 | if (len <= 0) |
| 9975 | return 0; |
| 9976 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9977 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9978 | smp->data.str.str = ptr; |
| 9979 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9980 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9981 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9982 | return 1; |
| 9983 | } |
| 9984 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9985 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9986 | smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9987 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9988 | { |
| 9989 | struct http_txn *txn = l7; |
| 9990 | char *ptr; |
| 9991 | int len; |
| 9992 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9993 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9994 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9995 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9996 | return 0; |
| 9997 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9998 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9999 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10000 | |
| 10001 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10002 | if (len <= 0) |
| 10003 | return 0; |
| 10004 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10005 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10006 | smp->data.str.str = ptr; |
| 10007 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10008 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10009 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10010 | return 1; |
| 10011 | } |
| 10012 | |
| 10013 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10014 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10015 | smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10016 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10017 | { |
| 10018 | struct http_txn *txn = l7; |
| 10019 | char *ptr; |
| 10020 | int len; |
| 10021 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10022 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10023 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10024 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10025 | return 0; |
| 10026 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10027 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10028 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10029 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10030 | smp->type = SMP_T_UINT; |
| 10031 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10032 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10033 | return 1; |
| 10034 | } |
| 10035 | |
| 10036 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10037 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10038 | smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10039 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10040 | { |
| 10041 | struct http_txn *txn = l7; |
| 10042 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10043 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10044 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10045 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10046 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10047 | smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10048 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10049 | return 1; |
| 10050 | } |
| 10051 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10052 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10053 | smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10054 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10055 | { |
| 10056 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10057 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10058 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10059 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10060 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10061 | 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] | 10062 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10063 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10064 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10065 | smp->type = SMP_T_IPV4; |
| 10066 | smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10067 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10068 | return 1; |
| 10069 | } |
| 10070 | |
| 10071 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10072 | smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10073 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10074 | { |
| 10075 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10076 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10077 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10078 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10079 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10080 | 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] | 10081 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10082 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10083 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10084 | smp->type = SMP_T_UINT; |
| 10085 | smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10086 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10087 | return 1; |
| 10088 | } |
| 10089 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10090 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10091 | * Accepts an optional argument of type string containing the header field name, |
| 10092 | * and an optional argument of type signed or unsigned integer to request an |
| 10093 | * 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] | 10094 | * headers are considered from the first one. It does not stop on commas and |
| 10095 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10096 | */ |
| 10097 | static int |
| 10098 | smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10099 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10100 | { |
| 10101 | struct http_txn *txn = l7; |
| 10102 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10103 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
| 10104 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 10105 | int occ = 0; |
| 10106 | const char *name_str = NULL; |
| 10107 | int name_len = 0; |
| 10108 | |
| 10109 | if (!ctx) { |
| 10110 | /* first call */ |
| 10111 | ctx = &static_hdr_ctx; |
| 10112 | ctx->idx = 0; |
| 10113 | smp->ctx.a[0] = ctx; |
| 10114 | } |
| 10115 | |
| 10116 | if (args) { |
| 10117 | if (args[0].type != ARGT_STR) |
| 10118 | return 0; |
| 10119 | name_str = args[0].data.str.str; |
| 10120 | name_len = args[0].data.str.len; |
| 10121 | |
| 10122 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10123 | occ = args[1].data.uint; |
| 10124 | } |
| 10125 | |
| 10126 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10127 | |
| 10128 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10129 | /* search for header from the beginning */ |
| 10130 | ctx->idx = 0; |
| 10131 | |
| 10132 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10133 | /* no explicit occurrence and single fetch => last header by default */ |
| 10134 | occ = -1; |
| 10135 | |
| 10136 | if (!occ) |
| 10137 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10138 | smp->flags |= SMP_F_NOT_LAST; |
| 10139 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10140 | smp->type = SMP_T_STR; |
| 10141 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10142 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
| 10143 | return 1; |
| 10144 | |
| 10145 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10146 | return 0; |
| 10147 | } |
| 10148 | |
| 10149 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10150 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10151 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10152 | */ |
| 10153 | static int |
| 10154 | smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10155 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10156 | { |
| 10157 | struct http_txn *txn = l7; |
| 10158 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10159 | struct hdr_ctx ctx; |
| 10160 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 10161 | int cnt; |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10162 | const char *name = NULL; |
| 10163 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10164 | |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10165 | if (args && args->type == ARGT_STR) { |
| 10166 | name = args->data.str.str; |
| 10167 | len = args->data.str.len; |
| 10168 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10169 | |
| 10170 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10171 | |
| 10172 | ctx.idx = 0; |
| 10173 | cnt = 0; |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10174 | 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] | 10175 | cnt++; |
| 10176 | |
| 10177 | smp->type = SMP_T_UINT; |
| 10178 | smp->data.uint = cnt; |
| 10179 | smp->flags = SMP_F_VOL_HDR; |
| 10180 | return 1; |
| 10181 | } |
| 10182 | |
| 10183 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10184 | * Accepts an optional argument of type string containing the header field name, |
| 10185 | * and an optional argument of type signed or unsigned integer to request an |
| 10186 | * 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] | 10187 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10188 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10189 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10190 | smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10191 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10192 | { |
| 10193 | struct http_txn *txn = l7; |
| 10194 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10195 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10196 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10197 | int occ = 0; |
| 10198 | const char *name_str = NULL; |
| 10199 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10200 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10201 | if (!ctx) { |
| 10202 | /* first call */ |
| 10203 | ctx = &static_hdr_ctx; |
| 10204 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10205 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10206 | } |
| 10207 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10208 | if (args) { |
| 10209 | if (args[0].type != ARGT_STR) |
| 10210 | return 0; |
| 10211 | name_str = args[0].data.str.str; |
| 10212 | name_len = args[0].data.str.len; |
| 10213 | |
| 10214 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10215 | occ = args[1].data.uint; |
| 10216 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10217 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10218 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10219 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10220 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10221 | /* search for header from the beginning */ |
| 10222 | ctx->idx = 0; |
| 10223 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10224 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10225 | /* no explicit occurrence and single fetch => last header by default */ |
| 10226 | occ = -1; |
| 10227 | |
| 10228 | if (!occ) |
| 10229 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10230 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10231 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10232 | smp->type = SMP_T_STR; |
| 10233 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10234 | if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10235 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10236 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10237 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10238 | return 0; |
| 10239 | } |
| 10240 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10241 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10242 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10243 | */ |
| 10244 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10245 | smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10246 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10247 | { |
| 10248 | struct http_txn *txn = l7; |
| 10249 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10250 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10251 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10252 | int cnt; |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10253 | const char *name = NULL; |
| 10254 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10255 | |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10256 | if (args && args->type == ARGT_STR) { |
| 10257 | name = args->data.str.str; |
| 10258 | len = args->data.str.len; |
| 10259 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10260 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10261 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10262 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10263 | ctx.idx = 0; |
| 10264 | cnt = 0; |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10265 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10266 | cnt++; |
| 10267 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10268 | smp->type = SMP_T_UINT; |
| 10269 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10270 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10271 | return 1; |
| 10272 | } |
| 10273 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10274 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10275 | * takes a mandatory argument of type string and an optional one of type int |
| 10276 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10277 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10278 | */ |
| 10279 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10280 | smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10281 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10282 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10283 | int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10284 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10285 | if (ret > 0) { |
| 10286 | smp->type = SMP_T_UINT; |
| 10287 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10288 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10289 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10290 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10291 | } |
| 10292 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10293 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10294 | * and an optional one of type int to designate a specific occurrence. |
| 10295 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10296 | */ |
| 10297 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10298 | smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10299 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10300 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10301 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10302 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10303 | while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) { |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10304 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 10305 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10306 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10307 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10308 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10309 | if (smp->data.str.len < temp->size - 1) { |
| 10310 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 10311 | temp->str[smp->data.str.len] = '\0'; |
| 10312 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 10313 | smp->type = SMP_T_IPV6; |
| 10314 | break; |
| 10315 | } |
| 10316 | } |
| 10317 | } |
| 10318 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10319 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10320 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10321 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10322 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10323 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10324 | } |
| 10325 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10326 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10327 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10328 | */ |
| 10329 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10330 | smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10331 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10332 | { |
| 10333 | struct http_txn *txn = l7; |
| 10334 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10335 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10336 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10337 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10338 | 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] | 10339 | ptr = http_get_path(txn); |
| 10340 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10341 | return 0; |
| 10342 | |
| 10343 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10344 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10345 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10346 | |
| 10347 | while (ptr < end && *ptr != '?') |
| 10348 | ptr++; |
| 10349 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10350 | smp->data.str.len = ptr - smp->data.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10351 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10352 | return 1; |
| 10353 | } |
| 10354 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10355 | /* This produces a concatenation of the first occurrence of the Host header |
| 10356 | * followed by the path component if it begins with a slash ('/'). This means |
| 10357 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10358 | * If no Host header is found, then the path is returned as-is. The returned |
| 10359 | * 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] | 10360 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10361 | */ |
| 10362 | static int |
| 10363 | smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10364 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10365 | { |
| 10366 | struct http_txn *txn = l7; |
| 10367 | char *ptr, *end, *beg; |
| 10368 | struct hdr_ctx ctx; |
Willy Tarreau | c1fbbd4 | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10369 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10370 | |
| 10371 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10372 | |
| 10373 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10374 | if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen) |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10375 | return smp_fetch_path(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10376 | |
| 10377 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
Willy Tarreau | c1fbbd4 | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10378 | temp = get_trash_chunk(); |
| 10379 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10380 | smp->type = SMP_T_STR; |
Willy Tarreau | c1fbbd4 | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10381 | smp->data.str.str = temp->str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10382 | smp->data.str.len = ctx.vlen; |
| 10383 | |
| 10384 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10385 | 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] | 10386 | beg = http_get_path(txn); |
| 10387 | if (!beg) |
| 10388 | beg = end; |
| 10389 | |
| 10390 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10391 | |
| 10392 | if (beg < ptr && *beg == '/') { |
| 10393 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 10394 | smp->data.str.len += ptr - beg; |
| 10395 | } |
| 10396 | |
| 10397 | smp->flags = SMP_F_VOL_1ST; |
| 10398 | return 1; |
| 10399 | } |
| 10400 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10401 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10402 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10403 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10404 | * 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] | 10405 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10406 | * 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] | 10407 | * high-traffic sites without having to store whole paths. |
| 10408 | */ |
| 10409 | static int |
| 10410 | smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10411 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10412 | { |
| 10413 | struct http_txn *txn = l7; |
| 10414 | struct hdr_ctx ctx; |
| 10415 | unsigned int hash = 0; |
| 10416 | char *ptr, *beg, *end; |
| 10417 | int len; |
| 10418 | |
| 10419 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10420 | |
| 10421 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10422 | 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] | 10423 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10424 | ptr = ctx.line + ctx.val; |
| 10425 | len = ctx.vlen; |
| 10426 | while (len--) |
| 10427 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10428 | } |
| 10429 | |
| 10430 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10431 | 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] | 10432 | beg = http_get_path(txn); |
| 10433 | if (!beg) |
| 10434 | beg = end; |
| 10435 | |
| 10436 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10437 | |
| 10438 | if (beg < ptr && *beg == '/') { |
| 10439 | while (beg < ptr) |
| 10440 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10441 | } |
| 10442 | hash = full_hash(hash); |
| 10443 | |
| 10444 | smp->type = SMP_T_UINT; |
| 10445 | smp->data.uint = hash; |
| 10446 | smp->flags = SMP_F_VOL_1ST; |
| 10447 | return 1; |
| 10448 | } |
| 10449 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10450 | /* 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] | 10451 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10452 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10453 | * 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] | 10454 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10455 | * 8 bytes would still work. |
| 10456 | */ |
| 10457 | static int |
| 10458 | smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10459 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10460 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10461 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10462 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
| 10463 | |
| 10464 | if (!cli_conn) |
| 10465 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10466 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10467 | if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10468 | return 0; |
| 10469 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10470 | temp = get_trash_chunk(); |
Willy Tarreau | 0dff81c | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10471 | *(unsigned int *)temp->str = htonl(smp->data.uint); |
| 10472 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10473 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10474 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10475 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10476 | 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] | 10477 | temp->len += 4; |
| 10478 | break; |
| 10479 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10480 | 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] | 10481 | temp->len += 16; |
| 10482 | break; |
| 10483 | default: |
| 10484 | return 0; |
| 10485 | } |
| 10486 | |
| 10487 | smp->data.str = *temp; |
| 10488 | smp->type = SMP_T_BIN; |
| 10489 | return 1; |
| 10490 | } |
| 10491 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10492 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10493 | smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10494 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10495 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10496 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10497 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10498 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10499 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10500 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10501 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10502 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 10503 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10504 | return 1; |
| 10505 | } |
| 10506 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10507 | /* return a valid test if the current request is the first one on the connection */ |
| 10508 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10509 | smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10510 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10511 | { |
| 10512 | if (!s) |
| 10513 | return 0; |
| 10514 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10515 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 10516 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10517 | return 1; |
| 10518 | } |
| 10519 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10520 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10521 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10522 | smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10523 | const struct arg *args, struct sample *smp, const char *kw) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10524 | { |
| 10525 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10526 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10527 | return 0; |
| 10528 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10529 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10530 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10531 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10532 | return 0; |
| 10533 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10534 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10535 | smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10536 | return 1; |
| 10537 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10538 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10539 | /* Accepts exactly 1 argument of type userlist */ |
| 10540 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10541 | smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10542 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10543 | { |
| 10544 | |
| 10545 | if (!args || args->type != ARGT_USR) |
| 10546 | return 0; |
| 10547 | |
| 10548 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10549 | |
| 10550 | if (!get_http_auth(l4)) |
| 10551 | return 0; |
| 10552 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10553 | /* if the user does not belong to the userlist or has a wrong password, |
| 10554 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10555 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10556 | */ |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10557 | if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass)) |
| 10558 | return 0; |
| 10559 | |
| 10560 | /* pat_match_auth() will need the user list */ |
| 10561 | smp->ctx.a[0] = args->data.usr; |
| 10562 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10563 | smp->type = SMP_T_STR; |
| 10564 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10565 | smp->data.str.str = l4->txn.auth.user; |
| 10566 | smp->data.str.len = strlen(l4->txn.auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10567 | |
| 10568 | return 1; |
| 10569 | } |
| 10570 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10571 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10572 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10573 | * the cookie value is returned into *value and *value_l, and the function |
| 10574 | * returns a pointer to the next pointer to search from if the value was found. |
| 10575 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10576 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10577 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10578 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10579 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10580 | * list is expected. |
| 10581 | */ |
| 10582 | static char * |
| 10583 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10584 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10585 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10586 | { |
| 10587 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10588 | char *next; |
| 10589 | |
| 10590 | /* we search at least a cookie name followed by an equal, and more |
| 10591 | * generally something like this : |
| 10592 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10593 | */ |
| 10594 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10595 | /* Iterate through all cookies on this line */ |
| 10596 | |
| 10597 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 10598 | att_beg++; |
| 10599 | |
| 10600 | /* find att_end : this is the first character after the last non |
| 10601 | * space before the equal. It may be equal to hdr_end. |
| 10602 | */ |
| 10603 | equal = att_end = att_beg; |
| 10604 | |
| 10605 | while (equal < hdr_end) { |
| 10606 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10607 | break; |
| 10608 | if (http_is_spht[(unsigned char)*equal++]) |
| 10609 | continue; |
| 10610 | att_end = equal; |
| 10611 | } |
| 10612 | |
| 10613 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10614 | * is between <att_beg> and <equal>, both may be identical. |
| 10615 | */ |
| 10616 | |
| 10617 | /* look for end of cookie if there is an equal sign */ |
| 10618 | if (equal < hdr_end && *equal == '=') { |
| 10619 | /* look for the beginning of the value */ |
| 10620 | val_beg = equal + 1; |
| 10621 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 10622 | val_beg++; |
| 10623 | |
| 10624 | /* find the end of the value, respecting quotes */ |
| 10625 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10626 | |
| 10627 | /* make val_end point to the first white space or delimitor after the value */ |
| 10628 | val_end = next; |
| 10629 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 10630 | val_end--; |
| 10631 | } else { |
| 10632 | val_beg = val_end = next = equal; |
| 10633 | } |
| 10634 | |
| 10635 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10636 | * they will automatically be removed if a header before them is removed, |
| 10637 | * since they're supposed to be linked together. |
| 10638 | */ |
| 10639 | if (*att_beg == '$') |
| 10640 | continue; |
| 10641 | |
| 10642 | /* Ignore cookies with no equal sign */ |
| 10643 | if (equal == next) |
| 10644 | continue; |
| 10645 | |
| 10646 | /* Now we have the cookie name between att_beg and att_end, and |
| 10647 | * its value between val_beg and val_end. |
| 10648 | */ |
| 10649 | |
| 10650 | if (att_end - att_beg == cookie_name_l && |
| 10651 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10652 | /* let's return this value and indicate where to go on from */ |
| 10653 | *value = val_beg; |
| 10654 | *value_l = val_end - val_beg; |
| 10655 | return next + 1; |
| 10656 | } |
| 10657 | |
| 10658 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10659 | if (!list) |
| 10660 | break; |
| 10661 | } |
| 10662 | |
| 10663 | return NULL; |
| 10664 | } |
| 10665 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10666 | /* Fetch a captured HTTP request header. The index is the position of |
| 10667 | * the "capture" option in the configuration file |
| 10668 | */ |
| 10669 | static int |
| 10670 | smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10671 | const struct arg *args, struct sample *smp, const char *kw) |
| 10672 | { |
| 10673 | struct proxy *fe = l4->fe; |
| 10674 | struct http_txn *txn = l7; |
| 10675 | int idx; |
| 10676 | |
| 10677 | if (!args || args->type != ARGT_UINT) |
| 10678 | return 0; |
| 10679 | |
| 10680 | idx = args->data.uint; |
| 10681 | |
| 10682 | if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL) |
| 10683 | return 0; |
| 10684 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10685 | smp->type = SMP_T_STR; |
| 10686 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10687 | smp->data.str.str = txn->req.cap[idx]; |
| 10688 | smp->data.str.len = strlen(txn->req.cap[idx]); |
| 10689 | |
| 10690 | return 1; |
| 10691 | } |
| 10692 | |
| 10693 | /* Fetch a captured HTTP response header. The index is the position of |
| 10694 | * the "capture" option in the configuration file |
| 10695 | */ |
| 10696 | static int |
| 10697 | smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10698 | const struct arg *args, struct sample *smp, const char *kw) |
| 10699 | { |
| 10700 | struct proxy *fe = l4->fe; |
| 10701 | struct http_txn *txn = l7; |
| 10702 | int idx; |
| 10703 | |
| 10704 | if (!args || args->type != ARGT_UINT) |
| 10705 | return 0; |
| 10706 | |
| 10707 | idx = args->data.uint; |
| 10708 | |
| 10709 | if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL) |
| 10710 | return 0; |
| 10711 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10712 | smp->type = SMP_T_STR; |
| 10713 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10714 | smp->data.str.str = txn->rsp.cap[idx]; |
| 10715 | smp->data.str.len = strlen(txn->rsp.cap[idx]); |
| 10716 | |
| 10717 | return 1; |
| 10718 | } |
| 10719 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10720 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 10721 | static int |
| 10722 | smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10723 | const struct arg *args, struct sample *smp, const char *kw) |
| 10724 | { |
| 10725 | struct chunk *temp; |
| 10726 | struct http_txn *txn = l7; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10727 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10728 | |
| 10729 | if (!txn->uri) |
| 10730 | return 0; |
| 10731 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10732 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10733 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10734 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10735 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10736 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10737 | temp = get_trash_chunk(); |
| 10738 | temp->str = txn->uri; |
| 10739 | temp->len = ptr - txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10740 | smp->data.str = *temp; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10741 | smp->type = SMP_T_STR; |
| 10742 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10743 | |
| 10744 | return 1; |
| 10745 | |
| 10746 | } |
| 10747 | |
| 10748 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 10749 | static int |
| 10750 | smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10751 | const struct arg *args, struct sample *smp, const char *kw) |
| 10752 | { |
| 10753 | struct chunk *temp; |
| 10754 | struct http_txn *txn = l7; |
| 10755 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10756 | |
| 10757 | if (!txn->uri) |
| 10758 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10759 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10760 | ptr = txn->uri; |
| 10761 | |
| 10762 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10763 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10764 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10765 | if (!*ptr) |
| 10766 | return 0; |
| 10767 | |
| 10768 | ptr++; /* skip the space */ |
| 10769 | |
| 10770 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10771 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10772 | if (!ptr) |
| 10773 | return 0; |
| 10774 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 10775 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10776 | |
| 10777 | smp->data.str = *temp; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10778 | smp->data.str.len = ptr - temp->str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10779 | smp->type = SMP_T_STR; |
| 10780 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10781 | |
| 10782 | return 1; |
| 10783 | } |
| 10784 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10785 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 10786 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10787 | */ |
| 10788 | static int |
| 10789 | smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10790 | const struct arg *args, struct sample *smp, const char *kw) |
| 10791 | { |
| 10792 | struct http_txn *txn = l7; |
| 10793 | |
| 10794 | if (txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
| 10795 | return 0; |
| 10796 | |
| 10797 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 10798 | smp->data.str.str = "HTTP/1.1"; |
| 10799 | else |
| 10800 | smp->data.str.str = "HTTP/1.0"; |
| 10801 | |
| 10802 | smp->data.str.len = 8; |
| 10803 | smp->type = SMP_T_STR; |
| 10804 | smp->flags = SMP_F_CONST; |
| 10805 | return 1; |
| 10806 | |
| 10807 | } |
| 10808 | |
| 10809 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 10810 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10811 | */ |
| 10812 | static int |
| 10813 | smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10814 | const struct arg *args, struct sample *smp, const char *kw) |
| 10815 | { |
| 10816 | struct http_txn *txn = l7; |
| 10817 | |
| 10818 | if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
| 10819 | return 0; |
| 10820 | |
| 10821 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
| 10822 | smp->data.str.str = "HTTP/1.1"; |
| 10823 | else |
| 10824 | smp->data.str.str = "HTTP/1.0"; |
| 10825 | |
| 10826 | smp->data.str.len = 8; |
| 10827 | smp->type = SMP_T_STR; |
| 10828 | smp->flags = SMP_F_CONST; |
| 10829 | return 1; |
| 10830 | |
| 10831 | } |
| 10832 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10833 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10834 | /* 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] | 10835 | * 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] | 10836 | * 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] | 10837 | * 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] | 10838 | * 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] | 10839 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 10840 | * 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] | 10841 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 10842 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10843 | */ |
William Lallemand | 07c8b24 | 2014-05-02 17:11:07 +0200 | [diff] [blame] | 10844 | int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10845 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10846 | { |
| 10847 | struct http_txn *txn = l7; |
| 10848 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10849 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10850 | const struct http_msg *msg; |
| 10851 | const char *hdr_name; |
| 10852 | int hdr_name_len; |
| 10853 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10854 | int occ = 0; |
| 10855 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10856 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10857 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10858 | return 0; |
| 10859 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10860 | if (!ctx) { |
| 10861 | /* first call */ |
| 10862 | ctx = &static_hdr_ctx; |
| 10863 | ctx->idx = 0; |
| 10864 | smp->ctx.a[2] = ctx; |
| 10865 | } |
| 10866 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10867 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10868 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10869 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10870 | msg = &txn->req; |
| 10871 | hdr_name = "Cookie"; |
| 10872 | hdr_name_len = 6; |
| 10873 | } else { |
| 10874 | msg = &txn->rsp; |
| 10875 | hdr_name = "Set-Cookie"; |
| 10876 | hdr_name_len = 10; |
| 10877 | } |
| 10878 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10879 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10880 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 10881 | occ = -1; |
| 10882 | |
| 10883 | /* OK so basically here, either we want only one value and it's the |
| 10884 | * last one, or we want to iterate over all of them and we fetch the |
| 10885 | * next one. |
| 10886 | */ |
| 10887 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10888 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10889 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10890 | /* search for the header from the beginning, we must first initialize |
| 10891 | * the search parameters. |
| 10892 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10893 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10894 | ctx->idx = 0; |
| 10895 | } |
| 10896 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10897 | smp->flags |= SMP_F_VOL_HDR; |
| 10898 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10899 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10900 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 10901 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10902 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 10903 | goto out; |
| 10904 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10905 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10906 | continue; |
| 10907 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10908 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 10909 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10910 | } |
| 10911 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10912 | smp->type = SMP_T_STR; |
| 10913 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10914 | 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] | 10915 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10916 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10917 | &smp->data.str.str, |
| 10918 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10919 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10920 | found = 1; |
| 10921 | if (occ >= 0) { |
| 10922 | /* one value was returned into smp->data.str.{str,len} */ |
| 10923 | smp->flags |= SMP_F_NOT_LAST; |
| 10924 | return 1; |
| 10925 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10926 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10927 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10928 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10929 | /* all cookie headers and values were scanned. If we're looking for the |
| 10930 | * last occurrence, we may return it now. |
| 10931 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10932 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10933 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10934 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10935 | } |
| 10936 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10937 | /* 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] | 10938 | * 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] | 10939 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 10940 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10941 | */ |
| 10942 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10943 | smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10944 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10945 | { |
| 10946 | struct http_txn *txn = l7; |
| 10947 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10948 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10949 | const struct http_msg *msg; |
| 10950 | const char *hdr_name; |
| 10951 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10952 | int cnt; |
| 10953 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10954 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10955 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10956 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10957 | return 0; |
| 10958 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10959 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10960 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10961 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10962 | msg = &txn->req; |
| 10963 | hdr_name = "Cookie"; |
| 10964 | hdr_name_len = 6; |
| 10965 | } else { |
| 10966 | msg = &txn->rsp; |
| 10967 | hdr_name = "Set-Cookie"; |
| 10968 | hdr_name_len = 10; |
| 10969 | } |
| 10970 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10971 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 10972 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10973 | ctx.idx = 0; |
| 10974 | cnt = 0; |
| 10975 | |
| 10976 | while (1) { |
| 10977 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 10978 | if (!val_beg) { |
| 10979 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 10980 | break; |
| 10981 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10982 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10983 | continue; |
| 10984 | |
| 10985 | val_beg = ctx.line + ctx.val; |
| 10986 | val_end = val_beg + ctx.vlen; |
| 10987 | } |
| 10988 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10989 | smp->type = SMP_T_STR; |
| 10990 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10991 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10992 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10993 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10994 | &smp->data.str.str, |
| 10995 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10996 | cnt++; |
| 10997 | } |
| 10998 | } |
| 10999 | |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 11000 | smp->type = SMP_T_UINT; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11001 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11002 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11003 | return 1; |
| 11004 | } |
| 11005 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11006 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 11007 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 11008 | */ |
| 11009 | static int |
| 11010 | smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 11011 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11012 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 11013 | int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11014 | |
| 11015 | if (ret > 0) { |
| 11016 | smp->type = SMP_T_UINT; |
| 11017 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 11018 | } |
| 11019 | |
| 11020 | return ret; |
| 11021 | } |
| 11022 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11023 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11024 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11025 | /************************************************************************/ |
| 11026 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11027 | /* |
| 11028 | * Given a path string and its length, find the position of beginning of the |
| 11029 | * query string. Returns NULL if no query string is found in the path. |
| 11030 | * |
| 11031 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11032 | * |
| 11033 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 11034 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11035 | 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] | 11036 | { |
| 11037 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11038 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11039 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11040 | return p ? p + 1 : NULL; |
| 11041 | } |
| 11042 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11043 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11044 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11045 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11046 | } |
| 11047 | |
| 11048 | /* |
| 11049 | * Given a url parameter, find the starting position of the first occurence, |
| 11050 | * or NULL if the parameter is not found. |
| 11051 | * |
| 11052 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11053 | * the function will return query_string+8. |
| 11054 | */ |
| 11055 | static char* |
| 11056 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11057 | char* url_param_name, size_t url_param_name_l, |
| 11058 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11059 | { |
| 11060 | char *pos, *last; |
| 11061 | |
| 11062 | pos = query_string; |
| 11063 | last = query_string + query_string_l - url_param_name_l - 1; |
| 11064 | |
| 11065 | while (pos <= last) { |
| 11066 | if (pos[url_param_name_l] == '=') { |
| 11067 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 11068 | return pos; |
| 11069 | pos += url_param_name_l + 1; |
| 11070 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11071 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11072 | pos++; |
| 11073 | pos++; |
| 11074 | } |
| 11075 | return NULL; |
| 11076 | } |
| 11077 | |
| 11078 | /* |
Cyril Bonté | 0f836e1 | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11079 | * Given a url parameter name and a query string, find the next value. |
| 11080 | * An empty url_param_name matches the first available parameter. |
| 11081 | * If the parameter is found, 1 is returned and *value / *value_l are updated |
| 11082 | * to respectively provide a pointer to the value and its length. |
| 11083 | * Otherwise, 0 is returned and value/value_l are not modified. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11084 | */ |
| 11085 | static int |
| 11086 | find_url_param_value(char* path, size_t path_l, |
| 11087 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11088 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11089 | { |
| 11090 | char *query_string, *qs_end; |
| 11091 | char *arg_start; |
| 11092 | char *value_start, *value_end; |
| 11093 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11094 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11095 | if (!query_string) |
| 11096 | return 0; |
| 11097 | |
| 11098 | qs_end = path + path_l; |
| 11099 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11100 | url_param_name, url_param_name_l, |
| 11101 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11102 | if (!arg_start) |
| 11103 | return 0; |
| 11104 | |
| 11105 | value_start = arg_start + url_param_name_l + 1; |
| 11106 | value_end = value_start; |
| 11107 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11108 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11109 | value_end++; |
| 11110 | |
| 11111 | *value = value_start; |
| 11112 | *value_l = value_end - value_start; |
Cyril Bonté | 0f836e1 | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11113 | return 1; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11114 | } |
| 11115 | |
| 11116 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11117 | smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 11118 | const struct arg *args, struct sample *smp, const char *kw) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11119 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11120 | char delim = '?'; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11121 | struct http_txn *txn = l7; |
| 11122 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11123 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11124 | if (!args || args[0].type != ARGT_STR || |
| 11125 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11126 | return 0; |
| 11127 | |
| 11128 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11129 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11130 | if (args[1].type) |
| 11131 | delim = *args[1].data.str.str; |
| 11132 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11133 | if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11134 | args->data.str.str, args->data.str.len, |
| 11135 | &smp->data.str.str, &smp->data.str.len, |
| 11136 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11137 | return 0; |
| 11138 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11139 | smp->type = SMP_T_STR; |
| 11140 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11141 | return 1; |
| 11142 | } |
| 11143 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11144 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11145 | * above). |
| 11146 | */ |
| 11147 | static int |
| 11148 | smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 11149 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11150 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 11151 | int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11152 | |
| 11153 | if (ret > 0) { |
| 11154 | smp->type = SMP_T_UINT; |
| 11155 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 11156 | } |
| 11157 | |
| 11158 | return ret; |
| 11159 | } |
| 11160 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11161 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11162 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11163 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11164 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11165 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11166 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11167 | * high-traffic sites without having to store whole paths. |
| 11168 | * this differs from the base32 functions in that it includes the url parameters |
| 11169 | * as well as the path |
| 11170 | */ |
| 11171 | static int |
| 11172 | smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 11173 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11174 | { |
| 11175 | struct http_txn *txn = l7; |
| 11176 | struct hdr_ctx ctx; |
| 11177 | unsigned int hash = 0; |
| 11178 | char *ptr, *beg, *end; |
| 11179 | int len; |
| 11180 | |
| 11181 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11182 | |
| 11183 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11184 | 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] | 11185 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11186 | ptr = ctx.line + ctx.val; |
| 11187 | len = ctx.vlen; |
| 11188 | while (len--) |
| 11189 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11190 | } |
| 11191 | |
| 11192 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11193 | 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] | 11194 | beg = http_get_path(txn); |
| 11195 | if (!beg) |
| 11196 | beg = end; |
| 11197 | |
| 11198 | for (ptr = beg; ptr < end ; ptr++); |
| 11199 | |
| 11200 | if (beg < ptr && *beg == '/') { |
| 11201 | while (beg < ptr) |
| 11202 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11203 | } |
| 11204 | hash = full_hash(hash); |
| 11205 | |
| 11206 | smp->type = SMP_T_UINT; |
| 11207 | smp->data.uint = hash; |
| 11208 | smp->flags = SMP_F_VOL_1ST; |
| 11209 | return 1; |
| 11210 | } |
| 11211 | |
| 11212 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11213 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11214 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11215 | * on the source address length. The URL hash is stored before the address so |
| 11216 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11217 | * 8 bytes would still work. |
| 11218 | */ |
| 11219 | static int |
| 11220 | smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 11221 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11222 | { |
| 11223 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11224 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11225 | |
Dragan Dosen | b3d91b8 | 2016-06-16 11:23:01 +0200 | [diff] [blame] | 11226 | if (!cli_conn) |
| 11227 | return 0; |
| 11228 | |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 11229 | if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11230 | return 0; |
| 11231 | |
| 11232 | temp = get_trash_chunk(); |
Dragan Dosen | 9410d75 | 2016-06-16 11:08:08 +0200 | [diff] [blame] | 11233 | *(unsigned int *)temp->str = htonl(smp->data.sint); |
| 11234 | temp->len += sizeof(unsigned int); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11235 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11236 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11237 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11238 | 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] | 11239 | temp->len += 4; |
| 11240 | break; |
| 11241 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11242 | 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] | 11243 | temp->len += 16; |
| 11244 | break; |
| 11245 | default: |
| 11246 | return 0; |
| 11247 | } |
| 11248 | |
| 11249 | smp->data.str = *temp; |
| 11250 | smp->type = SMP_T_BIN; |
| 11251 | return 1; |
| 11252 | } |
| 11253 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11254 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11255 | * keyword. These keywords support an optional positive or negative occurrence |
| 11256 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11257 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11258 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11259 | * error message in case of error, that the caller is responsible for freeing. |
| 11260 | * The initial location must either be freeable or NULL. |
| 11261 | */ |
| 11262 | static int val_hdr(struct arg *arg, char **err_msg) |
| 11263 | { |
| 11264 | 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] | 11265 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11266 | return 0; |
| 11267 | } |
| 11268 | return 1; |
| 11269 | } |
| 11270 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11271 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11272 | * adds an optional offset found in args[0] and emits a string representing |
| 11273 | * the date in RFC-1123/5322 format. |
| 11274 | */ |
| 11275 | static int sample_conv_http_date(const struct arg *args, struct sample *smp) |
| 11276 | { |
Cyril Bonté | f980b36 | 2016-01-22 19:40:28 +0100 | [diff] [blame] | 11277 | const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11278 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11279 | struct chunk *temp; |
| 11280 | struct tm *tm; |
| 11281 | time_t curr_date = smp->data.uint; |
| 11282 | |
| 11283 | /* add offset */ |
| 11284 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 11285 | curr_date += args[0].data.sint; |
| 11286 | |
| 11287 | tm = gmtime(&curr_date); |
Thierry FOURNIER | 9555872 | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 11288 | if (!tm) |
| 11289 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11290 | |
| 11291 | temp = get_trash_chunk(); |
| 11292 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11293 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11294 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11295 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11296 | |
| 11297 | smp->data.str = *temp; |
| 11298 | smp->type = SMP_T_STR; |
| 11299 | return 1; |
| 11300 | } |
| 11301 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11302 | /* Match language range with language tag. RFC2616 14.4: |
| 11303 | * |
| 11304 | * A language-range matches a language-tag if it exactly equals |
| 11305 | * the tag, or if it exactly equals a prefix of the tag such |
| 11306 | * that the first tag character following the prefix is "-". |
| 11307 | * |
| 11308 | * Return 1 if the strings match, else return 0. |
| 11309 | */ |
| 11310 | static inline int language_range_match(const char *range, int range_len, |
| 11311 | const char *tag, int tag_len) |
| 11312 | { |
| 11313 | const char *end = range + range_len; |
| 11314 | const char *tend = tag + tag_len; |
| 11315 | while (range < end) { |
| 11316 | if (*range == '-' && tag == tend) |
| 11317 | return 1; |
| 11318 | if (*range != *tag || tag == tend) |
| 11319 | return 0; |
| 11320 | range++; |
| 11321 | tag++; |
| 11322 | } |
| 11323 | /* Return true only if the last char of the tag is matched. */ |
| 11324 | return tag == tend; |
| 11325 | } |
| 11326 | |
| 11327 | /* Arguments: The list of expected value, the number of parts returned and the separator */ |
| 11328 | static int sample_conv_q_prefered(const struct arg *args, struct sample *smp) |
| 11329 | { |
| 11330 | const char *al = smp->data.str.str; |
| 11331 | const char *end = al + smp->data.str.len; |
| 11332 | const char *token; |
| 11333 | int toklen; |
| 11334 | int qvalue; |
| 11335 | const char *str; |
| 11336 | const char *w; |
| 11337 | int best_q = 0; |
| 11338 | |
| 11339 | /* Set the constant to the sample, because the output of the |
| 11340 | * function will be peek in the constant configuration string. |
| 11341 | */ |
| 11342 | smp->flags |= SMP_F_CONST; |
| 11343 | smp->data.str.size = 0; |
| 11344 | smp->data.str.str = ""; |
| 11345 | smp->data.str.len = 0; |
| 11346 | |
| 11347 | /* Parse the accept language */ |
| 11348 | while (1) { |
| 11349 | |
| 11350 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11351 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11352 | al++; |
| 11353 | if (al >= end) |
| 11354 | break; |
| 11355 | |
| 11356 | /* Start of the fisrt word. */ |
| 11357 | token = al; |
| 11358 | |
| 11359 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11360 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11361 | al++; |
| 11362 | if (al == token) |
| 11363 | goto expect_comma; |
| 11364 | |
| 11365 | /* Length of the token. */ |
| 11366 | toklen = al - token; |
| 11367 | qvalue = 1000; |
| 11368 | |
| 11369 | /* Check if the token exists in the list. If the token not exists, |
| 11370 | * jump to the next token. |
| 11371 | */ |
| 11372 | str = args[0].data.str.str; |
| 11373 | w = str; |
| 11374 | while (1) { |
| 11375 | if (*str == ';' || *str == '\0') { |
| 11376 | if (language_range_match(token, toklen, w, str-w)) |
| 11377 | goto look_for_q; |
| 11378 | if (*str == '\0') |
| 11379 | goto expect_comma; |
| 11380 | w = str + 1; |
| 11381 | } |
| 11382 | str++; |
| 11383 | } |
| 11384 | goto expect_comma; |
| 11385 | |
| 11386 | look_for_q: |
| 11387 | |
| 11388 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11389 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11390 | al++; |
| 11391 | if (al >= end) |
| 11392 | goto process_value; |
| 11393 | |
| 11394 | /* If ',' is found, process the result */ |
| 11395 | if (*al == ',') |
| 11396 | goto process_value; |
| 11397 | |
| 11398 | /* If the character is different from ';', look |
| 11399 | * for the end of the header part in best effort. |
| 11400 | */ |
| 11401 | if (*al != ';') |
| 11402 | goto expect_comma; |
| 11403 | |
| 11404 | /* Assumes that the char is ';', now expect "q=". */ |
| 11405 | al++; |
| 11406 | |
| 11407 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11408 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11409 | al++; |
| 11410 | if (al >= end) |
| 11411 | goto process_value; |
| 11412 | |
| 11413 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11414 | if (*al != 'q') |
| 11415 | goto process_value; |
| 11416 | al++; |
| 11417 | |
| 11418 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11419 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11420 | al++; |
| 11421 | if (al >= end) |
| 11422 | goto process_value; |
| 11423 | |
| 11424 | /* Expect '='. If no '=', continue in best effort */ |
| 11425 | if (*al != '=') |
| 11426 | goto process_value; |
| 11427 | al++; |
| 11428 | |
| 11429 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11430 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11431 | al++; |
| 11432 | if (al >= end) |
| 11433 | goto process_value; |
| 11434 | |
| 11435 | /* Parse the q value. */ |
| 11436 | qvalue = parse_qvalue(al, &al); |
| 11437 | |
| 11438 | process_value: |
| 11439 | |
| 11440 | /* If the new q value is the best q value, then store the associated |
| 11441 | * language in the response. If qvalue is the biggest value (1000), |
| 11442 | * break the process. |
| 11443 | */ |
| 11444 | if (qvalue > best_q) { |
| 11445 | smp->data.str.str = (char *)w; |
| 11446 | smp->data.str.len = str - w; |
| 11447 | if (qvalue >= 1000) |
| 11448 | break; |
| 11449 | best_q = qvalue; |
| 11450 | } |
| 11451 | |
| 11452 | expect_comma: |
| 11453 | |
| 11454 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11455 | while (al < end && *al != ',') |
| 11456 | al++; |
| 11457 | if (al >= end) |
| 11458 | break; |
| 11459 | |
| 11460 | /* Comma is found, jump it and restart the analyzer. */ |
| 11461 | al++; |
| 11462 | } |
| 11463 | |
| 11464 | /* Set default value if required. */ |
| 11465 | if (smp->data.str.len == 0 && args[1].type == ARGT_STR) { |
| 11466 | smp->data.str.str = args[1].data.str.str; |
| 11467 | smp->data.str.len = args[1].data.str.len; |
| 11468 | } |
| 11469 | |
| 11470 | /* Return true only if a matching language was found. */ |
| 11471 | return smp->data.str.len != 0; |
| 11472 | } |
| 11473 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 11474 | /* |
| 11475 | * Return the struct http_req_action_kw associated to a keyword. |
| 11476 | */ |
| 11477 | struct http_req_action_kw *action_http_req_custom(const char *kw) |
| 11478 | { |
| 11479 | if (!LIST_ISEMPTY(&http_req_keywords.list)) { |
| 11480 | struct http_req_action_kw_list *kw_list; |
| 11481 | int i; |
| 11482 | |
| 11483 | list_for_each_entry(kw_list, &http_req_keywords.list, list) { |
| 11484 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11485 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11486 | return &kw_list->kw[i]; |
| 11487 | } |
| 11488 | } |
| 11489 | } |
| 11490 | return NULL; |
| 11491 | } |
| 11492 | |
| 11493 | /* |
| 11494 | * Return the struct http_res_action_kw associated to a keyword. |
| 11495 | */ |
| 11496 | struct http_res_action_kw *action_http_res_custom(const char *kw) |
| 11497 | { |
| 11498 | if (!LIST_ISEMPTY(&http_res_keywords.list)) { |
| 11499 | struct http_res_action_kw_list *kw_list; |
| 11500 | int i; |
| 11501 | |
| 11502 | list_for_each_entry(kw_list, &http_res_keywords.list, list) { |
| 11503 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11504 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11505 | return &kw_list->kw[i]; |
| 11506 | } |
| 11507 | } |
| 11508 | } |
| 11509 | return NULL; |
| 11510 | } |
| 11511 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11512 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11513 | /* All supported ACL keywords must be declared here. */ |
| 11514 | /************************************************************************/ |
| 11515 | |
| 11516 | /* Note: must not be declared <const> as its list will be overwritten. |
| 11517 | * Please take care of keeping this list alphabetically sorted. |
| 11518 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11519 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11520 | { "base", "base", PAT_MATCH_STR }, |
| 11521 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 11522 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 11523 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 11524 | { "base_end", "base", PAT_MATCH_END }, |
| 11525 | { "base_len", "base", PAT_MATCH_LEN }, |
| 11526 | { "base_reg", "base", PAT_MATCH_REG }, |
| 11527 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11528 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11529 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 11530 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 11531 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 11532 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 11533 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 11534 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 11535 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 11536 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11537 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11538 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 11539 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 11540 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 11541 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 11542 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 11543 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 11544 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 11545 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11546 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11547 | /* these two declarations uses strings with list storage (in place |
| 11548 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 11549 | * and delete functions are relative to the list management. The parse |
| 11550 | * and match method are related to the corresponding fetch methods. This |
| 11551 | * is very particular ACL declaration mode. |
| 11552 | */ |
| 11553 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 11554 | { "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] | 11555 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11556 | { "path", "path", PAT_MATCH_STR }, |
| 11557 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 11558 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 11559 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 11560 | { "path_end", "path", PAT_MATCH_END }, |
| 11561 | { "path_len", "path", PAT_MATCH_LEN }, |
| 11562 | { "path_reg", "path", PAT_MATCH_REG }, |
| 11563 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11564 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11565 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 11566 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11567 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11568 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 11569 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 11570 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 11571 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 11572 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 11573 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 11574 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 11575 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11576 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11577 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 11578 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 11579 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 11580 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 11581 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 11582 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 11583 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 11584 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11585 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11586 | { "url", "url", PAT_MATCH_STR }, |
| 11587 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 11588 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 11589 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 11590 | { "url_end", "url", PAT_MATCH_END }, |
| 11591 | { "url_len", "url", PAT_MATCH_LEN }, |
| 11592 | { "url_reg", "url", PAT_MATCH_REG }, |
| 11593 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11594 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11595 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 11596 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 11597 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 11598 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 11599 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 11600 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 11601 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 11602 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11603 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 11604 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11605 | }}; |
| 11606 | |
| 11607 | /************************************************************************/ |
| 11608 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11609 | /************************************************************************/ |
| 11610 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11611 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11612 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11613 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11614 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 11615 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11616 | /* capture are allocated and are permanent in the session */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11617 | { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRQHP }, |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11618 | |
| 11619 | /* retrieve these captures from the HTTP logs */ |
| 11620 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11621 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11622 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11623 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11624 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11625 | { "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] | 11626 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11627 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 11628 | * are only here to match the ACL's name, are request-only and are used |
| 11629 | * for ACL compatibility only. |
| 11630 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11631 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11632 | { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11633 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11634 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11635 | |
| 11636 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 11637 | * only here to match the ACL's name, are request-only and are used for |
| 11638 | * ACL compatibility only. |
| 11639 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11640 | { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11641 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11642 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11643 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11644 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 11645 | { "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] | 11646 | { "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] | 11647 | { "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] | 11648 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11649 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11650 | |
| 11651 | /* HTTP protocol on the request path */ |
| 11652 | { "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] | 11653 | { "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] | 11654 | |
| 11655 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11656 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11657 | { "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] | 11658 | |
| 11659 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11660 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 11661 | { "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] | 11662 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11663 | /* 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] | 11664 | { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11665 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11666 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11667 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11668 | { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 11669 | { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11670 | { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11671 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11672 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11673 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11674 | |
| 11675 | /* 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] | 11676 | { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11677 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11678 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11679 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11680 | { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 11681 | { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11682 | { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11683 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11684 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 11685 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11686 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11687 | /* 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] | 11688 | { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11689 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11690 | { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11691 | { "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] | 11692 | |
| 11693 | /* 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] | 11694 | { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11695 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11696 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 11697 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11698 | |
| 11699 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11700 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11701 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11702 | { "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] | 11703 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11704 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11705 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11706 | { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11707 | { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11708 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11709 | }}; |
| 11710 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11711 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11712 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 11713 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11714 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR}, |
| 11715 | { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR}, |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11716 | { NULL, NULL, 0, 0, 0 }, |
| 11717 | }}; |
| 11718 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11719 | __attribute__((constructor)) |
| 11720 | static void __http_protocol_init(void) |
| 11721 | { |
| 11722 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11723 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11724 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11725 | } |
| 11726 | |
| 11727 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 11728 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 11729 | * Local variables: |
| 11730 | * c-indent-level: 8 |
| 11731 | * c-basic-offset: 8 |
| 11732 | * End: |
| 11733 | */ |