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); |
| 1405 | if (!p || p == h) |
| 1406 | return 0; |
| 1407 | |
| 1408 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1409 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1410 | |
| 1411 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1412 | |
| 1413 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1414 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1415 | |
| 1416 | if (len < 0) |
| 1417 | return 0; |
| 1418 | |
| 1419 | |
| 1420 | get_http_auth_buff[len] = '\0'; |
| 1421 | |
| 1422 | p = strchr(get_http_auth_buff, ':'); |
| 1423 | |
| 1424 | if (!p) |
| 1425 | return 0; |
| 1426 | |
| 1427 | txn->auth.user = get_http_auth_buff; |
| 1428 | *p = '\0'; |
| 1429 | txn->auth.pass = p+1; |
| 1430 | |
| 1431 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1432 | return 1; |
| 1433 | } |
| 1434 | |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1438 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1439 | /* |
| 1440 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1441 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1442 | * ensuring that the message does not wrap. The function can be preempted |
| 1443 | * everywhere when data are missing and recalled at the exact same location |
| 1444 | * with no information loss. The message may even be realigned between two |
| 1445 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1446 | * 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] | 1447 | * fields. Note that msg->sol will be initialized after completing the first |
| 1448 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1449 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1450 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1451 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1452 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1453 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1454 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1455 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1456 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1457 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1458 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1459 | ptr = buf->p + msg->next; |
| 1460 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1461 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1462 | if (unlikely(ptr >= end)) |
| 1463 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1464 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1465 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1466 | /* |
| 1467 | * First, states that are specific to the response only. |
| 1468 | * We check them first so that request and headers are |
| 1469 | * closer to each other (accessed more often). |
| 1470 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1471 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1472 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1473 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1474 | /* we have a start of message, but we have to check |
| 1475 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1476 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1477 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1478 | if (unlikely(ptr != buf->p)) { |
| 1479 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1480 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1481 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1482 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1483 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1484 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1485 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1486 | hdr_idx_init(idx); |
| 1487 | state = HTTP_MSG_RPVER; |
| 1488 | goto http_msg_rpver; |
| 1489 | } |
| 1490 | |
| 1491 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1492 | goto http_msg_invalid; |
| 1493 | |
| 1494 | if (unlikely(*ptr == '\n')) |
| 1495 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1496 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1497 | /* stop here */ |
| 1498 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1499 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1500 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1501 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1502 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1503 | /* stop here */ |
| 1504 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1505 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1506 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1507 | case HTTP_MSG_RPVER_SP: |
| 1508 | case HTTP_MSG_RPCODE: |
| 1509 | case HTTP_MSG_RPCODE_SP: |
| 1510 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1511 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1512 | state, ptr, end, |
| 1513 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1514 | if (unlikely(!ptr)) |
| 1515 | return; |
| 1516 | |
| 1517 | /* we have a full response and we know that we have either a CR |
| 1518 | * or an LF at <ptr>. |
| 1519 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1520 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1521 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1522 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1523 | if (likely(*ptr == '\r')) |
| 1524 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1525 | goto http_msg_rpline_end; |
| 1526 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1527 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1528 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1529 | /* msg->sol must point to the first of CR or LF. */ |
| 1530 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1531 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1532 | /* stop here */ |
| 1533 | |
| 1534 | /* |
| 1535 | * Second, states that are specific to the request only |
| 1536 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1537 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1538 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1539 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1540 | /* we have a start of message, but we have to check |
| 1541 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1542 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1543 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1544 | if (likely(ptr != buf->p)) { |
| 1545 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1546 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1547 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1548 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1549 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1550 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1551 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1552 | state = HTTP_MSG_RQMETH; |
| 1553 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1554 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1555 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1556 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1557 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1558 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1559 | if (unlikely(*ptr == '\n')) |
| 1560 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1561 | 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] | 1562 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1563 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1564 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1565 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1566 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1567 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1568 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1569 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1570 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1571 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1572 | case HTTP_MSG_RQMETH_SP: |
| 1573 | case HTTP_MSG_RQURI: |
| 1574 | case HTTP_MSG_RQURI_SP: |
| 1575 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1576 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1577 | state, ptr, end, |
| 1578 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1579 | if (unlikely(!ptr)) |
| 1580 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1581 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1582 | /* we have a full request and we know that we have either a CR |
| 1583 | * or an LF at <ptr>. |
| 1584 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1585 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1586 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1587 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1588 | if (likely(*ptr == '\r')) |
| 1589 | 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] | 1590 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1591 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1592 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1593 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1594 | /* check for HTTP/0.9 request : no version information available. |
| 1595 | * msg->sol must point to the first of CR or LF. |
| 1596 | */ |
| 1597 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1598 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1599 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1600 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1601 | 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] | 1602 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1603 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1604 | /* |
| 1605 | * Common states below |
| 1606 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1607 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1608 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1609 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1610 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1611 | goto http_msg_hdr_name; |
| 1612 | } |
| 1613 | |
| 1614 | if (likely(*ptr == '\r')) |
| 1615 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1616 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1617 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1618 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1619 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1620 | /* assumes msg->sol points to the first char */ |
| 1621 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1622 | 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] | 1623 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1624 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1625 | 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] | 1626 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1627 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1628 | goto http_msg_invalid; |
| 1629 | |
| 1630 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1631 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1632 | |
| 1633 | /* and we still accept this non-token character */ |
| 1634 | 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] | 1635 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1636 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1637 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1638 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1639 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1640 | 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] | 1641 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1642 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1643 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1644 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1645 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1646 | goto http_msg_hdr_val; |
| 1647 | } |
| 1648 | |
| 1649 | if (likely(*ptr == '\r')) |
| 1650 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1651 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1652 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1653 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1654 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1655 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1656 | 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] | 1657 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1658 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1659 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1660 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1661 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1662 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1663 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1664 | goto http_msg_hdr_l1_sp; |
| 1665 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1666 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1667 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1668 | goto http_msg_complete_header; |
| 1669 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1670 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1671 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1672 | /* assumes msg->sol points to the first char, and msg->sov |
| 1673 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1674 | */ |
| 1675 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1676 | 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] | 1677 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1678 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1679 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1680 | * real header end in case it ends with lots of LWS, but is this |
| 1681 | * really needed ? |
| 1682 | */ |
| 1683 | if (likely(*ptr == '\r')) |
| 1684 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1685 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1686 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1687 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1688 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1689 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1690 | 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] | 1691 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1692 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1693 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1694 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1695 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1696 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1697 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1698 | goto http_msg_hdr_val; |
| 1699 | } |
| 1700 | http_msg_complete_header: |
| 1701 | /* |
| 1702 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1703 | * Assumes msg->sol points to the first char, msg->sov points |
| 1704 | * to the first character of the value and msg->eol to the |
| 1705 | * first CR or LF so we know how the line ends. We insert last |
| 1706 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1707 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1708 | 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] | 1709 | idx, idx->tail) < 0)) |
| 1710 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1711 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1712 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1713 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1714 | goto http_msg_hdr_name; |
| 1715 | } |
| 1716 | |
| 1717 | if (likely(*ptr == '\r')) |
| 1718 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1719 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1720 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1721 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1722 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1723 | /* Assumes msg->sol points to the first of either CR or LF. |
| 1724 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 1725 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 1726 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1727 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1728 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1729 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1730 | msg->eoh = msg->sol; |
| 1731 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1732 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1733 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1734 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1735 | |
| 1736 | case HTTP_MSG_ERROR: |
| 1737 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1738 | break; |
| 1739 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1740 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1741 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1742 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1743 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1744 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1745 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1746 | } |
| 1747 | http_msg_ood: |
| 1748 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1749 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1750 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1751 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1752 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1753 | http_msg_invalid: |
| 1754 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1755 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1756 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1757 | return; |
| 1758 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1759 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1760 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1761 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1762 | * nothing is done and 1 is returned. |
| 1763 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1764 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1765 | { |
| 1766 | int delta; |
| 1767 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1768 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1769 | |
| 1770 | if (msg->sl.rq.v_l != 0) |
| 1771 | return 1; |
| 1772 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1773 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1774 | if (txn->meth != HTTP_METH_GET) |
| 1775 | return 0; |
| 1776 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1777 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1778 | delta = 0; |
| 1779 | |
| 1780 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1781 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1782 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1783 | } |
| 1784 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1785 | 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] | 1786 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1787 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1788 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1789 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1790 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1791 | NULL, NULL); |
| 1792 | if (unlikely(!cur_end)) |
| 1793 | return 0; |
| 1794 | |
| 1795 | /* we have a full HTTP/1.0 request now and we know that |
| 1796 | * we have either a CR or an LF at <ptr>. |
| 1797 | */ |
| 1798 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1799 | return 1; |
| 1800 | } |
| 1801 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1802 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1803 | * and "keep-alive" values. If we already know that some headers may safely |
| 1804 | * 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] | 1805 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1806 | * - 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] | 1807 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1808 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1809 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1810 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1811 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1812 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1813 | 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] | 1814 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1815 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1816 | const char *hdr_val = "Connection"; |
| 1817 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1818 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1819 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1820 | return; |
| 1821 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1822 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1823 | hdr_val = "Proxy-Connection"; |
| 1824 | hdr_len = 16; |
| 1825 | } |
| 1826 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1827 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1828 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1829 | 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] | 1830 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1831 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1832 | if (to_del & 2) |
| 1833 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1834 | else |
| 1835 | txn->flags |= TX_CON_KAL_SET; |
| 1836 | } |
| 1837 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1838 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1839 | if (to_del & 1) |
| 1840 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1841 | else |
| 1842 | txn->flags |= TX_CON_CLO_SET; |
| 1843 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1844 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1845 | txn->flags |= TX_HDR_CONN_UPG; |
| 1846 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1847 | } |
| 1848 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1849 | txn->flags |= TX_HDR_CONN_PRS; |
| 1850 | return; |
| 1851 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1852 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1853 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1854 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1855 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1856 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1857 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1858 | 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] | 1859 | { |
| 1860 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1861 | const char *hdr_val = "Connection"; |
| 1862 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1863 | |
| 1864 | ctx.idx = 0; |
| 1865 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1866 | |
| 1867 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1868 | hdr_val = "Proxy-Connection"; |
| 1869 | hdr_len = 16; |
| 1870 | } |
| 1871 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1872 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1873 | 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] | 1874 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1875 | if (wanted & TX_CON_KAL_SET) |
| 1876 | txn->flags |= TX_CON_KAL_SET; |
| 1877 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1878 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1879 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1880 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1881 | if (wanted & TX_CON_CLO_SET) |
| 1882 | txn->flags |= TX_CON_CLO_SET; |
| 1883 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1884 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1885 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1886 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1887 | |
| 1888 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1889 | return; |
| 1890 | |
| 1891 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1892 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1893 | hdr_val = "Connection: close"; |
| 1894 | hdr_len = 17; |
| 1895 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1896 | hdr_val = "Proxy-Connection: close"; |
| 1897 | hdr_len = 23; |
| 1898 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1899 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1903 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1904 | hdr_val = "Connection: keep-alive"; |
| 1905 | hdr_len = 22; |
| 1906 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1907 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1908 | hdr_len = 28; |
| 1909 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1910 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1911 | } |
| 1912 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1913 | } |
| 1914 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 1915 | /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to |
| 1916 | * the first byte of data after the chunk size, so that we know we can forward |
| 1917 | * exactly msg->next bytes. msg->sol contains the exact number of bytes forming |
| 1918 | * the chunk size. That way it is always possible to differentiate between the |
| 1919 | * start of the body and the start of the data. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1920 | * 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] | 1921 | * 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] | 1922 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1923 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1924 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1925 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1926 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1927 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1928 | const char *end = buf->data + buf->size; |
| 1929 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1930 | unsigned int chunk = 0; |
| 1931 | |
| 1932 | /* The chunk size is in the following form, though we are only |
| 1933 | * interested in the size and CRLF : |
| 1934 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1935 | */ |
| 1936 | while (1) { |
| 1937 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1938 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1939 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1940 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1941 | if (c < 0) /* not a hex digit anymore */ |
| 1942 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1943 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1944 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1945 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1946 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1947 | chunk = (chunk << 4) + c; |
| 1948 | } |
| 1949 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1950 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1951 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1952 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1953 | |
| 1954 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1955 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1956 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1957 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1958 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1959 | } |
| 1960 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1961 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1962 | * for the end of chunk size. |
| 1963 | */ |
| 1964 | while (1) { |
| 1965 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1966 | /* we now have a CR or an LF at ptr */ |
| 1967 | if (likely(*ptr == '\r')) { |
| 1968 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1969 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1970 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1971 | return 0; |
| 1972 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1973 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1974 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1975 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1976 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1977 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1978 | /* done */ |
| 1979 | break; |
| 1980 | } |
| 1981 | else if (*ptr == ';') { |
| 1982 | /* chunk extension, ends at next CRLF */ |
| 1983 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1984 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1985 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1986 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1987 | |
| 1988 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1989 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1990 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1991 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1992 | return 0; |
| 1993 | } |
| 1994 | /* we have a CRLF now, loop above */ |
| 1995 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1996 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1997 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1998 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1999 | } |
| 2000 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2001 | /* 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] | 2002 | * which may or may not be present. We save that into ->next, |
| 2003 | * and the number of bytes parsed into msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2004 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2005 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2006 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2007 | msg->sol += buf->size; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2008 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2009 | msg->chunk_len = chunk; |
| 2010 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2011 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2012 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2013 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2014 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2015 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2016 | } |
| 2017 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2018 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2019 | * 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] | 2020 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2021 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2022 | * If not enough data are available, the function does not change anything |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2023 | * except maybe msg->next if it could parse some lines, and returns zero. |
| 2024 | * If a parse error is encountered, the function returns < 0 and does not |
| 2025 | * change anything except maybe msg->next. Note that the message must |
| 2026 | * already be in HTTP_MSG_TRAILERS state before calling this function, |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2027 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2028 | * forwarding, and that msg->next exactly matches the length of trailers |
| 2029 | * already parsed and not forwarded. It is also important to note that this |
| 2030 | * 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] | 2031 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2032 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2033 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2034 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2035 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2036 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2037 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2038 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2039 | const char *ptr = b_ptr(buf, msg->next); |
| 2040 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2041 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2042 | |
| 2043 | /* scan current line and stop at LF or CRLF */ |
| 2044 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2045 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2046 | return 0; |
| 2047 | |
| 2048 | if (*ptr == '\n') { |
| 2049 | if (!p1) |
| 2050 | p1 = ptr; |
| 2051 | p2 = ptr; |
| 2052 | break; |
| 2053 | } |
| 2054 | |
| 2055 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2056 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2057 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2058 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2059 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2060 | p1 = ptr; |
| 2061 | } |
| 2062 | |
| 2063 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2064 | if (ptr >= buf->data + buf->size) |
| 2065 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | /* after LF; point to beginning of next line */ |
| 2069 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2070 | if (p2 >= buf->data + buf->size) |
| 2071 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2072 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2073 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2074 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2075 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2076 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2077 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2078 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2079 | * Everything was scheduled for forwarding, there's nothing |
| 2080 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2081 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2082 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2083 | msg->msg_state = HTTP_MSG_DONE; |
| 2084 | return 1; |
| 2085 | } |
| 2086 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2087 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2088 | } |
| 2089 | } |
| 2090 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2091 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF |
| 2092 | * or a possible LF alone at the end of a chunk. It automatically adjusts |
| 2093 | * 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] | 2094 | * 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] | 2095 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2096 | * not enough data are available, the function does not change anything and |
| 2097 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2098 | * does not change anything. Note: this function is designed to parse wrapped |
| 2099 | * CRLF at the end of the buffer. |
| 2100 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2101 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2102 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2103 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2104 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2105 | int bytes; |
| 2106 | |
| 2107 | /* NB: we'll check data availabilty at the end. It's not a |
| 2108 | * problem because whatever we match first will be checked |
| 2109 | * against the correct length. |
| 2110 | */ |
| 2111 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2112 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2113 | if (*ptr == '\r') { |
| 2114 | bytes++; |
| 2115 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2116 | if (ptr >= buf->data + buf->size) |
| 2117 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2118 | } |
| 2119 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2120 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2121 | return 0; |
| 2122 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2123 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2124 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2125 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2126 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2127 | |
| 2128 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2129 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2130 | ptr = buf->data; |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2131 | /* Advance ->next to allow the CRLF to be forwarded */ |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2132 | msg->next += bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2133 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2134 | return 1; |
| 2135 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2136 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2137 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2138 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2139 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2140 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2141 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2142 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2143 | { |
| 2144 | int q = 1000; |
| 2145 | |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2146 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2147 | goto out; |
| 2148 | q = (*qvalue++ - '0') * 1000; |
| 2149 | |
| 2150 | if (*qvalue++ != '.') |
| 2151 | goto out; |
| 2152 | |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2153 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2154 | goto out; |
| 2155 | q += (*qvalue++ - '0') * 100; |
| 2156 | |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2157 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2158 | goto out; |
| 2159 | q += (*qvalue++ - '0') * 10; |
| 2160 | |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2161 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2162 | goto out; |
| 2163 | q += (*qvalue++ - '0') * 1; |
| 2164 | out: |
| 2165 | if (q > 1000) |
| 2166 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2167 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2168 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2169 | return q; |
| 2170 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2171 | |
| 2172 | /* |
| 2173 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2174 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2175 | int select_compression_request_header(struct session *s, struct buffer *req) |
| 2176 | { |
| 2177 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2178 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2179 | struct hdr_ctx ctx; |
| 2180 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2181 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2182 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2183 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 2184 | * 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] | 2185 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 2186 | */ |
| 2187 | ctx.idx = 0; |
| 2188 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2189 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2190 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2191 | (ctx.vlen < 31 || |
| 2192 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2193 | ctx.line[ctx.val + 30] < '6' || |
| 2194 | (ctx.line[ctx.val + 30] == '6' && |
| 2195 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2196 | s->comp_algo = NULL; |
| 2197 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2198 | } |
| 2199 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2200 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2201 | 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] | 2202 | int best_q = 0; |
| 2203 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2204 | ctx.idx = 0; |
| 2205 | 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] | 2206 | const char *qval; |
| 2207 | int q; |
| 2208 | int toklen; |
| 2209 | |
| 2210 | /* try to isolate the token from the optional q-value */ |
| 2211 | toklen = 0; |
| 2212 | while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)]) |
| 2213 | toklen++; |
| 2214 | |
| 2215 | qval = ctx.line + ctx.val + toklen; |
| 2216 | while (1) { |
| 2217 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2218 | qval++; |
| 2219 | |
| 2220 | if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') { |
| 2221 | qval = NULL; |
| 2222 | break; |
| 2223 | } |
| 2224 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2225 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2226 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2227 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2228 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2229 | if (qval >= ctx.line + ctx.val + ctx.vlen) { |
| 2230 | qval = NULL; |
| 2231 | break; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2232 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2233 | if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0) |
| 2234 | break; |
| 2235 | |
| 2236 | while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';') |
| 2237 | qval++; |
| 2238 | } |
| 2239 | |
| 2240 | /* 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] | 2241 | q = qval ? parse_qvalue(qval + 2, NULL) : 1000; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2242 | |
| 2243 | if (q <= best_q) |
| 2244 | continue; |
| 2245 | |
| 2246 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 2247 | if (*(ctx.line + ctx.val) == '*' || |
| 2248 | word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) { |
| 2249 | s->comp_algo = comp_algo; |
| 2250 | best_q = q; |
| 2251 | break; |
| 2252 | } |
| 2253 | } |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2258 | if (s->comp_algo) { |
| 2259 | if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) { |
| 2260 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2261 | ctx.idx = 0; |
| 2262 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2263 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2264 | } |
| 2265 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2266 | return 1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | /* identity is implicit does not require headers */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2270 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
| 2271 | 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] | 2272 | if (comp_algo->add_data == identity_add_data) { |
| 2273 | s->comp_algo = comp_algo; |
| 2274 | return 1; |
| 2275 | } |
| 2276 | } |
| 2277 | } |
| 2278 | |
| 2279 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2280 | return 0; |
| 2281 | } |
| 2282 | |
| 2283 | /* |
| 2284 | * Selects a comression algorithm depending of the server response. |
| 2285 | */ |
| 2286 | int select_compression_response_header(struct session *s, struct buffer *res) |
| 2287 | { |
| 2288 | struct http_txn *txn = &s->txn; |
| 2289 | struct http_msg *msg = &txn->rsp; |
| 2290 | struct hdr_ctx ctx; |
| 2291 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2292 | |
| 2293 | /* no common compression algorithm was found in request header */ |
| 2294 | if (s->comp_algo == NULL) |
| 2295 | goto fail; |
| 2296 | |
| 2297 | /* HTTP < 1.1 should not be compressed */ |
Willy Tarreau | 7257550 | 2013-12-24 14:41:35 +0100 | [diff] [blame] | 2298 | 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] | 2299 | goto fail; |
| 2300 | |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2301 | /* 200 only */ |
| 2302 | if (txn->status != 200) |
| 2303 | goto fail; |
| 2304 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2305 | /* Content-Length is null */ |
| 2306 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2307 | goto fail; |
| 2308 | |
| 2309 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2310 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2311 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2312 | goto fail; |
| 2313 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2314 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2315 | ctx.idx = 0; |
| 2316 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2317 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2318 | goto fail; |
| 2319 | } |
| 2320 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2321 | comp_type = NULL; |
| 2322 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2323 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2324 | * not listed in the "compression type" directive if any. If no content-type was |
| 2325 | * found but configuration requires one, we don't compress either. Backend has |
| 2326 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2327 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2328 | ctx.idx = 0; |
| 2329 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2330 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2331 | goto fail; |
| 2332 | |
| 2333 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 2334 | (s->fe->comp && (comp_type = s->fe->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2335 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2336 | if (ctx.vlen >= comp_type->name_len && |
| 2337 | 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] | 2338 | /* this Content-Type should be compressed */ |
| 2339 | break; |
| 2340 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2341 | /* this Content-Type should not be compressed */ |
| 2342 | if (comp_type == NULL) |
| 2343 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2344 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2345 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2346 | else { /* no content-type header */ |
| 2347 | if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types)) |
| 2348 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2349 | } |
| 2350 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2351 | /* limit compression rate */ |
| 2352 | if (global.comp_rate_lim > 0) |
| 2353 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2354 | goto fail; |
| 2355 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2356 | /* limit cpu usage */ |
| 2357 | if (idle_pct < compress_min_idle) |
| 2358 | goto fail; |
| 2359 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2360 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2361 | 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] | 2362 | goto fail; |
| 2363 | |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2364 | s->flags |= SN_COMP_READY; |
| 2365 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2366 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2367 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2368 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2369 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2370 | |
| 2371 | /* add Transfer-Encoding header */ |
| 2372 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2373 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2374 | |
| 2375 | /* |
| 2376 | * Add Content-Encoding header when it's not identity encoding. |
| 2377 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2378 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2379 | * header. |
| 2380 | */ |
| 2381 | if (s->comp_algo->add_data != identity_add_data) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2382 | trash.len = 18; |
| 2383 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
| 2384 | memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len); |
| 2385 | trash.len += s->comp_algo->name_len; |
| 2386 | trash.str[trash.len] = '\0'; |
| 2387 | 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] | 2388 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2389 | return 1; |
| 2390 | |
| 2391 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2392 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2393 | return 0; |
| 2394 | } |
| 2395 | |
Willy Tarreau | 2e47a3a | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2396 | void http_adjust_conn_mode(struct session *s, struct http_txn *txn, struct http_msg *msg) |
| 2397 | { |
| 2398 | int tmp = TX_CON_WANT_KAL; |
| 2399 | |
| 2400 | if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 2401 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
| 2402 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2403 | tmp = TX_CON_WANT_TUN; |
| 2404 | |
| 2405 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 2406 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2407 | tmp = TX_CON_WANT_TUN; |
| 2408 | } |
| 2409 | |
| 2410 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 2411 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 2412 | /* option httpclose + server_close => forceclose */ |
| 2413 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 2414 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2415 | tmp = TX_CON_WANT_CLO; |
| 2416 | else |
| 2417 | tmp = TX_CON_WANT_SCL; |
| 2418 | } |
| 2419 | |
| 2420 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
| 2421 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 2422 | tmp = TX_CON_WANT_CLO; |
| 2423 | |
| 2424 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2425 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2426 | |
| 2427 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 2428 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2429 | /* parse the Connection header and possibly clean it */ |
| 2430 | int to_del = 0; |
| 2431 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 2432 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 2433 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
| 2434 | to_del |= 2; /* remove "keep-alive" */ |
| 2435 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2436 | to_del |= 1; /* remove "close" */ |
| 2437 | http_parse_connection_header(txn, msg, to_del); |
| 2438 | } |
| 2439 | |
| 2440 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2441 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2442 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2443 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2444 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 2445 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
| 2446 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
| 2447 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2448 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2449 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2450 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2451 | * processing can continue on next analysers, or zero if it either needs more |
| 2452 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2453 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2454 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2455 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2456 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2457 | 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] | 2458 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2459 | /* |
| 2460 | * We will parse the partial (or complete) lines. |
| 2461 | * We will check the request syntax, and also join multi-line |
| 2462 | * headers. An index of all the lines will be elaborated while |
| 2463 | * parsing. |
| 2464 | * |
| 2465 | * For the parsing, we use a 28 states FSM. |
| 2466 | * |
| 2467 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2468 | * req->buf->p = beginning of request |
| 2469 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2470 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2471 | * msg->eol = end of current header or line (LF or CRLF) |
| 2472 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2473 | * |
| 2474 | * 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] | 2475 | * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2476 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2477 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2478 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2479 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2480 | int cur_idx; |
| 2481 | struct http_txn *txn = &s->txn; |
| 2482 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2483 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2484 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2485 | 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] | 2486 | now_ms, __FUNCTION__, |
| 2487 | s, |
| 2488 | req, |
| 2489 | req->rex, req->wex, |
| 2490 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2491 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2492 | req->analysers); |
| 2493 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2494 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2495 | s->srv_error = http_return_srv_error; |
| 2496 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2497 | /* There's a protected area at the end of the buffer for rewriting |
| 2498 | * purposes. We don't want to start to parse the request if the |
| 2499 | * protected area is affected, because we may have to move processed |
| 2500 | * data later, which is much more complicated. |
| 2501 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2502 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2503 | if (txn->flags & TX_NOT_FIRST) { |
| 2504 | if (unlikely(!channel_reserved(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2505 | 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] | 2506 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2507 | /* 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] | 2508 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2509 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2510 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2511 | return 0; |
| 2512 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2513 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2514 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2515 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2516 | } |
| 2517 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2518 | /* Note that we have the same problem with the response ; we |
| 2519 | * may want to send a redirect, error or anything which requires |
| 2520 | * some spare space. So we'll ensure that we have at least |
| 2521 | * maxrewrite bytes available in the response buffer before |
| 2522 | * processing that one. This will only affect pipelined |
| 2523 | * keep-alive requests. |
| 2524 | */ |
| 2525 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2526 | unlikely(!channel_reserved(s->rep) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2527 | bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) || |
| 2528 | bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) { |
| 2529 | if (s->rep->buf->o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2530 | 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] | 2531 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2532 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2533 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2534 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2535 | s->rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2536 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2537 | return 0; |
| 2538 | } |
| 2539 | } |
| 2540 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2541 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2542 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2543 | } |
| 2544 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2545 | /* 1: we might have to print this header in debug mode */ |
| 2546 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2547 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 8767b13 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2548 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2549 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2550 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2551 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2552 | /* this is a bit complex : in case of error on the request line, |
| 2553 | * we know that rq.l is still zero, so we display only the part |
| 2554 | * up to the end of the line (truncated by debug_hdr). |
| 2555 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2556 | 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] | 2557 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2558 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2559 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2560 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2561 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2562 | while (cur_idx) { |
| 2563 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2564 | debug_hdr("clihdr", s, sol, eol); |
| 2565 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2566 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2567 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2568 | } |
| 2569 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2570 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2571 | /* |
| 2572 | * Now we quickly check if we have found a full valid request. |
| 2573 | * If not so, we check the FD and buffer states before leaving. |
| 2574 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2575 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2576 | * requests are checked first. When waiting for a second request |
| 2577 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2578 | * we note the error in the session flags but don't set any state. |
| 2579 | * Since the error will be noted there, it will not be counted by |
| 2580 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2581 | * Last, we may increase some tracked counters' http request errors on |
| 2582 | * the cases that are deliberately the client's fault. For instance, |
| 2583 | * a timeout or connection reset is not counted as an error. However |
| 2584 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2585 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2586 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2587 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2588 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2589 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2590 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2591 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2592 | session_inc_http_req_ctr(s); |
| 2593 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2594 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2595 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2596 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2597 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2598 | /* 1: Since we are in header mode, if there's no space |
| 2599 | * left for headers, we won't be able to free more |
| 2600 | * later, so the session will never terminate. We |
| 2601 | * must terminate it now. |
| 2602 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2603 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2604 | /* FIXME: check if URI is set and return Status |
| 2605 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2606 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2607 | session_inc_http_req_ctr(s); |
| 2608 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2609 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2610 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2611 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2612 | goto return_bad_req; |
| 2613 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2614 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2615 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2616 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2617 | if (!(s->flags & SN_ERR_MASK)) |
| 2618 | s->flags |= SN_ERR_CLICL; |
| 2619 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2620 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2621 | goto failed_keep_alive; |
| 2622 | |
Willy Tarreau | 1c3a612 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2623 | if (s->fe->options & PR_O_IGNORE_PRB) |
| 2624 | goto failed_keep_alive; |
| 2625 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2626 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2627 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2628 | 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] | 2629 | session_inc_http_err_ctr(s); |
| 2630 | } |
| 2631 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2632 | txn->status = 400; |
| 2633 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2634 | msg->msg_state = HTTP_MSG_ERROR; |
| 2635 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2636 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2637 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2638 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2639 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2640 | if (s->listener->counters) |
| 2641 | s->listener->counters->failed_req++; |
| 2642 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2643 | if (!(s->flags & SN_FINST_MASK)) |
| 2644 | s->flags |= SN_FINST_R; |
| 2645 | return 0; |
| 2646 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2647 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2648 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2649 | 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] | 2650 | if (!(s->flags & SN_ERR_MASK)) |
| 2651 | s->flags |= SN_ERR_CLITO; |
| 2652 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2653 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2654 | goto failed_keep_alive; |
| 2655 | |
Willy Tarreau | 1c3a612 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2656 | if (s->fe->options & PR_O_IGNORE_PRB) |
| 2657 | goto failed_keep_alive; |
| 2658 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2659 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2660 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2661 | 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] | 2662 | session_inc_http_err_ctr(s); |
| 2663 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2664 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2665 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2666 | msg->msg_state = HTTP_MSG_ERROR; |
| 2667 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2668 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2669 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2670 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2671 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2672 | if (s->listener->counters) |
| 2673 | s->listener->counters->failed_req++; |
| 2674 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2675 | if (!(s->flags & SN_FINST_MASK)) |
| 2676 | s->flags |= SN_FINST_R; |
| 2677 | return 0; |
| 2678 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2679 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2680 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2681 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2682 | if (!(s->flags & SN_ERR_MASK)) |
| 2683 | s->flags |= SN_ERR_CLICL; |
| 2684 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2685 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2686 | goto failed_keep_alive; |
| 2687 | |
Willy Tarreau | 1c3a612 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2688 | if (s->fe->options & PR_O_IGNORE_PRB) |
| 2689 | goto failed_keep_alive; |
| 2690 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2691 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2692 | 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] | 2693 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2694 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2695 | msg->msg_state = HTTP_MSG_ERROR; |
| 2696 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2697 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2698 | session_inc_http_err_ctr(s); |
| 2699 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2700 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2701 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2702 | if (s->listener->counters) |
| 2703 | s->listener->counters->failed_req++; |
| 2704 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2705 | if (!(s->flags & SN_FINST_MASK)) |
| 2706 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2707 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2708 | } |
| 2709 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2710 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2711 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2712 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2713 | #ifdef TCP_QUICKACK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 2714 | 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] | 2715 | /* We need more data, we have to re-enable quick-ack in case we |
| 2716 | * previously disabled it, otherwise we might cause the client |
| 2717 | * to delay next data. |
| 2718 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2719 | 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] | 2720 | } |
| 2721 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2722 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2723 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2724 | /* If the client starts to talk, let's fall back to |
| 2725 | * request timeout processing. |
| 2726 | */ |
| 2727 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2728 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2729 | } |
| 2730 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2731 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2732 | if (!tick_isset(req->analyse_exp)) { |
| 2733 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2734 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2735 | tick_isset(s->be->timeout.httpka)) |
| 2736 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2737 | else |
| 2738 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2739 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2740 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2741 | /* we're not ready yet */ |
| 2742 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2743 | |
| 2744 | failed_keep_alive: |
| 2745 | /* Here we process low-level errors for keep-alive requests. In |
| 2746 | * short, if the request is not the first one and it experiences |
| 2747 | * a timeout, read error or shutdown, we just silently close so |
| 2748 | * that the client can try again. |
| 2749 | */ |
| 2750 | txn->status = 0; |
| 2751 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2752 | req->analysers = 0; |
| 2753 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2754 | s->logs.level = 0; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2755 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2756 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2757 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2758 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2759 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2760 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2761 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2762 | * 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] | 2763 | * 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] | 2764 | * 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] | 2765 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2766 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2767 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2768 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2769 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2770 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2771 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2772 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2773 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2774 | /* kill the pending keep-alive timeout */ |
| 2775 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2776 | req->analyse_exp = TICK_ETERNITY; |
| 2777 | } |
| 2778 | |
| 2779 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2780 | /* Maybe we found in invalid header name while we were configured not |
| 2781 | * to block on that, so we have to capture it now. |
| 2782 | */ |
| 2783 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2784 | 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] | 2785 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2786 | /* |
| 2787 | * 1: identify the method |
| 2788 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2789 | 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] | 2790 | |
| 2791 | /* we can make use of server redirect on GET and HEAD */ |
| 2792 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2793 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2794 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2795 | /* |
| 2796 | * 2: check if the URI matches the monitor_uri. |
| 2797 | * We have to do this for every request which gets in, because |
| 2798 | * the monitor-uri is defined by the frontend. |
| 2799 | */ |
| 2800 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2801 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2802 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2803 | s->fe->monitor_uri, |
| 2804 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2805 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2806 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2807 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2808 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2809 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2810 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2811 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2812 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2813 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2814 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2815 | 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] | 2816 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2817 | ret = acl_pass(ret); |
| 2818 | if (cond->pol == ACL_COND_UNLESS) |
| 2819 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2820 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2821 | if (ret) { |
| 2822 | /* we fail this request, let's return 503 service unavail */ |
| 2823 | txn->status = 503; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2824 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2825 | if (!(s->flags & SN_ERR_MASK)) |
| 2826 | 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] | 2827 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2828 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2829 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2830 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2831 | /* nothing to fail, let's reply normaly */ |
| 2832 | txn->status = 200; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2833 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2834 | if (!(s->flags & SN_ERR_MASK)) |
| 2835 | 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] | 2836 | goto return_prx_cond; |
| 2837 | } |
| 2838 | |
| 2839 | /* |
| 2840 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2841 | * Note: we cannot log anymore if the request has been |
| 2842 | * classified as invalid. |
| 2843 | */ |
| 2844 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2845 | /* we have a complete HTTP request that we must log */ |
| 2846 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2847 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2848 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2849 | if (urilen >= REQURI_LEN) |
| 2850 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2851 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2852 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2853 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2854 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2855 | s->do_log(s); |
| 2856 | } else { |
| 2857 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2858 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2859 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2860 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2861 | /* 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] | 2862 | 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] | 2863 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2864 | |
Willy Tarreau | 5564555 | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2865 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 2866 | * exactly one digit "." one digit. This check may be disabled using |
| 2867 | * option accept-invalid-http-request. |
| 2868 | */ |
| 2869 | if (!(s->fe->options2 & PR_O2_REQBUG_OK)) { |
| 2870 | if (msg->sl.rq.v_l != 8) { |
| 2871 | msg->err_pos = msg->sl.rq.v; |
| 2872 | goto return_bad_req; |
| 2873 | } |
| 2874 | |
| 2875 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 2876 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 2877 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 2878 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 2879 | msg->err_pos = msg->sl.rq.v + 4; |
| 2880 | goto return_bad_req; |
| 2881 | } |
| 2882 | } |
| 2883 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2884 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2885 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2886 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2887 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2888 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2889 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2890 | |
| 2891 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2892 | 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] | 2893 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2894 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2895 | * we have what looks like a proxied connection instead of a connection, |
| 2896 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2897 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2898 | * happen to do that despite being non-standard :-( |
| 2899 | * We consider that a request not beginning with either '/' or '*' is |
| 2900 | * a proxied connection, which covers both "scheme://location" and |
| 2901 | * CONNECT ip:port. |
| 2902 | */ |
| 2903 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2904 | 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] | 2905 | txn->flags |= TX_USE_PX_CONN; |
| 2906 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2907 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2908 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2909 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2910 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2911 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2912 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2913 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2914 | |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2915 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 2916 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2917 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2918 | * The length of a message body is determined by one of the following |
| 2919 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2920 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2921 | * 1. Any response to a HEAD request and any response with a 1xx |
| 2922 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 2923 | * code is always terminated by the first empty line after the |
| 2924 | * header fields, regardless of the header fields present in the |
| 2925 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2926 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2927 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 2928 | * the connection will become a tunnel immediately after the empty |
| 2929 | * line that concludes the header fields. A client MUST ignore any |
| 2930 | * Content-Length or Transfer-Encoding header fields received in |
| 2931 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2932 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2933 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 2934 | * transfer coding (Section 4.1) is the final encoding, the message |
| 2935 | * body length is determined by reading and decoding the chunked |
| 2936 | * data until the transfer coding indicates the data is complete. |
| 2937 | * |
| 2938 | * If a Transfer-Encoding header field is present in a response and |
| 2939 | * the chunked transfer coding is not the final encoding, the |
| 2940 | * message body length is determined by reading the connection until |
| 2941 | * it is closed by the server. If a Transfer-Encoding header field |
| 2942 | * is present in a request and the chunked transfer coding is not |
| 2943 | * the final encoding, the message body length cannot be determined |
| 2944 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 2945 | * status code and then close the connection. |
| 2946 | * |
| 2947 | * If a message is received with both a Transfer-Encoding and a |
| 2948 | * Content-Length header field, the Transfer-Encoding overrides the |
| 2949 | * Content-Length. Such a message might indicate an attempt to |
| 2950 | * perform request smuggling (Section 9.5) or response splitting |
| 2951 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 2952 | * remove the received Content-Length field prior to forwarding such |
| 2953 | * a message downstream. |
| 2954 | * |
| 2955 | * 4. If a message is received without Transfer-Encoding and with |
| 2956 | * either multiple Content-Length header fields having differing |
| 2957 | * field-values or a single Content-Length header field having an |
| 2958 | * invalid value, then the message framing is invalid and the |
| 2959 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 2960 | * request message, the server MUST respond with a 400 (Bad Request) |
| 2961 | * status code and then close the connection. If this is a response |
| 2962 | * message received by a proxy, the proxy MUST close the connection |
| 2963 | * to the server, discard the received response, and send a 502 (Bad |
| 2964 | * Gateway) response to the client. If this is a response message |
| 2965 | * received by a user agent, the user agent MUST close the |
| 2966 | * connection to the server and discard the received response. |
| 2967 | * |
| 2968 | * 5. If a valid Content-Length header field is present without |
| 2969 | * Transfer-Encoding, its decimal value defines the expected message |
| 2970 | * body length in octets. If the sender closes the connection or |
| 2971 | * the recipient times out before the indicated number of octets are |
| 2972 | * received, the recipient MUST consider the message to be |
| 2973 | * incomplete and close the connection. |
| 2974 | * |
| 2975 | * 6. If this is a request message and none of the above are true, then |
| 2976 | * the message body length is zero (no message body is present). |
| 2977 | * |
| 2978 | * 7. Otherwise, this is a response message without a declared message |
| 2979 | * body length, so the message body length is determined by the |
| 2980 | * number of octets received prior to the server closing the |
| 2981 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2982 | */ |
| 2983 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2984 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2985 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | e77bc17 | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 2986 | 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] | 2987 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2988 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2989 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 49efa26 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2990 | /* chunked not last, return badreq */ |
| 2991 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2992 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2993 | } |
| 2994 | |
Willy Tarreau | dfa3d92 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2995 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2996 | ctx.idx = 0; |
Willy Tarreau | dfa3d92 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2997 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 2998 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 2999 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 3000 | } |
Willy Tarreau | 49efa26 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3001 | 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] | 3002 | signed long long cl; |
| 3003 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3004 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3005 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3006 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3007 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3008 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3009 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3010 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3011 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3012 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3013 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3014 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3015 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3016 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3017 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3018 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3019 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3020 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3021 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3022 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3023 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3024 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3025 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3026 | } |
| 3027 | |
Willy Tarreau | 49efa26 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3028 | /* even bodyless requests have a known length */ |
| 3029 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3030 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3031 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 3032 | * only change if both the request and the config reference something else. |
| 3033 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3034 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3035 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3036 | * if FE and BE have the same settings (common). The method consists in |
| 3037 | * checking if options changed between the two calls (implying that either |
| 3038 | * one is non-null, or one of them is non-null and we are there for the first |
| 3039 | * time. |
| 3040 | */ |
| 3041 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | 2e47a3a | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 3042 | ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) |
| 3043 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3044 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3045 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3046 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3047 | req->analyse_exp = TICK_ETERNITY; |
| 3048 | return 1; |
| 3049 | |
| 3050 | return_bad_req: |
| 3051 | /* We centralize bad requests processing here */ |
| 3052 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3053 | /* we detected a parsing error. We want to archive this request |
| 3054 | * in the dedicated proxy area for later troubleshooting. |
| 3055 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3056 | 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] | 3057 | } |
| 3058 | |
| 3059 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3060 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3061 | 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] | 3062 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3063 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3064 | if (s->listener->counters) |
| 3065 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3066 | |
| 3067 | return_prx_cond: |
| 3068 | if (!(s->flags & SN_ERR_MASK)) |
| 3069 | s->flags |= SN_ERR_PRXCOND; |
| 3070 | if (!(s->flags & SN_FINST_MASK)) |
| 3071 | s->flags |= SN_FINST_R; |
| 3072 | |
| 3073 | req->analysers = 0; |
| 3074 | req->analyse_exp = TICK_ETERNITY; |
| 3075 | return 0; |
| 3076 | } |
| 3077 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3078 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3079 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3080 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3081 | * and program a response message if something was unexpected. It cannot fail |
| 3082 | * 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] | 3083 | * 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] | 3084 | * s->target which is supposed to already point to the stats applet. The caller |
| 3085 | * is expected to have already assigned an appctx to the session. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3086 | */ |
| 3087 | int http_handle_stats(struct session *s, struct channel *req) |
| 3088 | { |
| 3089 | struct stats_admin_rule *stats_admin_rule; |
| 3090 | struct stream_interface *si = s->rep->prod; |
| 3091 | struct http_txn *txn = &s->txn; |
| 3092 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3093 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3094 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3095 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3096 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3097 | appctx = si_appctx(si); |
| 3098 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3099 | appctx->st1 = appctx->st2 = 0; |
| 3100 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3101 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | af3cf70 | 2014-04-22 22:19:53 +0200 | [diff] [blame] | 3102 | if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD)) |
| 3103 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3104 | |
| 3105 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3106 | lookup = uri + uri_auth->uri_len; |
| 3107 | |
| 3108 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3109 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3110 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3111 | break; |
| 3112 | } |
| 3113 | } |
| 3114 | |
| 3115 | if (uri_auth->refresh) { |
| 3116 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3117 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3118 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3119 | break; |
| 3120 | } |
| 3121 | } |
| 3122 | } |
| 3123 | |
| 3124 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3125 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3126 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3127 | break; |
| 3128 | } |
| 3129 | } |
| 3130 | |
| 3131 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3132 | if (memcmp(h, ";st=", 4) == 0) { |
| 3133 | int i; |
| 3134 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3135 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3136 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3137 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3138 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3139 | break; |
| 3140 | } |
| 3141 | } |
| 3142 | break; |
| 3143 | } |
| 3144 | } |
| 3145 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3146 | appctx->ctx.stats.scope_str = 0; |
| 3147 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3148 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3149 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3150 | int itx = 0; |
| 3151 | const char *h2; |
| 3152 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3153 | const char *err; |
| 3154 | |
| 3155 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3156 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3157 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3158 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3159 | itx++; |
| 3160 | h++; |
| 3161 | } |
| 3162 | |
| 3163 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3164 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3165 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3166 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3167 | /* 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] | 3168 | memcpy(scope_txt, h2, itx); |
| 3169 | scope_txt[itx] = '\0'; |
| 3170 | err = invalid_char(scope_txt); |
| 3171 | if (err) { |
| 3172 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3173 | appctx->ctx.stats.scope_str = 0; |
| 3174 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3175 | } |
| 3176 | break; |
| 3177 | } |
| 3178 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3179 | |
| 3180 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3181 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3182 | int ret = 1; |
| 3183 | |
| 3184 | if (stats_admin_rule->cond) { |
| 3185 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 3186 | ret = acl_pass(ret); |
| 3187 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3188 | ret = !ret; |
| 3189 | } |
| 3190 | |
| 3191 | if (ret) { |
| 3192 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3193 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3194 | break; |
| 3195 | } |
| 3196 | } |
| 3197 | |
| 3198 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3199 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3200 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3201 | /* we'll need the request body, possibly after sending 100-continue */ |
| 3202 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3203 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3204 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3205 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3206 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3207 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3208 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3209 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3210 | else { |
| 3211 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3212 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3213 | } |
| 3214 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3215 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3216 | return 1; |
| 3217 | } |
| 3218 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3219 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3220 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3221 | */ |
| 3222 | static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
| 3223 | { |
| 3224 | #ifdef IP_TOS |
| 3225 | if (from.ss_family == AF_INET) |
| 3226 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3227 | #endif |
| 3228 | #ifdef IPV6_TCLASS |
| 3229 | if (from.ss_family == AF_INET6) { |
| 3230 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3231 | /* v4-mapped addresses need IP_TOS */ |
| 3232 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3233 | else |
| 3234 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3235 | } |
| 3236 | #endif |
| 3237 | } |
| 3238 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3239 | 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] | 3240 | 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] | 3241 | struct hdr_ctx* ctx, int action) |
| 3242 | { |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3243 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 3244 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
| 3245 | struct chunk *replace = get_trash_chunk(); |
| 3246 | struct chunk *output = get_trash_chunk(); |
| 3247 | |
| 3248 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 3249 | if (replace->len >= replace->size - 1) |
| 3250 | return -1; |
| 3251 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3252 | ctx->idx = 0; |
| 3253 | |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3254 | /* Choose the header browsing function. */ |
| 3255 | switch (action) { |
| 3256 | case HTTP_REQ_ACT_REPLACE_VAL: |
| 3257 | case HTTP_RES_ACT_REPLACE_VAL: |
| 3258 | http_find_hdr_func = http_find_header2; |
| 3259 | break; |
| 3260 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3261 | case HTTP_RES_ACT_REPLACE_HDR: |
| 3262 | http_find_hdr_func = http_find_full_header2; |
| 3263 | break; |
| 3264 | default: /* impossible */ |
| 3265 | return -1; |
| 3266 | } |
| 3267 | |
| 3268 | while (http_find_hdr_func(name, name_len, buf, idx, ctx)) { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3269 | struct hdr_idx_elem *hdr = idx->v + ctx->idx; |
| 3270 | int delta; |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3271 | char *val = ctx->line + ctx->val; |
| 3272 | char* val_end = val + ctx->vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3273 | |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3274 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch)) |
| 3275 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3276 | |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3277 | output->len = exp_replace(output->str, output->size, val, replace->str, pmatch); |
| 3278 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3279 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3280 | |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3281 | 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] | 3282 | |
| 3283 | hdr->len += delta; |
| 3284 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3285 | |
| 3286 | /* Adjust the length of the current value of the index. */ |
| 3287 | ctx->vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3288 | } |
| 3289 | |
| 3290 | return 0; |
| 3291 | } |
| 3292 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3293 | /* Executes the http-request rules <rules> for session <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3294 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3295 | * further processing of the request (auth, deny, ...), and defaults to |
| 3296 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3297 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 3298 | * on txn->flags if it encounters a tarpit rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3299 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3300 | enum rule_result |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3301 | 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] | 3302 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3303 | struct connection *cli_conn; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3304 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3305 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3306 | const char *auth_realm; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3307 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3308 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3309 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3310 | continue; |
| 3311 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3312 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3313 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3314 | int ret; |
| 3315 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3316 | 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] | 3317 | ret = acl_pass(ret); |
| 3318 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3319 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3320 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3321 | |
| 3322 | if (!ret) /* condition not matched */ |
| 3323 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3324 | } |
| 3325 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3326 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3327 | switch (rule->action) { |
| 3328 | case HTTP_REQ_ACT_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3329 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3330 | |
| 3331 | case HTTP_REQ_ACT_DENY: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3332 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3333 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3334 | case HTTP_REQ_ACT_TARPIT: |
| 3335 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3336 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3337 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3338 | case HTTP_REQ_ACT_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3339 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3340 | auth_realm = rule->arg.auth.realm; |
| 3341 | if (!auth_realm) { |
| 3342 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3343 | auth_realm = STATS_DEFAULT_REALM; |
| 3344 | else |
| 3345 | auth_realm = px->id; |
| 3346 | } |
| 3347 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3348 | * count one error, because normal browsing won't significantly |
| 3349 | * increase the counter but brute force attempts will. |
| 3350 | */ |
| 3351 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3352 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
| 3353 | stream_int_retnclose(&s->si[0], &trash); |
| 3354 | session_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3355 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3356 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3357 | case HTTP_REQ_ACT_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3358 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3359 | return HTTP_RULE_RES_BADREQ; |
| 3360 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3361 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3362 | case HTTP_REQ_ACT_SET_NICE: |
| 3363 | s->task->nice = rule->arg.nice; |
| 3364 | break; |
| 3365 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3366 | case HTTP_REQ_ACT_SET_TOS: |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3367 | 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] | 3368 | 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] | 3369 | break; |
| 3370 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3371 | case HTTP_REQ_ACT_SET_MARK: |
| 3372 | #ifdef SO_MARK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3373 | 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] | 3374 | 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] | 3375 | #endif |
| 3376 | break; |
| 3377 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3378 | case HTTP_REQ_ACT_SET_LOGL: |
| 3379 | s->logs.level = rule->arg.loglevel; |
| 3380 | break; |
| 3381 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3382 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3383 | case HTTP_REQ_ACT_REPLACE_VAL: |
| 3384 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3385 | 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] | 3386 | &rule->arg.hdr_add.re, &ctx, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3387 | return HTTP_RULE_RES_BADREQ; |
| 3388 | break; |
| 3389 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3390 | case HTTP_REQ_ACT_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3391 | ctx.idx = 0; |
| 3392 | /* remove all occurrences of the header */ |
| 3393 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3394 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3395 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3396 | } |
Willy Tarreau | fcf75d2 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3397 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3398 | |
Willy Tarreau | fcf75d2 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3399 | case HTTP_REQ_ACT_SET_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3400 | case HTTP_REQ_ACT_ADD_HDR: |
| 3401 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 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)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +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... */ |
| 4154 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4155 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4156 | if (!req->analyse_exp) |
| 4157 | req->analyse_exp = tick_add(now_ms, 0); |
| 4158 | session_inc_http_err_ctr(s); |
| 4159 | s->fe->fe_counters.denied_req++; |
| 4160 | if (s->fe != s->be) |
| 4161 | s->be->be_counters.denied_req++; |
| 4162 | if (s->listener->counters) |
| 4163 | s->listener->counters->denied_req++; |
Thierry FOURNIER | fc566b5 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4164 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4165 | |
| 4166 | deny: /* this request was blocked (denied) */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4167 | txn->flags |= TX_CLDENY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4168 | txn->status = 403; |
| 4169 | s->logs.tv_request = now; |
| 4170 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
| 4171 | session_inc_http_err_ctr(s); |
| 4172 | s->fe->fe_counters.denied_req++; |
| 4173 | if (s->fe != s->be) |
| 4174 | s->be->be_counters.denied_req++; |
| 4175 | if (s->listener->counters) |
| 4176 | s->listener->counters->denied_req++; |
| 4177 | goto return_prx_cond; |
| 4178 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4179 | return_bad_req: |
| 4180 | /* We centralize bad requests processing here */ |
| 4181 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4182 | /* we detected a parsing error. We want to archive this request |
| 4183 | * in the dedicated proxy area for later troubleshooting. |
| 4184 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4185 | 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] | 4186 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4187 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4188 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4189 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4190 | 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] | 4191 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4192 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4193 | if (s->listener->counters) |
| 4194 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4195 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4196 | return_prx_cond: |
| 4197 | if (!(s->flags & SN_ERR_MASK)) |
| 4198 | s->flags |= SN_ERR_PRXCOND; |
| 4199 | if (!(s->flags & SN_FINST_MASK)) |
| 4200 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4201 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4202 | req->analysers = 0; |
| 4203 | req->analyse_exp = TICK_ETERNITY; |
| 4204 | return 0; |
| 4205 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4206 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4207 | /* This function performs all the processing enabled for the current request. |
| 4208 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4209 | * needs more data, encounters an error, or wants to immediately abort the |
| 4210 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 4211 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4212 | 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] | 4213 | { |
| 4214 | struct http_txn *txn = &s->txn; |
| 4215 | struct http_msg *msg = &txn->req; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4216 | struct connection *cli_conn = objt_conn(req->prod->end); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4217 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4218 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4219 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4220 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4221 | return 0; |
| 4222 | } |
| 4223 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4224 | 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] | 4225 | now_ms, __FUNCTION__, |
| 4226 | s, |
| 4227 | req, |
| 4228 | req->rex, req->wex, |
| 4229 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4230 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4231 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4232 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 4233 | if (s->fe->comp || s->be->comp) |
| 4234 | select_compression_request_header(s, req->buf); |
| 4235 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4236 | /* |
| 4237 | * Right now, we know that we have processed the entire headers |
| 4238 | * and that unwanted requests have been filtered out. We can do |
| 4239 | * whatever we want with the remaining request. Also, now we |
| 4240 | * may have separate values for ->fe, ->be. |
| 4241 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4242 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4243 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4244 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4245 | * incoming request. Note that this requires that a connection is |
| 4246 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4247 | */ |
| 4248 | 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] | 4249 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4250 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4251 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4252 | /* Note that for now we don't reuse existing proxy connections */ |
| 4253 | if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4254 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4255 | txn->status = 500; |
| 4256 | req->analysers = 0; |
| 4257 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
| 4258 | |
| 4259 | if (!(s->flags & SN_ERR_MASK)) |
| 4260 | s->flags |= SN_ERR_RESOURCE; |
| 4261 | if (!(s->flags & SN_FINST_MASK)) |
| 4262 | s->flags |= SN_FINST_R; |
| 4263 | |
| 4264 | return 0; |
| 4265 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4266 | |
| 4267 | path = http_get_path(txn); |
| 4268 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4269 | 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] | 4270 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4271 | /* if the path was found, we have to remove everything between |
| 4272 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4273 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4274 | * u_l characters by a single "/". |
| 4275 | */ |
| 4276 | if (path) { |
| 4277 | char *cur_ptr = req->buf->p; |
| 4278 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4279 | int delta; |
| 4280 | |
| 4281 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4282 | http_msg_move_end(&txn->req, delta); |
| 4283 | cur_end += delta; |
| 4284 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4285 | goto return_bad_req; |
| 4286 | } |
| 4287 | else { |
| 4288 | char *cur_ptr = req->buf->p; |
| 4289 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4290 | int delta; |
| 4291 | |
| 4292 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4293 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 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 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4299 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4300 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4301 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4302 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4303 | * Note that doing so might move headers in the request, but |
| 4304 | * the fields will stay coherent and the URI will not move. |
| 4305 | * This should only be performed in the backend. |
| 4306 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 4307 | 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] | 4308 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 4309 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4310 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4311 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4312 | * 8: the appsession cookie was looked up very early in 1.2, |
| 4313 | * so let's do the same now. |
| 4314 | */ |
| 4315 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4316 | /* It needs to look into the URI unless persistence must be ignored */ |
| 4317 | 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] | 4318 | 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] | 4319 | } |
| 4320 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4321 | /* add unique-id if "header-unique-id" is specified */ |
| 4322 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4323 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 4324 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4325 | goto return_bad_req; |
| 4326 | s->unique_id[0] = '\0'; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4327 | 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] | 4328 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4329 | |
| 4330 | if (s->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4331 | chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 4332 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4333 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4334 | 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] | 4335 | goto return_bad_req; |
| 4336 | } |
| 4337 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4338 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4339 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4340 | * asks for it. |
| 4341 | */ |
| 4342 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4343 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4344 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Cyril Bonté | a32d275 | 2012-05-29 23:27:41 +0200 | [diff] [blame] | 4345 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name, |
| 4346 | 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] | 4347 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4348 | /* The header is set to be added only if none is present |
| 4349 | * and we found it, so don't do anything. |
| 4350 | */ |
| 4351 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4352 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4353 | /* Add an X-Forwarded-For header unless the source IP is |
| 4354 | * in the 'except' network range. |
| 4355 | */ |
| 4356 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4357 | (((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] | 4358 | != s->fe->except_net.s_addr) && |
| 4359 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4360 | (((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] | 4361 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4362 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4363 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4364 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4365 | |
| 4366 | /* Note: we rely on the backend to get the header name to be used for |
| 4367 | * x-forwarded-for, because the header is really meant for the backends. |
| 4368 | * However, if the backend did not specify any option, we have to rely |
| 4369 | * on the frontend's header name. |
| 4370 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4371 | if (s->be->fwdfor_hdr_len) { |
| 4372 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4373 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4374 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4375 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4376 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4377 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4378 | 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] | 4379 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4380 | 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] | 4381 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4382 | } |
| 4383 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4384 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4385 | /* FIXME: for the sake of completeness, we should also support |
| 4386 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4387 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4388 | int len; |
| 4389 | char pn[INET6_ADDRSTRLEN]; |
| 4390 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4391 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4392 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4393 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4394 | /* Note: we rely on the backend to get the header name to be used for |
| 4395 | * x-forwarded-for, because the header is really meant for the backends. |
| 4396 | * However, if the backend did not specify any option, we have to rely |
| 4397 | * on the frontend's header name. |
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 | if (s->be->fwdfor_hdr_len) { |
| 4400 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4401 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4402 | } else { |
| 4403 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4404 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4405 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4406 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4407 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4408 | 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] | 4409 | goto return_bad_req; |
| 4410 | } |
| 4411 | } |
| 4412 | |
| 4413 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4414 | * 10: add X-Original-To if either the frontend or the backend |
| 4415 | * asks for it. |
| 4416 | */ |
| 4417 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 4418 | |
| 4419 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4420 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4421 | /* Add an X-Original-To header unless the destination IP is |
| 4422 | * in the 'except' network range. |
| 4423 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4424 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4425 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4426 | if (cli_conn->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4427 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4428 | (((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] | 4429 | != s->fe->except_to.s_addr) && |
| 4430 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4431 | (((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] | 4432 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4433 | int len; |
| 4434 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4435 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4436 | |
| 4437 | /* Note: we rely on the backend to get the header name to be used for |
| 4438 | * x-original-to, because the header is really meant for the backends. |
| 4439 | * However, if the backend did not specify any option, we have to rely |
| 4440 | * on the frontend's header name. |
| 4441 | */ |
| 4442 | if (s->be->orgto_hdr_len) { |
| 4443 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4444 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4445 | } else { |
| 4446 | len = s->fe->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4447 | memcpy(trash.str, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4448 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4449 | 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] | 4450 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4451 | 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] | 4452 | goto return_bad_req; |
| 4453 | } |
| 4454 | } |
| 4455 | } |
| 4456 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4457 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4458 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4459 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4460 | * "Upgrade" is present in the Connection header. |
| 4461 | */ |
| 4462 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4463 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4464 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4465 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4466 | unsigned int want_flags = 0; |
| 4467 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4468 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4469 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4470 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4471 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4472 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4473 | want_flags |= TX_CON_CLO_SET; |
| 4474 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4475 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4476 | ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
| 4477 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4478 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4479 | want_flags |= TX_CON_KAL_SET; |
| 4480 | } |
| 4481 | |
| 4482 | 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] | 4483 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4484 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4485 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4486 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4487 | /* If we have no server assigned yet and we're balancing on url_param |
| 4488 | * with a POST request, we may be interested in checking the body for |
| 4489 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4490 | */ |
| 4491 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 4492 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4493 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4494 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4495 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4496 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4497 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4498 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4499 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4500 | #ifdef TCP_QUICKACK |
| 4501 | /* We expect some data from the client. Unless we know for sure |
| 4502 | * we already have a full request, we have to re-enable quick-ack |
| 4503 | * in case we previously disabled it, otherwise we might cause |
| 4504 | * the client to delay further data. |
| 4505 | */ |
| 4506 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4507 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4508 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4509 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4510 | 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] | 4511 | #endif |
| 4512 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4513 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4514 | /************************************************************* |
| 4515 | * OK, that's finished for the headers. We have done what we * |
| 4516 | * could. Let's switch to the DATA state. * |
| 4517 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4518 | req->analyse_exp = TICK_ETERNITY; |
| 4519 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4520 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4521 | /* if the server closes the connection, we want to immediately react |
| 4522 | * and close the socket to save packets and syscalls. |
| 4523 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4524 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
| 4525 | req->cons->flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4526 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4527 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4528 | /* OK let's go on with the BODY now */ |
| 4529 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4530 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4531 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4532 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4533 | /* we detected a parsing error. We want to archive this request |
| 4534 | * in the dedicated proxy area for later troubleshooting. |
| 4535 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4536 | 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] | 4537 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4538 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4539 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4540 | txn->status = 400; |
| 4541 | req->analysers = 0; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4542 | 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] | 4543 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4544 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4545 | if (s->listener->counters) |
| 4546 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4547 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4548 | if (!(s->flags & SN_ERR_MASK)) |
| 4549 | s->flags |= SN_ERR_PRXCOND; |
| 4550 | if (!(s->flags & SN_FINST_MASK)) |
| 4551 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4552 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4553 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4554 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4555 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4556 | * returns zero, at the beginning because it prevents any other processing |
| 4557 | * from occurring, and at the end because it terminates the request. |
| 4558 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4559 | 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] | 4560 | { |
| 4561 | struct http_txn *txn = &s->txn; |
| 4562 | |
| 4563 | /* This connection is being tarpitted. The CLIENT side has |
| 4564 | * already set the connect expiration date to the right |
| 4565 | * timeout. We just have to check that the client is still |
| 4566 | * there and that the timeout has not expired. |
| 4567 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4568 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4569 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4570 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4571 | return 0; |
| 4572 | |
| 4573 | /* We will set the queue timer to the time spent, just for |
| 4574 | * logging purposes. We fake a 500 server error, so that the |
| 4575 | * attacker will not suspect his connection has been tarpitted. |
| 4576 | * It will not cause trouble to the logs because we can exclude |
| 4577 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4578 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4579 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4580 | |
| 4581 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4582 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4583 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4584 | |
| 4585 | req->analysers = 0; |
| 4586 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4587 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4588 | if (!(s->flags & SN_ERR_MASK)) |
| 4589 | s->flags |= SN_ERR_PRXCOND; |
| 4590 | if (!(s->flags & SN_FINST_MASK)) |
| 4591 | s->flags |= SN_FINST_T; |
| 4592 | return 0; |
| 4593 | } |
| 4594 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4595 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4596 | * for either the buffer to be full, or the full advertised contents to have |
| 4597 | * reached the buffer. It must only be called after the standard HTTP request |
| 4598 | * processing has occurred, because it expects the request to be parsed and will |
| 4599 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4600 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4601 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4602 | * 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] | 4603 | */ |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4604 | 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] | 4605 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4606 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4607 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4608 | |
| 4609 | /* We have to parse the HTTP request body to find any required data. |
| 4610 | * "balance url_param check_post" should have been the only way to get |
| 4611 | * into this. We were brought here after HTTP header analysis, so all |
| 4612 | * related structures are ready. |
| 4613 | */ |
| 4614 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4615 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4616 | /* This is the first call */ |
| 4617 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4618 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4619 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4620 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4621 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4622 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4623 | */ |
| 4624 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4625 | struct hdr_ctx ctx; |
| 4626 | ctx.idx = 0; |
| 4627 | /* Expect is allowed in 1.1, look for it */ |
| 4628 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4629 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 4630 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 4631 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4632 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4633 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4634 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4635 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4636 | /* 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] | 4637 | * req->buf->p still points to the beginning of the message. We |
| 4638 | * must save the body in msg->next because it survives buffer |
| 4639 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4640 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4641 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4642 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4643 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4644 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4645 | else |
| 4646 | msg->msg_state = HTTP_MSG_DATA; |
| 4647 | } |
| 4648 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4649 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4650 | /* 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] | 4651 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4652 | goto missing_data; |
| 4653 | |
| 4654 | /* OK we have everything we need now */ |
| 4655 | goto http_end; |
| 4656 | } |
| 4657 | |
| 4658 | /* OK here we're parsing a chunked-encoded message */ |
| 4659 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4660 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4661 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4662 | * 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] | 4663 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4664 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4665 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4666 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4667 | if (!ret) |
| 4668 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4669 | else if (ret < 0) { |
| 4670 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4671 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4672 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4673 | } |
| 4674 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4675 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 5b7f924 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4676 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 4677 | * for at least a whole chunk or the whole content length bytes after |
| 4678 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4679 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4680 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4681 | goto http_end; |
| 4682 | |
Willy Tarreau | 5b7f924 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4683 | 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] | 4684 | goto http_end; |
| 4685 | |
| 4686 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4687 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4688 | * we have the maximum we can expect. |
| 4689 | */ |
| 4690 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4691 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4692 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4693 | 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] | 4694 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4695 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4696 | |
| 4697 | if (!(s->flags & SN_ERR_MASK)) |
| 4698 | s->flags |= SN_ERR_CLITO; |
| 4699 | if (!(s->flags & SN_FINST_MASK)) |
| 4700 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4701 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4702 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4703 | |
| 4704 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4705 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4706 | /* Not enough data. We'll re-use the http-request |
| 4707 | * timeout here. Ideally, we should set the timeout |
| 4708 | * relative to the accept() date. We just set the |
| 4709 | * request timeout once at the beginning of the |
| 4710 | * request. |
| 4711 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4712 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4713 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4714 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4715 | return 0; |
| 4716 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4717 | |
| 4718 | http_end: |
| 4719 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4720 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4721 | req->analysers &= ~an_bit; |
| 4722 | req->analyse_exp = TICK_ETERNITY; |
| 4723 | return 1; |
| 4724 | |
| 4725 | return_bad_req: /* let's centralize all bad requests */ |
| 4726 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4727 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4728 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4729 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4730 | if (!(s->flags & SN_ERR_MASK)) |
| 4731 | s->flags |= SN_ERR_PRXCOND; |
| 4732 | if (!(s->flags & SN_FINST_MASK)) |
| 4733 | s->flags |= SN_FINST_R; |
| 4734 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4735 | return_err_msg: |
| 4736 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4737 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4738 | if (s->listener->counters) |
| 4739 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4740 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4741 | } |
| 4742 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4743 | /* send a server's name with an outgoing request over an established connection. |
| 4744 | * Note: this function is designed to be called once the request has been scheduled |
| 4745 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4746 | * proceeding. |
| 4747 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4748 | 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] | 4749 | |
| 4750 | struct hdr_ctx ctx; |
| 4751 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4752 | char *hdr_name = be->server_id_hdr_name; |
| 4753 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4754 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4755 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4756 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4757 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4758 | ctx.idx = 0; |
| 4759 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4760 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4761 | if (old_o) { |
| 4762 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4763 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4764 | txn->req.next += old_o; |
| 4765 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4766 | } |
| 4767 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4768 | old_i = chn->buf->i; |
| 4769 | 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] | 4770 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4771 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4772 | } |
| 4773 | |
| 4774 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4775 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4776 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4777 | hdr_val += hdr_name_len; |
| 4778 | *hdr_val++ = ':'; |
| 4779 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4780 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4781 | 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] | 4782 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4783 | if (old_o) { |
| 4784 | /* If this was a forwarded request, we must readjust the amount of |
| 4785 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4786 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4787 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4788 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4789 | old_o += chn->buf->i - old_i; |
| 4790 | b_adv(chn->buf, old_o); |
| 4791 | txn->req.next -= old_o; |
| 4792 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4793 | } |
| 4794 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4795 | return 0; |
| 4796 | } |
| 4797 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4798 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4799 | * right now but it works. |
| 4800 | */ |
| 4801 | void http_end_txn_clean_session(struct session *s) |
| 4802 | { |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4803 | int prev_status = s->txn.status; |
| 4804 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4805 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4806 | * server's connections. We need a safer way to reinitialize buffer |
| 4807 | * flags. We also need a more accurate method for computing per-request |
| 4808 | * data. |
| 4809 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4810 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4811 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4812 | * to the server. |
| 4813 | */ |
| 4814 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4815 | !si_conn_ready(s->req->cons)) { |
| 4816 | s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4817 | si_shutr(s->req->cons); |
| 4818 | si_shutw(s->req->cons); |
| 4819 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4820 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4821 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4822 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4823 | if (unlikely(s->srv_conn)) |
| 4824 | sess_change_server(s, NULL); |
| 4825 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4826 | |
| 4827 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4828 | session_process_counters(s); |
| 4829 | |
| 4830 | if (s->txn.status) { |
| 4831 | int n; |
| 4832 | |
| 4833 | n = s->txn.status / 100; |
| 4834 | if (n < 1 || n > 5) |
| 4835 | n = 0; |
| 4836 | |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4837 | if (s->fe->mode == PR_MODE_HTTP) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4838 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4839 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4840 | s->fe->fe_counters.p.http.comp_rsp++; |
| 4841 | } |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 4842 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4843 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4844 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4845 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4846 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4847 | s->be->be_counters.p.http.comp_rsp++; |
| 4848 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4849 | } |
| 4850 | |
| 4851 | /* don't count other requests' data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4852 | s->logs.bytes_in -= s->req->buf->i; |
| 4853 | s->logs.bytes_out -= s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4854 | |
| 4855 | /* let's do a final log if we need it */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 4856 | if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4857 | !(s->flags & SN_MONITOR) && |
| 4858 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 4859 | s->do_log(s); |
| 4860 | } |
| 4861 | |
Willy Tarreau | c177ea7 | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 4862 | /* stop tracking content-based counters */ |
| 4863 | session_stop_content_counters(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 4864 | session_update_time_stats(s); |
| 4865 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4866 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4867 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4868 | tv_zero(&s->logs.tv_request); |
| 4869 | s->logs.t_queue = -1; |
| 4870 | s->logs.t_connect = -1; |
| 4871 | s->logs.t_data = -1; |
| 4872 | s->logs.t_close = 0; |
| 4873 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4874 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4875 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4876 | s->logs.bytes_in = s->req->total = s->req->buf->i; |
| 4877 | s->logs.bytes_out = s->rep->total = s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4878 | |
| 4879 | if (s->pend_pos) |
| 4880 | pendconn_free(s->pend_pos); |
| 4881 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4882 | if (objt_server(s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4883 | if (s->flags & SN_CURR_SESS) { |
| 4884 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4885 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4886 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4887 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 4888 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4889 | } |
| 4890 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4891 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4892 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4893 | /* only release our endpoint if we don't intend to reuse the |
| 4894 | * connection. |
| 4895 | */ |
| 4896 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4897 | !si_conn_ready(s->req->cons)) { |
| 4898 | si_release_endpoint(s->req->cons); |
| 4899 | } |
| 4900 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4901 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4902 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 4903 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4904 | s->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | c920096 | 2013-12-31 23:03:09 +0100 | [diff] [blame] | 4905 | 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] | 4906 | 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); |
| 4907 | 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] | 4908 | 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] | 4909 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED); |
Cyril Bonté | 17f7707 | 2014-10-22 22:30:13 +0200 | [diff] [blame] | 4910 | s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4911 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4912 | s->txn.meth = 0; |
| 4913 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4914 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4915 | |
| 4916 | if (prev_status == 401 || prev_status == 407) { |
| 4917 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 4918 | * a chance of being able to send the visitor again to the same |
| 4919 | * server over the same connection. This is required by some |
| 4920 | * broken protocols such as NTLM, and anyway whenever there is |
| 4921 | * an opportunity for sending the challenge to the proper place, |
| 4922 | * it's better to do it (at least it helps with debugging). |
| 4923 | */ |
| 4924 | s->txn.flags |= TX_PREFER_LAST; |
| 4925 | } |
| 4926 | |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 4927 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4928 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 4929 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4930 | if (s->fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4931 | s->req->flags |= CF_NEVER_WAIT; |
| 4932 | s->rep->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4933 | } |
| 4934 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4935 | /* if the request buffer is not empty, it means we're |
| 4936 | * about to process another request, so send pending |
| 4937 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4938 | * Just don't do this if the buffer is close to be full, |
| 4939 | * because the request will wait for it to flush a little |
| 4940 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4941 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4942 | if (s->req->buf->i) { |
| 4943 | if (s->rep->buf->o && |
| 4944 | !buffer_full(s->rep->buf, global.tune.maxrewrite) && |
| 4945 | 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] | 4946 | s->rep->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4947 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4948 | |
Willy Tarreau | 3de8e7a | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 4949 | /* we're removing the analysers, we MUST re-enable events detection. |
| 4950 | * We don't enable close on the response channel since it's either |
| 4951 | * already closed, or in keep-alive with an idle connection handler. |
| 4952 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4953 | channel_auto_read(s->req); |
| 4954 | channel_auto_close(s->req); |
| 4955 | channel_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4956 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 4957 | /* we're in keep-alive with an idle connection, monitor it */ |
| 4958 | si_idle_conn(s->req->cons); |
| 4959 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 4960 | s->req->analysers = s->listener->analysers; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4961 | s->rep->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4962 | } |
| 4963 | |
| 4964 | |
| 4965 | /* This function updates the request state machine according to the response |
| 4966 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4967 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4968 | * it is only used to find when a request/response couple is complete. Both |
| 4969 | * this function and its equivalent should loop until both return zero. It |
| 4970 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4971 | */ |
| 4972 | int http_sync_req_state(struct session *s) |
| 4973 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4974 | struct channel *chn = s->req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4975 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4976 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4977 | unsigned int old_state = txn->req.msg_state; |
| 4978 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4979 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 4980 | return 0; |
| 4981 | |
| 4982 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4983 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4984 | * We can shut the read side unless we want to abort_on_close, |
| 4985 | * or we have a POST request. The issue with POST requests is |
| 4986 | * that some browsers still send a CRLF after the request, and |
| 4987 | * this CRLF must be read so that it does not remain in the kernel |
| 4988 | * buffers, otherwise a close could cause an RST on some systems |
| 4989 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4990 | * Note that if we're using keep-alive on the client side, we'd |
| 4991 | * rather poll now and keep the polling enabled for the whole |
| 4992 | * session's life than enabling/disabling it between each |
| 4993 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4994 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4995 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4996 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4997 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 4998 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4999 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5000 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5001 | /* if the server closes the connection, we want to immediately react |
| 5002 | * and close the socket to save packets and syscalls. |
| 5003 | */ |
| 5004 | chn->cons->flags |= SI_FL_NOHALF; |
| 5005 | |
Willy Tarreau | c0d5613 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 5006 | /* In any case we've finished parsing the request so we must |
| 5007 | * disable Nagle when sending data because 1) we're not going |
| 5008 | * to shut this side, and 2) the server is waiting for us to |
| 5009 | * send pending data. |
| 5010 | */ |
| 5011 | chn->flags |= CF_NEVER_WAIT; |
| 5012 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5013 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5014 | goto wait_other_side; |
| 5015 | |
| 5016 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5017 | /* The server has not finished to respond, so we |
| 5018 | * don't want to move in order not to upset it. |
| 5019 | */ |
| 5020 | goto wait_other_side; |
| 5021 | } |
| 5022 | |
| 5023 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5024 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5025 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5026 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5027 | goto wait_other_side; |
| 5028 | } |
| 5029 | |
| 5030 | /* When we get here, it means that both the request and the |
| 5031 | * response have finished receiving. Depending on the connection |
| 5032 | * mode, we'll have to wait for the last bytes to leave in either |
| 5033 | * direction, and sometimes for a close to be effective. |
| 5034 | */ |
| 5035 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5036 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5037 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5038 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5039 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5040 | } |
| 5041 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5042 | /* Option forceclose is set, or either side wants to close, |
| 5043 | * let's enforce it now that we're not expecting any new |
| 5044 | * data to come. The caller knows the session is complete |
| 5045 | * once both states are CLOSED. |
| 5046 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5047 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5048 | channel_shutr_now(chn); |
| 5049 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5050 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5051 | } |
| 5052 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5053 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5054 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5055 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5056 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5057 | channel_auto_read(chn); |
| 5058 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5059 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5060 | } |
| 5061 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5062 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5063 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5064 | chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5065 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5066 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5067 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5068 | goto http_msg_closing; |
| 5069 | } |
| 5070 | else { |
| 5071 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5072 | goto http_msg_closed; |
| 5073 | } |
| 5074 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5075 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5076 | } |
| 5077 | |
| 5078 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5079 | http_msg_closing: |
| 5080 | /* nothing else to forward, just waiting for the output buffer |
| 5081 | * to be empty and for the shutw_now to take effect. |
| 5082 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5083 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5084 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5085 | goto http_msg_closed; |
| 5086 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5087 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5088 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5089 | goto wait_other_side; |
| 5090 | } |
| 5091 | } |
| 5092 | |
| 5093 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5094 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5095 | /* see above in MSG_DONE why we only do this in these states */ |
| 5096 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5097 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5098 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5099 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5100 | goto wait_other_side; |
| 5101 | } |
| 5102 | |
| 5103 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5104 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5105 | } |
| 5106 | |
| 5107 | |
| 5108 | /* This function updates the response state machine according to the request |
| 5109 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5110 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5111 | * it is only used to find when a request/response couple is complete. Both |
| 5112 | * this function and its equivalent should loop until both return zero. It |
| 5113 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5114 | */ |
| 5115 | int http_sync_res_state(struct session *s) |
| 5116 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5117 | struct channel *chn = s->rep; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5118 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5119 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5120 | unsigned int old_state = txn->rsp.msg_state; |
| 5121 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5122 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5123 | return 0; |
| 5124 | |
| 5125 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5126 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5127 | * still monitor the server connection for a possible close |
| 5128 | * while the request is being uploaded, so we don't disable |
| 5129 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5130 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5131 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5132 | |
| 5133 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5134 | goto wait_other_side; |
| 5135 | |
| 5136 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5137 | /* The client seems to still be sending data, probably |
| 5138 | * because we got an error response during an upload. |
| 5139 | * We have the choice of either breaking the connection |
| 5140 | * or letting it pass through. Let's do the later. |
| 5141 | */ |
| 5142 | goto wait_other_side; |
| 5143 | } |
| 5144 | |
| 5145 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5146 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5147 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5148 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5149 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5150 | goto wait_other_side; |
| 5151 | } |
| 5152 | |
| 5153 | /* When we get here, it means that both the request and the |
| 5154 | * response have finished receiving. Depending on the connection |
| 5155 | * mode, we'll have to wait for the last bytes to leave in either |
| 5156 | * direction, and sometimes for a close to be effective. |
| 5157 | */ |
| 5158 | |
| 5159 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5160 | /* Server-close mode : shut read and wait for the request |
| 5161 | * side to close its output buffer. The caller will detect |
| 5162 | * when we're in DONE and the other is in CLOSED and will |
| 5163 | * catch that for the final cleanup. |
| 5164 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5165 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5166 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5167 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5168 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5169 | /* Option forceclose is set, or either side wants to close, |
| 5170 | * let's enforce it now that we're not expecting any new |
| 5171 | * data to come. The caller knows the session is complete |
| 5172 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5173 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5174 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5175 | channel_shutr_now(chn); |
| 5176 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5177 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5178 | } |
| 5179 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5180 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5181 | * need to forward remaining data. Keep-alive will need to monitor |
| 5182 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5183 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5184 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5185 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5186 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5187 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5188 | } |
| 5189 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5190 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5191 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5192 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5193 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5194 | goto http_msg_closing; |
| 5195 | } |
| 5196 | else { |
| 5197 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5198 | goto http_msg_closed; |
| 5199 | } |
| 5200 | } |
| 5201 | goto wait_other_side; |
| 5202 | } |
| 5203 | |
| 5204 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5205 | http_msg_closing: |
| 5206 | /* nothing else to forward, just waiting for the output buffer |
| 5207 | * to be empty and for the shutw_now to take effect. |
| 5208 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5209 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5210 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5211 | goto http_msg_closed; |
| 5212 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5213 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5214 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5215 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5216 | if (objt_server(s->target)) |
| 5217 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5218 | goto wait_other_side; |
| 5219 | } |
| 5220 | } |
| 5221 | |
| 5222 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5223 | http_msg_closed: |
| 5224 | /* drop any pending data */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5225 | bi_erase(chn); |
| 5226 | channel_auto_close(chn); |
| 5227 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5228 | goto wait_other_side; |
| 5229 | } |
| 5230 | |
| 5231 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5232 | /* We force the response to leave immediately if we're waiting for the |
| 5233 | * other side, since there is no pending shutdown to push it out. |
| 5234 | */ |
| 5235 | if (!channel_is_empty(chn)) |
| 5236 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5237 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5238 | } |
| 5239 | |
| 5240 | |
| 5241 | /* Resync the request and response state machines. Return 1 if either state |
| 5242 | * changes. |
| 5243 | */ |
| 5244 | int http_resync_states(struct session *s) |
| 5245 | { |
| 5246 | struct http_txn *txn = &s->txn; |
| 5247 | int old_req_state = txn->req.msg_state; |
| 5248 | int old_res_state = txn->rsp.msg_state; |
| 5249 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5250 | http_sync_req_state(s); |
| 5251 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5252 | if (!http_sync_res_state(s)) |
| 5253 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5254 | if (!http_sync_req_state(s)) |
| 5255 | break; |
| 5256 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5257 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5258 | /* OK, both state machines agree on a compatible state. |
| 5259 | * There are a few cases we're interested in : |
| 5260 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5261 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5262 | * directions, so let's simply disable both analysers. |
| 5263 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5264 | * request. |
| 5265 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5266 | * with server-close mode means we've completed one request and we |
| 5267 | * must re-initialize the server connection. |
| 5268 | */ |
| 5269 | |
| 5270 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5271 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5272 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5273 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 5274 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5275 | channel_auto_close(s->req); |
| 5276 | channel_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5277 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5278 | channel_auto_close(s->rep); |
| 5279 | channel_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5280 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5281 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
| 5282 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5283 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5284 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5285 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5286 | channel_auto_close(s->rep); |
| 5287 | channel_auto_read(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5288 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5289 | channel_abort(s->req); |
| 5290 | channel_auto_close(s->req); |
| 5291 | channel_auto_read(s->req); |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5292 | bi_erase(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5293 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5294 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5295 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5296 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5297 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5298 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5299 | /* server-close/keep-alive: terminate this transaction, |
| 5300 | * possibly killing the server connection and reinitialize |
| 5301 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5302 | */ |
| 5303 | http_end_txn_clean_session(s); |
| 5304 | } |
| 5305 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5306 | return txn->req.msg_state != old_req_state || |
| 5307 | txn->rsp.msg_state != old_res_state; |
| 5308 | } |
| 5309 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5310 | /* This function is an analyser which forwards request body (including chunk |
| 5311 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5312 | * zero byte. The only situation where it must not be called is when we're in |
| 5313 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5314 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5315 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5316 | * 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] | 5317 | * 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] | 5318 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5319 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5320 | 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] | 5321 | { |
| 5322 | struct http_txn *txn = &s->txn; |
| 5323 | struct http_msg *msg = &s->txn.req; |
| 5324 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5325 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5326 | return 0; |
| 5327 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5328 | 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] | 5329 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5330 | /* Output closed while we were sending data. We must abort and |
| 5331 | * wake the other side up. |
| 5332 | */ |
| 5333 | msg->msg_state = HTTP_MSG_ERROR; |
| 5334 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5335 | return 1; |
| 5336 | } |
| 5337 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5338 | /* Note that we don't have to send 100-continue back because we don't |
| 5339 | * need the data to complete our job, and it's up to the server to |
| 5340 | * decide whether to return 100, 417 or anything else in return of |
| 5341 | * an "Expect: 100-continue" header. |
| 5342 | */ |
| 5343 | |
Willy Tarreau | 5bebcd0 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5344 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5345 | /* we have msg->sov which points to the first byte of message |
| 5346 | * body, and req->buf.p still points to the beginning of the |
| 5347 | * message. We forward the headers now, as we don't need them |
| 5348 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5349 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5350 | b_adv(req->buf, msg->sov); |
| 5351 | msg->next -= msg->sov; |
| 5352 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5353 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5354 | /* The previous analysers guarantee that the state is somewhere |
| 5355 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 5356 | * msg->next are always correct. |
| 5357 | */ |
| 5358 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5359 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 5360 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5361 | else |
| 5362 | msg->msg_state = HTTP_MSG_DATA; |
| 5363 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5364 | } |
| 5365 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5366 | /* Some post-connect processing might want us to refrain from starting to |
| 5367 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5368 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5369 | */ |
| 5370 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
| 5371 | if (!(s->rep->flags & CF_READ_ATTACHED)) { |
| 5372 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5373 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5374 | goto missing_data; |
| 5375 | } |
| 5376 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5377 | } |
| 5378 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5379 | /* in most states, we should abort in case of early close */ |
| 5380 | channel_auto_close(req); |
| 5381 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5382 | if (req->to_forward) { |
| 5383 | /* We can't process the buffer's contents yet */ |
| 5384 | req->flags |= CF_WAKE_WRITE; |
| 5385 | goto missing_data; |
| 5386 | } |
| 5387 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5388 | while (1) { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5389 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5390 | /* must still forward */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5391 | /* we may have some pending data starting at req->buf->p */ |
| 5392 | if (msg->chunk_len > req->buf->i - msg->next) { |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5393 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5394 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5395 | } |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5396 | msg->next += msg->chunk_len; |
| 5397 | msg->chunk_len = 0; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5398 | |
| 5399 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5400 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5401 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5402 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5403 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5404 | } |
| 5405 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5406 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 5407 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5408 | * TRAILERS state. |
| 5409 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5410 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5411 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5412 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5413 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5414 | else if (ret < 0) { |
| 5415 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5416 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5417 | 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] | 5418 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5419 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5420 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5421 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5422 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5423 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5424 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5425 | |
| 5426 | if (ret == 0) |
| 5427 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5428 | else if (ret < 0) { |
| 5429 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5430 | if (msg->err_pos >= 0) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5431 | 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] | 5432 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5433 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5434 | /* we're in MSG_CHUNK_SIZE now */ |
| 5435 | } |
| 5436 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5437 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5438 | |
| 5439 | if (ret == 0) |
| 5440 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5441 | else if (ret < 0) { |
| 5442 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5443 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5444 | 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] | 5445 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5446 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5447 | /* we're in HTTP_MSG_DONE now */ |
| 5448 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5449 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5450 | int old_state = msg->msg_state; |
| 5451 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5452 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5453 | |
| 5454 | /* we may have some pending data starting at req->buf->p |
| 5455 | * such as last chunk of data or trailers. |
| 5456 | */ |
| 5457 | b_adv(req->buf, msg->next); |
Willy Tarreau | b4d0509 | 2014-09-01 20:35:55 +0200 | [diff] [blame] | 5458 | if (unlikely(!(s->req->flags & CF_WROTE_DATA))) |
Willy Tarreau | 5bebcd0 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5459 | msg->sov -= msg->next; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5460 | msg->next = 0; |
| 5461 | |
Willy Tarreau | 294e467 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5462 | /* we don't want to forward closes on DONE except in |
| 5463 | * tunnel mode. |
| 5464 | */ |
| 5465 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5466 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5467 | if (http_resync_states(s)) { |
| 5468 | /* some state changes occurred, maybe the analyser |
| 5469 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 5470 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5471 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5472 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5473 | /* request errors are most likely due to |
| 5474 | * the server aborting the transfer. |
| 5475 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5476 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5477 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5478 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5479 | 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] | 5480 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5481 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5482 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5483 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5484 | |
| 5485 | /* If "option abortonclose" is set on the backend, we |
| 5486 | * want to monitor the client's connection and forward |
| 5487 | * any shutdown notification to the server, which will |
| 5488 | * decide whether to close or to go on processing the |
Willy Tarreau | 294e467 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5489 | * request. We only do that in tunnel mode, and not in |
| 5490 | * other modes since it can be abused to exhaust source |
| 5491 | * ports. |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5492 | */ |
| 5493 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5494 | channel_auto_read(req); |
Willy Tarreau | 294e467 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5495 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 5496 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 5497 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5498 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5499 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5500 | else if (s->txn.meth == HTTP_METH_POST) { |
| 5501 | /* POST requests may require to read extra CRLF |
| 5502 | * sent by broken browsers and which could cause |
| 5503 | * an RST to be sent upon close on some systems |
| 5504 | * (eg: Linux). |
| 5505 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5506 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5507 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5508 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5509 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5510 | } |
| 5511 | } |
| 5512 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5513 | missing_data: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5514 | /* we may have some pending data starting at req->buf->p */ |
| 5515 | b_adv(req->buf, msg->next); |
Willy Tarreau | b4d0509 | 2014-09-01 20:35:55 +0200 | [diff] [blame] | 5516 | if (unlikely(!(s->req->flags & CF_WROTE_DATA))) |
Willy Tarreau | 5bebcd0 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5517 | msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i); |
| 5518 | |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5519 | msg->next = 0; |
| 5520 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
| 5521 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5522 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5523 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5524 | if (!(s->flags & SN_ERR_MASK)) |
| 5525 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5526 | if (!(s->flags & SN_FINST_MASK)) { |
| 5527 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5528 | s->flags |= SN_FINST_H; |
| 5529 | else |
| 5530 | s->flags |= SN_FINST_D; |
| 5531 | } |
| 5532 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5533 | s->fe->fe_counters.cli_aborts++; |
| 5534 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5535 | if (objt_server(s->target)) |
| 5536 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5537 | |
| 5538 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5539 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5540 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5541 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5542 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5543 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5544 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5545 | /* 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] | 5546 | * 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] | 5547 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5548 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5549 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5550 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5551 | /* 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] | 5552 | * 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] | 5553 | * 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] | 5554 | * modes are already handled by the stream sock layer. We must not do |
| 5555 | * this in content-length mode because it could present the MSG_MORE |
| 5556 | * flag with the last block of forwarded data, which would cause an |
| 5557 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5558 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5559 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5560 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5561 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5562 | return 0; |
| 5563 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5564 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5565 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5566 | if (s->listener->counters) |
| 5567 | s->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5568 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5569 | return_bad_req_stats_ok: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5570 | /* we may have some pending data starting at req->buf->p */ |
| 5571 | b_adv(req->buf, msg->next); |
| 5572 | msg->next = 0; |
| 5573 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5574 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5575 | if (txn->status) { |
| 5576 | /* Note: we don't send any error if some data were already sent */ |
| 5577 | stream_int_retnclose(req->prod, NULL); |
| 5578 | } else { |
| 5579 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5580 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5581 | } |
| 5582 | req->analysers = 0; |
| 5583 | 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] | 5584 | |
| 5585 | if (!(s->flags & SN_ERR_MASK)) |
| 5586 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5587 | if (!(s->flags & SN_FINST_MASK)) { |
| 5588 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5589 | s->flags |= SN_FINST_H; |
| 5590 | else |
| 5591 | s->flags |= SN_FINST_D; |
| 5592 | } |
| 5593 | return 0; |
| 5594 | |
| 5595 | aborted_xfer: |
| 5596 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5597 | if (txn->status) { |
| 5598 | /* Note: we don't send any error if some data were already sent */ |
| 5599 | stream_int_retnclose(req->prod, NULL); |
| 5600 | } else { |
| 5601 | txn->status = 502; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5602 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5603 | } |
| 5604 | req->analysers = 0; |
| 5605 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5606 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5607 | s->fe->fe_counters.srv_aborts++; |
| 5608 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5609 | if (objt_server(s->target)) |
| 5610 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5611 | |
| 5612 | if (!(s->flags & SN_ERR_MASK)) |
| 5613 | s->flags |= SN_ERR_SRVCL; |
| 5614 | if (!(s->flags & SN_FINST_MASK)) { |
| 5615 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5616 | s->flags |= SN_FINST_H; |
| 5617 | else |
| 5618 | s->flags |= SN_FINST_D; |
| 5619 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5620 | return 0; |
| 5621 | } |
| 5622 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5623 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5624 | * processing can continue on next analysers, or zero if it either needs more |
| 5625 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 5626 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 5627 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5628 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5629 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5630 | 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] | 5631 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5632 | struct http_txn *txn = &s->txn; |
| 5633 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5634 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5635 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5636 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5637 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5638 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5639 | 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] | 5640 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5641 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5642 | rep, |
| 5643 | rep->rex, rep->wex, |
| 5644 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5645 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5646 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5647 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5648 | /* |
| 5649 | * Now parse the partial (or complete) lines. |
| 5650 | * We will check the response syntax, and also join multi-line |
| 5651 | * headers. An index of all the lines will be elaborated while |
| 5652 | * parsing. |
| 5653 | * |
| 5654 | * For the parsing, we use a 28 states FSM. |
| 5655 | * |
| 5656 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5657 | * rep->buf->p = beginning of response |
| 5658 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5659 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5660 | * msg->eol = end of current header or line (LF or CRLF) |
| 5661 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5662 | */ |
| 5663 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5664 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5665 | /* There's a protected area at the end of the buffer for rewriting |
| 5666 | * purposes. We don't want to start to parse the request if the |
| 5667 | * protected area is affected, because we may have to move processed |
| 5668 | * data later, which is much more complicated. |
| 5669 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5670 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5671 | if (unlikely(!channel_reserved(rep))) { |
| 5672 | /* some data has still not left the buffer, wake us once that's done */ |
| 5673 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5674 | goto abort_response; |
| 5675 | channel_dont_close(rep); |
| 5676 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5677 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5678 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5679 | } |
| 5680 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5681 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5682 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5683 | buffer_slow_realign(rep->buf); |
| 5684 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5685 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5686 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5687 | } |
| 5688 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5689 | /* 1: we might have to print this header in debug mode */ |
| 5690 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5691 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 8767b13 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5692 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5693 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5694 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5695 | sol = rep->buf->p; |
| 5696 | 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] | 5697 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5698 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5699 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5700 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5701 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5702 | while (cur_idx) { |
| 5703 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5704 | debug_hdr("srvhdr", s, sol, eol); |
| 5705 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5706 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5707 | } |
| 5708 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5709 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5710 | /* |
| 5711 | * Now we quickly check if we have found a full valid response. |
| 5712 | * If not so, we check the FD and buffer states before leaving. |
| 5713 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5714 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5715 | * responses are checked first. |
| 5716 | * |
| 5717 | * Depending on whether the client is still there or not, we |
| 5718 | * may send an error response back or not. Note that normally |
| 5719 | * we should only check for HTTP status there, and check I/O |
| 5720 | * errors somewhere else. |
| 5721 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5722 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5723 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5724 | /* Invalid response */ |
| 5725 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5726 | /* we detected a parsing error. We want to archive this response |
| 5727 | * in the dedicated proxy area for later troubleshooting. |
| 5728 | */ |
| 5729 | hdr_response_bad: |
| 5730 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5731 | 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] | 5732 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5733 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5734 | if (objt_server(s->target)) { |
| 5735 | objt_server(s->target)->counters.failed_resp++; |
| 5736 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5737 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5738 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5739 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5740 | rep->analysers = 0; |
| 5741 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5742 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5743 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5744 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5745 | |
| 5746 | if (!(s->flags & SN_ERR_MASK)) |
| 5747 | s->flags |= SN_ERR_PRXCOND; |
| 5748 | if (!(s->flags & SN_FINST_MASK)) |
| 5749 | s->flags |= SN_FINST_H; |
| 5750 | |
| 5751 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5752 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5753 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5754 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5755 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5756 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5757 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5758 | goto hdr_response_bad; |
| 5759 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5760 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5761 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5762 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5763 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5764 | 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] | 5765 | else if (txn->flags & TX_NOT_FIRST) |
| 5766 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5767 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5768 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5769 | if (objt_server(s->target)) { |
| 5770 | objt_server(s->target)->counters.failed_resp++; |
| 5771 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5772 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5773 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5774 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5775 | rep->analysers = 0; |
| 5776 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5777 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5778 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5779 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5780 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5781 | if (!(s->flags & SN_ERR_MASK)) |
| 5782 | s->flags |= SN_ERR_SRVCL; |
| 5783 | if (!(s->flags & SN_FINST_MASK)) |
| 5784 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5785 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5786 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5787 | |
Willy Tarreau | 2a4f511 | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 5788 | /* read timeout : return a 504 to the client. */ |
| 5789 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5790 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5791 | 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] | 5792 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5793 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5794 | if (objt_server(s->target)) { |
| 5795 | objt_server(s->target)->counters.failed_resp++; |
| 5796 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5797 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5798 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5799 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5800 | rep->analysers = 0; |
| 5801 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5802 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5803 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5804 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5805 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5806 | if (!(s->flags & SN_ERR_MASK)) |
| 5807 | s->flags |= SN_ERR_SRVTO; |
| 5808 | if (!(s->flags & SN_FINST_MASK)) |
| 5809 | s->flags |= SN_FINST_H; |
| 5810 | return 0; |
| 5811 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5812 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5813 | /* client abort with an abortonclose */ |
| 5814 | else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
| 5815 | s->fe->fe_counters.cli_aborts++; |
| 5816 | s->be->be_counters.cli_aborts++; |
| 5817 | if (objt_server(s->target)) |
| 5818 | objt_server(s->target)->counters.cli_aborts++; |
| 5819 | |
| 5820 | rep->analysers = 0; |
| 5821 | channel_auto_close(rep); |
| 5822 | |
| 5823 | txn->status = 400; |
| 5824 | bi_erase(rep); |
| 5825 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400)); |
| 5826 | |
| 5827 | if (!(s->flags & SN_ERR_MASK)) |
| 5828 | s->flags |= SN_ERR_CLICL; |
| 5829 | if (!(s->flags & SN_FINST_MASK)) |
| 5830 | s->flags |= SN_FINST_H; |
| 5831 | |
| 5832 | /* process_session() will take care of the error */ |
| 5833 | return 0; |
| 5834 | } |
| 5835 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5836 | /* 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] | 5837 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5838 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5839 | 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] | 5840 | else if (txn->flags & TX_NOT_FIRST) |
| 5841 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5842 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5843 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5844 | if (objt_server(s->target)) { |
| 5845 | objt_server(s->target)->counters.failed_resp++; |
| 5846 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5847 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5848 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5849 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5850 | rep->analysers = 0; |
| 5851 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5852 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5853 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5854 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5855 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5856 | if (!(s->flags & SN_ERR_MASK)) |
| 5857 | s->flags |= SN_ERR_SRVCL; |
| 5858 | if (!(s->flags & SN_FINST_MASK)) |
| 5859 | s->flags |= SN_FINST_H; |
| 5860 | return 0; |
| 5861 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5862 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5863 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5864 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5865 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5866 | 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] | 5867 | else if (txn->flags & TX_NOT_FIRST) |
| 5868 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5869 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5870 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5871 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5872 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5873 | |
| 5874 | if (!(s->flags & SN_ERR_MASK)) |
| 5875 | s->flags |= SN_ERR_CLICL; |
| 5876 | if (!(s->flags & SN_FINST_MASK)) |
| 5877 | s->flags |= SN_FINST_H; |
| 5878 | |
| 5879 | /* process_session() will take care of the error */ |
| 5880 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5881 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5882 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5883 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5884 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5885 | return 0; |
| 5886 | } |
| 5887 | |
| 5888 | /* More interesting part now : we know that we have a complete |
| 5889 | * response which at least looks like HTTP. We have an indicator |
| 5890 | * of each header's length, so we can parse them quickly. |
| 5891 | */ |
| 5892 | |
| 5893 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5894 | 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] | 5895 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5896 | /* |
| 5897 | * 1: get the status code |
| 5898 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5899 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5900 | if (n < 1 || n > 5) |
| 5901 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5902 | /* when the client triggers a 4xx from the server, it's most often due |
| 5903 | * to a missing object or permission. These events should be tracked |
| 5904 | * because if they happen often, it may indicate a brute force or a |
| 5905 | * vulnerability scan. |
| 5906 | */ |
| 5907 | if (n == 4) |
| 5908 | session_inc_http_err_ctr(s); |
| 5909 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5910 | if (objt_server(s->target)) |
| 5911 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5912 | |
Willy Tarreau | 5564555 | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 5913 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 5914 | * exactly one digit "." one digit. This check may be disabled using |
| 5915 | * option accept-invalid-http-response. |
| 5916 | */ |
| 5917 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 5918 | if (msg->sl.st.v_l != 8) { |
| 5919 | msg->err_pos = 0; |
| 5920 | goto hdr_response_bad; |
| 5921 | } |
| 5922 | |
| 5923 | if (rep->buf->p[4] != '/' || |
| 5924 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 5925 | rep->buf->p[6] != '.' || |
| 5926 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 5927 | msg->err_pos = 4; |
| 5928 | goto hdr_response_bad; |
| 5929 | } |
| 5930 | } |
| 5931 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5932 | /* check if the response is HTTP/1.1 or above */ |
| 5933 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5934 | ((rep->buf->p[5] > '1') || |
| 5935 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5936 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5937 | |
| 5938 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5939 | 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] | 5940 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5941 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5942 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5943 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5944 | 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] | 5945 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5946 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5947 | * and 505 are triggered on demand by client request, so we must not |
| 5948 | * count them as server failures. |
| 5949 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5950 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5951 | 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] | 5952 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5953 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5954 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5955 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5956 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5957 | /* |
| 5958 | * 2: check for cacheability. |
| 5959 | */ |
| 5960 | |
| 5961 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5962 | case 100: |
| 5963 | /* |
| 5964 | * We may be facing a 100-continue response, in which case this |
| 5965 | * is not the right response, and we're waiting for the next one. |
| 5966 | * Let's allow this response to go to the client and wait for the |
| 5967 | * next one. |
| 5968 | */ |
| 5969 | hdr_idx_init(&txn->hdr_idx); |
| 5970 | msg->next -= channel_forward(rep, msg->next); |
| 5971 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5972 | txn->status = 0; |
| 5973 | s->logs.t_data = -1; /* was not a response yet */ |
| 5974 | goto next_one; |
| 5975 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5976 | case 200: |
| 5977 | case 203: |
| 5978 | case 206: |
| 5979 | case 300: |
| 5980 | case 301: |
| 5981 | case 410: |
| 5982 | /* RFC2616 @13.4: |
| 5983 | * "A response received with a status code of |
| 5984 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 5985 | * by a cache (...) unless a cache-control |
| 5986 | * directive prohibits caching." |
| 5987 | * |
| 5988 | * RFC2616 @9.5: POST method : |
| 5989 | * "Responses to this method are not cacheable, |
| 5990 | * unless the response includes appropriate |
| 5991 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5992 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5993 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5994 | ((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] | 5995 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 5996 | break; |
| 5997 | default: |
| 5998 | break; |
| 5999 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6000 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6001 | /* |
| 6002 | * 3: we may need to capture headers |
| 6003 | */ |
| 6004 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 6005 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6006 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6007 | txn->rsp.cap, s->fe->rsp_cap); |
| 6008 | |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6009 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 6010 | * by RFC7230#3.3.3 : |
| 6011 | * |
| 6012 | * The length of a message body is determined by one of the following |
| 6013 | * (in order of precedence): |
| 6014 | * |
| 6015 | * 1. Any response to a HEAD request and any response with a 1xx |
| 6016 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 6017 | * code is always terminated by the first empty line after the |
| 6018 | * header fields, regardless of the header fields present in the |
| 6019 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6020 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6021 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 6022 | * the connection will become a tunnel immediately after the empty |
| 6023 | * line that concludes the header fields. A client MUST ignore any |
| 6024 | * Content-Length or Transfer-Encoding header fields received in |
| 6025 | * such a message. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6026 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6027 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 6028 | * transfer coding (Section 4.1) is the final encoding, the message |
| 6029 | * body length is determined by reading and decoding the chunked |
| 6030 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6031 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6032 | * If a Transfer-Encoding header field is present in a response and |
| 6033 | * the chunked transfer coding is not the final encoding, the |
| 6034 | * message body length is determined by reading the connection until |
| 6035 | * it is closed by the server. If a Transfer-Encoding header field |
| 6036 | * is present in a request and the chunked transfer coding is not |
| 6037 | * the final encoding, the message body length cannot be determined |
| 6038 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 6039 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6040 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6041 | * If a message is received with both a Transfer-Encoding and a |
| 6042 | * Content-Length header field, the Transfer-Encoding overrides the |
| 6043 | * Content-Length. Such a message might indicate an attempt to |
| 6044 | * perform request smuggling (Section 9.5) or response splitting |
| 6045 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 6046 | * remove the received Content-Length field prior to forwarding such |
| 6047 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6048 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6049 | * 4. If a message is received without Transfer-Encoding and with |
| 6050 | * either multiple Content-Length header fields having differing |
| 6051 | * field-values or a single Content-Length header field having an |
| 6052 | * invalid value, then the message framing is invalid and the |
| 6053 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 6054 | * request message, the server MUST respond with a 400 (Bad Request) |
| 6055 | * status code and then close the connection. If this is a response |
| 6056 | * message received by a proxy, the proxy MUST close the connection |
| 6057 | * to the server, discard the received response, and send a 502 (Bad |
| 6058 | * Gateway) response to the client. If this is a response message |
| 6059 | * received by a user agent, the user agent MUST close the |
| 6060 | * connection to the server and discard the received response. |
| 6061 | * |
| 6062 | * 5. If a valid Content-Length header field is present without |
| 6063 | * Transfer-Encoding, its decimal value defines the expected message |
| 6064 | * body length in octets. If the sender closes the connection or |
| 6065 | * the recipient times out before the indicated number of octets are |
| 6066 | * received, the recipient MUST consider the message to be |
| 6067 | * incomplete and close the connection. |
| 6068 | * |
| 6069 | * 6. If this is a request message and none of the above are true, then |
| 6070 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6071 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6072 | * 7. Otherwise, this is a response message without a declared message |
| 6073 | * body length, so the message body length is determined by the |
| 6074 | * number of octets received prior to the server closing the |
| 6075 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6076 | */ |
| 6077 | |
| 6078 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6079 | * 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] | 6080 | * 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] | 6081 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6082 | */ |
| 6083 | if (txn->meth == HTTP_METH_HEAD || |
| 6084 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6085 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6086 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 6087 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6088 | goto skip_content_length; |
| 6089 | } |
| 6090 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6091 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6092 | ctx.idx = 0; |
Willy Tarreau | e77bc17 | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 6093 | 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] | 6094 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6095 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6096 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6097 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6098 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6099 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6100 | break; |
| 6101 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6102 | } |
| 6103 | |
Willy Tarreau | dfa3d92 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6104 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6105 | ctx.idx = 0; |
Willy Tarreau | 660418d | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6106 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | dfa3d92 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6107 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 6108 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 6109 | } |
Willy Tarreau | 660418d | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6110 | 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] | 6111 | signed long long cl; |
| 6112 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6113 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6114 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6115 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6116 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6117 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6118 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6119 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6120 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6121 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6122 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6123 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6124 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6125 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6126 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6127 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6128 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6129 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6130 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6131 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6132 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6133 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6134 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6135 | } |
| 6136 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6137 | if (s->fe->comp || s->be->comp) |
| 6138 | select_compression_response_header(s, rep->buf); |
| 6139 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6140 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6141 | /* Now we have to check if we need to modify the Connection header. |
| 6142 | * This is more difficult on the response than it is on the request, |
| 6143 | * because we can have two different HTTP versions and we don't know |
| 6144 | * how the client will interprete a response. For instance, let's say |
| 6145 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6146 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6147 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6148 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6149 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6150 | * indicating how a request MAY be understood by the client. In case |
| 6151 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6152 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6153 | * will fall back to explicit close. Note that we won't take risks with |
| 6154 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6155 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6156 | */ |
| 6157 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6158 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6159 | txn->status == 101)) { |
| 6160 | /* Either we've established an explicit tunnel, or we're |
| 6161 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6162 | * to understand the next protocols. We have to switch to tunnel |
| 6163 | * mode, so that we transfer the request and responses then let |
| 6164 | * this protocol pass unmodified. When we later implement specific |
| 6165 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6166 | * header which contains information about that protocol for |
| 6167 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6168 | */ |
| 6169 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6170 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6171 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6172 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6173 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6174 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6175 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6176 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6177 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6178 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6179 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6180 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6181 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6182 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6183 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6184 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6185 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6186 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6187 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6188 | /* now adjust header transformations depending on current state */ |
| 6189 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6190 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6191 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6192 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6193 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6194 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6195 | else { /* SCL / KAL */ |
| 6196 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6197 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6198 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6199 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6200 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6201 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6202 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6203 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6204 | /* Some keep-alive responses are converted to Server-close if |
| 6205 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6206 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6207 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6208 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6209 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6210 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6211 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6212 | } |
| 6213 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6214 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6215 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6216 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6217 | /* end of job, return OK */ |
| 6218 | rep->analysers &= ~an_bit; |
| 6219 | rep->analyse_exp = TICK_ETERNITY; |
| 6220 | channel_auto_close(rep); |
| 6221 | return 1; |
| 6222 | |
| 6223 | abort_keep_alive: |
| 6224 | /* A keep-alive request to the server failed on a network error. |
| 6225 | * The client is required to retry. We need to close without returning |
| 6226 | * any other information so that the client retries. |
| 6227 | */ |
| 6228 | txn->status = 0; |
| 6229 | rep->analysers = 0; |
| 6230 | s->req->analysers = 0; |
| 6231 | channel_auto_close(rep); |
| 6232 | s->logs.logwait = 0; |
| 6233 | s->logs.level = 0; |
| 6234 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 6235 | bi_erase(rep); |
| 6236 | stream_int_retnclose(rep->cons, NULL); |
| 6237 | return 0; |
| 6238 | } |
| 6239 | |
| 6240 | /* This function performs all the processing enabled for the current response. |
| 6241 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 6242 | * and updates s->rep->analysers. It might make sense to explode it into several |
| 6243 | * other functions. It works like process_request (see indications above). |
| 6244 | */ |
| 6245 | int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px) |
| 6246 | { |
| 6247 | struct http_txn *txn = &s->txn; |
| 6248 | struct http_msg *msg = &txn->rsp; |
| 6249 | struct proxy *cur_proxy; |
| 6250 | struct cond_wordlist *wl; |
| 6251 | struct http_res_rule *http_res_last_rule = NULL; |
| 6252 | |
| 6253 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 6254 | now_ms, __FUNCTION__, |
| 6255 | s, |
| 6256 | rep, |
| 6257 | rep->rex, rep->wex, |
| 6258 | rep->flags, |
| 6259 | rep->buf->i, |
| 6260 | rep->analysers); |
| 6261 | |
| 6262 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6263 | return 0; |
| 6264 | |
| 6265 | rep->analysers &= ~an_bit; |
| 6266 | rep->analyse_exp = TICK_ETERNITY; |
| 6267 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6268 | /* The stats applet needs to adjust the Connection header but we don't |
| 6269 | * apply any filter there. |
| 6270 | */ |
| 6271 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) |
| 6272 | goto skip_filters; |
| 6273 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6274 | /* |
| 6275 | * We will have to evaluate the filters. |
| 6276 | * As opposed to version 1.2, now they will be evaluated in the |
| 6277 | * filters order and not in the header order. This means that |
| 6278 | * each filter has to be validated among all headers. |
| 6279 | * |
| 6280 | * Filters are tried with ->be first, then with ->fe if it is |
| 6281 | * different from ->be. |
| 6282 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6283 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6284 | cur_proxy = s->be; |
| 6285 | while (1) { |
| 6286 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6287 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6288 | /* evaluate http-response rules */ |
| 6289 | if (!http_res_last_rule) |
| 6290 | 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] | 6291 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6292 | /* try headers filters */ |
| 6293 | if (rule_set->rsp_exp != NULL) { |
| 6294 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6295 | return_bad_resp: |
| 6296 | if (objt_server(s->target)) { |
| 6297 | objt_server(s->target)->counters.failed_resp++; |
| 6298 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6299 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6300 | s->be->be_counters.failed_resp++; |
| 6301 | return_srv_prx_502: |
| 6302 | rep->analysers = 0; |
| 6303 | txn->status = 502; |
| 6304 | s->logs.t_data = -1; /* was not a valid response */ |
| 6305 | rep->prod->flags |= SI_FL_NOLINGER; |
| 6306 | bi_erase(rep); |
| 6307 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
| 6308 | if (!(s->flags & SN_ERR_MASK)) |
| 6309 | s->flags |= SN_ERR_PRXCOND; |
| 6310 | if (!(s->flags & SN_FINST_MASK)) |
| 6311 | s->flags |= SN_FINST_H; |
| 6312 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6313 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6314 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6315 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6316 | /* has the response been denied ? */ |
| 6317 | if (txn->flags & TX_SVDENY) { |
| 6318 | if (objt_server(s->target)) |
| 6319 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6320 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6321 | s->be->be_counters.denied_resp++; |
| 6322 | s->fe->fe_counters.denied_resp++; |
| 6323 | if (s->listener->counters) |
| 6324 | s->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6325 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6326 | goto return_srv_prx_502; |
| 6327 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6328 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6329 | /* add response headers from the rule sets in the same order */ |
| 6330 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 0cb4b89 | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6331 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6332 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6333 | if (wl->cond) { |
| 6334 | int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 6335 | ret = acl_pass(ret); |
| 6336 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6337 | ret = !ret; |
| 6338 | if (!ret) |
| 6339 | continue; |
| 6340 | } |
| 6341 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6342 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6343 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6344 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6345 | /* check whether we're already working on the frontend */ |
| 6346 | if (cur_proxy == s->fe) |
| 6347 | break; |
| 6348 | cur_proxy = s->fe; |
| 6349 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6350 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6351 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | 0cb4b89 | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6352 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6353 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6354 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6355 | /* |
| 6356 | * Now check for a server cookie. |
| 6357 | */ |
| 6358 | if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name || |
| 6359 | (s->be->options & PR_O_CHK_CACHE)) |
| 6360 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6361 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6362 | /* |
| 6363 | * Check for cache-control or pragma headers if required. |
| 6364 | */ |
Willy Tarreau | 0cb4b89 | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6365 | 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] | 6366 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6367 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6368 | /* |
| 6369 | * Add server cookie in the response if needed |
| 6370 | */ |
| 6371 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6372 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 6373 | (!(s->flags & SN_DIRECT) || |
| 6374 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6375 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6376 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6377 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6378 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 6379 | !(s->flags & SN_IGNORE_PRST)) { |
| 6380 | /* the server is known, it's not the one the client requested, or the |
| 6381 | * cookie's last seen date needs to be refreshed. We have to |
| 6382 | * insert a set-cookie here, except if we want to insert only on POST |
| 6383 | * requests and this one isn't. Note that servers which don't have cookies |
| 6384 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6385 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6386 | if (!objt_server(s->target)->cookie) { |
| 6387 | chunk_printf(&trash, |
| 6388 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6389 | s->be->cookie_name); |
| 6390 | } |
| 6391 | else { |
| 6392 | 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] | 6393 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6394 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6395 | /* emit last_date, which is mandatory */ |
| 6396 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6397 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6398 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6399 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6400 | if (s->be->cookie_maxlife) { |
| 6401 | /* emit first_date, which is either the original one or |
| 6402 | * the current date. |
| 6403 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6404 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6405 | s30tob64(txn->cookie_first_date ? |
| 6406 | txn->cookie_first_date >> 2 : |
| 6407 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6408 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6409 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6410 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6411 | chunk_appendf(&trash, "; path=/"); |
| 6412 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6413 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6414 | if (s->be->cookie_domain) |
| 6415 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6416 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6417 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6418 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6419 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6420 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6421 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6422 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6423 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6424 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6425 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6426 | txn->flags &= ~TX_SCK_MASK; |
| 6427 | if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT)) |
| 6428 | /* the server did not change, only the date was updated */ |
| 6429 | txn->flags |= TX_SCK_UPDATED; |
| 6430 | else |
| 6431 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6432 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6433 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6434 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6435 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6436 | * 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] | 6437 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6438 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6439 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6440 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6441 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6442 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6443 | "Cache-control: private", 22) < 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 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6447 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6448 | /* |
| 6449 | * Check if result will be cacheable with a cookie. |
| 6450 | * We'll block the response if security checks have caught |
| 6451 | * nasty things such as a cacheable cookie. |
| 6452 | */ |
| 6453 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6454 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6455 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6456 | /* we're in presence of a cacheable response containing |
| 6457 | * a set-cookie header. We'll block it as requested by |
| 6458 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6459 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6460 | if (objt_server(s->target)) |
| 6461 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6462 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6463 | s->be->be_counters.denied_resp++; |
| 6464 | s->fe->fe_counters.denied_resp++; |
| 6465 | if (s->listener->counters) |
| 6466 | s->listener->counters->denied_resp++; |
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 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6469 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6470 | send_log(s->be, LOG_ALERT, |
| 6471 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6472 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6473 | goto return_srv_prx_502; |
| 6474 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6475 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6476 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6477 | /* |
| 6478 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6479 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6480 | * 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] | 6481 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6482 | * want to touch any 101 response either since it's switching to another |
| 6483 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6484 | */ |
Willy Tarreau | 0cb4b89 | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6485 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6486 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 6487 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6488 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6489 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6490 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6491 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6492 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6493 | /* we want a keep-alive response here. Keep-alive header |
| 6494 | * required if either side is not 1.1. |
| 6495 | */ |
| 6496 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6497 | want_flags |= TX_CON_KAL_SET; |
| 6498 | } |
| 6499 | else { |
| 6500 | /* we want a close response here. Close header required if |
| 6501 | * the server is 1.1, regardless of the client. |
| 6502 | */ |
| 6503 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6504 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6505 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6506 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6507 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6508 | http_change_connection_header(txn, msg, want_flags); |
| 6509 | } |
| 6510 | |
| 6511 | skip_header_mangling: |
| 6512 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
| 6513 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 6514 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6515 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6516 | /* if the user wants to log as soon as possible, without counting |
| 6517 | * bytes from the server, then this is the right moment. We have |
| 6518 | * to temporarily assign bytes_out to log what we currently have. |
| 6519 | */ |
| 6520 | if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 6521 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6522 | s->logs.bytes_out = txn->rsp.eoh; |
| 6523 | s->do_log(s); |
| 6524 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6525 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6526 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6527 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6528 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6529 | /* This function is an analyser which forwards response body (including chunk |
| 6530 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6531 | * zero byte. The only situation where it must not be called is when we're in |
| 6532 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6533 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6534 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 6535 | * 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] | 6536 | * |
| 6537 | * It is capable of compressing response data both in content-length mode and |
| 6538 | * in chunked mode. The state machines follows different flows depending on |
| 6539 | * whether content-length and chunked modes are used, since there are no |
| 6540 | * trailers in content-length : |
| 6541 | * |
| 6542 | * chk-mode cl-mode |
| 6543 | * ,----- BODY -----. |
| 6544 | * / \ |
| 6545 | * V size > 0 V chk-mode |
| 6546 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6547 | * | | size == 0 | last byte | |
| 6548 | * | v final crlf v inspected | |
| 6549 | * | TRAILERS -----------> DONE | |
| 6550 | * | | |
| 6551 | * `----------------------------------------------' |
| 6552 | * |
| 6553 | * Compression only happens in the DATA state, and must be flushed in final |
| 6554 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6555 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6556 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6557 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6558 | 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] | 6559 | { |
| 6560 | struct http_txn *txn = &s->txn; |
| 6561 | struct http_msg *msg = &s->txn.rsp; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6562 | static struct buffer *tmpbuf = NULL; |
| 6563 | int compressing = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6564 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6565 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6566 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6567 | return 0; |
| 6568 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6569 | 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] | 6570 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 6571 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6572 | /* Output closed while we were sending data. We must abort and |
| 6573 | * wake the other side up. |
| 6574 | */ |
| 6575 | msg->msg_state = HTTP_MSG_ERROR; |
| 6576 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6577 | return 1; |
| 6578 | } |
| 6579 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6580 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6581 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6582 | |
Willy Tarreau | 5bebcd0 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 6583 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6584 | /* we have msg->sov which points to the first byte of message |
| 6585 | * body, and res->buf.p still points to the beginning of the |
| 6586 | * message. We forward the headers now, as we don't need them |
| 6587 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6588 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6589 | b_adv(res->buf, msg->sov); |
| 6590 | msg->next -= msg->sov; |
| 6591 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6592 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6593 | /* The previous analysers guarantee that the state is somewhere |
| 6594 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 6595 | * msg->next are always correct. |
| 6596 | */ |
| 6597 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 6598 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 6599 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6600 | else |
| 6601 | msg->msg_state = HTTP_MSG_DATA; |
| 6602 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6603 | } |
| 6604 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6605 | if (res->to_forward) { |
| 6606 | /* We can't process the buffer's contents yet */ |
| 6607 | res->flags |= CF_WAKE_WRITE; |
| 6608 | goto missing_data; |
| 6609 | } |
| 6610 | |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6611 | if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) { |
| 6612 | /* We need a compression buffer in the DATA state to put the |
| 6613 | * output of compressed data, and in CRLF state to let the |
| 6614 | * TRAILERS state finish the job of removing the trailing CRLF. |
| 6615 | */ |
| 6616 | if (unlikely(tmpbuf == NULL)) { |
| 6617 | /* this is the first time we need the compression buffer */ |
| 6618 | tmpbuf = pool_alloc2(pool2_buffer); |
| 6619 | if (tmpbuf == NULL) |
| 6620 | goto aborted_xfer; /* no memory */ |
| 6621 | } |
| 6622 | |
| 6623 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6624 | if (ret < 0) { |
| 6625 | res->flags |= CF_WAKE_WRITE; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6626 | goto missing_data; /* not enough spaces in buffers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6627 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6628 | compressing = 1; |
| 6629 | } |
| 6630 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6631 | while (1) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6632 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6633 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6634 | /* we may have some pending data starting at res->buf->p */ |
| 6635 | if (unlikely(s->comp_algo)) { |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 6636 | ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6637 | if (ret < 0) |
| 6638 | goto aborted_xfer; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6639 | |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6640 | if (msg->chunk_len) { |
| 6641 | /* input empty or output full */ |
| 6642 | if (res->buf->i > msg->next) |
| 6643 | res->flags |= CF_WAKE_WRITE; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6644 | goto missing_data; |
| 6645 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6646 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6647 | else { |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6648 | if (msg->chunk_len > res->buf->i - msg->next) { |
| 6649 | /* output full */ |
| 6650 | res->flags |= CF_WAKE_WRITE; |
| 6651 | goto missing_data; |
| 6652 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6653 | msg->next += msg->chunk_len; |
| 6654 | msg->chunk_len = 0; |
| 6655 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6656 | |
| 6657 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6658 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6659 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6660 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6661 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6662 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6663 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6664 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6665 | |
| 6666 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 6667 | /* we want the CRLF after the data */ |
| 6668 | |
| 6669 | ret = http_skip_chunk_crlf(msg); |
| 6670 | if (ret == 0) |
| 6671 | goto missing_data; |
| 6672 | else if (ret < 0) { |
| 6673 | if (msg->err_pos >= 0) |
| 6674 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe); |
| 6675 | goto return_bad_res; |
| 6676 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6677 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 6678 | |
| 6679 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6680 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 6681 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6682 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6683 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6684 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6685 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6686 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6687 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6688 | else if (ret < 0) { |
| 6689 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6690 | 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] | 6691 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6692 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 6693 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6694 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6695 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6696 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6697 | if (unlikely(compressing)) { |
| 6698 | /* we need to flush output contents before syncing FSMs */ |
| 6699 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6700 | compressing = 0; |
| 6701 | } |
| 6702 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6703 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6704 | if (ret == 0) |
| 6705 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6706 | else if (ret < 0) { |
| 6707 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6708 | 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] | 6709 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6710 | } |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6711 | /* we're in HTTP_MSG_DONE now, fall through */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6712 | |
| 6713 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6714 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6715 | if (unlikely(compressing)) { |
| 6716 | /* we need to flush output contents before syncing FSMs */ |
| 6717 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6718 | compressing = 0; |
| 6719 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6720 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6721 | /* we may have some pending data starting at res->buf->p |
| 6722 | * such as a last chunk of data or trailers. |
| 6723 | */ |
| 6724 | b_adv(res->buf, msg->next); |
| 6725 | msg->next = 0; |
| 6726 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6727 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6728 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6729 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6730 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6731 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6732 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6733 | if (http_resync_states(s)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6734 | /* some state changes occurred, maybe the analyser |
| 6735 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6736 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6737 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6738 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6739 | /* response errors are most likely due to |
| 6740 | * the client aborting the transfer. |
| 6741 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6742 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6743 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6744 | if (msg->err_pos >= 0) |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6745 | 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] | 6746 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6747 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6748 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6749 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6750 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6751 | } |
| 6752 | } |
| 6753 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6754 | missing_data: |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6755 | /* we may have some pending data starting at res->buf->p */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6756 | if (unlikely(compressing)) { |
| 6757 | 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] | 6758 | compressing = 0; |
| 6759 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6760 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6761 | if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) { |
| 6762 | b_adv(res->buf, msg->next); |
| 6763 | msg->next = 0; |
| 6764 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6765 | } |
| 6766 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6767 | if (res->flags & CF_SHUTW) |
| 6768 | goto aborted_xfer; |
| 6769 | |
| 6770 | /* stop waiting for data if the input is closed before the end. If the |
| 6771 | * client side was already closed, it means that the client has aborted, |
| 6772 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6773 | * server abort. |
| 6774 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6775 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | b2c6a78 | 2014-04-23 20:29:01 +0200 | [diff] [blame] | 6776 | if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6777 | goto aborted_xfer; |
Christopher Faulet | be80167 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6778 | /* If we have some pending data, we continue the processing */ |
| 6779 | if (!buffer_pending(res->buf)) { |
| 6780 | if (!(s->flags & SN_ERR_MASK)) |
| 6781 | s->flags |= SN_ERR_SRVCL; |
| 6782 | s->be->be_counters.srv_aborts++; |
| 6783 | if (objt_server(s->target)) |
| 6784 | objt_server(s->target)->counters.srv_aborts++; |
| 6785 | goto return_bad_res_stats_ok; |
| 6786 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6787 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6788 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6789 | /* 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] | 6790 | if (!s->req->analysers) |
| 6791 | goto return_bad_res; |
| 6792 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6793 | /* 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] | 6794 | * 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] | 6795 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 6796 | * close. |
| 6797 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6798 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6799 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6800 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6801 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6802 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6803 | /* 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] | 6804 | * 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] | 6805 | * 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] | 6806 | * modes are already handled by the stream sock layer. We must not do |
| 6807 | * this in content-length mode because it could present the MSG_MORE |
| 6808 | * flag with the last block of forwarded data, which would cause an |
| 6809 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6810 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6811 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6812 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6813 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6814 | /* the session handler will take care of timeouts and errors */ |
| 6815 | return 0; |
| 6816 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6817 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6818 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6819 | if (objt_server(s->target)) |
| 6820 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6821 | |
| 6822 | return_bad_res_stats_ok: |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 6823 | if (unlikely(compressing)) { |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6824 | 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] | 6825 | compressing = 0; |
| 6826 | } |
| 6827 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6828 | /* we may have some pending data starting at res->buf->p */ |
| 6829 | if (s->comp_algo == NULL) { |
| 6830 | b_adv(res->buf, msg->next); |
| 6831 | msg->next = 0; |
| 6832 | } |
| 6833 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6834 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6835 | /* don't send any error message as we're in the body */ |
| 6836 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6837 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6838 | 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] | 6839 | if (objt_server(s->target)) |
| 6840 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6841 | |
| 6842 | if (!(s->flags & SN_ERR_MASK)) |
| 6843 | s->flags |= SN_ERR_PRXCOND; |
| 6844 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6845 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6846 | return 0; |
| 6847 | |
| 6848 | aborted_xfer: |
Willy Tarreau | 6fef8ae | 2014-04-22 21:22:06 +0200 | [diff] [blame] | 6849 | if (unlikely(compressing)) { |
| 6850 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
| 6851 | compressing = 0; |
| 6852 | } |
| 6853 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6854 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6855 | /* don't send any error message as we're in the body */ |
| 6856 | stream_int_retnclose(res->cons, NULL); |
| 6857 | res->analysers = 0; |
| 6858 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 6859 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6860 | s->fe->fe_counters.cli_aborts++; |
| 6861 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6862 | if (objt_server(s->target)) |
| 6863 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6864 | |
| 6865 | if (!(s->flags & SN_ERR_MASK)) |
| 6866 | s->flags |= SN_ERR_CLICL; |
| 6867 | if (!(s->flags & SN_FINST_MASK)) |
| 6868 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6869 | return 0; |
| 6870 | } |
| 6871 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6872 | /* Iterate the same filter through all request headers. |
| 6873 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6874 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6875 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6876 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6877 | 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] | 6878 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6879 | char *cur_ptr, *cur_end, *cur_next; |
| 6880 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6881 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6882 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6883 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6884 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6885 | last_hdr = 0; |
| 6886 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6887 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6888 | old_idx = 0; |
| 6889 | |
| 6890 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6891 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6892 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6893 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6894 | (exp->action == ACT_ALLOW || |
| 6895 | exp->action == ACT_DENY || |
| 6896 | exp->action == ACT_TARPIT)) |
| 6897 | return 0; |
| 6898 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6899 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6900 | if (!cur_idx) |
| 6901 | break; |
| 6902 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6903 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6904 | cur_ptr = cur_next; |
| 6905 | cur_end = cur_ptr + cur_hdr->len; |
| 6906 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6907 | |
| 6908 | /* Now we have one header between cur_ptr and cur_end, |
| 6909 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6910 | */ |
| 6911 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 6912 | 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] | 6913 | switch (exp->action) { |
| 6914 | case ACT_SETBE: |
| 6915 | /* It is not possible to jump a second time. |
| 6916 | * FIXME: should we return an HTTP/500 here so that |
| 6917 | * the admin knows there's a problem ? |
| 6918 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6919 | if (s->be != s->fe) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6920 | break; |
| 6921 | |
| 6922 | /* Swithing Proxy */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6923 | session_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6924 | last_hdr = 1; |
| 6925 | break; |
| 6926 | |
| 6927 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6928 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6929 | last_hdr = 1; |
| 6930 | break; |
| 6931 | |
| 6932 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6933 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6934 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6935 | break; |
| 6936 | |
| 6937 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6938 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6939 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6940 | break; |
| 6941 | |
| 6942 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6943 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6944 | if (trash.len < 0) |
| 6945 | return -1; |
| 6946 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6947 | 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] | 6948 | /* FIXME: if the user adds a newline in the replacement, the |
| 6949 | * index will not be recalculated for now, and the new line |
| 6950 | * will not be counted as a new header. |
| 6951 | */ |
| 6952 | |
| 6953 | cur_end += delta; |
| 6954 | cur_next += delta; |
| 6955 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6956 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6957 | break; |
| 6958 | |
| 6959 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6960 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6961 | cur_next += delta; |
| 6962 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6963 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6964 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6965 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6966 | cur_hdr->len = 0; |
| 6967 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6968 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6969 | break; |
| 6970 | |
| 6971 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6972 | } |
| 6973 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6974 | /* keep the link from this header to next one in case of later |
| 6975 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6976 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6977 | old_idx = cur_idx; |
| 6978 | } |
| 6979 | return 0; |
| 6980 | } |
| 6981 | |
| 6982 | |
| 6983 | /* Apply the filter to the request line. |
| 6984 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6985 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6986 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6987 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6988 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6989 | 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] | 6990 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6991 | char *cur_ptr, *cur_end; |
| 6992 | int done; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6993 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6994 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6995 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6996 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6997 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6998 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6999 | (exp->action == ACT_ALLOW || |
| 7000 | exp->action == ACT_DENY || |
| 7001 | exp->action == ACT_TARPIT)) |
| 7002 | return 0; |
| 7003 | else if (exp->action == ACT_REMOVE) |
| 7004 | return 0; |
| 7005 | |
| 7006 | done = 0; |
| 7007 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7008 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7009 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7010 | |
| 7011 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7012 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 7013 | 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] | 7014 | switch (exp->action) { |
| 7015 | case ACT_SETBE: |
| 7016 | /* It is not possible to jump a second time. |
| 7017 | * FIXME: should we return an HTTP/500 here so that |
| 7018 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7019 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7020 | if (s->be != s->fe) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7021 | break; |
| 7022 | |
| 7023 | /* Swithing Proxy */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7024 | session_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7025 | done = 1; |
| 7026 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7027 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7028 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7029 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7030 | done = 1; |
| 7031 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7032 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7033 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7034 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7035 | done = 1; |
| 7036 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7037 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7038 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7039 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7040 | done = 1; |
| 7041 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7042 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7043 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7044 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7045 | if (trash.len < 0) |
| 7046 | return -1; |
| 7047 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7048 | 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] | 7049 | /* FIXME: if the user adds a newline in the replacement, the |
| 7050 | * index will not be recalculated for now, and the new line |
| 7051 | * will not be counted as a new header. |
| 7052 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7053 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7054 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7055 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7056 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7057 | HTTP_MSG_RQMETH, |
| 7058 | cur_ptr, cur_end + 1, |
| 7059 | NULL, NULL); |
| 7060 | if (unlikely(!cur_end)) |
| 7061 | return -1; |
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 | /* we have a full request and we know that we have either a CR |
| 7064 | * or an LF at <ptr>. |
| 7065 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7066 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7067 | 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] | 7068 | /* there is no point trying this regex on headers */ |
| 7069 | return 1; |
| 7070 | } |
| 7071 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7072 | return done; |
| 7073 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7074 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7075 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7076 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7077 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7078 | * 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] | 7079 | * 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] | 7080 | * unparsable request. Since it can manage the switch to another backend, it |
| 7081 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7082 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7083 | 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] | 7084 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7085 | struct http_txn *txn = &s->txn; |
| 7086 | struct hdr_exp *exp; |
| 7087 | |
| 7088 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7089 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7090 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7091 | /* |
| 7092 | * The interleaving of transformations and verdicts |
| 7093 | * makes it difficult to decide to continue or stop |
| 7094 | * the evaluation. |
| 7095 | */ |
| 7096 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7097 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7098 | break; |
| 7099 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7100 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7101 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7102 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7103 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7104 | |
| 7105 | /* if this filter had a condition, evaluate it now and skip to |
| 7106 | * next filter if the condition does not match. |
| 7107 | */ |
| 7108 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7109 | 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] | 7110 | ret = acl_pass(ret); |
| 7111 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7112 | ret = !ret; |
| 7113 | |
| 7114 | if (!ret) |
| 7115 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7116 | } |
| 7117 | |
| 7118 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7119 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7120 | if (unlikely(ret < 0)) |
| 7121 | return -1; |
| 7122 | |
| 7123 | if (likely(ret == 0)) { |
| 7124 | /* The filter did not match the request, it can be |
| 7125 | * iterated through all headers. |
| 7126 | */ |
Willy Tarreau | a256fae | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7127 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7128 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7129 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7130 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7131 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7132 | } |
| 7133 | |
| 7134 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7135 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7136 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7137 | * Try to retrieve the server associated to the appsession. |
| 7138 | * If the server is found, it's assigned to the session. |
| 7139 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7140 | void manage_client_side_appsession(struct session *s, const char *buf, int len) { |
| 7141 | struct http_txn *txn = &s->txn; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7142 | appsess *asession = NULL; |
| 7143 | char *sessid_temp = NULL; |
| 7144 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7145 | if (len > s->be->appsession_len) { |
| 7146 | len = s->be->appsession_len; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7147 | } |
| 7148 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7149 | if (s->be->options2 & PR_O2_AS_REQL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7150 | /* 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] | 7151 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7152 | /* 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] | 7153 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7154 | } |
| 7155 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7156 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7157 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7158 | 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] | 7159 | return; |
| 7160 | } |
| 7161 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7162 | memcpy(txn->sessid, buf, len); |
| 7163 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7164 | } |
| 7165 | |
| 7166 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 7167 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7168 | 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] | 7169 | return; |
| 7170 | } |
| 7171 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7172 | memcpy(sessid_temp, buf, len); |
| 7173 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7174 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7175 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7176 | /* free previously allocated memory */ |
| 7177 | pool_free2(apools.sessid, sessid_temp); |
| 7178 | |
| 7179 | if (asession != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7180 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
| 7181 | if (!(s->be->options2 & PR_O2_AS_REQL)) |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7182 | asession->request_count++; |
| 7183 | |
| 7184 | if (asession->serverid != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7185 | struct server *srv = s->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7186 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7187 | while (srv) { |
| 7188 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7189 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7190 | (s->be->options & PR_O_PERSIST) || |
| 7191 | (s->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7192 | /* we found the server and it's usable */ |
| 7193 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7194 | 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] | 7195 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 7196 | s->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 7197 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7198 | break; |
| 7199 | } else { |
| 7200 | txn->flags &= ~TX_CK_MASK; |
| 7201 | txn->flags |= TX_CK_DOWN; |
| 7202 | } |
| 7203 | } |
| 7204 | srv = srv->next; |
| 7205 | } |
| 7206 | } |
| 7207 | } |
| 7208 | } |
| 7209 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7210 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7211 | * same way as with headers above except that the semi-colon also ends a token. |
| 7212 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7213 | * return a valid result. |
| 7214 | */ |
| 7215 | char *find_cookie_value_end(char *s, const char *e) |
| 7216 | { |
| 7217 | int quoted, qdpair; |
| 7218 | |
| 7219 | quoted = qdpair = 0; |
| 7220 | for (; s < e; s++) { |
| 7221 | if (qdpair) qdpair = 0; |
| 7222 | else if (quoted) { |
| 7223 | if (*s == '\\') qdpair = 1; |
| 7224 | else if (*s == '"') quoted = 0; |
| 7225 | } |
| 7226 | else if (*s == '"') quoted = 1; |
| 7227 | else if (*s == ',' || *s == ';') return s; |
| 7228 | } |
| 7229 | return s; |
| 7230 | } |
| 7231 | |
| 7232 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7233 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7234 | * the first delimiter is updated if required. The function tries as much as |
| 7235 | * possible to respect the following principles : |
| 7236 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7237 | * colon, in which case <next> is simply removed |
| 7238 | * - set exactly one space character after the new first delimiter, unless |
| 7239 | * there are not enough characters in the block being moved to do so. |
| 7240 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7241 | * one. |
| 7242 | * |
| 7243 | * It is the caller's responsibility to ensure that : |
| 7244 | * - <from> points to a valid delimiter or the colon ; |
| 7245 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7246 | * - there are non-space chars before <from> ; |
| 7247 | * - there is a CR/LF at or after <next>. |
| 7248 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7249 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7250 | { |
| 7251 | char *prev = *from; |
| 7252 | |
| 7253 | if (*prev == ':') { |
| 7254 | /* We're removing the first value, preserve the colon and add a |
| 7255 | * space if possible. |
| 7256 | */ |
| 7257 | if (!http_is_crlf[(unsigned char)*next]) |
| 7258 | next++; |
| 7259 | prev++; |
| 7260 | if (prev < next) |
| 7261 | *prev++ = ' '; |
| 7262 | |
| 7263 | while (http_is_spht[(unsigned char)*next]) |
| 7264 | next++; |
| 7265 | } else { |
| 7266 | /* Remove useless spaces before the old delimiter. */ |
| 7267 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 7268 | prev--; |
| 7269 | *from = prev; |
| 7270 | |
| 7271 | /* copy the delimiter and if possible a space if we're |
| 7272 | * not at the end of the line. |
| 7273 | */ |
| 7274 | if (!http_is_crlf[(unsigned char)*next]) { |
| 7275 | *prev++ = *next++; |
| 7276 | if (prev + 1 < next) |
| 7277 | *prev++ = ' '; |
| 7278 | while (http_is_spht[(unsigned char)*next]) |
| 7279 | next++; |
| 7280 | } |
| 7281 | } |
| 7282 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7283 | } |
| 7284 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7285 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7286 | * 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] | 7287 | * desirable to call it only when needed. This code is quite complex because |
| 7288 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7289 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7290 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7291 | void manage_client_side_cookies(struct session *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7292 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7293 | struct http_txn *txn = &s->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7294 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7295 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7296 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7297 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7298 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7299 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7300 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7301 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7302 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7303 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7304 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7305 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7306 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7307 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7308 | hdr_beg = hdr_next; |
| 7309 | hdr_end = hdr_beg + cur_hdr->len; |
| 7310 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7311 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7312 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7313 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7314 | * "Cookie:" headers. |
| 7315 | */ |
| 7316 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7317 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7318 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7319 | old_idx = cur_idx; |
| 7320 | continue; |
| 7321 | } |
| 7322 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7323 | del_from = NULL; /* nothing to be deleted */ |
| 7324 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7325 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7326 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7327 | * attributes whose name begin with a '$', and associate them with |
| 7328 | * the right cookie, if we want to delete this cookie. |
| 7329 | * So there are 3 cases for each cookie read : |
| 7330 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7331 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7332 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7333 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7334 | * "special" cookie. |
| 7335 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7336 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7337 | * *MUST* delete it. |
| 7338 | * |
| 7339 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7340 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7341 | * the RFC explicitly allows spaces before it, and not within the |
| 7342 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7343 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7344 | * after the equal sign too, resulting in some (rare) buggy |
| 7345 | * implementations trying to do that. So let's do what servers do. |
| 7346 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7347 | * allowed quoted strings in values, with any possible character |
| 7348 | * after a backslash, including control chars and delimitors, which |
| 7349 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7350 | * within values even without quotes. |
| 7351 | * |
| 7352 | * We have to keep multiple pointers in order to support cookie |
| 7353 | * removal at the beginning, middle or end of header without |
| 7354 | * corrupting the header. All of these headers are valid : |
| 7355 | * |
| 7356 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7357 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7358 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7359 | * | | | | | | | | | |
| 7360 | * | | | | | | | | hdr_end <--+ |
| 7361 | * | | | | | | | +--> next |
| 7362 | * | | | | | | +----> val_end |
| 7363 | * | | | | | +-----------> val_beg |
| 7364 | * | | | | +--------------> equal |
| 7365 | * | | | +----------------> att_end |
| 7366 | * | | +---------------------> att_beg |
| 7367 | * | +--------------------------> prev |
| 7368 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7369 | */ |
| 7370 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7371 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7372 | /* Iterate through all cookies on this line */ |
| 7373 | |
| 7374 | /* find att_beg */ |
| 7375 | att_beg = prev + 1; |
| 7376 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7377 | att_beg++; |
| 7378 | |
| 7379 | /* find att_end : this is the first character after the last non |
| 7380 | * space before the equal. It may be equal to hdr_end. |
| 7381 | */ |
| 7382 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7383 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7384 | while (equal < hdr_end) { |
| 7385 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7386 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7387 | if (http_is_spht[(unsigned char)*equal++]) |
| 7388 | continue; |
| 7389 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7390 | } |
| 7391 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7392 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7393 | * is between <att_beg> and <equal>, both may be identical. |
| 7394 | */ |
| 7395 | |
| 7396 | /* look for end of cookie if there is an equal sign */ |
| 7397 | if (equal < hdr_end && *equal == '=') { |
| 7398 | /* look for the beginning of the value */ |
| 7399 | val_beg = equal + 1; |
| 7400 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7401 | val_beg++; |
| 7402 | |
| 7403 | /* find the end of the value, respecting quotes */ |
| 7404 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7405 | |
| 7406 | /* make val_end point to the first white space or delimitor after the value */ |
| 7407 | val_end = next; |
| 7408 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7409 | val_end--; |
| 7410 | } else { |
| 7411 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7412 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7413 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7414 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7415 | * they will automatically be removed if a header before them is removed, |
| 7416 | * since they're supposed to be linked together. |
| 7417 | */ |
| 7418 | if (*att_beg == '$') |
| 7419 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7420 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7421 | /* Ignore cookies with no equal sign */ |
| 7422 | if (equal == next) { |
| 7423 | /* This is not our cookie, so we must preserve it. But if we already |
| 7424 | * scheduled another cookie for removal, we cannot remove the |
| 7425 | * complete header, but we can remove the previous block itself. |
| 7426 | */ |
| 7427 | preserve_hdr = 1; |
| 7428 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7429 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7430 | val_end += delta; |
| 7431 | next += delta; |
| 7432 | hdr_end += delta; |
| 7433 | hdr_next += delta; |
| 7434 | cur_hdr->len += delta; |
| 7435 | http_msg_move_end(&txn->req, delta); |
| 7436 | prev = del_from; |
| 7437 | del_from = NULL; |
| 7438 | } |
| 7439 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7440 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7441 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7442 | /* if there are spaces around the equal sign, we need to |
| 7443 | * strip them otherwise we'll get trouble for cookie captures, |
| 7444 | * or even for rewrites. Since this happens extremely rarely, |
| 7445 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7446 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7447 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7448 | int stripped_before = 0; |
| 7449 | int stripped_after = 0; |
| 7450 | |
| 7451 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7452 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7453 | equal += stripped_before; |
| 7454 | val_beg += stripped_before; |
| 7455 | } |
| 7456 | |
| 7457 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7458 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7459 | val_beg += stripped_after; |
| 7460 | stripped_before += stripped_after; |
| 7461 | } |
| 7462 | |
| 7463 | val_end += stripped_before; |
| 7464 | next += stripped_before; |
| 7465 | hdr_end += stripped_before; |
| 7466 | hdr_next += stripped_before; |
| 7467 | cur_hdr->len += stripped_before; |
| 7468 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7469 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7470 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7471 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7472 | /* First, let's see if we want to capture this cookie. We check |
| 7473 | * that we don't already have a client side cookie, because we |
| 7474 | * can only capture one. Also as an optimisation, we ignore |
| 7475 | * cookies shorter than the declared name. |
| 7476 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7477 | if (s->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7478 | (val_end - att_beg >= s->fe->capture_namelen) && |
| 7479 | memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7480 | int log_len = val_end - att_beg; |
| 7481 | |
| 7482 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7483 | Alert("HTTP logging : out of memory.\n"); |
| 7484 | } else { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7485 | if (log_len > s->fe->capture_len) |
| 7486 | log_len = s->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7487 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7488 | txn->cli_cookie[log_len] = 0; |
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 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7491 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7492 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7493 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7494 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7495 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7496 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7497 | * For cookies in prefix mode, the form is : |
| 7498 | * |
| 7499 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7500 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7501 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7502 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7503 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7504 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7505 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7506 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7507 | * have the server ID between val_beg and delim, and the original cookie between |
| 7508 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7509 | * |
| 7510 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7511 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7512 | * | || || | |+-> next |
| 7513 | * | || || | +--> val_end |
| 7514 | * | || || +---------> delim |
| 7515 | * | || |+------------> val_beg |
| 7516 | * | || +-------------> att_end = equal |
| 7517 | * | |+-----------------> att_beg |
| 7518 | * | +------------------> prev |
| 7519 | * +-------------------------> hdr_beg |
| 7520 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7521 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7522 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7523 | for (delim = val_beg; delim < val_end; delim++) |
| 7524 | if (*delim == COOKIE_DELIM) |
| 7525 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7526 | } else { |
| 7527 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7528 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7529 | /* Now check if the cookie contains a date field, which would |
| 7530 | * appear after a vertical bar ('|') just after the server name |
| 7531 | * and before the delimiter. |
| 7532 | */ |
| 7533 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7534 | if (vbar1) { |
| 7535 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7536 | * right is the last seen date. It is a base64 encoded |
| 7537 | * 30-bit value representing the UNIX date since the |
| 7538 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7539 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7540 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7541 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7542 | if (val_end - vbar1 >= 5) { |
| 7543 | val = b64tos30(vbar1); |
| 7544 | if (val > 0) |
| 7545 | txn->cookie_last_date = val << 2; |
| 7546 | } |
| 7547 | /* look for a second vertical bar */ |
| 7548 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7549 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7550 | val = b64tos30(vbar1 + 1); |
| 7551 | if (val > 0) |
| 7552 | txn->cookie_first_date = val << 2; |
| 7553 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7554 | } |
| 7555 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7556 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7557 | /* if the cookie has an expiration date and the proxy wants to check |
| 7558 | * it, then we do that now. We first check if the cookie is too old, |
| 7559 | * then only if it has expired. We detect strict overflow because the |
| 7560 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7561 | * in the future are ignored if their offset is beyond one day. This |
| 7562 | * allows an admin to fix timezone issues without expiring everyone |
| 7563 | * and at the same time avoids keeping unwanted side effects for too |
| 7564 | * long. |
| 7565 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7566 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7567 | (((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] | 7568 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7569 | txn->flags &= ~TX_CK_MASK; |
| 7570 | txn->flags |= TX_CK_OLD; |
| 7571 | delim = val_beg; // let's pretend we have not found the cookie |
| 7572 | txn->cookie_first_date = 0; |
| 7573 | txn->cookie_last_date = 0; |
| 7574 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7575 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7576 | (((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] | 7577 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7578 | txn->flags &= ~TX_CK_MASK; |
| 7579 | txn->flags |= TX_CK_EXPIRED; |
| 7580 | delim = val_beg; // let's pretend we have not found the cookie |
| 7581 | txn->cookie_first_date = 0; |
| 7582 | txn->cookie_last_date = 0; |
| 7583 | } |
| 7584 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7585 | /* Here, we'll look for the first running server which supports the cookie. |
| 7586 | * This allows to share a same cookie between several servers, for example |
| 7587 | * to dedicate backup servers to specific servers only. |
| 7588 | * However, to prevent clients from sticking to cookie-less backup server |
| 7589 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7590 | * empty cookies and mark them as invalid. |
| 7591 | * The same behaviour is applied when persistence must be ignored. |
| 7592 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7593 | if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7594 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7595 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7596 | while (srv) { |
| 7597 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7598 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7599 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7600 | (s->be->options & PR_O_PERSIST) || |
| 7601 | (s->flags & SN_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7602 | /* we found the server and we can use it */ |
| 7603 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7604 | 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] | 7605 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 7606 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7607 | break; |
| 7608 | } else { |
| 7609 | /* we found a server, but it's down, |
| 7610 | * mark it as such and go on in case |
| 7611 | * another one is available. |
| 7612 | */ |
| 7613 | txn->flags &= ~TX_CK_MASK; |
| 7614 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7615 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7616 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7617 | srv = srv->next; |
| 7618 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7619 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7620 | 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] | 7621 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7622 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7623 | if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7624 | txn->flags |= TX_CK_UNUSED; |
| 7625 | else |
| 7626 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7627 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7628 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7629 | /* depending on the cookie mode, we may have to either : |
| 7630 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7631 | * the server never sees it ; |
| 7632 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7633 | * application cookie so that it does not get accidentely removed later, |
| 7634 | * if we're in cookie prefix mode |
| 7635 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7636 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7637 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7638 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7639 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7640 | val_end += delta; |
| 7641 | next += delta; |
| 7642 | hdr_end += delta; |
| 7643 | hdr_next += delta; |
| 7644 | cur_hdr->len += delta; |
| 7645 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7646 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7647 | del_from = NULL; |
| 7648 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7649 | } |
| 7650 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7651 | (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] | 7652 | del_from = prev; |
| 7653 | } |
| 7654 | } else { |
| 7655 | /* This is not our cookie, so we must preserve it. But if we already |
| 7656 | * scheduled another cookie for removal, we cannot remove the |
| 7657 | * complete header, but we can remove the previous block itself. |
| 7658 | */ |
| 7659 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7660 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7661 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7662 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7663 | if (att_beg >= del_from) |
| 7664 | att_beg += delta; |
| 7665 | if (att_end >= del_from) |
| 7666 | att_end += delta; |
| 7667 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7668 | val_end += delta; |
| 7669 | next += delta; |
| 7670 | hdr_end += delta; |
| 7671 | hdr_next += delta; |
| 7672 | cur_hdr->len += delta; |
| 7673 | http_msg_move_end(&txn->req, delta); |
| 7674 | prev = del_from; |
| 7675 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7676 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7677 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7678 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7679 | /* Look for the appsession cookie unless persistence must be ignored */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7680 | if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7681 | int cmp_len, value_len; |
| 7682 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7683 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7684 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 7685 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 7686 | value_begin = att_beg + s->be->appsession_name_len; |
| 7687 | value_len = val_end - att_beg - s->be->appsession_name_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7688 | } else { |
| 7689 | cmp_len = att_end - att_beg; |
| 7690 | value_begin = val_beg; |
| 7691 | value_len = val_end - val_beg; |
| 7692 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7693 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7694 | /* let's see if the cookie is our appcookie */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7695 | if (cmp_len == s->be->appsession_name_len && |
| 7696 | memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) { |
| 7697 | manage_client_side_appsession(s, value_begin, value_len); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7698 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7699 | } |
| 7700 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7701 | /* continue with next cookie on this header line */ |
| 7702 | att_beg = next; |
| 7703 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7704 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7705 | /* There are no more cookies on this line. |
| 7706 | * We may still have one (or several) marked for deletion at the |
| 7707 | * end of the line. We must do this now in two ways : |
| 7708 | * - if some cookies must be preserved, we only delete from the |
| 7709 | * mark to the end of line ; |
| 7710 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7711 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7712 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7713 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7714 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7715 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7716 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7717 | cur_hdr->len += delta; |
| 7718 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7719 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7720 | |
| 7721 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7722 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7723 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7724 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7725 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7726 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7727 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7728 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7729 | } |
| 7730 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7731 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7732 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7733 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7734 | } |
| 7735 | |
| 7736 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7737 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7738 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7739 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7740 | 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] | 7741 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7742 | char *cur_ptr, *cur_end, *cur_next; |
| 7743 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7744 | struct http_txn *txn = &s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7745 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7746 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7747 | |
| 7748 | last_hdr = 0; |
| 7749 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7750 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7751 | old_idx = 0; |
| 7752 | |
| 7753 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7754 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7755 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7756 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7757 | (exp->action == ACT_ALLOW || |
| 7758 | exp->action == ACT_DENY)) |
| 7759 | return 0; |
| 7760 | |
| 7761 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7762 | if (!cur_idx) |
| 7763 | break; |
| 7764 | |
| 7765 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7766 | cur_ptr = cur_next; |
| 7767 | cur_end = cur_ptr + cur_hdr->len; |
| 7768 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7769 | |
| 7770 | /* Now we have one header between cur_ptr and cur_end, |
| 7771 | * and the next header starts at cur_next. |
| 7772 | */ |
| 7773 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 7774 | 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] | 7775 | switch (exp->action) { |
| 7776 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7777 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7778 | last_hdr = 1; |
| 7779 | break; |
| 7780 | |
| 7781 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7782 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7783 | last_hdr = 1; |
| 7784 | break; |
| 7785 | |
| 7786 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7787 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7788 | if (trash.len < 0) |
| 7789 | return -1; |
| 7790 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7791 | 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] | 7792 | /* FIXME: if the user adds a newline in the replacement, the |
| 7793 | * index will not be recalculated for now, and the new line |
| 7794 | * will not be counted as a new header. |
| 7795 | */ |
| 7796 | |
| 7797 | cur_end += delta; |
| 7798 | cur_next += delta; |
| 7799 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7800 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7801 | break; |
| 7802 | |
| 7803 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7804 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7805 | cur_next += delta; |
| 7806 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7807 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7808 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7809 | txn->hdr_idx.used--; |
| 7810 | cur_hdr->len = 0; |
| 7811 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7812 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7813 | break; |
| 7814 | |
| 7815 | } |
| 7816 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7817 | |
| 7818 | /* keep the link from this header to next one in case of later |
| 7819 | * removal of next header. |
| 7820 | */ |
| 7821 | old_idx = cur_idx; |
| 7822 | } |
| 7823 | return 0; |
| 7824 | } |
| 7825 | |
| 7826 | |
| 7827 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7828 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7829 | * or -1 if a replacement resulted in an invalid status line. |
| 7830 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7831 | 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] | 7832 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7833 | char *cur_ptr, *cur_end; |
| 7834 | int done; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7835 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7836 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7837 | |
| 7838 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7839 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7840 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7841 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7842 | (exp->action == ACT_ALLOW || |
| 7843 | exp->action == ACT_DENY)) |
| 7844 | return 0; |
| 7845 | else if (exp->action == ACT_REMOVE) |
| 7846 | return 0; |
| 7847 | |
| 7848 | done = 0; |
| 7849 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7850 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7851 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7852 | |
| 7853 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7854 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 7855 | 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] | 7856 | switch (exp->action) { |
| 7857 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7858 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7859 | done = 1; |
| 7860 | break; |
| 7861 | |
| 7862 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7863 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7864 | done = 1; |
| 7865 | break; |
| 7866 | |
| 7867 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7868 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7869 | if (trash.len < 0) |
| 7870 | return -1; |
| 7871 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7872 | 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] | 7873 | /* FIXME: if the user adds a newline in the replacement, the |
| 7874 | * index will not be recalculated for now, and the new line |
| 7875 | * will not be counted as a new header. |
| 7876 | */ |
| 7877 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7878 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7879 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7880 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7881 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7882 | cur_ptr, cur_end + 1, |
| 7883 | NULL, NULL); |
| 7884 | if (unlikely(!cur_end)) |
| 7885 | return -1; |
| 7886 | |
| 7887 | /* we have a full respnse and we know that we have either a CR |
| 7888 | * or an LF at <ptr>. |
| 7889 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7890 | 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] | 7891 | 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] | 7892 | /* there is no point trying this regex on headers */ |
| 7893 | return 1; |
| 7894 | } |
| 7895 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7896 | return done; |
| 7897 | } |
| 7898 | |
| 7899 | |
| 7900 | |
| 7901 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7902 | * 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] | 7903 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7904 | * unparsable response. |
| 7905 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7906 | 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] | 7907 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7908 | struct http_txn *txn = &s->txn; |
| 7909 | struct hdr_exp *exp; |
| 7910 | |
| 7911 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7912 | int ret; |
| 7913 | |
| 7914 | /* |
| 7915 | * The interleaving of transformations and verdicts |
| 7916 | * makes it difficult to decide to continue or stop |
| 7917 | * the evaluation. |
| 7918 | */ |
| 7919 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7920 | if (txn->flags & TX_SVDENY) |
| 7921 | break; |
| 7922 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7923 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7924 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7925 | exp->action == ACT_PASS)) { |
| 7926 | exp = exp->next; |
| 7927 | continue; |
| 7928 | } |
| 7929 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7930 | /* if this filter had a condition, evaluate it now and skip to |
| 7931 | * next filter if the condition does not match. |
| 7932 | */ |
| 7933 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7934 | 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] | 7935 | ret = acl_pass(ret); |
| 7936 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7937 | ret = !ret; |
| 7938 | if (!ret) |
| 7939 | continue; |
| 7940 | } |
| 7941 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7942 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7943 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7944 | if (unlikely(ret < 0)) |
| 7945 | return -1; |
| 7946 | |
| 7947 | if (likely(ret == 0)) { |
| 7948 | /* The filter did not match the response, it can be |
| 7949 | * iterated through all headers. |
| 7950 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7951 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 7952 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7953 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7954 | } |
| 7955 | return 0; |
| 7956 | } |
| 7957 | |
| 7958 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7959 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7960 | * 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] | 7961 | * desirable to call it only when needed. This function is also used when we |
| 7962 | * 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] | 7963 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7964 | void manage_server_side_cookies(struct session *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7965 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7966 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7967 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7968 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7969 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7970 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7971 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7972 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7973 | /* Iterate through the headers. |
| 7974 | * we start with the start line. |
| 7975 | */ |
| 7976 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7977 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7978 | |
| 7979 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7980 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7981 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7982 | |
| 7983 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7984 | hdr_beg = hdr_next; |
| 7985 | hdr_end = hdr_beg + cur_hdr->len; |
| 7986 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7987 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7988 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7989 | * next header starts at hdr_next. We're only interested in |
| 7990 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7991 | */ |
| 7992 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7993 | is_cookie2 = 0; |
| 7994 | prev = hdr_beg + 10; |
| 7995 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7996 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7997 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7998 | if (!val) { |
| 7999 | old_idx = cur_idx; |
| 8000 | continue; |
| 8001 | } |
| 8002 | is_cookie2 = 1; |
| 8003 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8004 | } |
| 8005 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8006 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 8007 | * <prev> points to the colon. |
| 8008 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8009 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8010 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8011 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8012 | * check-cache is enabled) and we are not interested in checking |
| 8013 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8014 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8015 | if (s->be->cookie_name == NULL && |
| 8016 | s->be->appsession_name == NULL && |
| 8017 | s->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8018 | return; |
| 8019 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8020 | /* OK so now we know we have to process this response cookie. |
| 8021 | * The format of the Set-Cookie header is slightly different |
| 8022 | * from the format of the Cookie header in that it does not |
| 8023 | * support the comma as a cookie delimiter (thus the header |
| 8024 | * cannot be folded) because the Expires attribute described in |
| 8025 | * the original Netscape's spec may contain an unquoted date |
| 8026 | * with a comma inside. We have to live with this because |
| 8027 | * many browsers don't support Max-Age and some browsers don't |
| 8028 | * support quoted strings. However the Set-Cookie2 header is |
| 8029 | * clean. |
| 8030 | * |
| 8031 | * We have to keep multiple pointers in order to support cookie |
| 8032 | * removal at the beginning, middle or end of header without |
| 8033 | * corrupting the header (in case of set-cookie2). A special |
| 8034 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8035 | * fields after the first semi-colon. The <next> pointer points |
| 8036 | * either to the end of line (set-cookie) or next unquoted comma |
| 8037 | * (set-cookie2). All of these headers are valid : |
| 8038 | * |
| 8039 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8040 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8041 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8042 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8043 | * | | | | | | | | | | |
| 8044 | * | | | | | | | | +-> next hdr_end <--+ |
| 8045 | * | | | | | | | +------------> scav |
| 8046 | * | | | | | | +--------------> val_end |
| 8047 | * | | | | | +--------------------> val_beg |
| 8048 | * | | | | +----------------------> equal |
| 8049 | * | | | +------------------------> att_end |
| 8050 | * | | +----------------------------> att_beg |
| 8051 | * | +------------------------------> prev |
| 8052 | * +-----------------------------------------> hdr_beg |
| 8053 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8054 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8055 | for (; prev < hdr_end; prev = next) { |
| 8056 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8057 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8058 | /* find att_beg */ |
| 8059 | att_beg = prev + 1; |
| 8060 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8061 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8062 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8063 | /* find att_end : this is the first character after the last non |
| 8064 | * space before the equal. It may be equal to hdr_end. |
| 8065 | */ |
| 8066 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8067 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8068 | while (equal < hdr_end) { |
| 8069 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8070 | break; |
| 8071 | if (http_is_spht[(unsigned char)*equal++]) |
| 8072 | continue; |
| 8073 | att_end = equal; |
| 8074 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8075 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8076 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8077 | * is between <att_beg> and <equal>, both may be identical. |
| 8078 | */ |
| 8079 | |
| 8080 | /* look for end of cookie if there is an equal sign */ |
| 8081 | if (equal < hdr_end && *equal == '=') { |
| 8082 | /* look for the beginning of the value */ |
| 8083 | val_beg = equal + 1; |
| 8084 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8085 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8086 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8087 | /* find the end of the value, respecting quotes */ |
| 8088 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8089 | |
| 8090 | /* make val_end point to the first white space or delimitor after the value */ |
| 8091 | val_end = next; |
| 8092 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8093 | val_end--; |
| 8094 | } else { |
| 8095 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8096 | val_beg = val_end = next = equal; |
| 8097 | } |
| 8098 | |
| 8099 | if (next < hdr_end) { |
| 8100 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8101 | * a colon or semi-colon before the end. So skip all attr-value |
| 8102 | * pairs and look for the next comma. For Set-Cookie, since |
| 8103 | * commas are permitted in values, skip to the end. |
| 8104 | */ |
| 8105 | if (is_cookie2) |
| 8106 | next = find_hdr_value_end(next, hdr_end); |
| 8107 | else |
| 8108 | next = hdr_end; |
| 8109 | } |
| 8110 | |
| 8111 | /* Now everything is as on the diagram above */ |
| 8112 | |
| 8113 | /* Ignore cookies with no equal sign */ |
| 8114 | if (equal == val_end) |
| 8115 | continue; |
| 8116 | |
| 8117 | /* If there are spaces around the equal sign, we need to |
| 8118 | * strip them otherwise we'll get trouble for cookie captures, |
| 8119 | * or even for rewrites. Since this happens extremely rarely, |
| 8120 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8121 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8122 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8123 | int stripped_before = 0; |
| 8124 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8125 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8126 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8127 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8128 | equal += stripped_before; |
| 8129 | val_beg += stripped_before; |
| 8130 | } |
| 8131 | |
| 8132 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8133 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8134 | val_beg += stripped_after; |
| 8135 | stripped_before += stripped_after; |
| 8136 | } |
| 8137 | |
| 8138 | val_end += stripped_before; |
| 8139 | next += stripped_before; |
| 8140 | hdr_end += stripped_before; |
| 8141 | hdr_next += stripped_before; |
| 8142 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8143 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8144 | } |
| 8145 | |
| 8146 | /* First, let's see if we want to capture this cookie. We check |
| 8147 | * that we don't already have a server side cookie, because we |
| 8148 | * can only capture one. Also as an optimisation, we ignore |
| 8149 | * cookies shorter than the declared name. |
| 8150 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8151 | if (s->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8152 | txn->srv_cookie == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8153 | (val_end - att_beg >= s->fe->capture_namelen) && |
| 8154 | memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8155 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8156 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8157 | Alert("HTTP logging : out of memory.\n"); |
| 8158 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8159 | else { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8160 | if (log_len > s->fe->capture_len) |
| 8161 | log_len = s->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8162 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8163 | txn->srv_cookie[log_len] = 0; |
| 8164 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8165 | } |
| 8166 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8167 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8168 | /* now check if we need to process it for persistence */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8169 | if (!(s->flags & SN_IGNORE_PRST) && |
| 8170 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8171 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8172 | /* assume passive cookie by default */ |
| 8173 | txn->flags &= ~TX_SCK_MASK; |
| 8174 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8175 | |
| 8176 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8177 | * this occurrence because we'll insert another one later. |
| 8178 | * 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] | 8179 | * a direct access. |
| 8180 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8181 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8182 | /* The "preserve" flag was set, we don't want to touch the |
| 8183 | * server's cookie. |
| 8184 | */ |
| 8185 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8186 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 8187 | ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8188 | /* this cookie must be deleted */ |
| 8189 | if (*prev == ':' && next == hdr_end) { |
| 8190 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8191 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8192 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8193 | txn->hdr_idx.used--; |
| 8194 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8195 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8196 | hdr_next += delta; |
| 8197 | http_msg_move_end(&txn->rsp, delta); |
| 8198 | /* note: while both invalid now, <next> and <hdr_end> |
| 8199 | * are still equal, so the for() will stop as expected. |
| 8200 | */ |
| 8201 | } else { |
| 8202 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8203 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8204 | next = prev; |
| 8205 | hdr_end += delta; |
| 8206 | hdr_next += delta; |
| 8207 | cur_hdr->len += delta; |
| 8208 | http_msg_move_end(&txn->rsp, delta); |
| 8209 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8210 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8211 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8212 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8213 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8214 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8215 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8216 | * with this server since we know it. |
| 8217 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8218 | 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] | 8219 | next += delta; |
| 8220 | hdr_end += delta; |
| 8221 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8222 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8223 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8224 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8225 | txn->flags &= ~TX_SCK_MASK; |
| 8226 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8227 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8228 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8229 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8230 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8231 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8232 | 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] | 8233 | next += delta; |
| 8234 | hdr_end += delta; |
| 8235 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8236 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8237 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8238 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8239 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8240 | txn->flags &= ~TX_SCK_MASK; |
| 8241 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8242 | } |
| 8243 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 8244 | /* 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] | 8245 | else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8246 | int cmp_len, value_len; |
| 8247 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8248 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8249 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 8250 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 8251 | value_begin = att_beg + s->be->appsession_name_len; |
| 8252 | 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] | 8253 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8254 | cmp_len = att_end - att_beg; |
| 8255 | value_begin = val_beg; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8256 | value_len = MIN(s->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8257 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8258 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8259 | if ((cmp_len == s->be->appsession_name_len) && |
| 8260 | (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] | 8261 | /* free a possibly previously allocated memory */ |
| 8262 | pool_free2(apools.sessid, txn->sessid); |
| 8263 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8264 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8265 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8266 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8267 | 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] | 8268 | return; |
| 8269 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8270 | memcpy(txn->sessid, value_begin, value_len); |
| 8271 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8272 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8273 | } |
| 8274 | /* that's done for this cookie, check the next one on the same |
| 8275 | * line when next != hdr_end (only if is_cookie2). |
| 8276 | */ |
| 8277 | } |
| 8278 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8279 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8280 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8281 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8282 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8283 | appsess *asession = NULL; |
| 8284 | /* only do insert, if lookup fails */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8285 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8286 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 8287 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8288 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 8289 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8290 | 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] | 8291 | return; |
| 8292 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8293 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 8294 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8295 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 8296 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8297 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8298 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8299 | return; |
| 8300 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8301 | memcpy(asession->sessid, txn->sessid, s->be->appsession_len); |
| 8302 | asession->sessid[s->be->appsession_len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8303 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8304 | server_id_len = strlen(objt_server(s->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8305 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8306 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8307 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8308 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8309 | return; |
| 8310 | } |
| 8311 | asession->serverid[0] = '\0'; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8312 | memcpy(asession->serverid, objt_server(s->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8313 | |
| 8314 | asession->request_count = 0; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8315 | appsession_hash_insert(&(s->be->htbl_proxy), asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8316 | } |
| 8317 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8318 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8319 | asession->request_count++; |
| 8320 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8321 | } |
| 8322 | |
| 8323 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8324 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8325 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8326 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8327 | void check_response_for_cacheability(struct session *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8328 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8329 | struct http_txn *txn = &s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8330 | char *p1, *p2; |
| 8331 | |
| 8332 | char *cur_ptr, *cur_end, *cur_next; |
| 8333 | int cur_idx; |
| 8334 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8335 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8336 | return; |
| 8337 | |
| 8338 | /* Iterate through the headers. |
| 8339 | * we start with the start line. |
| 8340 | */ |
| 8341 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8342 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8343 | |
| 8344 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8345 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8346 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8347 | |
| 8348 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8349 | cur_ptr = cur_next; |
| 8350 | cur_end = cur_ptr + cur_hdr->len; |
| 8351 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8352 | |
| 8353 | /* We have one full header between cur_ptr and cur_end, and the |
| 8354 | * next header starts at cur_next. We're only interested in |
| 8355 | * "Cookie:" headers. |
| 8356 | */ |
| 8357 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8358 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8359 | if (val) { |
| 8360 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8361 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8362 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8363 | return; |
| 8364 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8365 | } |
| 8366 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8367 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8368 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8369 | continue; |
| 8370 | |
| 8371 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8372 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8373 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8374 | |
| 8375 | if (p1 >= cur_end) /* no more info */ |
| 8376 | continue; |
| 8377 | |
| 8378 | /* p1 is at the beginning of the value */ |
| 8379 | p2 = p1; |
| 8380 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8381 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8382 | p2++; |
| 8383 | |
| 8384 | /* we have a complete value between p1 and p2 */ |
| 8385 | if (p2 < cur_end && *p2 == '=') { |
| 8386 | /* we have something of the form no-cache="set-cookie" */ |
| 8387 | if ((cur_end - p1 >= 21) && |
| 8388 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8389 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8390 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8391 | continue; |
| 8392 | } |
| 8393 | |
| 8394 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8395 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8396 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8397 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8398 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8399 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8400 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8401 | return; |
| 8402 | } |
| 8403 | |
| 8404 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8405 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8406 | continue; |
| 8407 | } |
| 8408 | } |
| 8409 | } |
| 8410 | |
| 8411 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8412 | /* |
| 8413 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 8414 | * If the server is found, it's assigned to the session. |
| 8415 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8416 | 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] | 8417 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8418 | char *end_params, *first_param, *cur_param, *next_param; |
| 8419 | char separator; |
| 8420 | int value_len; |
| 8421 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8422 | int mode = s->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8423 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8424 | if (s->be->appsession_name == NULL || |
| 8425 | (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] | 8426 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8427 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8428 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8429 | first_param = NULL; |
| 8430 | switch (mode) { |
| 8431 | case PR_O2_AS_M_PP: |
| 8432 | first_param = memchr(begin, ';', len); |
| 8433 | break; |
| 8434 | case PR_O2_AS_M_QS: |
| 8435 | first_param = memchr(begin, '?', len); |
| 8436 | break; |
| 8437 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8438 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8439 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8440 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8441 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8442 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8443 | switch (mode) { |
| 8444 | case PR_O2_AS_M_PP: |
| 8445 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 8446 | end_params = (char *) begin + len; |
| 8447 | } |
| 8448 | separator = ';'; |
| 8449 | break; |
| 8450 | case PR_O2_AS_M_QS: |
| 8451 | end_params = (char *) begin + len; |
| 8452 | separator = '&'; |
| 8453 | break; |
| 8454 | default: |
| 8455 | /* unknown mode, shouldn't happen */ |
| 8456 | return; |
| 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 | cur_param = next_param = end_params; |
| 8460 | while (cur_param > first_param) { |
| 8461 | cur_param--; |
| 8462 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 8463 | /* let's see if this is the appsession parameter */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8464 | if ((cur_param + s->be->appsession_name_len + 1 < next_param) && |
| 8465 | ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') && |
| 8466 | (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] | 8467 | /* Cool... it's the right one */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8468 | cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 8469 | value_len = MIN(s->be->appsession_len, next_param - cur_param); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8470 | if (value_len > 0) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8471 | manage_client_side_appsession(s, cur_param, value_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8472 | } |
| 8473 | break; |
| 8474 | } |
| 8475 | next_param = cur_param; |
| 8476 | } |
| 8477 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8478 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 8479 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8480 | appsession_hash_dump(&(s->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8481 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8482 | } |
| 8483 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8484 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8485 | * 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] | 8486 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8487 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8488 | * 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] | 8489 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8490 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8491 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8492 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8493 | 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] | 8494 | { |
| 8495 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8496 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8497 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8498 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8499 | if (!uri_auth) |
| 8500 | return 0; |
| 8501 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8502 | 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] | 8503 | return 0; |
| 8504 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8505 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8506 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8507 | return 0; |
| 8508 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8509 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8510 | return 0; |
| 8511 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8512 | return 1; |
| 8513 | } |
| 8514 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8515 | /* |
| 8516 | * 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] | 8517 | * By default it tries to report the error position as msg->err_pos. However if |
| 8518 | * this one is not set, it will then report msg->next, which is the last known |
| 8519 | * 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] | 8520 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8521 | */ |
| 8522 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8523 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8524 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8525 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8526 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8527 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8528 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8529 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8530 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8531 | len1 = MIN(len1, es->len); |
| 8532 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8533 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8534 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8535 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8536 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8537 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8538 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8539 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8540 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8541 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8542 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8543 | es->when = date; // user-visible date |
| 8544 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8545 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8546 | es->oe = other_end; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8547 | if (objt_conn(s->req->prod->end)) |
| 8548 | es->src = __objt_conn(s->req->prod->end)->addr.from; |
| 8549 | else |
| 8550 | memset(&es->src, 0, sizeof(es->src)); |
| 8551 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8552 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8553 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8554 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8555 | es->s_flags = s->flags; |
| 8556 | es->t_flags = s->txn.flags; |
| 8557 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8558 | es->b_out = chn->buf->o; |
| 8559 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8560 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8561 | es->m_clen = msg->chunk_len; |
| 8562 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8563 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8564 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8565 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8566 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8567 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8568 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8569 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8570 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8571 | * 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] | 8572 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8573 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8574 | * 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] | 8575 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8576 | 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] | 8577 | struct hdr_idx *idx, int occ, |
| 8578 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8579 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8580 | struct hdr_ctx local_ctx; |
| 8581 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8582 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8583 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8584 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8585 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8586 | if (!ctx) { |
| 8587 | local_ctx.idx = 0; |
| 8588 | ctx = &local_ctx; |
| 8589 | } |
| 8590 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8591 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8592 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8593 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8594 | occ--; |
| 8595 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8596 | *vptr = ctx->line + ctx->val; |
| 8597 | *vlen = ctx->vlen; |
| 8598 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8599 | } |
| 8600 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8601 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8602 | } |
| 8603 | |
| 8604 | /* negative occurrence, we scan all the list then walk back */ |
| 8605 | if (-occ > MAX_HDR_HISTORY) |
| 8606 | return 0; |
| 8607 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8608 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8609 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8610 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8611 | len_hist[hist_ptr] = ctx->vlen; |
| 8612 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8613 | hist_ptr = 0; |
| 8614 | found++; |
| 8615 | } |
| 8616 | if (-occ > found) |
| 8617 | return 0; |
| 8618 | /* 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] | 8619 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8620 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8621 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8622 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8623 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8624 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8625 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8626 | *vptr = ptr_hist[hist_ptr]; |
| 8627 | *vlen = len_hist[hist_ptr]; |
| 8628 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8629 | } |
| 8630 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8631 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8632 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8633 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8634 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8635 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8636 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8637 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8638 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8639 | * line header values and does not stop at commas. |
| 8640 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8641 | */ |
| 8642 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8643 | struct hdr_idx *idx, int occ, |
| 8644 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8645 | { |
| 8646 | struct hdr_ctx local_ctx; |
| 8647 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8648 | int len_hist[MAX_HDR_HISTORY]; |
| 8649 | unsigned int hist_ptr; |
| 8650 | int found; |
| 8651 | |
| 8652 | if (!ctx) { |
| 8653 | local_ctx.idx = 0; |
| 8654 | ctx = &local_ctx; |
| 8655 | } |
| 8656 | |
| 8657 | if (occ >= 0) { |
| 8658 | /* search from the beginning */ |
| 8659 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8660 | occ--; |
| 8661 | if (occ <= 0) { |
| 8662 | *vptr = ctx->line + ctx->val; |
| 8663 | *vlen = ctx->vlen; |
| 8664 | return 1; |
| 8665 | } |
| 8666 | } |
| 8667 | return 0; |
| 8668 | } |
| 8669 | |
| 8670 | /* negative occurrence, we scan all the list then walk back */ |
| 8671 | if (-occ > MAX_HDR_HISTORY) |
| 8672 | return 0; |
| 8673 | |
| 8674 | found = hist_ptr = 0; |
| 8675 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8676 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8677 | len_hist[hist_ptr] = ctx->vlen; |
| 8678 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8679 | hist_ptr = 0; |
| 8680 | found++; |
| 8681 | } |
| 8682 | if (-occ > found) |
| 8683 | return 0; |
| 8684 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8685 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8686 | */ |
| 8687 | hist_ptr += occ; |
| 8688 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8689 | hist_ptr -= MAX_HDR_HISTORY; |
| 8690 | *vptr = ptr_hist[hist_ptr]; |
| 8691 | *vlen = len_hist[hist_ptr]; |
| 8692 | return 1; |
| 8693 | } |
| 8694 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8695 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8696 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8697 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8698 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8699 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8700 | 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] | 8701 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8702 | int max; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8703 | 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] | 8704 | dir, |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8705 | objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1, |
| 8706 | 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] | 8707 | |
| 8708 | for (max = 0; start + max < end; max++) |
| 8709 | if (start[max] == '\r' || start[max] == '\n') |
| 8710 | break; |
| 8711 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8712 | UBOUND(max, trash.size - trash.len - 3); |
| 8713 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8714 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8715 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8716 | } |
| 8717 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8718 | /* |
| 8719 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 8720 | * the required fields are properly allocated and that we only need to (re)init |
| 8721 | * them. This should be used before processing any new request. |
| 8722 | */ |
| 8723 | void http_init_txn(struct session *s) |
| 8724 | { |
| 8725 | struct http_txn *txn = &s->txn; |
| 8726 | struct proxy *fe = s->fe; |
| 8727 | |
| 8728 | txn->flags = 0; |
| 8729 | txn->status = -1; |
| 8730 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8731 | txn->cookie_first_date = 0; |
| 8732 | txn->cookie_last_date = 0; |
| 8733 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8734 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8735 | 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] | 8736 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8737 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8738 | 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] | 8739 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 8740 | txn->req.chunk_len = 0LL; |
| 8741 | txn->req.body_len = 0LL; |
| 8742 | txn->rsp.chunk_len = 0LL; |
| 8743 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8744 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8745 | 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] | 8746 | txn->req.chn = s->req; |
| 8747 | txn->rsp.chn = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8748 | |
| 8749 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8750 | |
| 8751 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8752 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8753 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8754 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8755 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8756 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 8757 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8758 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8759 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 8760 | |
| 8761 | if (txn->hdr_idx.v) |
| 8762 | hdr_idx_init(&txn->hdr_idx); |
| 8763 | } |
| 8764 | |
| 8765 | /* to be used at the end of a transaction */ |
| 8766 | void http_end_txn(struct session *s) |
| 8767 | { |
| 8768 | struct http_txn *txn = &s->txn; |
| 8769 | |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8770 | /* release any possible compression context */ |
| 8771 | if (s->flags & SN_COMP_READY) |
| 8772 | s->comp_algo->end(&s->comp_ctx); |
| 8773 | s->comp_algo = NULL; |
| 8774 | s->flags &= ~SN_COMP_READY; |
| 8775 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8776 | /* these ones will have been dynamically allocated */ |
| 8777 | pool_free2(pool2_requri, txn->uri); |
| 8778 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8779 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8780 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8781 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8782 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8783 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8784 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8785 | txn->uri = NULL; |
| 8786 | txn->srv_cookie = NULL; |
| 8787 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8788 | |
| 8789 | if (txn->req.cap) { |
| 8790 | struct cap_hdr *h; |
| 8791 | for (h = s->fe->req_cap; h; h = h->next) |
| 8792 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 8793 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 8794 | } |
| 8795 | |
| 8796 | if (txn->rsp.cap) { |
| 8797 | struct cap_hdr *h; |
| 8798 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 8799 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 8800 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 8801 | } |
| 8802 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8803 | } |
| 8804 | |
| 8805 | /* to be used at the end of a transaction to prepare a new one */ |
| 8806 | void http_reset_txn(struct session *s) |
| 8807 | { |
| 8808 | http_end_txn(s); |
| 8809 | http_init_txn(s); |
| 8810 | |
| 8811 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8812 | s->logs.logwait = s->fe->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8813 | s->logs.level = 0; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 8814 | session_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8815 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8816 | /* re-init store persistence */ |
| 8817 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8818 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8819 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8820 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8821 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 8822 | s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8823 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8824 | /* We must trim any excess data from the response buffer, because we |
| 8825 | * may have blocked an invalid response from a server that we don't |
| 8826 | * want to accidentely forward once we disable the analysers, nor do |
| 8827 | * we want those data to come along with next response. A typical |
| 8828 | * example of such data would be from a buggy server responding to |
| 8829 | * a HEAD with some data, or sending more than the advertised |
| 8830 | * content-length. |
| 8831 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8832 | if (unlikely(s->rep->buf->i)) |
| 8833 | s->rep->buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8834 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8835 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8836 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8837 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8838 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8839 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8840 | |
| 8841 | s->req->rex = TICK_ETERNITY; |
| 8842 | s->req->wex = TICK_ETERNITY; |
| 8843 | s->req->analyse_exp = TICK_ETERNITY; |
| 8844 | s->rep->rex = TICK_ETERNITY; |
| 8845 | s->rep->wex = TICK_ETERNITY; |
| 8846 | s->rep->analyse_exp = TICK_ETERNITY; |
| 8847 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8848 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8849 | void free_http_res_rules(struct list *r) |
| 8850 | { |
| 8851 | struct http_res_rule *tr, *pr; |
| 8852 | |
| 8853 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8854 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8855 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8856 | free(pr); |
| 8857 | } |
| 8858 | } |
| 8859 | |
| 8860 | void free_http_req_rules(struct list *r) |
| 8861 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8862 | struct http_req_rule *tr, *pr; |
| 8863 | |
| 8864 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8865 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8866 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8867 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8868 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8869 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8870 | free(pr); |
| 8871 | } |
| 8872 | } |
| 8873 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8874 | /* parse an "http-request" rule */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8875 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8876 | { |
| 8877 | struct http_req_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8878 | struct http_req_action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8879 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8880 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8881 | |
| 8882 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 8883 | if (!rule) { |
| 8884 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8885 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8886 | } |
| 8887 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8888 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8889 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 8890 | cur_arg = 1; |
Willy Tarreau | 5bd6759 | 2014-04-28 22:00:46 +0200 | [diff] [blame] | 8891 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8892 | rule->action = HTTP_REQ_ACT_DENY; |
| 8893 | cur_arg = 1; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8894 | } else if (!strcmp(args[0], "tarpit")) { |
| 8895 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 8896 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8897 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8898 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8899 | cur_arg = 1; |
| 8900 | |
| 8901 | while(*args[cur_arg]) { |
| 8902 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8903 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8904 | cur_arg+=2; |
| 8905 | continue; |
| 8906 | } else |
| 8907 | break; |
| 8908 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8909 | } else if (!strcmp(args[0], "set-nice")) { |
| 8910 | rule->action = HTTP_REQ_ACT_SET_NICE; |
| 8911 | cur_arg = 1; |
| 8912 | |
| 8913 | if (!*args[cur_arg] || |
| 8914 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8915 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8916 | file, linenum, args[0]); |
| 8917 | goto out_err; |
| 8918 | } |
| 8919 | rule->arg.nice = atoi(args[cur_arg]); |
| 8920 | if (rule->arg.nice < -1024) |
| 8921 | rule->arg.nice = -1024; |
| 8922 | else if (rule->arg.nice > 1024) |
| 8923 | rule->arg.nice = 1024; |
| 8924 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8925 | } else if (!strcmp(args[0], "set-tos")) { |
| 8926 | #ifdef IP_TOS |
| 8927 | char *err; |
| 8928 | rule->action = HTTP_REQ_ACT_SET_TOS; |
| 8929 | cur_arg = 1; |
| 8930 | |
| 8931 | if (!*args[cur_arg] || |
| 8932 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8933 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8934 | file, linenum, args[0]); |
| 8935 | goto out_err; |
| 8936 | } |
| 8937 | |
| 8938 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8939 | if (err && *err != '\0') { |
| 8940 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8941 | file, linenum, err, args[0]); |
| 8942 | goto out_err; |
| 8943 | } |
| 8944 | cur_arg++; |
| 8945 | #else |
| 8946 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8947 | goto out_err; |
| 8948 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8949 | } else if (!strcmp(args[0], "set-mark")) { |
| 8950 | #ifdef SO_MARK |
| 8951 | char *err; |
| 8952 | rule->action = HTTP_REQ_ACT_SET_MARK; |
| 8953 | cur_arg = 1; |
| 8954 | |
| 8955 | if (!*args[cur_arg] || |
| 8956 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8957 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8958 | file, linenum, args[0]); |
| 8959 | goto out_err; |
| 8960 | } |
| 8961 | |
| 8962 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8963 | if (err && *err != '\0') { |
| 8964 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8965 | file, linenum, err, args[0]); |
| 8966 | goto out_err; |
| 8967 | } |
| 8968 | cur_arg++; |
| 8969 | global.last_checks |= LSTCHK_NETADM; |
| 8970 | #else |
| 8971 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8972 | goto out_err; |
| 8973 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8974 | } else if (!strcmp(args[0], "set-log-level")) { |
| 8975 | rule->action = HTTP_REQ_ACT_SET_LOGL; |
| 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 | bad_log_level: |
| 8981 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8982 | file, linenum, args[0]); |
| 8983 | goto out_err; |
| 8984 | } |
| 8985 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8986 | rule->arg.loglevel = -1; |
| 8987 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8988 | goto bad_log_level; |
| 8989 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8990 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 8991 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 8992 | cur_arg = 1; |
| 8993 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 8994 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8995 | (*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] | 8996 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8997 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8998 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8999 | } |
| 9000 | |
| 9001 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9002 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9003 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 9004 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9005 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9006 | 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] | 9007 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9008 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9009 | free(proxy->conf.lfs_file); |
| 9010 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9011 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9012 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9013 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
| 9014 | 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] | 9015 | cur_arg = 1; |
| 9016 | |
| 9017 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 8f249f4 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 9018 | (*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] | 9019 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 9020 | file, linenum, args[0]); |
| 9021 | goto out_err; |
| 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); |
| 9027 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9028 | error = NULL; |
| 9029 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9030 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9031 | args[cur_arg + 1], error); |
| 9032 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9033 | goto out_err; |
| 9034 | } |
| 9035 | |
| 9036 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9037 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9038 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9039 | file, linenum); |
| 9040 | |
| 9041 | free(proxy->conf.lfs_file); |
| 9042 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9043 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9044 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9045 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9046 | rule->action = HTTP_REQ_ACT_DEL_HDR; |
| 9047 | cur_arg = 1; |
| 9048 | |
| 9049 | if (!*args[cur_arg] || |
| 9050 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9051 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9052 | file, linenum, args[0]); |
| 9053 | goto out_err; |
| 9054 | } |
| 9055 | |
| 9056 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9057 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9058 | |
| 9059 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9060 | free(proxy->conf.lfs_file); |
| 9061 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9062 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9063 | cur_arg += 1; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9064 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9065 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9066 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9067 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9068 | 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] | 9069 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9070 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9071 | goto out_err; |
| 9072 | } |
| 9073 | |
| 9074 | /* this redirect rule might already contain a parsed condition which |
| 9075 | * we'll pass to the http-request rule. |
| 9076 | */ |
| 9077 | rule->action = HTTP_REQ_ACT_REDIR; |
| 9078 | rule->arg.redir = redir; |
| 9079 | rule->cond = redir->cond; |
| 9080 | redir->cond = NULL; |
| 9081 | cur_arg = 2; |
| 9082 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9083 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9084 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9085 | rule->action = HTTP_REQ_ACT_ADD_ACL; |
| 9086 | /* |
| 9087 | * '+ 8' for 'add-acl(' |
| 9088 | * '- 9' for 'add-acl(' + trailing ')' |
| 9089 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9090 | 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] | 9091 | |
| 9092 | cur_arg = 1; |
| 9093 | |
| 9094 | if (!*args[cur_arg] || |
| 9095 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9096 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9097 | file, linenum, args[0]); |
| 9098 | goto out_err; |
| 9099 | } |
| 9100 | |
| 9101 | LIST_INIT(&rule->arg.map.key); |
| 9102 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9103 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9104 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9105 | file, linenum); |
| 9106 | free(proxy->conf.lfs_file); |
| 9107 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9108 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9109 | cur_arg += 1; |
| 9110 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9111 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
| 9112 | rule->action = HTTP_REQ_ACT_DEL_ACL; |
| 9113 | /* |
| 9114 | * '+ 8' for 'del-acl(' |
| 9115 | * '- 9' for 'del-acl(' + trailing ')' |
| 9116 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9117 | 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] | 9118 | |
| 9119 | cur_arg = 1; |
| 9120 | |
| 9121 | if (!*args[cur_arg] || |
| 9122 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9123 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9124 | file, linenum, args[0]); |
| 9125 | goto out_err; |
| 9126 | } |
| 9127 | |
| 9128 | LIST_INIT(&rule->arg.map.key); |
| 9129 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9130 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9131 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9132 | file, linenum); |
| 9133 | free(proxy->conf.lfs_file); |
| 9134 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9135 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9136 | cur_arg += 1; |
| 9137 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9138 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
| 9139 | rule->action = HTTP_REQ_ACT_DEL_MAP; |
| 9140 | /* |
| 9141 | * '+ 8' for 'del-map(' |
| 9142 | * '- 9' for 'del-map(' + trailing ')' |
| 9143 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9144 | 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] | 9145 | |
| 9146 | cur_arg = 1; |
| 9147 | |
| 9148 | if (!*args[cur_arg] || |
| 9149 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9150 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9151 | file, linenum, args[0]); |
| 9152 | goto out_err; |
| 9153 | } |
| 9154 | |
| 9155 | LIST_INIT(&rule->arg.map.key); |
| 9156 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9157 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9158 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9159 | file, linenum); |
| 9160 | free(proxy->conf.lfs_file); |
| 9161 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9162 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9163 | cur_arg += 1; |
| 9164 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9165 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9166 | rule->action = HTTP_REQ_ACT_SET_MAP; |
| 9167 | /* |
| 9168 | * '+ 8' for 'set-map(' |
| 9169 | * '- 9' for 'set-map(' + trailing ')' |
| 9170 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9171 | 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] | 9172 | |
| 9173 | cur_arg = 1; |
| 9174 | |
| 9175 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9176 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9177 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9178 | file, linenum, args[0]); |
| 9179 | goto out_err; |
| 9180 | } |
| 9181 | |
| 9182 | LIST_INIT(&rule->arg.map.key); |
| 9183 | LIST_INIT(&rule->arg.map.value); |
| 9184 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9185 | |
| 9186 | /* key pattern */ |
| 9187 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9188 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9189 | file, linenum); |
| 9190 | |
| 9191 | /* value pattern */ |
| 9192 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9193 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9194 | file, linenum); |
| 9195 | free(proxy->conf.lfs_file); |
| 9196 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9197 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9198 | |
| 9199 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9200 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9201 | char *errmsg = NULL; |
| 9202 | cur_arg = 1; |
| 9203 | /* try in the module list */ |
| 9204 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9205 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9206 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9207 | free(errmsg); |
| 9208 | goto out_err; |
| 9209 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9210 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9211 | 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] | 9212 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9213 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9214 | } |
| 9215 | |
| 9216 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9217 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9218 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9219 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9220 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9221 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9222 | file, linenum, args[0], errmsg); |
| 9223 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9224 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9225 | } |
| 9226 | rule->cond = cond; |
| 9227 | } |
| 9228 | else if (*args[cur_arg]) { |
| 9229 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9230 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9231 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9232 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9233 | } |
| 9234 | |
| 9235 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9236 | out_err: |
| 9237 | free(rule); |
| 9238 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9239 | } |
| 9240 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9241 | /* parse an "http-respose" rule */ |
| 9242 | struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 9243 | { |
| 9244 | struct http_res_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9245 | struct http_res_action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9246 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9247 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9248 | |
| 9249 | rule = calloc(1, sizeof(*rule)); |
| 9250 | if (!rule) { |
| 9251 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9252 | goto out_err; |
| 9253 | } |
| 9254 | |
| 9255 | if (!strcmp(args[0], "allow")) { |
| 9256 | rule->action = HTTP_RES_ACT_ALLOW; |
| 9257 | cur_arg = 1; |
| 9258 | } else if (!strcmp(args[0], "deny")) { |
| 9259 | rule->action = HTTP_RES_ACT_DENY; |
| 9260 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9261 | } else if (!strcmp(args[0], "set-nice")) { |
| 9262 | rule->action = HTTP_RES_ACT_SET_NICE; |
| 9263 | cur_arg = 1; |
| 9264 | |
| 9265 | if (!*args[cur_arg] || |
| 9266 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9267 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9268 | file, linenum, args[0]); |
| 9269 | goto out_err; |
| 9270 | } |
| 9271 | rule->arg.nice = atoi(args[cur_arg]); |
| 9272 | if (rule->arg.nice < -1024) |
| 9273 | rule->arg.nice = -1024; |
| 9274 | else if (rule->arg.nice > 1024) |
| 9275 | rule->arg.nice = 1024; |
| 9276 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9277 | } else if (!strcmp(args[0], "set-tos")) { |
| 9278 | #ifdef IP_TOS |
| 9279 | char *err; |
| 9280 | rule->action = HTTP_RES_ACT_SET_TOS; |
| 9281 | cur_arg = 1; |
| 9282 | |
| 9283 | if (!*args[cur_arg] || |
| 9284 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9285 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9286 | file, linenum, args[0]); |
| 9287 | goto out_err; |
| 9288 | } |
| 9289 | |
| 9290 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9291 | if (err && *err != '\0') { |
| 9292 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9293 | file, linenum, err, args[0]); |
| 9294 | goto out_err; |
| 9295 | } |
| 9296 | cur_arg++; |
| 9297 | #else |
| 9298 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9299 | goto out_err; |
| 9300 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9301 | } else if (!strcmp(args[0], "set-mark")) { |
| 9302 | #ifdef SO_MARK |
| 9303 | char *err; |
| 9304 | rule->action = HTTP_RES_ACT_SET_MARK; |
| 9305 | cur_arg = 1; |
| 9306 | |
| 9307 | if (!*args[cur_arg] || |
| 9308 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9309 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9310 | file, linenum, args[0]); |
| 9311 | goto out_err; |
| 9312 | } |
| 9313 | |
| 9314 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9315 | if (err && *err != '\0') { |
| 9316 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9317 | file, linenum, err, args[0]); |
| 9318 | goto out_err; |
| 9319 | } |
| 9320 | cur_arg++; |
| 9321 | global.last_checks |= LSTCHK_NETADM; |
| 9322 | #else |
| 9323 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9324 | goto out_err; |
| 9325 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9326 | } else if (!strcmp(args[0], "set-log-level")) { |
| 9327 | rule->action = HTTP_RES_ACT_SET_LOGL; |
| 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 | bad_log_level: |
| 9333 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9334 | file, linenum, args[0]); |
| 9335 | goto out_err; |
| 9336 | } |
| 9337 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9338 | rule->arg.loglevel = -1; |
| 9339 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 9340 | goto bad_log_level; |
| 9341 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9342 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9343 | rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR; |
| 9344 | cur_arg = 1; |
| 9345 | |
| 9346 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9347 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9348 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9349 | file, linenum, args[0]); |
| 9350 | goto out_err; |
| 9351 | } |
| 9352 | |
| 9353 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9354 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9355 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9356 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9357 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9358 | 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] | 9359 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9360 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9361 | free(proxy->conf.lfs_file); |
| 9362 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9363 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9364 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9365 | } 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] | 9366 | 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] | 9367 | cur_arg = 1; |
| 9368 | |
| 9369 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | a772b94 | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9370 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9371 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 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 | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9380 | error = NULL; |
| 9381 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9382 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9383 | args[cur_arg + 1], error); |
| 9384 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9385 | goto out_err; |
| 9386 | } |
| 9387 | |
| 9388 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9389 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9390 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9391 | file, linenum); |
| 9392 | |
| 9393 | free(proxy->conf.lfs_file); |
| 9394 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9395 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9396 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9397 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9398 | rule->action = HTTP_RES_ACT_DEL_HDR; |
| 9399 | cur_arg = 1; |
| 9400 | |
| 9401 | if (!*args[cur_arg] || |
| 9402 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9403 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9404 | file, linenum, args[0]); |
| 9405 | goto out_err; |
| 9406 | } |
| 9407 | |
| 9408 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9409 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9410 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9411 | proxy->conf.args.ctx = ARGC_HRS; |
| 9412 | free(proxy->conf.lfs_file); |
| 9413 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9414 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9415 | cur_arg += 1; |
| 9416 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9417 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9418 | rule->action = HTTP_RES_ACT_ADD_ACL; |
| 9419 | /* |
| 9420 | * '+ 8' for 'add-acl(' |
| 9421 | * '- 9' for 'add-acl(' + trailing ')' |
| 9422 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9423 | 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] | 9424 | |
| 9425 | cur_arg = 1; |
| 9426 | |
| 9427 | if (!*args[cur_arg] || |
| 9428 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9429 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9430 | file, linenum, args[0]); |
| 9431 | goto out_err; |
| 9432 | } |
| 9433 | |
| 9434 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9435 | proxy->conf.args.ctx = ARGC_HRS; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9436 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9437 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9438 | file, linenum); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9439 | free(proxy->conf.lfs_file); |
| 9440 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9441 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9442 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9443 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9444 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9445 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
| 9446 | rule->action = HTTP_RES_ACT_DEL_ACL; |
| 9447 | /* |
| 9448 | * '+ 8' for 'del-acl(' |
| 9449 | * '- 9' for 'del-acl(' + trailing ')' |
| 9450 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9451 | 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] | 9452 | |
| 9453 | cur_arg = 1; |
| 9454 | |
| 9455 | if (!*args[cur_arg] || |
| 9456 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9457 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9458 | file, linenum, args[0]); |
| 9459 | goto out_err; |
| 9460 | } |
| 9461 | |
| 9462 | LIST_INIT(&rule->arg.map.key); |
| 9463 | proxy->conf.args.ctx = ARGC_HRS; |
| 9464 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9465 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9466 | file, linenum); |
| 9467 | free(proxy->conf.lfs_file); |
| 9468 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9469 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9470 | cur_arg += 1; |
| 9471 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9472 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
| 9473 | rule->action = HTTP_RES_ACT_DEL_MAP; |
| 9474 | /* |
| 9475 | * '+ 8' for 'del-map(' |
| 9476 | * '- 9' for 'del-map(' + trailing ')' |
| 9477 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9478 | 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] | 9479 | |
| 9480 | cur_arg = 1; |
| 9481 | |
| 9482 | if (!*args[cur_arg] || |
| 9483 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9484 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9485 | file, linenum, args[0]); |
| 9486 | goto out_err; |
| 9487 | } |
| 9488 | |
| 9489 | LIST_INIT(&rule->arg.map.key); |
| 9490 | proxy->conf.args.ctx = ARGC_HRS; |
| 9491 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9492 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9493 | file, linenum); |
| 9494 | free(proxy->conf.lfs_file); |
| 9495 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9496 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9497 | cur_arg += 1; |
| 9498 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9499 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9500 | rule->action = HTTP_RES_ACT_SET_MAP; |
| 9501 | /* |
| 9502 | * '+ 8' for 'set-map(' |
| 9503 | * '- 9' for 'set-map(' + trailing ')' |
| 9504 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9505 | 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] | 9506 | |
| 9507 | cur_arg = 1; |
| 9508 | |
| 9509 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9510 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9511 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9512 | file, linenum, args[0]); |
| 9513 | goto out_err; |
| 9514 | } |
| 9515 | |
| 9516 | LIST_INIT(&rule->arg.map.key); |
| 9517 | LIST_INIT(&rule->arg.map.value); |
| 9518 | |
| 9519 | proxy->conf.args.ctx = ARGC_HRS; |
| 9520 | |
| 9521 | /* key pattern */ |
| 9522 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9523 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9524 | file, linenum); |
| 9525 | |
| 9526 | /* value pattern */ |
| 9527 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9528 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9529 | file, linenum); |
| 9530 | |
| 9531 | free(proxy->conf.lfs_file); |
| 9532 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9533 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9534 | |
| 9535 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9536 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9537 | char *errmsg = NULL; |
| 9538 | cur_arg = 1; |
| 9539 | /* try in the module list */ |
| 9540 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9541 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9542 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9543 | free(errmsg); |
| 9544 | goto out_err; |
| 9545 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9546 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9547 | 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] | 9548 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 9549 | goto out_err; |
| 9550 | } |
| 9551 | |
| 9552 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9553 | struct acl_cond *cond; |
| 9554 | char *errmsg = NULL; |
| 9555 | |
| 9556 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9557 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9558 | file, linenum, args[0], errmsg); |
| 9559 | free(errmsg); |
| 9560 | goto out_err; |
| 9561 | } |
| 9562 | rule->cond = cond; |
| 9563 | } |
| 9564 | else if (*args[cur_arg]) { |
| 9565 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9566 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9567 | file, linenum, args[0], args[cur_arg]); |
| 9568 | goto out_err; |
| 9569 | } |
| 9570 | |
| 9571 | return rule; |
| 9572 | out_err: |
| 9573 | free(rule); |
| 9574 | return NULL; |
| 9575 | } |
| 9576 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9577 | /* 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] | 9578 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
| 9579 | * dynamic log-format rule instead of a static string. |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9580 | */ |
| 9581 | 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] | 9582 | const char **args, char **errmsg, int use_fmt) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9583 | { |
| 9584 | struct redirect_rule *rule; |
| 9585 | int cur_arg; |
| 9586 | int type = REDIRECT_TYPE_NONE; |
| 9587 | int code = 302; |
| 9588 | const char *destination = NULL; |
| 9589 | const char *cookie = NULL; |
| 9590 | int cookie_set = 0; |
| 9591 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9592 | struct acl_cond *cond = NULL; |
| 9593 | |
| 9594 | cur_arg = 0; |
| 9595 | while (*(args[cur_arg])) { |
| 9596 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9597 | if (!*args[cur_arg + 1]) |
| 9598 | goto missing_arg; |
| 9599 | |
| 9600 | type = REDIRECT_TYPE_LOCATION; |
| 9601 | cur_arg++; |
| 9602 | destination = args[cur_arg]; |
| 9603 | } |
| 9604 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9605 | if (!*args[cur_arg + 1]) |
| 9606 | goto missing_arg; |
| 9607 | |
| 9608 | type = REDIRECT_TYPE_PREFIX; |
| 9609 | cur_arg++; |
| 9610 | destination = args[cur_arg]; |
| 9611 | } |
| 9612 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9613 | if (!*args[cur_arg + 1]) |
| 9614 | goto missing_arg; |
| 9615 | |
| 9616 | type = REDIRECT_TYPE_SCHEME; |
| 9617 | cur_arg++; |
| 9618 | destination = args[cur_arg]; |
| 9619 | } |
| 9620 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9621 | if (!*args[cur_arg + 1]) |
| 9622 | goto missing_arg; |
| 9623 | |
| 9624 | cur_arg++; |
| 9625 | cookie = args[cur_arg]; |
| 9626 | cookie_set = 1; |
| 9627 | } |
| 9628 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9629 | if (!*args[cur_arg + 1]) |
| 9630 | goto missing_arg; |
| 9631 | |
| 9632 | cur_arg++; |
| 9633 | cookie = args[cur_arg]; |
| 9634 | cookie_set = 0; |
| 9635 | } |
| 9636 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9637 | if (!*args[cur_arg + 1]) |
| 9638 | goto missing_arg; |
| 9639 | |
| 9640 | cur_arg++; |
| 9641 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9642 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9643 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9644 | "'%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] | 9645 | args[cur_arg - 1], args[cur_arg]); |
| 9646 | return NULL; |
| 9647 | } |
| 9648 | } |
| 9649 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9650 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9651 | } |
| 9652 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9653 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9654 | } |
| 9655 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9656 | strcmp(args[cur_arg], "unless") == 0) { |
| 9657 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 9658 | if (!cond) { |
| 9659 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9660 | return NULL; |
| 9661 | } |
| 9662 | break; |
| 9663 | } |
| 9664 | else { |
| 9665 | memprintf(errmsg, |
| 9666 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9667 | args[cur_arg]); |
| 9668 | return NULL; |
| 9669 | } |
| 9670 | cur_arg++; |
| 9671 | } |
| 9672 | |
| 9673 | if (type == REDIRECT_TYPE_NONE) { |
| 9674 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9675 | return NULL; |
| 9676 | } |
| 9677 | |
| 9678 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 9679 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9680 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9681 | |
| 9682 | if (!use_fmt) { |
| 9683 | /* old-style static redirect rule */ |
| 9684 | rule->rdr_str = strdup(destination); |
| 9685 | rule->rdr_len = strlen(destination); |
| 9686 | } |
| 9687 | else { |
| 9688 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9689 | |
| 9690 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9691 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9692 | * makes it hard for the user to configure a self-redirection. |
| 9693 | */ |
Godbach | 543b978 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9694 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9695 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9696 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9697 | (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9698 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9699 | free(curproxy->conf.lfs_file); |
| 9700 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9701 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9702 | } |
| 9703 | } |
| 9704 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9705 | if (cookie) { |
| 9706 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9707 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9708 | */ |
| 9709 | rule->cookie_len = strlen(cookie); |
| 9710 | if (cookie_set) { |
| 9711 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9712 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9713 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9714 | rule->cookie_len += 9; |
| 9715 | } else { |
| 9716 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9717 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9718 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9719 | rule->cookie_len += 20; |
| 9720 | } |
| 9721 | } |
| 9722 | rule->type = type; |
| 9723 | rule->code = code; |
| 9724 | rule->flags = flags; |
| 9725 | LIST_INIT(&rule->list); |
| 9726 | return rule; |
| 9727 | |
| 9728 | missing_arg: |
| 9729 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9730 | return NULL; |
| 9731 | } |
| 9732 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9733 | /************************************************************************/ |
| 9734 | /* The code below is dedicated to ACL parsing and matching */ |
| 9735 | /************************************************************************/ |
| 9736 | |
| 9737 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9738 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9739 | * which means that a request or response is ready. If some data is missing, |
| 9740 | * 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] | 9741 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9742 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9743 | * |
| 9744 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9745 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9746 | * decide whether or not an HTTP message is present ; |
| 9747 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9748 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9749 | * 1 if an HTTP message is ready |
| 9750 | */ |
| 9751 | static int |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9752 | 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] | 9753 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9754 | { |
| 9755 | struct http_txn *txn = l7; |
| 9756 | struct http_msg *msg = &txn->req; |
| 9757 | |
| 9758 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 9759 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 9760 | */ |
| 9761 | |
| 9762 | if (unlikely(!s || !txn)) |
| 9763 | return 0; |
| 9764 | |
| 9765 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9766 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9767 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9768 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9769 | if (unlikely(!s->req)) |
| 9770 | return 0; |
| 9771 | |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9772 | /* If the buffer does not leave enough free space at the end, |
| 9773 | * we must first realign it. |
| 9774 | */ |
| 9775 | if (s->req->buf->p > s->req->buf->data && |
| 9776 | s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite) |
| 9777 | buffer_slow_realign(s->req->buf); |
| 9778 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9779 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9780 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9781 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9782 | |
| 9783 | /* Try to decode HTTP request */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9784 | if (likely(msg->next < s->req->buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9785 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9786 | |
| 9787 | /* Still no valid request ? */ |
| 9788 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9789 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9790 | buffer_full(s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9791 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9792 | } |
| 9793 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9794 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9795 | return 0; |
| 9796 | } |
| 9797 | |
| 9798 | /* OK we just got a valid HTTP request. We have some minor |
| 9799 | * preparation to perform so that further checks can rely |
| 9800 | * on HTTP tests. |
| 9801 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9802 | |
| 9803 | /* If the request was parsed but was too large, we must absolutely |
| 9804 | * return an error so that it is not processed. At the moment this |
| 9805 | * cannot happen, but if the parsers are to change in the future, |
| 9806 | * we want this check to be maintained. |
| 9807 | */ |
| 9808 | if (unlikely(s->req->buf->i + s->req->buf->p > |
| 9809 | s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) { |
| 9810 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9811 | smp->data.uint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9812 | return 1; |
| 9813 | } |
| 9814 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9815 | 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] | 9816 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 9817 | s->flags |= SN_REDIRECTABLE; |
| 9818 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9819 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9820 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9821 | } |
| 9822 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9823 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9824 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9825 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9826 | |
| 9827 | /* otherwise everything's ready for the request */ |
| 9828 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9829 | else { |
| 9830 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9831 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9832 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9833 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9834 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9835 | } |
| 9836 | |
| 9837 | /* everything's OK */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9838 | smp->data.uint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9839 | return 1; |
| 9840 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9841 | |
Willy Tarreau | a4ba9db | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 9842 | /* Note: these functinos *do* modify the sample. Even in case of success, at |
| 9843 | * least the type and uint value are modified. |
| 9844 | */ |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9845 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9846 | 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] | 9847 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9848 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9849 | 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] | 9850 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9851 | |
| 9852 | /* 1. Check on METHOD |
| 9853 | * We use the pre-parsed method if it is known, and store its number as an |
| 9854 | * integer. If it is unknown, we use the pointer and the length. |
| 9855 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9856 | 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] | 9857 | { |
| 9858 | int len, meth; |
| 9859 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9860 | len = strlen(text); |
| 9861 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9862 | |
| 9863 | pattern->val.i = meth; |
| 9864 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 71196f3 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 9865 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9866 | pattern->len = len; |
| 9867 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9868 | else { |
| 9869 | pattern->ptr.str = NULL; |
| 9870 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9871 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9872 | return 1; |
| 9873 | } |
| 9874 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9875 | /* This function fetches the method of current HTTP request and stores |
| 9876 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 9877 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 9878 | * in <len> and <ptr> is NULL ; |
| 9879 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 9880 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9881 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9882 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9883 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9884 | 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] | 9885 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9886 | { |
| 9887 | int meth; |
| 9888 | struct http_txn *txn = l7; |
| 9889 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9890 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9891 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9892 | meth = txn->meth; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9893 | smp->type = SMP_T_METH; |
| 9894 | smp->data.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9895 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9896 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9897 | /* ensure the indexes are not affected */ |
| 9898 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9899 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9900 | smp->data.meth.str.len = txn->req.sl.rq.m_l; |
| 9901 | smp->data.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9902 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9903 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9904 | return 1; |
| 9905 | } |
| 9906 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9907 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9908 | 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] | 9909 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9910 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9911 | struct pattern_list *lst; |
| 9912 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9913 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9914 | list_for_each_entry(lst, &expr->patterns, list) { |
| 9915 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9916 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9917 | /* well-known method */ |
| 9918 | if (pattern->val.i != HTTP_METH_OTHER) { |
| 9919 | if (smp->data.meth.meth == pattern->val.i) |
| 9920 | return pattern; |
| 9921 | else |
| 9922 | continue; |
| 9923 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9924 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9925 | /* Other method, we must compare the strings */ |
| 9926 | if (pattern->len != smp->data.meth.str.len) |
| 9927 | continue; |
| 9928 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9929 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | c62a0c6 | 2014-08-28 20:42:57 +0200 | [diff] [blame] | 9930 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) || |
| 9931 | (!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] | 9932 | return pattern; |
| 9933 | } |
| 9934 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9935 | } |
| 9936 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9937 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9938 | 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] | 9939 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9940 | { |
| 9941 | struct http_txn *txn = l7; |
| 9942 | char *ptr; |
| 9943 | int len; |
| 9944 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9945 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9946 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9947 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9948 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9949 | |
| 9950 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9951 | if (len <= 0) |
| 9952 | return 0; |
| 9953 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9954 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9955 | smp->data.str.str = ptr; |
| 9956 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9957 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9958 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9959 | return 1; |
| 9960 | } |
| 9961 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9962 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9963 | 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] | 9964 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9965 | { |
| 9966 | struct http_txn *txn = l7; |
| 9967 | char *ptr; |
| 9968 | int len; |
| 9969 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9970 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9971 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9972 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9973 | return 0; |
| 9974 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9975 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9976 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9977 | |
| 9978 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9979 | if (len <= 0) |
| 9980 | return 0; |
| 9981 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9982 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9983 | smp->data.str.str = ptr; |
| 9984 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9985 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9986 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9987 | return 1; |
| 9988 | } |
| 9989 | |
| 9990 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9991 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9992 | 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] | 9993 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9994 | { |
| 9995 | struct http_txn *txn = l7; |
| 9996 | char *ptr; |
| 9997 | int len; |
| 9998 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9999 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10000 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10001 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10002 | return 0; |
| 10003 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10004 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10005 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10006 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10007 | smp->type = SMP_T_UINT; |
| 10008 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10009 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10010 | return 1; |
| 10011 | } |
| 10012 | |
| 10013 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10014 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10015 | 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] | 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 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10020 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10021 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10022 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10023 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10024 | 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] | 10025 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10026 | return 1; |
| 10027 | } |
| 10028 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10029 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10030 | 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] | 10031 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10032 | { |
| 10033 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10034 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10035 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10036 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10037 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10038 | 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] | 10039 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10040 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10041 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10042 | smp->type = SMP_T_IPV4; |
| 10043 | smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10044 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10045 | return 1; |
| 10046 | } |
| 10047 | |
| 10048 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10049 | 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] | 10050 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10051 | { |
| 10052 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10053 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10054 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10055 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10056 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10057 | 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] | 10058 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10059 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10060 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10061 | smp->type = SMP_T_UINT; |
| 10062 | smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10063 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10064 | return 1; |
| 10065 | } |
| 10066 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10067 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10068 | * Accepts an optional argument of type string containing the header field name, |
| 10069 | * and an optional argument of type signed or unsigned integer to request an |
| 10070 | * 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] | 10071 | * headers are considered from the first one. It does not stop on commas and |
| 10072 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10073 | */ |
| 10074 | static int |
| 10075 | 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] | 10076 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10077 | { |
| 10078 | struct http_txn *txn = l7; |
| 10079 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10080 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
| 10081 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 10082 | int occ = 0; |
| 10083 | const char *name_str = NULL; |
| 10084 | int name_len = 0; |
| 10085 | |
| 10086 | if (!ctx) { |
| 10087 | /* first call */ |
| 10088 | ctx = &static_hdr_ctx; |
| 10089 | ctx->idx = 0; |
| 10090 | smp->ctx.a[0] = ctx; |
| 10091 | } |
| 10092 | |
| 10093 | if (args) { |
| 10094 | if (args[0].type != ARGT_STR) |
| 10095 | return 0; |
| 10096 | name_str = args[0].data.str.str; |
| 10097 | name_len = args[0].data.str.len; |
| 10098 | |
| 10099 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10100 | occ = args[1].data.uint; |
| 10101 | } |
| 10102 | |
| 10103 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10104 | |
| 10105 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10106 | /* search for header from the beginning */ |
| 10107 | ctx->idx = 0; |
| 10108 | |
| 10109 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10110 | /* no explicit occurrence and single fetch => last header by default */ |
| 10111 | occ = -1; |
| 10112 | |
| 10113 | if (!occ) |
| 10114 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10115 | smp->flags |= SMP_F_NOT_LAST; |
| 10116 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10117 | smp->type = SMP_T_STR; |
| 10118 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10119 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
| 10120 | return 1; |
| 10121 | |
| 10122 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10123 | return 0; |
| 10124 | } |
| 10125 | |
| 10126 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10127 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10128 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10129 | */ |
| 10130 | static int |
| 10131 | 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] | 10132 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10133 | { |
| 10134 | struct http_txn *txn = l7; |
| 10135 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10136 | struct hdr_ctx ctx; |
| 10137 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 10138 | int cnt; |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10139 | const char *name = NULL; |
| 10140 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10141 | |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10142 | if (args && args->type == ARGT_STR) { |
| 10143 | name = args->data.str.str; |
| 10144 | len = args->data.str.len; |
| 10145 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10146 | |
| 10147 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10148 | |
| 10149 | ctx.idx = 0; |
| 10150 | cnt = 0; |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10151 | 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] | 10152 | cnt++; |
| 10153 | |
| 10154 | smp->type = SMP_T_UINT; |
| 10155 | smp->data.uint = cnt; |
| 10156 | smp->flags = SMP_F_VOL_HDR; |
| 10157 | return 1; |
| 10158 | } |
| 10159 | |
| 10160 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10161 | * Accepts an optional argument of type string containing the header field name, |
| 10162 | * and an optional argument of type signed or unsigned integer to request an |
| 10163 | * 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] | 10164 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10165 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10166 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10167 | 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] | 10168 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10169 | { |
| 10170 | struct http_txn *txn = l7; |
| 10171 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10172 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10173 | 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] | 10174 | int occ = 0; |
| 10175 | const char *name_str = NULL; |
| 10176 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10177 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10178 | if (!ctx) { |
| 10179 | /* first call */ |
| 10180 | ctx = &static_hdr_ctx; |
| 10181 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10182 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10183 | } |
| 10184 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10185 | if (args) { |
| 10186 | if (args[0].type != ARGT_STR) |
| 10187 | return 0; |
| 10188 | name_str = args[0].data.str.str; |
| 10189 | name_len = args[0].data.str.len; |
| 10190 | |
| 10191 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10192 | occ = args[1].data.uint; |
| 10193 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10194 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10195 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10196 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10197 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10198 | /* search for header from the beginning */ |
| 10199 | ctx->idx = 0; |
| 10200 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10201 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10202 | /* no explicit occurrence and single fetch => last header by default */ |
| 10203 | occ = -1; |
| 10204 | |
| 10205 | if (!occ) |
| 10206 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10207 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10208 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10209 | smp->type = SMP_T_STR; |
| 10210 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10211 | 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] | 10212 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10213 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10214 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10215 | return 0; |
| 10216 | } |
| 10217 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10218 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10219 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10220 | */ |
| 10221 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10222 | 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] | 10223 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10224 | { |
| 10225 | struct http_txn *txn = l7; |
| 10226 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10227 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10228 | 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] | 10229 | int cnt; |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10230 | const char *name = NULL; |
| 10231 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10232 | |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10233 | if (args && args->type == ARGT_STR) { |
| 10234 | name = args->data.str.str; |
| 10235 | len = args->data.str.len; |
| 10236 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10237 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10238 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10239 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10240 | ctx.idx = 0; |
| 10241 | cnt = 0; |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10242 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10243 | cnt++; |
| 10244 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10245 | smp->type = SMP_T_UINT; |
| 10246 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10247 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10248 | return 1; |
| 10249 | } |
| 10250 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10251 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10252 | * takes a mandatory argument of type string and an optional one of type int |
| 10253 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10254 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10255 | */ |
| 10256 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10257 | 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] | 10258 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10259 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10260 | int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10261 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10262 | if (ret > 0) { |
| 10263 | smp->type = SMP_T_UINT; |
| 10264 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10265 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10266 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10267 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10268 | } |
| 10269 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10270 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10271 | * and an optional one of type int to designate a specific occurrence. |
| 10272 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10273 | */ |
| 10274 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10275 | 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] | 10276 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10277 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10278 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10279 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10280 | 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] | 10281 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 10282 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10283 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10284 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10285 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10286 | if (smp->data.str.len < temp->size - 1) { |
| 10287 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 10288 | temp->str[smp->data.str.len] = '\0'; |
| 10289 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 10290 | smp->type = SMP_T_IPV6; |
| 10291 | break; |
| 10292 | } |
| 10293 | } |
| 10294 | } |
| 10295 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10296 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10297 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10298 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10299 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10300 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10301 | } |
| 10302 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10303 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10304 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10305 | */ |
| 10306 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10307 | 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] | 10308 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10309 | { |
| 10310 | struct http_txn *txn = l7; |
| 10311 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10312 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10313 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10314 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10315 | 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] | 10316 | ptr = http_get_path(txn); |
| 10317 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10318 | return 0; |
| 10319 | |
| 10320 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10321 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10322 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10323 | |
| 10324 | while (ptr < end && *ptr != '?') |
| 10325 | ptr++; |
| 10326 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10327 | smp->data.str.len = ptr - smp->data.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10328 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10329 | return 1; |
| 10330 | } |
| 10331 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10332 | /* This produces a concatenation of the first occurrence of the Host header |
| 10333 | * followed by the path component if it begins with a slash ('/'). This means |
| 10334 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10335 | * If no Host header is found, then the path is returned as-is. The returned |
| 10336 | * 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] | 10337 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10338 | */ |
| 10339 | static int |
| 10340 | 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] | 10341 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10342 | { |
| 10343 | struct http_txn *txn = l7; |
| 10344 | char *ptr, *end, *beg; |
| 10345 | struct hdr_ctx ctx; |
Willy Tarreau | c1fbbd4 | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10346 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10347 | |
| 10348 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10349 | |
| 10350 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10351 | 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] | 10352 | return smp_fetch_path(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10353 | |
| 10354 | /* 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] | 10355 | temp = get_trash_chunk(); |
| 10356 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10357 | smp->type = SMP_T_STR; |
Willy Tarreau | c1fbbd4 | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10358 | smp->data.str.str = temp->str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10359 | smp->data.str.len = ctx.vlen; |
| 10360 | |
| 10361 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10362 | 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] | 10363 | beg = http_get_path(txn); |
| 10364 | if (!beg) |
| 10365 | beg = end; |
| 10366 | |
| 10367 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10368 | |
| 10369 | if (beg < ptr && *beg == '/') { |
| 10370 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 10371 | smp->data.str.len += ptr - beg; |
| 10372 | } |
| 10373 | |
| 10374 | smp->flags = SMP_F_VOL_1ST; |
| 10375 | return 1; |
| 10376 | } |
| 10377 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10378 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10379 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10380 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10381 | * 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] | 10382 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10383 | * 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] | 10384 | * high-traffic sites without having to store whole paths. |
| 10385 | */ |
| 10386 | static int |
| 10387 | 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] | 10388 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10389 | { |
| 10390 | struct http_txn *txn = l7; |
| 10391 | struct hdr_ctx ctx; |
| 10392 | unsigned int hash = 0; |
| 10393 | char *ptr, *beg, *end; |
| 10394 | int len; |
| 10395 | |
| 10396 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10397 | |
| 10398 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10399 | 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] | 10400 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10401 | ptr = ctx.line + ctx.val; |
| 10402 | len = ctx.vlen; |
| 10403 | while (len--) |
| 10404 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10405 | } |
| 10406 | |
| 10407 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10408 | 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] | 10409 | beg = http_get_path(txn); |
| 10410 | if (!beg) |
| 10411 | beg = end; |
| 10412 | |
| 10413 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10414 | |
| 10415 | if (beg < ptr && *beg == '/') { |
| 10416 | while (beg < ptr) |
| 10417 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10418 | } |
| 10419 | hash = full_hash(hash); |
| 10420 | |
| 10421 | smp->type = SMP_T_UINT; |
| 10422 | smp->data.uint = hash; |
| 10423 | smp->flags = SMP_F_VOL_1ST; |
| 10424 | return 1; |
| 10425 | } |
| 10426 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10427 | /* 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] | 10428 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10429 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10430 | * 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] | 10431 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10432 | * 8 bytes would still work. |
| 10433 | */ |
| 10434 | static int |
| 10435 | 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] | 10436 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10437 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10438 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10439 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
| 10440 | |
| 10441 | if (!cli_conn) |
| 10442 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10443 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10444 | if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10445 | return 0; |
| 10446 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10447 | temp = get_trash_chunk(); |
Willy Tarreau | 0dff81c | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10448 | *(unsigned int *)temp->str = htonl(smp->data.uint); |
| 10449 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10450 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10451 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10452 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10453 | 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] | 10454 | temp->len += 4; |
| 10455 | break; |
| 10456 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10457 | 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] | 10458 | temp->len += 16; |
| 10459 | break; |
| 10460 | default: |
| 10461 | return 0; |
| 10462 | } |
| 10463 | |
| 10464 | smp->data.str = *temp; |
| 10465 | smp->type = SMP_T_BIN; |
| 10466 | return 1; |
| 10467 | } |
| 10468 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10469 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10470 | 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] | 10471 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10472 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10473 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10474 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10475 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10476 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10477 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10478 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10479 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 10480 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10481 | return 1; |
| 10482 | } |
| 10483 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10484 | /* return a valid test if the current request is the first one on the connection */ |
| 10485 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10486 | 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] | 10487 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10488 | { |
| 10489 | if (!s) |
| 10490 | return 0; |
| 10491 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10492 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 10493 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10494 | return 1; |
| 10495 | } |
| 10496 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10497 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10498 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10499 | 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] | 10500 | const struct arg *args, struct sample *smp, const char *kw) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10501 | { |
| 10502 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10503 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10504 | return 0; |
| 10505 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10506 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10507 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10508 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10509 | return 0; |
| 10510 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10511 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10512 | 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] | 10513 | return 1; |
| 10514 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10515 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10516 | /* Accepts exactly 1 argument of type userlist */ |
| 10517 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10518 | 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] | 10519 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10520 | { |
| 10521 | |
| 10522 | if (!args || args->type != ARGT_USR) |
| 10523 | return 0; |
| 10524 | |
| 10525 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10526 | |
| 10527 | if (!get_http_auth(l4)) |
| 10528 | return 0; |
| 10529 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10530 | /* if the user does not belong to the userlist or has a wrong password, |
| 10531 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10532 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10533 | */ |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10534 | if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass)) |
| 10535 | return 0; |
| 10536 | |
| 10537 | /* pat_match_auth() will need the user list */ |
| 10538 | smp->ctx.a[0] = args->data.usr; |
| 10539 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10540 | smp->type = SMP_T_STR; |
| 10541 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10542 | smp->data.str.str = l4->txn.auth.user; |
| 10543 | smp->data.str.len = strlen(l4->txn.auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10544 | |
| 10545 | return 1; |
| 10546 | } |
| 10547 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10548 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10549 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10550 | * the cookie value is returned into *value and *value_l, and the function |
| 10551 | * returns a pointer to the next pointer to search from if the value was found. |
| 10552 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10553 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10554 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10555 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10556 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10557 | * list is expected. |
| 10558 | */ |
| 10559 | static char * |
| 10560 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10561 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10562 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10563 | { |
| 10564 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10565 | char *next; |
| 10566 | |
| 10567 | /* we search at least a cookie name followed by an equal, and more |
| 10568 | * generally something like this : |
| 10569 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10570 | */ |
| 10571 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10572 | /* Iterate through all cookies on this line */ |
| 10573 | |
| 10574 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 10575 | att_beg++; |
| 10576 | |
| 10577 | /* find att_end : this is the first character after the last non |
| 10578 | * space before the equal. It may be equal to hdr_end. |
| 10579 | */ |
| 10580 | equal = att_end = att_beg; |
| 10581 | |
| 10582 | while (equal < hdr_end) { |
| 10583 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10584 | break; |
| 10585 | if (http_is_spht[(unsigned char)*equal++]) |
| 10586 | continue; |
| 10587 | att_end = equal; |
| 10588 | } |
| 10589 | |
| 10590 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10591 | * is between <att_beg> and <equal>, both may be identical. |
| 10592 | */ |
| 10593 | |
| 10594 | /* look for end of cookie if there is an equal sign */ |
| 10595 | if (equal < hdr_end && *equal == '=') { |
| 10596 | /* look for the beginning of the value */ |
| 10597 | val_beg = equal + 1; |
| 10598 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 10599 | val_beg++; |
| 10600 | |
| 10601 | /* find the end of the value, respecting quotes */ |
| 10602 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10603 | |
| 10604 | /* make val_end point to the first white space or delimitor after the value */ |
| 10605 | val_end = next; |
| 10606 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 10607 | val_end--; |
| 10608 | } else { |
| 10609 | val_beg = val_end = next = equal; |
| 10610 | } |
| 10611 | |
| 10612 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10613 | * they will automatically be removed if a header before them is removed, |
| 10614 | * since they're supposed to be linked together. |
| 10615 | */ |
| 10616 | if (*att_beg == '$') |
| 10617 | continue; |
| 10618 | |
| 10619 | /* Ignore cookies with no equal sign */ |
| 10620 | if (equal == next) |
| 10621 | continue; |
| 10622 | |
| 10623 | /* Now we have the cookie name between att_beg and att_end, and |
| 10624 | * its value between val_beg and val_end. |
| 10625 | */ |
| 10626 | |
| 10627 | if (att_end - att_beg == cookie_name_l && |
| 10628 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10629 | /* let's return this value and indicate where to go on from */ |
| 10630 | *value = val_beg; |
| 10631 | *value_l = val_end - val_beg; |
| 10632 | return next + 1; |
| 10633 | } |
| 10634 | |
| 10635 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10636 | if (!list) |
| 10637 | break; |
| 10638 | } |
| 10639 | |
| 10640 | return NULL; |
| 10641 | } |
| 10642 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10643 | /* Fetch a captured HTTP request header. The index is the position of |
| 10644 | * the "capture" option in the configuration file |
| 10645 | */ |
| 10646 | static int |
| 10647 | smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10648 | const struct arg *args, struct sample *smp, const char *kw) |
| 10649 | { |
| 10650 | struct proxy *fe = l4->fe; |
| 10651 | struct http_txn *txn = l7; |
| 10652 | int idx; |
| 10653 | |
| 10654 | if (!args || args->type != ARGT_UINT) |
| 10655 | return 0; |
| 10656 | |
| 10657 | idx = args->data.uint; |
| 10658 | |
| 10659 | if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL) |
| 10660 | return 0; |
| 10661 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10662 | smp->type = SMP_T_STR; |
| 10663 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10664 | smp->data.str.str = txn->req.cap[idx]; |
| 10665 | smp->data.str.len = strlen(txn->req.cap[idx]); |
| 10666 | |
| 10667 | return 1; |
| 10668 | } |
| 10669 | |
| 10670 | /* Fetch a captured HTTP response header. The index is the position of |
| 10671 | * the "capture" option in the configuration file |
| 10672 | */ |
| 10673 | static int |
| 10674 | smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10675 | const struct arg *args, struct sample *smp, const char *kw) |
| 10676 | { |
| 10677 | struct proxy *fe = l4->fe; |
| 10678 | struct http_txn *txn = l7; |
| 10679 | int idx; |
| 10680 | |
| 10681 | if (!args || args->type != ARGT_UINT) |
| 10682 | return 0; |
| 10683 | |
| 10684 | idx = args->data.uint; |
| 10685 | |
| 10686 | if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL) |
| 10687 | return 0; |
| 10688 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10689 | smp->type = SMP_T_STR; |
| 10690 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10691 | smp->data.str.str = txn->rsp.cap[idx]; |
| 10692 | smp->data.str.len = strlen(txn->rsp.cap[idx]); |
| 10693 | |
| 10694 | return 1; |
| 10695 | } |
| 10696 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10697 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 10698 | static int |
| 10699 | smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10700 | const struct arg *args, struct sample *smp, const char *kw) |
| 10701 | { |
| 10702 | struct chunk *temp; |
| 10703 | struct http_txn *txn = l7; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10704 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10705 | |
| 10706 | if (!txn->uri) |
| 10707 | return 0; |
| 10708 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10709 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10710 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10711 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10712 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10713 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10714 | temp = get_trash_chunk(); |
| 10715 | temp->str = txn->uri; |
| 10716 | temp->len = ptr - txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10717 | smp->data.str = *temp; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10718 | smp->type = SMP_T_STR; |
| 10719 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10720 | |
| 10721 | return 1; |
| 10722 | |
| 10723 | } |
| 10724 | |
| 10725 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 10726 | static int |
| 10727 | smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10728 | const struct arg *args, struct sample *smp, const char *kw) |
| 10729 | { |
| 10730 | struct chunk *temp; |
| 10731 | struct http_txn *txn = l7; |
| 10732 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10733 | |
| 10734 | if (!txn->uri) |
| 10735 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10736 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10737 | ptr = txn->uri; |
| 10738 | |
| 10739 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10740 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10741 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10742 | if (!*ptr) |
| 10743 | return 0; |
| 10744 | |
| 10745 | ptr++; /* skip the space */ |
| 10746 | |
| 10747 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10748 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10749 | if (!ptr) |
| 10750 | return 0; |
| 10751 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 10752 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10753 | |
| 10754 | smp->data.str = *temp; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10755 | smp->data.str.len = ptr - temp->str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10756 | smp->type = SMP_T_STR; |
| 10757 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10758 | |
| 10759 | return 1; |
| 10760 | } |
| 10761 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10762 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 10763 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10764 | */ |
| 10765 | static int |
| 10766 | smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10767 | const struct arg *args, struct sample *smp, const char *kw) |
| 10768 | { |
| 10769 | struct http_txn *txn = l7; |
| 10770 | |
| 10771 | if (txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
| 10772 | return 0; |
| 10773 | |
| 10774 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 10775 | smp->data.str.str = "HTTP/1.1"; |
| 10776 | else |
| 10777 | smp->data.str.str = "HTTP/1.0"; |
| 10778 | |
| 10779 | smp->data.str.len = 8; |
| 10780 | smp->type = SMP_T_STR; |
| 10781 | smp->flags = SMP_F_CONST; |
| 10782 | return 1; |
| 10783 | |
| 10784 | } |
| 10785 | |
| 10786 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 10787 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10788 | */ |
| 10789 | static int |
| 10790 | smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10791 | const struct arg *args, struct sample *smp, const char *kw) |
| 10792 | { |
| 10793 | struct http_txn *txn = l7; |
| 10794 | |
| 10795 | if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
| 10796 | return 0; |
| 10797 | |
| 10798 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
| 10799 | smp->data.str.str = "HTTP/1.1"; |
| 10800 | else |
| 10801 | smp->data.str.str = "HTTP/1.0"; |
| 10802 | |
| 10803 | smp->data.str.len = 8; |
| 10804 | smp->type = SMP_T_STR; |
| 10805 | smp->flags = SMP_F_CONST; |
| 10806 | return 1; |
| 10807 | |
| 10808 | } |
| 10809 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10810 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10811 | /* 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] | 10812 | * 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] | 10813 | * 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] | 10814 | * 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] | 10815 | * 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] | 10816 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 10817 | * 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] | 10818 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 10819 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10820 | */ |
William Lallemand | 07c8b24 | 2014-05-02 17:11:07 +0200 | [diff] [blame] | 10821 | 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] | 10822 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10823 | { |
| 10824 | struct http_txn *txn = l7; |
| 10825 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10826 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10827 | const struct http_msg *msg; |
| 10828 | const char *hdr_name; |
| 10829 | int hdr_name_len; |
| 10830 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10831 | int occ = 0; |
| 10832 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10833 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10834 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10835 | return 0; |
| 10836 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10837 | if (!ctx) { |
| 10838 | /* first call */ |
| 10839 | ctx = &static_hdr_ctx; |
| 10840 | ctx->idx = 0; |
| 10841 | smp->ctx.a[2] = ctx; |
| 10842 | } |
| 10843 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10844 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10845 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10846 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10847 | msg = &txn->req; |
| 10848 | hdr_name = "Cookie"; |
| 10849 | hdr_name_len = 6; |
| 10850 | } else { |
| 10851 | msg = &txn->rsp; |
| 10852 | hdr_name = "Set-Cookie"; |
| 10853 | hdr_name_len = 10; |
| 10854 | } |
| 10855 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10856 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10857 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 10858 | occ = -1; |
| 10859 | |
| 10860 | /* OK so basically here, either we want only one value and it's the |
| 10861 | * last one, or we want to iterate over all of them and we fetch the |
| 10862 | * next one. |
| 10863 | */ |
| 10864 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10865 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10866 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10867 | /* search for the header from the beginning, we must first initialize |
| 10868 | * the search parameters. |
| 10869 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10870 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10871 | ctx->idx = 0; |
| 10872 | } |
| 10873 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10874 | smp->flags |= SMP_F_VOL_HDR; |
| 10875 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10876 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10877 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 10878 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10879 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 10880 | goto out; |
| 10881 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10882 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10883 | continue; |
| 10884 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10885 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 10886 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10887 | } |
| 10888 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10889 | smp->type = SMP_T_STR; |
| 10890 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10891 | 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] | 10892 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10893 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10894 | &smp->data.str.str, |
| 10895 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10896 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10897 | found = 1; |
| 10898 | if (occ >= 0) { |
| 10899 | /* one value was returned into smp->data.str.{str,len} */ |
| 10900 | smp->flags |= SMP_F_NOT_LAST; |
| 10901 | return 1; |
| 10902 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10903 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10904 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10905 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10906 | /* all cookie headers and values were scanned. If we're looking for the |
| 10907 | * last occurrence, we may return it now. |
| 10908 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10909 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10910 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10911 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10912 | } |
| 10913 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10914 | /* 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] | 10915 | * 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] | 10916 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 10917 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10918 | */ |
| 10919 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10920 | 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] | 10921 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10922 | { |
| 10923 | struct http_txn *txn = l7; |
| 10924 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10925 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10926 | const struct http_msg *msg; |
| 10927 | const char *hdr_name; |
| 10928 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10929 | int cnt; |
| 10930 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10931 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10932 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10933 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10934 | return 0; |
| 10935 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10936 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10937 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10938 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10939 | msg = &txn->req; |
| 10940 | hdr_name = "Cookie"; |
| 10941 | hdr_name_len = 6; |
| 10942 | } else { |
| 10943 | msg = &txn->rsp; |
| 10944 | hdr_name = "Set-Cookie"; |
| 10945 | hdr_name_len = 10; |
| 10946 | } |
| 10947 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10948 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 10949 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10950 | ctx.idx = 0; |
| 10951 | cnt = 0; |
| 10952 | |
| 10953 | while (1) { |
| 10954 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 10955 | if (!val_beg) { |
| 10956 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 10957 | break; |
| 10958 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10959 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10960 | continue; |
| 10961 | |
| 10962 | val_beg = ctx.line + ctx.val; |
| 10963 | val_end = val_beg + ctx.vlen; |
| 10964 | } |
| 10965 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10966 | smp->type = SMP_T_STR; |
| 10967 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10968 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10969 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10970 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10971 | &smp->data.str.str, |
| 10972 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10973 | cnt++; |
| 10974 | } |
| 10975 | } |
| 10976 | |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 10977 | smp->type = SMP_T_UINT; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10978 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10979 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10980 | return 1; |
| 10981 | } |
| 10982 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10983 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 10984 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 10985 | */ |
| 10986 | static int |
| 10987 | 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] | 10988 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10989 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10990 | int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10991 | |
| 10992 | if (ret > 0) { |
| 10993 | smp->type = SMP_T_UINT; |
| 10994 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10995 | } |
| 10996 | |
| 10997 | return ret; |
| 10998 | } |
| 10999 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11000 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11001 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11002 | /************************************************************************/ |
| 11003 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11004 | /* |
| 11005 | * Given a path string and its length, find the position of beginning of the |
| 11006 | * query string. Returns NULL if no query string is found in the path. |
| 11007 | * |
| 11008 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11009 | * |
| 11010 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 11011 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11012 | 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] | 11013 | { |
| 11014 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11015 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11016 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11017 | return p ? p + 1 : NULL; |
| 11018 | } |
| 11019 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11020 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11021 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11022 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11023 | } |
| 11024 | |
| 11025 | /* |
| 11026 | * Given a url parameter, find the starting position of the first occurence, |
| 11027 | * or NULL if the parameter is not found. |
| 11028 | * |
| 11029 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11030 | * the function will return query_string+8. |
| 11031 | */ |
| 11032 | static char* |
| 11033 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11034 | char* url_param_name, size_t url_param_name_l, |
| 11035 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11036 | { |
| 11037 | char *pos, *last; |
| 11038 | |
| 11039 | pos = query_string; |
| 11040 | last = query_string + query_string_l - url_param_name_l - 1; |
| 11041 | |
| 11042 | while (pos <= last) { |
| 11043 | if (pos[url_param_name_l] == '=') { |
| 11044 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 11045 | return pos; |
| 11046 | pos += url_param_name_l + 1; |
| 11047 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11048 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11049 | pos++; |
| 11050 | pos++; |
| 11051 | } |
| 11052 | return NULL; |
| 11053 | } |
| 11054 | |
| 11055 | /* |
Cyril Bonté | 0f836e1 | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11056 | * Given a url parameter name and a query string, find the next value. |
| 11057 | * An empty url_param_name matches the first available parameter. |
| 11058 | * If the parameter is found, 1 is returned and *value / *value_l are updated |
| 11059 | * to respectively provide a pointer to the value and its length. |
| 11060 | * Otherwise, 0 is returned and value/value_l are not modified. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11061 | */ |
| 11062 | static int |
| 11063 | find_url_param_value(char* path, size_t path_l, |
| 11064 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11065 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11066 | { |
| 11067 | char *query_string, *qs_end; |
| 11068 | char *arg_start; |
| 11069 | char *value_start, *value_end; |
| 11070 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11071 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11072 | if (!query_string) |
| 11073 | return 0; |
| 11074 | |
| 11075 | qs_end = path + path_l; |
| 11076 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11077 | url_param_name, url_param_name_l, |
| 11078 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11079 | if (!arg_start) |
| 11080 | return 0; |
| 11081 | |
| 11082 | value_start = arg_start + url_param_name_l + 1; |
| 11083 | value_end = value_start; |
| 11084 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11085 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11086 | value_end++; |
| 11087 | |
| 11088 | *value = value_start; |
| 11089 | *value_l = value_end - value_start; |
Cyril Bonté | 0f836e1 | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11090 | return 1; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11091 | } |
| 11092 | |
| 11093 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11094 | 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] | 11095 | const struct arg *args, struct sample *smp, const char *kw) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11096 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11097 | char delim = '?'; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11098 | struct http_txn *txn = l7; |
| 11099 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11100 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11101 | if (!args || args[0].type != ARGT_STR || |
| 11102 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11103 | return 0; |
| 11104 | |
| 11105 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11106 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11107 | if (args[1].type) |
| 11108 | delim = *args[1].data.str.str; |
| 11109 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11110 | 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] | 11111 | args->data.str.str, args->data.str.len, |
| 11112 | &smp->data.str.str, &smp->data.str.len, |
| 11113 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11114 | return 0; |
| 11115 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11116 | smp->type = SMP_T_STR; |
| 11117 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11118 | return 1; |
| 11119 | } |
| 11120 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11121 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11122 | * above). |
| 11123 | */ |
| 11124 | static int |
| 11125 | 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] | 11126 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11127 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 11128 | 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] | 11129 | |
| 11130 | if (ret > 0) { |
| 11131 | smp->type = SMP_T_UINT; |
| 11132 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 11133 | } |
| 11134 | |
| 11135 | return ret; |
| 11136 | } |
| 11137 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11138 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11139 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11140 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11141 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11142 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11143 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11144 | * high-traffic sites without having to store whole paths. |
| 11145 | * this differs from the base32 functions in that it includes the url parameters |
| 11146 | * as well as the path |
| 11147 | */ |
| 11148 | static int |
| 11149 | 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] | 11150 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11151 | { |
| 11152 | struct http_txn *txn = l7; |
| 11153 | struct hdr_ctx ctx; |
| 11154 | unsigned int hash = 0; |
| 11155 | char *ptr, *beg, *end; |
| 11156 | int len; |
| 11157 | |
| 11158 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11159 | |
| 11160 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11161 | 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] | 11162 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11163 | ptr = ctx.line + ctx.val; |
| 11164 | len = ctx.vlen; |
| 11165 | while (len--) |
| 11166 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11167 | } |
| 11168 | |
| 11169 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11170 | 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] | 11171 | beg = http_get_path(txn); |
| 11172 | if (!beg) |
| 11173 | beg = end; |
| 11174 | |
| 11175 | for (ptr = beg; ptr < end ; ptr++); |
| 11176 | |
| 11177 | if (beg < ptr && *beg == '/') { |
| 11178 | while (beg < ptr) |
| 11179 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11180 | } |
| 11181 | hash = full_hash(hash); |
| 11182 | |
| 11183 | smp->type = SMP_T_UINT; |
| 11184 | smp->data.uint = hash; |
| 11185 | smp->flags = SMP_F_VOL_1ST; |
| 11186 | return 1; |
| 11187 | } |
| 11188 | |
| 11189 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11190 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11191 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11192 | * on the source address length. The URL hash is stored before the address so |
| 11193 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11194 | * 8 bytes would still work. |
| 11195 | */ |
| 11196 | static int |
| 11197 | 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] | 11198 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11199 | { |
| 11200 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11201 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11202 | |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 11203 | if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11204 | return 0; |
| 11205 | |
| 11206 | temp = get_trash_chunk(); |
| 11207 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 11208 | temp->len += sizeof(smp->data.uint); |
| 11209 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11210 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11211 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11212 | 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] | 11213 | temp->len += 4; |
| 11214 | break; |
| 11215 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11216 | 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] | 11217 | temp->len += 16; |
| 11218 | break; |
| 11219 | default: |
| 11220 | return 0; |
| 11221 | } |
| 11222 | |
| 11223 | smp->data.str = *temp; |
| 11224 | smp->type = SMP_T_BIN; |
| 11225 | return 1; |
| 11226 | } |
| 11227 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11228 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11229 | * keyword. These keywords support an optional positive or negative occurrence |
| 11230 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11231 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11232 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11233 | * error message in case of error, that the caller is responsible for freeing. |
| 11234 | * The initial location must either be freeable or NULL. |
| 11235 | */ |
| 11236 | static int val_hdr(struct arg *arg, char **err_msg) |
| 11237 | { |
| 11238 | 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] | 11239 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11240 | return 0; |
| 11241 | } |
| 11242 | return 1; |
| 11243 | } |
| 11244 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11245 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11246 | * adds an optional offset found in args[0] and emits a string representing |
| 11247 | * the date in RFC-1123/5322 format. |
| 11248 | */ |
| 11249 | static int sample_conv_http_date(const struct arg *args, struct sample *smp) |
| 11250 | { |
Cyril Bonté | f980b36 | 2016-01-22 19:40:28 +0100 | [diff] [blame] | 11251 | const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11252 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11253 | struct chunk *temp; |
| 11254 | struct tm *tm; |
| 11255 | time_t curr_date = smp->data.uint; |
| 11256 | |
| 11257 | /* add offset */ |
| 11258 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 11259 | curr_date += args[0].data.sint; |
| 11260 | |
| 11261 | tm = gmtime(&curr_date); |
Thierry FOURNIER | 9555872 | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 11262 | if (!tm) |
| 11263 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11264 | |
| 11265 | temp = get_trash_chunk(); |
| 11266 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11267 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11268 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11269 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11270 | |
| 11271 | smp->data.str = *temp; |
| 11272 | smp->type = SMP_T_STR; |
| 11273 | return 1; |
| 11274 | } |
| 11275 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11276 | /* Match language range with language tag. RFC2616 14.4: |
| 11277 | * |
| 11278 | * A language-range matches a language-tag if it exactly equals |
| 11279 | * the tag, or if it exactly equals a prefix of the tag such |
| 11280 | * that the first tag character following the prefix is "-". |
| 11281 | * |
| 11282 | * Return 1 if the strings match, else return 0. |
| 11283 | */ |
| 11284 | static inline int language_range_match(const char *range, int range_len, |
| 11285 | const char *tag, int tag_len) |
| 11286 | { |
| 11287 | const char *end = range + range_len; |
| 11288 | const char *tend = tag + tag_len; |
| 11289 | while (range < end) { |
| 11290 | if (*range == '-' && tag == tend) |
| 11291 | return 1; |
| 11292 | if (*range != *tag || tag == tend) |
| 11293 | return 0; |
| 11294 | range++; |
| 11295 | tag++; |
| 11296 | } |
| 11297 | /* Return true only if the last char of the tag is matched. */ |
| 11298 | return tag == tend; |
| 11299 | } |
| 11300 | |
| 11301 | /* Arguments: The list of expected value, the number of parts returned and the separator */ |
| 11302 | static int sample_conv_q_prefered(const struct arg *args, struct sample *smp) |
| 11303 | { |
| 11304 | const char *al = smp->data.str.str; |
| 11305 | const char *end = al + smp->data.str.len; |
| 11306 | const char *token; |
| 11307 | int toklen; |
| 11308 | int qvalue; |
| 11309 | const char *str; |
| 11310 | const char *w; |
| 11311 | int best_q = 0; |
| 11312 | |
| 11313 | /* Set the constant to the sample, because the output of the |
| 11314 | * function will be peek in the constant configuration string. |
| 11315 | */ |
| 11316 | smp->flags |= SMP_F_CONST; |
| 11317 | smp->data.str.size = 0; |
| 11318 | smp->data.str.str = ""; |
| 11319 | smp->data.str.len = 0; |
| 11320 | |
| 11321 | /* Parse the accept language */ |
| 11322 | while (1) { |
| 11323 | |
| 11324 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11325 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11326 | al++; |
| 11327 | if (al >= end) |
| 11328 | break; |
| 11329 | |
| 11330 | /* Start of the fisrt word. */ |
| 11331 | token = al; |
| 11332 | |
| 11333 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11334 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11335 | al++; |
| 11336 | if (al == token) |
| 11337 | goto expect_comma; |
| 11338 | |
| 11339 | /* Length of the token. */ |
| 11340 | toklen = al - token; |
| 11341 | qvalue = 1000; |
| 11342 | |
| 11343 | /* Check if the token exists in the list. If the token not exists, |
| 11344 | * jump to the next token. |
| 11345 | */ |
| 11346 | str = args[0].data.str.str; |
| 11347 | w = str; |
| 11348 | while (1) { |
| 11349 | if (*str == ';' || *str == '\0') { |
| 11350 | if (language_range_match(token, toklen, w, str-w)) |
| 11351 | goto look_for_q; |
| 11352 | if (*str == '\0') |
| 11353 | goto expect_comma; |
| 11354 | w = str + 1; |
| 11355 | } |
| 11356 | str++; |
| 11357 | } |
| 11358 | goto expect_comma; |
| 11359 | |
| 11360 | look_for_q: |
| 11361 | |
| 11362 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11363 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11364 | al++; |
| 11365 | if (al >= end) |
| 11366 | goto process_value; |
| 11367 | |
| 11368 | /* If ',' is found, process the result */ |
| 11369 | if (*al == ',') |
| 11370 | goto process_value; |
| 11371 | |
| 11372 | /* If the character is different from ';', look |
| 11373 | * for the end of the header part in best effort. |
| 11374 | */ |
| 11375 | if (*al != ';') |
| 11376 | goto expect_comma; |
| 11377 | |
| 11378 | /* Assumes that the char is ';', now expect "q=". */ |
| 11379 | al++; |
| 11380 | |
| 11381 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11382 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11383 | al++; |
| 11384 | if (al >= end) |
| 11385 | goto process_value; |
| 11386 | |
| 11387 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11388 | if (*al != 'q') |
| 11389 | goto process_value; |
| 11390 | al++; |
| 11391 | |
| 11392 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11393 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11394 | al++; |
| 11395 | if (al >= end) |
| 11396 | goto process_value; |
| 11397 | |
| 11398 | /* Expect '='. If no '=', continue in best effort */ |
| 11399 | if (*al != '=') |
| 11400 | goto process_value; |
| 11401 | al++; |
| 11402 | |
| 11403 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11404 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11405 | al++; |
| 11406 | if (al >= end) |
| 11407 | goto process_value; |
| 11408 | |
| 11409 | /* Parse the q value. */ |
| 11410 | qvalue = parse_qvalue(al, &al); |
| 11411 | |
| 11412 | process_value: |
| 11413 | |
| 11414 | /* If the new q value is the best q value, then store the associated |
| 11415 | * language in the response. If qvalue is the biggest value (1000), |
| 11416 | * break the process. |
| 11417 | */ |
| 11418 | if (qvalue > best_q) { |
| 11419 | smp->data.str.str = (char *)w; |
| 11420 | smp->data.str.len = str - w; |
| 11421 | if (qvalue >= 1000) |
| 11422 | break; |
| 11423 | best_q = qvalue; |
| 11424 | } |
| 11425 | |
| 11426 | expect_comma: |
| 11427 | |
| 11428 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11429 | while (al < end && *al != ',') |
| 11430 | al++; |
| 11431 | if (al >= end) |
| 11432 | break; |
| 11433 | |
| 11434 | /* Comma is found, jump it and restart the analyzer. */ |
| 11435 | al++; |
| 11436 | } |
| 11437 | |
| 11438 | /* Set default value if required. */ |
| 11439 | if (smp->data.str.len == 0 && args[1].type == ARGT_STR) { |
| 11440 | smp->data.str.str = args[1].data.str.str; |
| 11441 | smp->data.str.len = args[1].data.str.len; |
| 11442 | } |
| 11443 | |
| 11444 | /* Return true only if a matching language was found. */ |
| 11445 | return smp->data.str.len != 0; |
| 11446 | } |
| 11447 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 11448 | /* |
| 11449 | * Return the struct http_req_action_kw associated to a keyword. |
| 11450 | */ |
| 11451 | struct http_req_action_kw *action_http_req_custom(const char *kw) |
| 11452 | { |
| 11453 | if (!LIST_ISEMPTY(&http_req_keywords.list)) { |
| 11454 | struct http_req_action_kw_list *kw_list; |
| 11455 | int i; |
| 11456 | |
| 11457 | list_for_each_entry(kw_list, &http_req_keywords.list, list) { |
| 11458 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11459 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11460 | return &kw_list->kw[i]; |
| 11461 | } |
| 11462 | } |
| 11463 | } |
| 11464 | return NULL; |
| 11465 | } |
| 11466 | |
| 11467 | /* |
| 11468 | * Return the struct http_res_action_kw associated to a keyword. |
| 11469 | */ |
| 11470 | struct http_res_action_kw *action_http_res_custom(const char *kw) |
| 11471 | { |
| 11472 | if (!LIST_ISEMPTY(&http_res_keywords.list)) { |
| 11473 | struct http_res_action_kw_list *kw_list; |
| 11474 | int i; |
| 11475 | |
| 11476 | list_for_each_entry(kw_list, &http_res_keywords.list, list) { |
| 11477 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11478 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11479 | return &kw_list->kw[i]; |
| 11480 | } |
| 11481 | } |
| 11482 | } |
| 11483 | return NULL; |
| 11484 | } |
| 11485 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11486 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11487 | /* All supported ACL keywords must be declared here. */ |
| 11488 | /************************************************************************/ |
| 11489 | |
| 11490 | /* Note: must not be declared <const> as its list will be overwritten. |
| 11491 | * Please take care of keeping this list alphabetically sorted. |
| 11492 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11493 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11494 | { "base", "base", PAT_MATCH_STR }, |
| 11495 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 11496 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 11497 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 11498 | { "base_end", "base", PAT_MATCH_END }, |
| 11499 | { "base_len", "base", PAT_MATCH_LEN }, |
| 11500 | { "base_reg", "base", PAT_MATCH_REG }, |
| 11501 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11502 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11503 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 11504 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 11505 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 11506 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 11507 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 11508 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 11509 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 11510 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11511 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11512 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 11513 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 11514 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 11515 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 11516 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 11517 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 11518 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 11519 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11520 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11521 | /* these two declarations uses strings with list storage (in place |
| 11522 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 11523 | * and delete functions are relative to the list management. The parse |
| 11524 | * and match method are related to the corresponding fetch methods. This |
| 11525 | * is very particular ACL declaration mode. |
| 11526 | */ |
| 11527 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 11528 | { "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] | 11529 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11530 | { "path", "path", PAT_MATCH_STR }, |
| 11531 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 11532 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 11533 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 11534 | { "path_end", "path", PAT_MATCH_END }, |
| 11535 | { "path_len", "path", PAT_MATCH_LEN }, |
| 11536 | { "path_reg", "path", PAT_MATCH_REG }, |
| 11537 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11538 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11539 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 11540 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11541 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11542 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 11543 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 11544 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 11545 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 11546 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 11547 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 11548 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 11549 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11550 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11551 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 11552 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 11553 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 11554 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 11555 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 11556 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 11557 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 11558 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11559 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11560 | { "url", "url", PAT_MATCH_STR }, |
| 11561 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 11562 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 11563 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 11564 | { "url_end", "url", PAT_MATCH_END }, |
| 11565 | { "url_len", "url", PAT_MATCH_LEN }, |
| 11566 | { "url_reg", "url", PAT_MATCH_REG }, |
| 11567 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11568 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11569 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 11570 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 11571 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 11572 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 11573 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 11574 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 11575 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 11576 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11577 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 11578 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11579 | }}; |
| 11580 | |
| 11581 | /************************************************************************/ |
| 11582 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11583 | /************************************************************************/ |
| 11584 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11585 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11586 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11587 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11588 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 11589 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11590 | /* capture are allocated and are permanent in the session */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11591 | { "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] | 11592 | |
| 11593 | /* retrieve these captures from the HTTP logs */ |
| 11594 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11595 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11596 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11597 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11598 | { "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] | 11599 | { "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] | 11600 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11601 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 11602 | * are only here to match the ACL's name, are request-only and are used |
| 11603 | * for ACL compatibility only. |
| 11604 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11605 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11606 | { "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] | 11607 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11608 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11609 | |
| 11610 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 11611 | * only here to match the ACL's name, are request-only and are used for |
| 11612 | * ACL compatibility only. |
| 11613 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11614 | { "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] | 11615 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11616 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11617 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11618 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 11619 | { "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] | 11620 | { "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] | 11621 | { "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] | 11622 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11623 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11624 | |
| 11625 | /* HTTP protocol on the request path */ |
| 11626 | { "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] | 11627 | { "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] | 11628 | |
| 11629 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11630 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11631 | { "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] | 11632 | |
| 11633 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11634 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 11635 | { "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] | 11636 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11637 | /* 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] | 11638 | { "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] | 11639 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11640 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11641 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11642 | { "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] | 11643 | { "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] | 11644 | { "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] | 11645 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11646 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11647 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11648 | |
| 11649 | /* 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] | 11650 | { "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] | 11651 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11652 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11653 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11654 | { "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] | 11655 | { "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] | 11656 | { "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] | 11657 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11658 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 11659 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11660 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11661 | /* 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] | 11662 | { "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] | 11663 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11664 | { "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] | 11665 | { "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] | 11666 | |
| 11667 | /* 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] | 11668 | { "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] | 11669 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11670 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 11671 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11672 | |
| 11673 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11674 | { "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] | 11675 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11676 | { "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] | 11677 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11678 | { "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] | 11679 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11680 | { "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] | 11681 | { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11682 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11683 | }}; |
| 11684 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11685 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11686 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 11687 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11688 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR}, |
| 11689 | { "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] | 11690 | { NULL, NULL, 0, 0, 0 }, |
| 11691 | }}; |
| 11692 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11693 | __attribute__((constructor)) |
| 11694 | static void __http_protocol_init(void) |
| 11695 | { |
| 11696 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11697 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11698 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11699 | } |
| 11700 | |
| 11701 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 11702 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 11703 | * Local variables: |
| 11704 | * c-indent-level: 8 |
| 11705 | * c-basic-offset: 8 |
| 11706 | * End: |
| 11707 | */ |