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 | }, |
| 356 | ['P' - 'A'] = { |
| 357 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 358 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 359 | }, |
| 360 | ['T' - 'A'] = { |
| 361 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 362 | }, |
| 363 | /* rest is empty like this : |
Willy Tarreau | bcd0336 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 364 | * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 365 | */ |
| 366 | }; |
| 367 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 368 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 369 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 370 | [HTTP_METH_GET] = { "GET", 3 }, |
| 371 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 372 | [HTTP_METH_POST] = { "POST", 4 }, |
| 373 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 374 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 375 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 376 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 377 | }; |
| 378 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 379 | /* 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] | 380 | * 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] | 381 | * RFC2616 for those chars. |
| 382 | */ |
| 383 | |
| 384 | const char http_is_spht[256] = { |
| 385 | [' '] = 1, ['\t'] = 1, |
| 386 | }; |
| 387 | |
| 388 | const char http_is_crlf[256] = { |
| 389 | ['\r'] = 1, ['\n'] = 1, |
| 390 | }; |
| 391 | |
| 392 | const char http_is_lws[256] = { |
| 393 | [' '] = 1, ['\t'] = 1, |
| 394 | ['\r'] = 1, ['\n'] = 1, |
| 395 | }; |
| 396 | |
| 397 | const char http_is_sep[256] = { |
| 398 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 399 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 400 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 401 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 402 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 403 | }; |
| 404 | |
| 405 | const char http_is_ctl[256] = { |
| 406 | [0 ... 31] = 1, |
| 407 | [127] = 1, |
| 408 | }; |
| 409 | |
| 410 | /* |
| 411 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 412 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 413 | * them correctly. Instead, define every non-CTL char's status. |
| 414 | */ |
| 415 | const char http_is_token[256] = { |
| 416 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 417 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 418 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 419 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 420 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 421 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 422 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 423 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 424 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 425 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 426 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 427 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 428 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 429 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 430 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 431 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 432 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 433 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 434 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 435 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 436 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 437 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 438 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 439 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 440 | }; |
| 441 | |
| 442 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 443 | /* |
| 444 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 445 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 446 | */ |
| 447 | const char http_is_ver_token[256] = { |
| 448 | ['.'] = 1, ['/'] = 1, |
| 449 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 450 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 451 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 452 | }; |
| 453 | |
| 454 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 455 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 456 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 457 | * 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] | 458 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 459 | * of headers is automatically adjusted. The number of bytes added is returned |
| 460 | * on success, otherwise <0 is returned indicating an error. |
| 461 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 462 | 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] | 463 | { |
| 464 | int bytes, len; |
| 465 | |
| 466 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 467 | 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] | 468 | if (!bytes) |
| 469 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 470 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 471 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 472 | } |
| 473 | |
| 474 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 475 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 476 | * 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] | 477 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 478 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 479 | * of headers is automatically adjusted. The number of bytes added is returned |
| 480 | * on success, otherwise <0 is returned indicating an error. |
| 481 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 482 | int http_header_add_tail2(struct http_msg *msg, |
| 483 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 484 | { |
| 485 | int bytes; |
| 486 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 487 | 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] | 488 | if (!bytes) |
| 489 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 490 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 491 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 492 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 493 | |
| 494 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 495 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 496 | * If so, returns the position of the first non-space character relative to |
| 497 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 498 | * to return a pointer to the place after the first space. Returns 0 if the |
| 499 | * header name does not match. Checks are case-insensitive. |
| 500 | */ |
| 501 | int http_header_match2(const char *hdr, const char *end, |
| 502 | const char *name, int len) |
| 503 | { |
| 504 | const char *val; |
| 505 | |
| 506 | if (hdr + len >= end) |
| 507 | return 0; |
| 508 | if (hdr[len] != ':') |
| 509 | return 0; |
| 510 | if (strncasecmp(hdr, name, len) != 0) |
| 511 | return 0; |
| 512 | val = hdr + len + 1; |
| 513 | while (val < end && HTTP_IS_SPHT(*val)) |
| 514 | val++; |
| 515 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 516 | return len + 2; /* we may replace starting from second space */ |
| 517 | return val - hdr; |
| 518 | } |
| 519 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 520 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 521 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 522 | * structure holds everything necessary to use the header and find next |
| 523 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 524 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 525 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 526 | * http_find_header2() except that it is designed to work with full-line headers |
| 527 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 528 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 529 | * header is rescanned. This allows rescanning after a header deletion. |
| 530 | */ |
| 531 | int http_find_full_header2(const char *name, int len, |
| 532 | char *sol, struct hdr_idx *idx, |
| 533 | struct hdr_ctx *ctx) |
| 534 | { |
| 535 | char *eol, *sov; |
| 536 | int cur_idx, old_idx; |
| 537 | |
| 538 | cur_idx = ctx->idx; |
| 539 | if (cur_idx) { |
| 540 | /* We have previously returned a header, let's search another one */ |
| 541 | sol = ctx->line; |
| 542 | eol = sol + idx->v[cur_idx].len; |
| 543 | goto next_hdr; |
| 544 | } |
| 545 | |
| 546 | /* first request for this header */ |
| 547 | sol += hdr_idx_first_pos(idx); |
| 548 | old_idx = 0; |
| 549 | cur_idx = hdr_idx_first_idx(idx); |
| 550 | while (cur_idx) { |
| 551 | eol = sol + idx->v[cur_idx].len; |
| 552 | |
| 553 | if (len == 0) { |
| 554 | /* No argument was passed, we want any header. |
| 555 | * To achieve this, we simply build a fake request. */ |
| 556 | while (sol + len < eol && sol[len] != ':') |
| 557 | len++; |
| 558 | name = sol; |
| 559 | } |
| 560 | |
| 561 | if ((len < eol - sol) && |
| 562 | (sol[len] == ':') && |
| 563 | (strncasecmp(sol, name, len) == 0)) { |
| 564 | ctx->del = len; |
| 565 | sov = sol + len + 1; |
| 566 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 567 | sov++; |
| 568 | |
| 569 | ctx->line = sol; |
| 570 | ctx->prev = old_idx; |
| 571 | ctx->idx = cur_idx; |
| 572 | ctx->val = sov - sol; |
| 573 | ctx->tws = 0; |
| 574 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 575 | eol--; |
| 576 | ctx->tws++; |
| 577 | } |
| 578 | ctx->vlen = eol - sov; |
| 579 | return 1; |
| 580 | } |
| 581 | next_hdr: |
| 582 | sol = eol + idx->v[cur_idx].cr + 1; |
| 583 | old_idx = cur_idx; |
| 584 | cur_idx = idx->v[cur_idx].next; |
| 585 | } |
| 586 | return 0; |
| 587 | } |
| 588 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 589 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 590 | * par 2.2 for more information. Note that it requires a valid header to return |
| 591 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 592 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 593 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 594 | { |
| 595 | int quoted, qdpair; |
| 596 | |
| 597 | quoted = qdpair = 0; |
| 598 | for (; s < e; s++) { |
| 599 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 600 | else if (quoted) { |
| 601 | if (*s == '\\') qdpair = 1; |
| 602 | else if (*s == '"') quoted = 0; |
| 603 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 604 | else if (*s == '"') quoted = 1; |
| 605 | else if (*s == ',') return s; |
| 606 | } |
| 607 | return s; |
| 608 | } |
| 609 | |
| 610 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 611 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 612 | * structure holds everything necessary to use the header and find next |
| 613 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 614 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 615 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 616 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 617 | * is NULL when searching for a new values of a header, the current header is |
| 618 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 619 | */ |
| 620 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 621 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 622 | struct hdr_ctx *ctx) |
| 623 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 624 | char *eol, *sov; |
| 625 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 626 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 627 | cur_idx = ctx->idx; |
| 628 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 629 | /* We have previously returned a value, let's search |
| 630 | * another one on the same line. |
| 631 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 632 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 633 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 634 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 635 | eol = sol + idx->v[cur_idx].len; |
| 636 | |
| 637 | if (sov >= eol) |
| 638 | /* no more values in this header */ |
| 639 | goto next_hdr; |
| 640 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 641 | /* values remaining for this header, skip the comma but save it |
| 642 | * for later use (eg: for header deletion). |
| 643 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 644 | sov++; |
| 645 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 646 | sov++; |
| 647 | |
| 648 | goto return_hdr; |
| 649 | } |
| 650 | |
| 651 | /* first request for this header */ |
| 652 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 653 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 654 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 655 | while (cur_idx) { |
| 656 | eol = sol + idx->v[cur_idx].len; |
| 657 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 658 | if (len == 0) { |
| 659 | /* No argument was passed, we want any header. |
| 660 | * To achieve this, we simply build a fake request. */ |
| 661 | while (sol + len < eol && sol[len] != ':') |
| 662 | len++; |
| 663 | name = sol; |
| 664 | } |
| 665 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 666 | if ((len < eol - sol) && |
| 667 | (sol[len] == ':') && |
| 668 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 669 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 670 | sov = sol + len + 1; |
| 671 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 672 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 673 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 674 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 675 | ctx->prev = old_idx; |
| 676 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 677 | ctx->idx = cur_idx; |
| 678 | ctx->val = sov - sol; |
| 679 | |
| 680 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 681 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 682 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 683 | eol--; |
| 684 | ctx->tws++; |
| 685 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 686 | ctx->vlen = eol - sov; |
| 687 | return 1; |
| 688 | } |
| 689 | next_hdr: |
| 690 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 691 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 692 | cur_idx = idx->v[cur_idx].next; |
| 693 | } |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 698 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 699 | struct hdr_ctx *ctx) |
| 700 | { |
| 701 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 702 | } |
| 703 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 704 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 705 | * the http_find_header functions. The value is removed, as well as surrounding |
| 706 | * 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] | 707 | * 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] | 708 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 709 | * no more header, so any processing may stop. The ctx is always left in a form |
| 710 | * that can be handled by http_find_header2() to find next occurrence. |
| 711 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 712 | 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] | 713 | { |
| 714 | int cur_idx = ctx->idx; |
| 715 | char *sol = ctx->line; |
| 716 | struct hdr_idx_elem *hdr; |
| 717 | int delta, skip_comma; |
| 718 | |
| 719 | if (!cur_idx) |
| 720 | return 0; |
| 721 | |
| 722 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 723 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 724 | /* 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] | 725 | 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] | 726 | http_msg_move_end(msg, delta); |
| 727 | idx->used--; |
| 728 | hdr->len = 0; /* unused entry */ |
| 729 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 730 | if (idx->tail == ctx->idx) |
| 731 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 732 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 14acc65 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 733 | 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] | 734 | 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] | 735 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 736 | return ctx->idx; |
| 737 | } |
| 738 | |
| 739 | /* 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] | 740 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 741 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 742 | */ |
| 743 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 744 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 745 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 746 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 747 | NULL, 0); |
| 748 | hdr->len += delta; |
| 749 | http_msg_move_end(msg, delta); |
| 750 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 751 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 752 | return ctx->idx; |
| 753 | } |
| 754 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 755 | /* This function handles a server error at the stream interface level. The |
| 756 | * stream interface is assumed to be already in a closed state. An optional |
| 757 | * message is copied into the input buffer, and an HTTP status code stored. |
| 758 | * 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] | 759 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 760 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 761 | static void http_server_error(struct session *s, struct stream_interface *si, |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 762 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 763 | { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 764 | channel_auto_read(si->ob); |
| 765 | channel_abort(si->ob); |
| 766 | channel_auto_close(si->ob); |
| 767 | channel_erase(si->ob); |
| 768 | channel_auto_close(si->ib); |
| 769 | channel_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 770 | if (status > 0 && msg) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 771 | s->txn.status = status; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 772 | bo_inject(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 773 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 774 | if (!(s->flags & SN_ERR_MASK)) |
| 775 | s->flags |= err; |
| 776 | if (!(s->flags & SN_FINST_MASK)) |
| 777 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 778 | } |
| 779 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 780 | /* This function returns the appropriate error location for the given session |
| 781 | * and message. |
| 782 | */ |
| 783 | |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 784 | struct chunk *http_error_message(struct session *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 785 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 786 | if (s->be->errmsg[msgnum].str) |
| 787 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 788 | else if (s->fe->errmsg[msgnum].str) |
| 789 | return &s->fe->errmsg[msgnum]; |
| 790 | else |
| 791 | return &http_err_chunks[msgnum]; |
| 792 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 793 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 794 | /* |
Willy Tarreau | bcd0336 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 795 | * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown |
| 796 | * ones. |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 797 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 798 | 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] | 799 | { |
| 800 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 801 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 802 | |
| 803 | m = ((unsigned)*str - 'A'); |
| 804 | |
| 805 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 806 | for (h = http_methods[m]; h->len > 0; h++) { |
| 807 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 808 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 809 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 810 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 811 | }; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 812 | } |
Willy Tarreau | bcd0336 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 813 | return HTTP_METH_OTHER; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 814 | } |
| 815 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 816 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 817 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 818 | * It is returned otherwise. |
| 819 | */ |
| 820 | static char * |
| 821 | http_get_path(struct http_txn *txn) |
| 822 | { |
| 823 | char *ptr, *end; |
| 824 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 825 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 826 | end = ptr + txn->req.sl.rq.u_l; |
| 827 | |
| 828 | if (ptr >= end) |
| 829 | return NULL; |
| 830 | |
| 831 | /* RFC2616, par. 5.1.2 : |
| 832 | * Request-URI = "*" | absuri | abspath | authority |
| 833 | */ |
| 834 | |
| 835 | if (*ptr == '*') |
| 836 | return NULL; |
| 837 | |
| 838 | if (isalpha((unsigned char)*ptr)) { |
| 839 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 840 | ptr++; |
| 841 | while (ptr < end && |
| 842 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 843 | ptr++; |
| 844 | /* skip '://' */ |
| 845 | if (ptr == end || *ptr++ != ':') |
| 846 | return NULL; |
| 847 | if (ptr == end || *ptr++ != '/') |
| 848 | return NULL; |
| 849 | if (ptr == end || *ptr++ != '/') |
| 850 | return NULL; |
| 851 | } |
| 852 | /* skip [user[:passwd]@]host[:[port]] */ |
| 853 | |
| 854 | while (ptr < end && *ptr != '/') |
| 855 | ptr++; |
| 856 | |
| 857 | if (ptr == end) |
| 858 | return NULL; |
| 859 | |
| 860 | /* OK, we got the '/' ! */ |
| 861 | return ptr; |
| 862 | } |
| 863 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 864 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 865 | * If not found, return NULL. It is returned otherwise. |
| 866 | */ |
| 867 | static char * |
| 868 | http_get_path_from_string(char *str) |
| 869 | { |
| 870 | char *ptr = str; |
| 871 | |
| 872 | /* RFC2616, par. 5.1.2 : |
| 873 | * Request-URI = "*" | absuri | abspath | authority |
| 874 | */ |
| 875 | |
| 876 | if (*ptr == '*') |
| 877 | return NULL; |
| 878 | |
| 879 | if (isalpha((unsigned char)*ptr)) { |
| 880 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 881 | ptr++; |
| 882 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 883 | ptr++; |
| 884 | /* skip '://' */ |
| 885 | if (*ptr == '\0' || *ptr++ != ':') |
| 886 | return NULL; |
| 887 | if (*ptr == '\0' || *ptr++ != '/') |
| 888 | return NULL; |
| 889 | if (*ptr == '\0' || *ptr++ != '/') |
| 890 | return NULL; |
| 891 | } |
| 892 | /* skip [user[:passwd]@]host[:[port]] */ |
| 893 | |
| 894 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 895 | ptr++; |
| 896 | |
| 897 | if (*ptr == '\0' || *ptr == ' ') |
| 898 | return NULL; |
| 899 | |
| 900 | /* OK, we got the '/' ! */ |
| 901 | return ptr; |
| 902 | } |
| 903 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 904 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 905 | * in redirect mode. This may only be called just after the stream interface |
| 906 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 907 | * follow normal proxy processing. NOTE: this function is designed to support |
| 908 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 909 | */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 910 | void http_perform_server_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 911 | { |
| 912 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 913 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 914 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 915 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 916 | |
| 917 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 918 | trash.len = strlen(HTTP_302); |
| 919 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 920 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 921 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 922 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 923 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 924 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 925 | return; |
| 926 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 927 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 928 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 929 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 930 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 931 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 932 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 933 | /* 3: add the request URI. Since it was already forwarded, we need |
| 934 | * to temporarily rewind the buffer. |
| 935 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 936 | txn = &s->txn; |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 937 | b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 938 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 939 | path = http_get_path(txn); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 940 | 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] | 941 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 942 | b_adv(s->req->buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 943 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 944 | if (!path) |
| 945 | return; |
| 946 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 947 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 948 | return; |
| 949 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 950 | memcpy(trash.str + trash.len, path, len); |
| 951 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 952 | |
| 953 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 954 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 955 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 956 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 957 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 958 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 959 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 960 | |
| 961 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 962 | si_shutr(si); |
| 963 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 964 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 965 | si->state = SI_ST_CLO; |
| 966 | |
| 967 | /* send the message */ |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 968 | 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] | 969 | |
| 970 | /* 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] | 971 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 972 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 973 | } |
| 974 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 975 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 976 | * that the server side is closed. Note that err_type is actually a |
| 977 | * bitmask, where almost only aborts may be cumulated with other |
| 978 | * values. We consider that aborted operations are more important |
| 979 | * than timeouts or errors due to the fact that nobody else in the |
| 980 | * logs might explain incomplete retries. All others should avoid |
| 981 | * being cumulated. It should normally not be possible to have multiple |
| 982 | * 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] | 983 | * Note that connection errors appearing on the second request of a keep-alive |
| 984 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 985 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 986 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 987 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 988 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 989 | |
| 990 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 991 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 992 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 993 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 994 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 995 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 996 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 997 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 998 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 999 | 503, 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_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1001 | http_server_error(s, si, SN_ERR_SRVCL, 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_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1004 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1005 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 1006 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1007 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1008 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
Willy Tarreau | 3634624 | 2014-02-24 18:26:30 +0100 | [diff] [blame] | 1009 | 503, (s->flags & SN_SRV_REUSED) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1010 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1011 | else if (err_type & SI_ET_CONN_RES) |
| 1012 | http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1013 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 1014 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1015 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1016 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1017 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1018 | } |
| 1019 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1020 | extern const char sess_term_cond[8]; |
| 1021 | extern const char sess_fin_state[8]; |
| 1022 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1023 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1024 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1025 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1026 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1027 | /* |
| 1028 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1029 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1030 | */ |
| 1031 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1032 | char **cap, struct cap_hdr *cap_hdr) |
| 1033 | { |
| 1034 | char *eol, *sol, *col, *sov; |
| 1035 | int cur_idx; |
| 1036 | struct cap_hdr *h; |
| 1037 | int len; |
| 1038 | |
| 1039 | sol = som + hdr_idx_first_pos(idx); |
| 1040 | cur_idx = hdr_idx_first_idx(idx); |
| 1041 | |
| 1042 | while (cur_idx) { |
| 1043 | eol = sol + idx->v[cur_idx].len; |
| 1044 | |
| 1045 | col = sol; |
| 1046 | while (col < eol && *col != ':') |
| 1047 | col++; |
| 1048 | |
| 1049 | sov = col + 1; |
| 1050 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1051 | sov++; |
| 1052 | |
| 1053 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1054 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1055 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1056 | if (cap[h->index] == NULL) |
| 1057 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1058 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1059 | |
| 1060 | if (cap[h->index] == NULL) { |
| 1061 | Alert("HTTP capture : out of memory.\n"); |
| 1062 | continue; |
| 1063 | } |
| 1064 | |
| 1065 | len = eol - sov; |
| 1066 | if (len > h->len) |
| 1067 | len = h->len; |
| 1068 | |
| 1069 | memcpy(cap[h->index], sov, len); |
| 1070 | cap[h->index][len]=0; |
| 1071 | } |
| 1072 | } |
| 1073 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1074 | cur_idx = idx->v[cur_idx].next; |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1079 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1080 | */ |
| 1081 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1082 | |
| 1083 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1084 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1085 | */ |
| 1086 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1087 | ptr++; \ |
| 1088 | if (likely(ptr < end)) \ |
| 1089 | goto good; \ |
| 1090 | else { \ |
| 1091 | state = (st); \ |
| 1092 | goto http_msg_ood; \ |
| 1093 | } \ |
| 1094 | } while (0) |
| 1095 | |
| 1096 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1097 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1098 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1099 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1100 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1101 | * will give undefined results. |
| 1102 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1103 | * and that msg->sol points to the beginning of the response. |
| 1104 | * If a complete line is found (which implies that at least one CR or LF is |
| 1105 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1106 | * returned indicating an incomplete line (which does not mean that parts have |
| 1107 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1108 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1109 | * upon next call. |
| 1110 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1111 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1112 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1113 | * 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] | 1114 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1115 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1116 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1117 | enum ht_state state, const char *ptr, const char *end, |
| 1118 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1119 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1120 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1121 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1122 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1123 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1124 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1125 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1126 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1127 | |
| 1128 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1129 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1130 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1131 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1132 | state = HTTP_MSG_ERROR; |
| 1133 | break; |
| 1134 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1135 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1136 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1137 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1138 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1139 | goto http_msg_rpcode; |
| 1140 | } |
| 1141 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1142 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1143 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1144 | state = HTTP_MSG_ERROR; |
| 1145 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1146 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1147 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1148 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1149 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1150 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1151 | |
| 1152 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1153 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1154 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1155 | } |
| 1156 | |
| 1157 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1158 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1159 | http_msg_rsp_reason: |
| 1160 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1161 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1162 | msg->sl.st.r_l = 0; |
| 1163 | goto http_msg_rpline_eol; |
| 1164 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1165 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1166 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1167 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1168 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1169 | goto http_msg_rpreason; |
| 1170 | } |
| 1171 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1172 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1173 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1174 | goto http_msg_rsp_reason; |
| 1175 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1176 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1177 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1178 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1179 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1180 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1181 | http_msg_rpline_eol: |
| 1182 | /* We have seen the end of line. Note that we do not |
| 1183 | * necessarily have the \n yet, but at least we know that we |
| 1184 | * have EITHER \r OR \n, otherwise the response would not be |
| 1185 | * complete. We can then record the response length and return |
| 1186 | * to the caller which will be able to register it. |
| 1187 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1188 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1189 | return ptr; |
| 1190 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1191 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1192 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1193 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1194 | exit(1); |
| 1195 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1196 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1200 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1201 | if (ret_state) |
| 1202 | *ret_state = state; |
| 1203 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1204 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1205 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1206 | } |
| 1207 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1208 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1209 | * This function parses a request line between <ptr> and <end>, starting with |
| 1210 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1211 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1212 | * will give undefined results. |
| 1213 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1214 | * and that msg->sol points to the beginning of the request. |
| 1215 | * If a complete line is found (which implies that at least one CR or LF is |
| 1216 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1217 | * returned indicating an incomplete line (which does not mean that parts have |
| 1218 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1219 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1220 | * upon next call. |
| 1221 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1222 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1223 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1224 | * 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] | 1225 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1226 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1227 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1228 | enum ht_state state, const char *ptr, const char *end, |
| 1229 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1230 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1231 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1232 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1233 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1234 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1235 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1236 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1237 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1238 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1239 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1240 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1241 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1242 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1243 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1244 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1245 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1246 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1247 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1248 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1249 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1250 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1251 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1252 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1253 | msg->sl.rq.v_l = 0; |
| 1254 | goto http_msg_rqline_eol; |
| 1255 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1256 | state = HTTP_MSG_ERROR; |
| 1257 | break; |
| 1258 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1259 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1260 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1261 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1262 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1263 | goto http_msg_rquri; |
| 1264 | } |
| 1265 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1266 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1267 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1268 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1269 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1270 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1271 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1272 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1273 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1274 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1275 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1276 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1277 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1278 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1279 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1280 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1281 | /* non-ASCII chars are forbidden unless option |
| 1282 | * accept-invalid-http-request is enabled in the frontend. |
| 1283 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1284 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1285 | if (msg->err_pos < -1) |
| 1286 | goto invalid_char; |
| 1287 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1288 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1289 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1290 | } |
| 1291 | |
| 1292 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1293 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1294 | goto http_msg_req09_uri_e; |
| 1295 | } |
| 1296 | |
| 1297 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1298 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1299 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1300 | state = HTTP_MSG_ERROR; |
| 1301 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1302 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1303 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1304 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1305 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1306 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1307 | goto http_msg_rqver; |
| 1308 | } |
| 1309 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1310 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1311 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1312 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1313 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1314 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1315 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1316 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1317 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1318 | |
| 1319 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1320 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1321 | http_msg_rqline_eol: |
| 1322 | /* We have seen the end of line. Note that we do not |
| 1323 | * necessarily have the \n yet, but at least we know that we |
| 1324 | * have EITHER \r OR \n, otherwise the request would not be |
| 1325 | * complete. We can then record the request length and return |
| 1326 | * to the caller which will be able to register it. |
| 1327 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1328 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1329 | return ptr; |
| 1330 | } |
| 1331 | |
| 1332 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1333 | state = HTTP_MSG_ERROR; |
| 1334 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1335 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1336 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1337 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1338 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1339 | exit(1); |
| 1340 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1341 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1342 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1343 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1344 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1345 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1346 | if (ret_state) |
| 1347 | *ret_state = state; |
| 1348 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1349 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1350 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1351 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1352 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1353 | /* |
| 1354 | * Returns the data from Authorization header. Function may be called more |
| 1355 | * than once so data is stored in txn->auth_data. When no header is found |
| 1356 | * 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] | 1357 | * searching again for something we are unable to find anyway. However, if |
| 1358 | * the result if valid, the cache is not reused because we would risk to |
| 1359 | * have the credentials overwritten by another session in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1360 | */ |
| 1361 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 1362 | /* This bufffer is initialized in the file 'src/haproxy.c'. This length is |
| 1363 | * set according to global.tune.bufsize. |
| 1364 | */ |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1365 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1366 | |
| 1367 | int |
| 1368 | get_http_auth(struct session *s) |
| 1369 | { |
| 1370 | |
| 1371 | struct http_txn *txn = &s->txn; |
| 1372 | struct chunk auth_method; |
| 1373 | struct hdr_ctx ctx; |
| 1374 | char *h, *p; |
| 1375 | int len; |
| 1376 | |
| 1377 | #ifdef DEBUG_AUTH |
| 1378 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1379 | #endif |
| 1380 | |
| 1381 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1382 | return 0; |
| 1383 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1384 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1385 | |
| 1386 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1387 | |
| 1388 | if (txn->flags & TX_USE_PX_CONN) { |
| 1389 | h = "Proxy-Authorization"; |
| 1390 | len = strlen(h); |
| 1391 | } else { |
| 1392 | h = "Authorization"; |
| 1393 | len = strlen(h); |
| 1394 | } |
| 1395 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1396 | 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] | 1397 | return 0; |
| 1398 | |
| 1399 | h = ctx.line + ctx.val; |
| 1400 | |
| 1401 | p = memchr(h, ' ', ctx.vlen); |
| 1402 | if (!p || p == h) |
| 1403 | return 0; |
| 1404 | |
| 1405 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1406 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1407 | |
| 1408 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1409 | |
| 1410 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1411 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1412 | |
| 1413 | if (len < 0) |
| 1414 | return 0; |
| 1415 | |
| 1416 | |
| 1417 | get_http_auth_buff[len] = '\0'; |
| 1418 | |
| 1419 | p = strchr(get_http_auth_buff, ':'); |
| 1420 | |
| 1421 | if (!p) |
| 1422 | return 0; |
| 1423 | |
| 1424 | txn->auth.user = get_http_auth_buff; |
| 1425 | *p = '\0'; |
| 1426 | txn->auth.pass = p+1; |
| 1427 | |
| 1428 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1429 | return 1; |
| 1430 | } |
| 1431 | |
| 1432 | return 0; |
| 1433 | } |
| 1434 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1435 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1436 | /* |
| 1437 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1438 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1439 | * ensuring that the message does not wrap. The function can be preempted |
| 1440 | * everywhere when data are missing and recalled at the exact same location |
| 1441 | * with no information loss. The message may even be realigned between two |
| 1442 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1443 | * 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] | 1444 | * fields. Note that msg->sol will be initialized after completing the first |
| 1445 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1446 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1447 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1448 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1449 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1450 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1451 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1452 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1453 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1454 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1455 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1456 | ptr = buf->p + msg->next; |
| 1457 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1458 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1459 | if (unlikely(ptr >= end)) |
| 1460 | goto http_msg_ood; |
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 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1463 | /* |
| 1464 | * First, states that are specific to the response only. |
| 1465 | * We check them first so that request and headers are |
| 1466 | * closer to each other (accessed more often). |
| 1467 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1468 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1469 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1470 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1471 | /* we have a start of message, but we have to check |
| 1472 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1473 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1474 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1475 | if (unlikely(ptr != buf->p)) { |
| 1476 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1477 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1478 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1479 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1480 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1481 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1482 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1483 | hdr_idx_init(idx); |
| 1484 | state = HTTP_MSG_RPVER; |
| 1485 | goto http_msg_rpver; |
| 1486 | } |
| 1487 | |
| 1488 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1489 | goto http_msg_invalid; |
| 1490 | |
| 1491 | if (unlikely(*ptr == '\n')) |
| 1492 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1493 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1494 | /* stop here */ |
| 1495 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1496 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1497 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1498 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1499 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1500 | /* stop here */ |
| 1501 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1502 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1503 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1504 | case HTTP_MSG_RPVER_SP: |
| 1505 | case HTTP_MSG_RPCODE: |
| 1506 | case HTTP_MSG_RPCODE_SP: |
| 1507 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1508 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1509 | state, ptr, end, |
| 1510 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1511 | if (unlikely(!ptr)) |
| 1512 | return; |
| 1513 | |
| 1514 | /* we have a full response and we know that we have either a CR |
| 1515 | * or an LF at <ptr>. |
| 1516 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1517 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1518 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1519 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1520 | if (likely(*ptr == '\r')) |
| 1521 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1522 | goto http_msg_rpline_end; |
| 1523 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1524 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1525 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1526 | /* msg->sol must point to the first of CR or LF. */ |
| 1527 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1528 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1529 | /* stop here */ |
| 1530 | |
| 1531 | /* |
| 1532 | * Second, states that are specific to the request only |
| 1533 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1534 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1535 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1536 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1537 | /* we have a start of message, but we have to check |
| 1538 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1539 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1540 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1541 | if (likely(ptr != buf->p)) { |
| 1542 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1543 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1544 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1545 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1546 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1547 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1548 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1549 | state = HTTP_MSG_RQMETH; |
| 1550 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1551 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1552 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1553 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1554 | goto http_msg_invalid; |
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(*ptr == '\n')) |
| 1557 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1558 | 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] | 1559 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1560 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1561 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1562 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1563 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1564 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1565 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1566 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1567 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1568 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1569 | case HTTP_MSG_RQMETH_SP: |
| 1570 | case HTTP_MSG_RQURI: |
| 1571 | case HTTP_MSG_RQURI_SP: |
| 1572 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1573 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1574 | state, ptr, end, |
| 1575 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1576 | if (unlikely(!ptr)) |
| 1577 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1578 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1579 | /* we have a full request and we know that we have either a CR |
| 1580 | * or an LF at <ptr>. |
| 1581 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1582 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1583 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1584 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1585 | if (likely(*ptr == '\r')) |
| 1586 | 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] | 1587 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1588 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1589 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1590 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1591 | /* check for HTTP/0.9 request : no version information available. |
| 1592 | * msg->sol must point to the first of CR or LF. |
| 1593 | */ |
| 1594 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1595 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1596 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1597 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1598 | 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] | 1599 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1600 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1601 | /* |
| 1602 | * Common states below |
| 1603 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1604 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1605 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1606 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1607 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1608 | goto http_msg_hdr_name; |
| 1609 | } |
| 1610 | |
| 1611 | if (likely(*ptr == '\r')) |
| 1612 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1613 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1614 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1615 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1616 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1617 | /* assumes msg->sol points to the first char */ |
| 1618 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1619 | 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] | 1620 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1621 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1622 | 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] | 1623 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1624 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1625 | goto http_msg_invalid; |
| 1626 | |
| 1627 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1628 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1629 | |
| 1630 | /* and we still accept this non-token character */ |
| 1631 | 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] | 1632 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1633 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1634 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1635 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1636 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1637 | 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] | 1638 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1639 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1640 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1641 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1642 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1643 | goto http_msg_hdr_val; |
| 1644 | } |
| 1645 | |
| 1646 | if (likely(*ptr == '\r')) |
| 1647 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1648 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1649 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1650 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1651 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1652 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1653 | 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] | 1654 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1655 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1656 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1657 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1658 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1659 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1660 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1661 | goto http_msg_hdr_l1_sp; |
| 1662 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1663 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1664 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1665 | goto http_msg_complete_header; |
| 1666 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1667 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1668 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1669 | /* assumes msg->sol points to the first char, and msg->sov |
| 1670 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1671 | */ |
| 1672 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1673 | 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] | 1674 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1675 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1676 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1677 | * real header end in case it ends with lots of LWS, but is this |
| 1678 | * really needed ? |
| 1679 | */ |
| 1680 | if (likely(*ptr == '\r')) |
| 1681 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1682 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1683 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1684 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1685 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1686 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1687 | 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] | 1688 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1689 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1690 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1691 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1692 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1693 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1694 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1695 | goto http_msg_hdr_val; |
| 1696 | } |
| 1697 | http_msg_complete_header: |
| 1698 | /* |
| 1699 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1700 | * Assumes msg->sol points to the first char, msg->sov points |
| 1701 | * to the first character of the value and msg->eol to the |
| 1702 | * first CR or LF so we know how the line ends. We insert last |
| 1703 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1704 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1705 | 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] | 1706 | idx, idx->tail) < 0)) |
| 1707 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1708 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1709 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1710 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1711 | goto http_msg_hdr_name; |
| 1712 | } |
| 1713 | |
| 1714 | if (likely(*ptr == '\r')) |
| 1715 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1716 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1717 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1718 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1719 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1720 | /* Assumes msg->sol points to the first of either CR or LF. |
| 1721 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 1722 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 1723 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1724 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1725 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1726 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1727 | msg->eoh = msg->sol; |
| 1728 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1729 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1730 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1731 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1732 | |
| 1733 | case HTTP_MSG_ERROR: |
| 1734 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1735 | break; |
| 1736 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1737 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1738 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1739 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1740 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1741 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1742 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1743 | } |
| 1744 | http_msg_ood: |
| 1745 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1746 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1747 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1748 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1749 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1750 | http_msg_invalid: |
| 1751 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1752 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1753 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1754 | return; |
| 1755 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1756 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1757 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1758 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1759 | * nothing is done and 1 is returned. |
| 1760 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1761 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1762 | { |
| 1763 | int delta; |
| 1764 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1765 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1766 | |
| 1767 | if (msg->sl.rq.v_l != 0) |
| 1768 | return 1; |
| 1769 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1770 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1771 | if (txn->meth != HTTP_METH_GET) |
| 1772 | return 0; |
| 1773 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1774 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1775 | delta = 0; |
| 1776 | |
| 1777 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1778 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1779 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1780 | } |
| 1781 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1782 | 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] | 1783 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1784 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1785 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1786 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1787 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1788 | NULL, NULL); |
| 1789 | if (unlikely(!cur_end)) |
| 1790 | return 0; |
| 1791 | |
| 1792 | /* we have a full HTTP/1.0 request now and we know that |
| 1793 | * we have either a CR or an LF at <ptr>. |
| 1794 | */ |
| 1795 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1796 | return 1; |
| 1797 | } |
| 1798 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1799 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1800 | * and "keep-alive" values. If we already know that some headers may safely |
| 1801 | * 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] | 1802 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1803 | * - 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] | 1804 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1805 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1806 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1807 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1808 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1809 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1810 | 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] | 1811 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1812 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1813 | const char *hdr_val = "Connection"; |
| 1814 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1815 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1816 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1817 | return; |
| 1818 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1819 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1820 | hdr_val = "Proxy-Connection"; |
| 1821 | hdr_len = 16; |
| 1822 | } |
| 1823 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1824 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1825 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1826 | 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] | 1827 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1828 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1829 | if (to_del & 2) |
| 1830 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1831 | else |
| 1832 | txn->flags |= TX_CON_KAL_SET; |
| 1833 | } |
| 1834 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1835 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1836 | if (to_del & 1) |
| 1837 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1838 | else |
| 1839 | txn->flags |= TX_CON_CLO_SET; |
| 1840 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1841 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1842 | txn->flags |= TX_HDR_CONN_UPG; |
| 1843 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1844 | } |
| 1845 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1846 | txn->flags |= TX_HDR_CONN_PRS; |
| 1847 | return; |
| 1848 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1849 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1850 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1851 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1852 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1853 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1854 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1855 | 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] | 1856 | { |
| 1857 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1858 | const char *hdr_val = "Connection"; |
| 1859 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1860 | |
| 1861 | ctx.idx = 0; |
| 1862 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1863 | |
| 1864 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1865 | hdr_val = "Proxy-Connection"; |
| 1866 | hdr_len = 16; |
| 1867 | } |
| 1868 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1869 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1870 | 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] | 1871 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1872 | if (wanted & TX_CON_KAL_SET) |
| 1873 | txn->flags |= TX_CON_KAL_SET; |
| 1874 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1875 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1876 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1877 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1878 | if (wanted & TX_CON_CLO_SET) |
| 1879 | txn->flags |= TX_CON_CLO_SET; |
| 1880 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1881 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1882 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1883 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1884 | |
| 1885 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1886 | return; |
| 1887 | |
| 1888 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1889 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1890 | hdr_val = "Connection: close"; |
| 1891 | hdr_len = 17; |
| 1892 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1893 | hdr_val = "Proxy-Connection: close"; |
| 1894 | hdr_len = 23; |
| 1895 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1896 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1900 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1901 | hdr_val = "Connection: keep-alive"; |
| 1902 | hdr_len = 22; |
| 1903 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1904 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1905 | hdr_len = 28; |
| 1906 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1907 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1908 | } |
| 1909 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1910 | } |
| 1911 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 1912 | /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to |
| 1913 | * the first byte of data after the chunk size, so that we know we can forward |
| 1914 | * exactly msg->next bytes. msg->sol contains the exact number of bytes forming |
| 1915 | * the chunk size. That way it is always possible to differentiate between the |
| 1916 | * start of the body and the start of the data. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1917 | * 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] | 1918 | * 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] | 1919 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1920 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1921 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1922 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1923 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1924 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1925 | const char *end = buf->data + buf->size; |
| 1926 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1927 | unsigned int chunk = 0; |
| 1928 | |
| 1929 | /* The chunk size is in the following form, though we are only |
| 1930 | * interested in the size and CRLF : |
| 1931 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1932 | */ |
| 1933 | while (1) { |
| 1934 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1935 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1936 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1937 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1938 | if (c < 0) /* not a hex digit anymore */ |
| 1939 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1940 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1941 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1942 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1943 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1944 | chunk = (chunk << 4) + c; |
| 1945 | } |
| 1946 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1947 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1948 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1949 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1950 | |
| 1951 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1952 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1953 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1954 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1955 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1956 | } |
| 1957 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1958 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1959 | * for the end of chunk size. |
| 1960 | */ |
| 1961 | while (1) { |
| 1962 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1963 | /* we now have a CR or an LF at ptr */ |
| 1964 | if (likely(*ptr == '\r')) { |
| 1965 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1966 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1967 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1968 | return 0; |
| 1969 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1970 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1971 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1972 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1973 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1974 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1975 | /* done */ |
| 1976 | break; |
| 1977 | } |
| 1978 | else if (*ptr == ';') { |
| 1979 | /* chunk extension, ends at next CRLF */ |
| 1980 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1981 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1982 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1983 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1984 | |
| 1985 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1986 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1987 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1988 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1989 | return 0; |
| 1990 | } |
| 1991 | /* we have a CRLF now, loop above */ |
| 1992 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1993 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1994 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1995 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1996 | } |
| 1997 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1998 | /* 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] | 1999 | * which may or may not be present. We save that into ->next, |
| 2000 | * and the number of bytes parsed into msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2001 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2002 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2003 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2004 | msg->sol += buf->size; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2005 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2006 | msg->chunk_len = chunk; |
| 2007 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2008 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2009 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2010 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2011 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2012 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2013 | } |
| 2014 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2015 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2016 | * 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] | 2017 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2018 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2019 | * If not enough data are available, the function does not change anything |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2020 | * except maybe msg->next if it could parse some lines, and returns zero. |
| 2021 | * If a parse error is encountered, the function returns < 0 and does not |
| 2022 | * change anything except maybe msg->next. Note that the message must |
| 2023 | * already be in HTTP_MSG_TRAILERS state before calling this function, |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2024 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2025 | * forwarding, and that msg->next exactly matches the length of trailers |
| 2026 | * already parsed and not forwarded. It is also important to note that this |
| 2027 | * 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] | 2028 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2029 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2030 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2031 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2032 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2033 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2034 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2035 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2036 | const char *ptr = b_ptr(buf, msg->next); |
| 2037 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2038 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2039 | |
| 2040 | /* scan current line and stop at LF or CRLF */ |
| 2041 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2042 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2043 | return 0; |
| 2044 | |
| 2045 | if (*ptr == '\n') { |
| 2046 | if (!p1) |
| 2047 | p1 = ptr; |
| 2048 | p2 = ptr; |
| 2049 | break; |
| 2050 | } |
| 2051 | |
| 2052 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2053 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2054 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2055 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2056 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2057 | p1 = ptr; |
| 2058 | } |
| 2059 | |
| 2060 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2061 | if (ptr >= buf->data + buf->size) |
| 2062 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | /* after LF; point to beginning of next line */ |
| 2066 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2067 | if (p2 >= buf->data + buf->size) |
| 2068 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2069 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2070 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2071 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2072 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2073 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2074 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2075 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2076 | * Everything was scheduled for forwarding, there's nothing |
| 2077 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2078 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2079 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2080 | msg->msg_state = HTTP_MSG_DONE; |
| 2081 | return 1; |
| 2082 | } |
| 2083 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2084 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2085 | } |
| 2086 | } |
| 2087 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2088 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF |
| 2089 | * or a possible LF alone at the end of a chunk. It automatically adjusts |
| 2090 | * 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] | 2091 | * 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] | 2092 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2093 | * not enough data are available, the function does not change anything and |
| 2094 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2095 | * does not change anything. Note: this function is designed to parse wrapped |
| 2096 | * CRLF at the end of the buffer. |
| 2097 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2098 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2099 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2100 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2101 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2102 | int bytes; |
| 2103 | |
| 2104 | /* NB: we'll check data availabilty at the end. It's not a |
| 2105 | * problem because whatever we match first will be checked |
| 2106 | * against the correct length. |
| 2107 | */ |
| 2108 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2109 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2110 | if (*ptr == '\r') { |
| 2111 | bytes++; |
| 2112 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2113 | if (ptr >= buf->data + buf->size) |
| 2114 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2115 | } |
| 2116 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2117 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2118 | return 0; |
| 2119 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2120 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2121 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2122 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2123 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2124 | |
| 2125 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2126 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2127 | ptr = buf->data; |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2128 | /* Advance ->next to allow the CRLF to be forwarded */ |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2129 | msg->next += bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2130 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2131 | return 1; |
| 2132 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2133 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2134 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2135 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2136 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2137 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2138 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2139 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2140 | { |
| 2141 | int q = 1000; |
| 2142 | |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2143 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2144 | goto out; |
| 2145 | q = (*qvalue++ - '0') * 1000; |
| 2146 | |
| 2147 | if (*qvalue++ != '.') |
| 2148 | goto out; |
| 2149 | |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2150 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2151 | goto out; |
| 2152 | q += (*qvalue++ - '0') * 100; |
| 2153 | |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2154 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2155 | goto out; |
| 2156 | q += (*qvalue++ - '0') * 10; |
| 2157 | |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2158 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2159 | goto out; |
| 2160 | q += (*qvalue++ - '0') * 1; |
| 2161 | out: |
| 2162 | if (q > 1000) |
| 2163 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2164 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2165 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2166 | return q; |
| 2167 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2168 | |
| 2169 | /* |
| 2170 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2171 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2172 | int select_compression_request_header(struct session *s, struct buffer *req) |
| 2173 | { |
| 2174 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2175 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2176 | struct hdr_ctx ctx; |
| 2177 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2178 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2179 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2180 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 2181 | * 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] | 2182 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 2183 | */ |
| 2184 | ctx.idx = 0; |
| 2185 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2186 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2187 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2188 | (ctx.vlen < 31 || |
| 2189 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2190 | ctx.line[ctx.val + 30] < '6' || |
| 2191 | (ctx.line[ctx.val + 30] == '6' && |
| 2192 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2193 | s->comp_algo = NULL; |
| 2194 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2195 | } |
| 2196 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2197 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2198 | 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] | 2199 | int best_q = 0; |
| 2200 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2201 | ctx.idx = 0; |
| 2202 | 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] | 2203 | const char *qval; |
| 2204 | int q; |
| 2205 | int toklen; |
| 2206 | |
| 2207 | /* try to isolate the token from the optional q-value */ |
| 2208 | toklen = 0; |
| 2209 | while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)]) |
| 2210 | toklen++; |
| 2211 | |
| 2212 | qval = ctx.line + ctx.val + toklen; |
| 2213 | while (1) { |
| 2214 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2215 | qval++; |
| 2216 | |
| 2217 | if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') { |
| 2218 | qval = NULL; |
| 2219 | break; |
| 2220 | } |
| 2221 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2222 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2223 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 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 | if (qval >= ctx.line + ctx.val + ctx.vlen) { |
| 2227 | qval = NULL; |
| 2228 | break; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2229 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2230 | if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0) |
| 2231 | break; |
| 2232 | |
| 2233 | while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';') |
| 2234 | qval++; |
| 2235 | } |
| 2236 | |
| 2237 | /* 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] | 2238 | q = qval ? parse_qvalue(qval + 2, NULL) : 1000; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2239 | |
| 2240 | if (q <= best_q) |
| 2241 | continue; |
| 2242 | |
| 2243 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 2244 | if (*(ctx.line + ctx.val) == '*' || |
| 2245 | word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) { |
| 2246 | s->comp_algo = comp_algo; |
| 2247 | best_q = q; |
| 2248 | break; |
| 2249 | } |
| 2250 | } |
| 2251 | } |
| 2252 | } |
| 2253 | |
| 2254 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2255 | if (s->comp_algo) { |
| 2256 | if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) { |
| 2257 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2258 | ctx.idx = 0; |
| 2259 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2260 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2261 | } |
| 2262 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2263 | return 1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | /* identity is implicit does not require headers */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2267 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
| 2268 | 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] | 2269 | if (comp_algo->add_data == identity_add_data) { |
| 2270 | s->comp_algo = comp_algo; |
| 2271 | return 1; |
| 2272 | } |
| 2273 | } |
| 2274 | } |
| 2275 | |
| 2276 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2277 | return 0; |
| 2278 | } |
| 2279 | |
| 2280 | /* |
| 2281 | * Selects a comression algorithm depending of the server response. |
| 2282 | */ |
| 2283 | int select_compression_response_header(struct session *s, struct buffer *res) |
| 2284 | { |
| 2285 | struct http_txn *txn = &s->txn; |
| 2286 | struct http_msg *msg = &txn->rsp; |
| 2287 | struct hdr_ctx ctx; |
| 2288 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2289 | |
| 2290 | /* no common compression algorithm was found in request header */ |
| 2291 | if (s->comp_algo == NULL) |
| 2292 | goto fail; |
| 2293 | |
| 2294 | /* HTTP < 1.1 should not be compressed */ |
Willy Tarreau | 7257550 | 2013-12-24 14:41:35 +0100 | [diff] [blame] | 2295 | 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] | 2296 | goto fail; |
| 2297 | |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2298 | /* 200 only */ |
| 2299 | if (txn->status != 200) |
| 2300 | goto fail; |
| 2301 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2302 | /* Content-Length is null */ |
| 2303 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2304 | goto fail; |
| 2305 | |
| 2306 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2307 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2308 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2309 | goto fail; |
| 2310 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2311 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2312 | ctx.idx = 0; |
| 2313 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2314 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2315 | goto fail; |
| 2316 | } |
| 2317 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2318 | comp_type = NULL; |
| 2319 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2320 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2321 | * not listed in the "compression type" directive if any. If no content-type was |
| 2322 | * found but configuration requires one, we don't compress either. Backend has |
| 2323 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2324 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2325 | ctx.idx = 0; |
| 2326 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2327 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2328 | goto fail; |
| 2329 | |
| 2330 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 2331 | (s->fe->comp && (comp_type = s->fe->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2332 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2333 | if (ctx.vlen >= comp_type->name_len && |
| 2334 | 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] | 2335 | /* this Content-Type should be compressed */ |
| 2336 | break; |
| 2337 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2338 | /* this Content-Type should not be compressed */ |
| 2339 | if (comp_type == NULL) |
| 2340 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2341 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2342 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2343 | else { /* no content-type header */ |
| 2344 | if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types)) |
| 2345 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2346 | } |
| 2347 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2348 | /* limit compression rate */ |
| 2349 | if (global.comp_rate_lim > 0) |
| 2350 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2351 | goto fail; |
| 2352 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2353 | /* limit cpu usage */ |
| 2354 | if (idle_pct < compress_min_idle) |
| 2355 | goto fail; |
| 2356 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2357 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2358 | 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] | 2359 | goto fail; |
| 2360 | |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2361 | s->flags |= SN_COMP_READY; |
| 2362 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2363 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2364 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2365 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2366 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2367 | |
| 2368 | /* add Transfer-Encoding header */ |
| 2369 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2370 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2371 | |
| 2372 | /* |
| 2373 | * Add Content-Encoding header when it's not identity encoding. |
| 2374 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2375 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2376 | * header. |
| 2377 | */ |
| 2378 | if (s->comp_algo->add_data != identity_add_data) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2379 | trash.len = 18; |
| 2380 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
| 2381 | memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len); |
| 2382 | trash.len += s->comp_algo->name_len; |
| 2383 | trash.str[trash.len] = '\0'; |
| 2384 | 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] | 2385 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2386 | return 1; |
| 2387 | |
| 2388 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2389 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2390 | return 0; |
| 2391 | } |
| 2392 | |
Willy Tarreau | 2e47a3a | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2393 | void http_adjust_conn_mode(struct session *s, struct http_txn *txn, struct http_msg *msg) |
| 2394 | { |
| 2395 | int tmp = TX_CON_WANT_KAL; |
| 2396 | |
| 2397 | if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 2398 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
| 2399 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2400 | tmp = TX_CON_WANT_TUN; |
| 2401 | |
| 2402 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 2403 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2404 | tmp = TX_CON_WANT_TUN; |
| 2405 | } |
| 2406 | |
| 2407 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 2408 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 2409 | /* option httpclose + server_close => forceclose */ |
| 2410 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 2411 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2412 | tmp = TX_CON_WANT_CLO; |
| 2413 | else |
| 2414 | tmp = TX_CON_WANT_SCL; |
| 2415 | } |
| 2416 | |
| 2417 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
| 2418 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 2419 | tmp = TX_CON_WANT_CLO; |
| 2420 | |
| 2421 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2422 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2423 | |
| 2424 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 2425 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2426 | /* parse the Connection header and possibly clean it */ |
| 2427 | int to_del = 0; |
| 2428 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 2429 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 2430 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
| 2431 | to_del |= 2; /* remove "keep-alive" */ |
| 2432 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2433 | to_del |= 1; /* remove "close" */ |
| 2434 | http_parse_connection_header(txn, msg, to_del); |
| 2435 | } |
| 2436 | |
| 2437 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2438 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2439 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2440 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2441 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 2442 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
| 2443 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
| 2444 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2445 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2446 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2447 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2448 | * processing can continue on next analysers, or zero if it either needs more |
| 2449 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2450 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2451 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2452 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2453 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2454 | 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] | 2455 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2456 | /* |
| 2457 | * We will parse the partial (or complete) lines. |
| 2458 | * We will check the request syntax, and also join multi-line |
| 2459 | * headers. An index of all the lines will be elaborated while |
| 2460 | * parsing. |
| 2461 | * |
| 2462 | * For the parsing, we use a 28 states FSM. |
| 2463 | * |
| 2464 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2465 | * req->buf->p = beginning of request |
| 2466 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2467 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2468 | * msg->eol = end of current header or line (LF or CRLF) |
| 2469 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2470 | * |
| 2471 | * 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] | 2472 | * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2473 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2474 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2475 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2476 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2477 | int cur_idx; |
| 2478 | struct http_txn *txn = &s->txn; |
| 2479 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2480 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2481 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2482 | 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] | 2483 | now_ms, __FUNCTION__, |
| 2484 | s, |
| 2485 | req, |
| 2486 | req->rex, req->wex, |
| 2487 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2488 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2489 | req->analysers); |
| 2490 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2491 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2492 | s->srv_error = http_return_srv_error; |
| 2493 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2494 | /* There's a protected area at the end of the buffer for rewriting |
| 2495 | * purposes. We don't want to start to parse the request if the |
| 2496 | * protected area is affected, because we may have to move processed |
| 2497 | * data later, which is much more complicated. |
| 2498 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2499 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2500 | if (txn->flags & TX_NOT_FIRST) { |
| 2501 | if (unlikely(!channel_reserved(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2502 | 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] | 2503 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2504 | /* 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] | 2505 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2506 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2507 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2508 | return 0; |
| 2509 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2510 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2511 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2512 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2513 | } |
| 2514 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2515 | /* Note that we have the same problem with the response ; we |
| 2516 | * may want to send a redirect, error or anything which requires |
| 2517 | * some spare space. So we'll ensure that we have at least |
| 2518 | * maxrewrite bytes available in the response buffer before |
| 2519 | * processing that one. This will only affect pipelined |
| 2520 | * keep-alive requests. |
| 2521 | */ |
| 2522 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2523 | unlikely(!channel_reserved(s->rep) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2524 | bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) || |
| 2525 | bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) { |
| 2526 | if (s->rep->buf->o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2527 | 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] | 2528 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2529 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2530 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2531 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2532 | s->rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2533 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2534 | return 0; |
| 2535 | } |
| 2536 | } |
| 2537 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2538 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2539 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2540 | } |
| 2541 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2542 | /* 1: we might have to print this header in debug mode */ |
| 2543 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2544 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 8767b13 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2545 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2546 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2547 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2548 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2549 | /* this is a bit complex : in case of error on the request line, |
| 2550 | * we know that rq.l is still zero, so we display only the part |
| 2551 | * up to the end of the line (truncated by debug_hdr). |
| 2552 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2553 | 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] | 2554 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2555 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2556 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2557 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2558 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2559 | while (cur_idx) { |
| 2560 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2561 | debug_hdr("clihdr", s, sol, eol); |
| 2562 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2563 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2564 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2565 | } |
| 2566 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2567 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2568 | /* |
| 2569 | * Now we quickly check if we have found a full valid request. |
| 2570 | * If not so, we check the FD and buffer states before leaving. |
| 2571 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2572 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2573 | * requests are checked first. When waiting for a second request |
| 2574 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2575 | * we note the error in the session flags but don't set any state. |
| 2576 | * Since the error will be noted there, it will not be counted by |
| 2577 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2578 | * Last, we may increase some tracked counters' http request errors on |
| 2579 | * the cases that are deliberately the client's fault. For instance, |
| 2580 | * a timeout or connection reset is not counted as an error. However |
| 2581 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2582 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2583 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2584 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2585 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2586 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2587 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2588 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2589 | session_inc_http_req_ctr(s); |
| 2590 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2591 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2592 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2593 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2594 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2595 | /* 1: Since we are in header mode, if there's no space |
| 2596 | * left for headers, we won't be able to free more |
| 2597 | * later, so the session will never terminate. We |
| 2598 | * must terminate it now. |
| 2599 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2600 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2601 | /* FIXME: check if URI is set and return Status |
| 2602 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2603 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2604 | session_inc_http_req_ctr(s); |
| 2605 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2606 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2607 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2608 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2609 | goto return_bad_req; |
| 2610 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2611 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2612 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2613 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2614 | if (!(s->flags & SN_ERR_MASK)) |
| 2615 | s->flags |= SN_ERR_CLICL; |
| 2616 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2617 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2618 | goto failed_keep_alive; |
| 2619 | |
Willy Tarreau | 1c3a612 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2620 | if (s->fe->options & PR_O_IGNORE_PRB) |
| 2621 | goto failed_keep_alive; |
| 2622 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2623 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2624 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2625 | 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] | 2626 | session_inc_http_err_ctr(s); |
| 2627 | } |
| 2628 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2629 | txn->status = 400; |
| 2630 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2631 | msg->msg_state = HTTP_MSG_ERROR; |
| 2632 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2633 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2634 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2635 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2636 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2637 | if (s->listener->counters) |
| 2638 | s->listener->counters->failed_req++; |
| 2639 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2640 | if (!(s->flags & SN_FINST_MASK)) |
| 2641 | s->flags |= SN_FINST_R; |
| 2642 | return 0; |
| 2643 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2644 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2645 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2646 | 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] | 2647 | if (!(s->flags & SN_ERR_MASK)) |
| 2648 | s->flags |= SN_ERR_CLITO; |
| 2649 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2650 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2651 | goto failed_keep_alive; |
| 2652 | |
Willy Tarreau | 1c3a612 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2653 | if (s->fe->options & PR_O_IGNORE_PRB) |
| 2654 | goto failed_keep_alive; |
| 2655 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2656 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2657 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2658 | 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] | 2659 | session_inc_http_err_ctr(s); |
| 2660 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2661 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2662 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2663 | msg->msg_state = HTTP_MSG_ERROR; |
| 2664 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2665 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2666 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2667 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2668 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2669 | if (s->listener->counters) |
| 2670 | s->listener->counters->failed_req++; |
| 2671 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2672 | if (!(s->flags & SN_FINST_MASK)) |
| 2673 | s->flags |= SN_FINST_R; |
| 2674 | return 0; |
| 2675 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2676 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2677 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2678 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2679 | if (!(s->flags & SN_ERR_MASK)) |
| 2680 | s->flags |= SN_ERR_CLICL; |
| 2681 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2682 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2683 | goto failed_keep_alive; |
| 2684 | |
Willy Tarreau | 1c3a612 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2685 | if (s->fe->options & PR_O_IGNORE_PRB) |
| 2686 | goto failed_keep_alive; |
| 2687 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2688 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2689 | 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] | 2690 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2691 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2692 | msg->msg_state = HTTP_MSG_ERROR; |
| 2693 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2694 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2695 | session_inc_http_err_ctr(s); |
| 2696 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2697 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2698 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2699 | if (s->listener->counters) |
| 2700 | s->listener->counters->failed_req++; |
| 2701 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2702 | if (!(s->flags & SN_FINST_MASK)) |
| 2703 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2704 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2705 | } |
| 2706 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2707 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2708 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2709 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2710 | #ifdef TCP_QUICKACK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 2711 | 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] | 2712 | /* We need more data, we have to re-enable quick-ack in case we |
| 2713 | * previously disabled it, otherwise we might cause the client |
| 2714 | * to delay next data. |
| 2715 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2716 | 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] | 2717 | } |
| 2718 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2719 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2720 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2721 | /* If the client starts to talk, let's fall back to |
| 2722 | * request timeout processing. |
| 2723 | */ |
| 2724 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2725 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2726 | } |
| 2727 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2728 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2729 | if (!tick_isset(req->analyse_exp)) { |
| 2730 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2731 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2732 | tick_isset(s->be->timeout.httpka)) |
| 2733 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2734 | else |
| 2735 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2736 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2737 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2738 | /* we're not ready yet */ |
| 2739 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2740 | |
| 2741 | failed_keep_alive: |
| 2742 | /* Here we process low-level errors for keep-alive requests. In |
| 2743 | * short, if the request is not the first one and it experiences |
| 2744 | * a timeout, read error or shutdown, we just silently close so |
| 2745 | * that the client can try again. |
| 2746 | */ |
| 2747 | txn->status = 0; |
| 2748 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2749 | req->analysers = 0; |
| 2750 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2751 | s->logs.level = 0; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2752 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2753 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2754 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2755 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2756 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2757 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2758 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2759 | * 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] | 2760 | * 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] | 2761 | * 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] | 2762 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2763 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2764 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2765 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2766 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2767 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2768 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2769 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2770 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2771 | /* kill the pending keep-alive timeout */ |
| 2772 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2773 | req->analyse_exp = TICK_ETERNITY; |
| 2774 | } |
| 2775 | |
| 2776 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2777 | /* Maybe we found in invalid header name while we were configured not |
| 2778 | * to block on that, so we have to capture it now. |
| 2779 | */ |
| 2780 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2781 | 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] | 2782 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2783 | /* |
| 2784 | * 1: identify the method |
| 2785 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2786 | 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] | 2787 | |
| 2788 | /* we can make use of server redirect on GET and HEAD */ |
| 2789 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2790 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2791 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2792 | /* |
| 2793 | * 2: check if the URI matches the monitor_uri. |
| 2794 | * We have to do this for every request which gets in, because |
| 2795 | * the monitor-uri is defined by the frontend. |
| 2796 | */ |
| 2797 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2798 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2799 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2800 | s->fe->monitor_uri, |
| 2801 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2802 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2803 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2804 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2805 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2806 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2807 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2808 | s->fe->fe_counters.intercepted_req++; |
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 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2811 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2812 | 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] | 2813 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2814 | ret = acl_pass(ret); |
| 2815 | if (cond->pol == ACL_COND_UNLESS) |
| 2816 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2817 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2818 | if (ret) { |
| 2819 | /* we fail this request, let's return 503 service unavail */ |
| 2820 | txn->status = 503; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2821 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2822 | if (!(s->flags & SN_ERR_MASK)) |
| 2823 | 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] | 2824 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2825 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2826 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2827 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2828 | /* nothing to fail, let's reply normaly */ |
| 2829 | txn->status = 200; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2830 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2831 | if (!(s->flags & SN_ERR_MASK)) |
| 2832 | 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] | 2833 | goto return_prx_cond; |
| 2834 | } |
| 2835 | |
| 2836 | /* |
| 2837 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2838 | * Note: we cannot log anymore if the request has been |
| 2839 | * classified as invalid. |
| 2840 | */ |
| 2841 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2842 | /* we have a complete HTTP request that we must log */ |
| 2843 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2844 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2845 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2846 | if (urilen >= REQURI_LEN) |
| 2847 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2848 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2849 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2850 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2851 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2852 | s->do_log(s); |
| 2853 | } else { |
| 2854 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2855 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2856 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2857 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2858 | /* 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] | 2859 | 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] | 2860 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2861 | |
Willy Tarreau | 5564555 | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2862 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 2863 | * exactly one digit "." one digit. This check may be disabled using |
| 2864 | * option accept-invalid-http-request. |
| 2865 | */ |
| 2866 | if (!(s->fe->options2 & PR_O2_REQBUG_OK)) { |
| 2867 | if (msg->sl.rq.v_l != 8) { |
| 2868 | msg->err_pos = msg->sl.rq.v; |
| 2869 | goto return_bad_req; |
| 2870 | } |
| 2871 | |
| 2872 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 2873 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 2874 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 2875 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 2876 | msg->err_pos = msg->sl.rq.v + 4; |
| 2877 | goto return_bad_req; |
| 2878 | } |
| 2879 | } |
| 2880 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2881 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2882 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2883 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2884 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2885 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2886 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2887 | |
| 2888 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2889 | 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] | 2890 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2891 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2892 | * we have what looks like a proxied connection instead of a connection, |
| 2893 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2894 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2895 | * happen to do that despite being non-standard :-( |
| 2896 | * We consider that a request not beginning with either '/' or '*' is |
| 2897 | * a proxied connection, which covers both "scheme://location" and |
| 2898 | * CONNECT ip:port. |
| 2899 | */ |
| 2900 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2901 | 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] | 2902 | txn->flags |= TX_USE_PX_CONN; |
| 2903 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2904 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2905 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2906 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2907 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2908 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2909 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2910 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2911 | |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2912 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 2913 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2914 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2915 | * The length of a message body is determined by one of the following |
| 2916 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2917 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2918 | * 1. Any response to a HEAD request and any response with a 1xx |
| 2919 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 2920 | * code is always terminated by the first empty line after the |
| 2921 | * header fields, regardless of the header fields present in the |
| 2922 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2923 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2924 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 2925 | * the connection will become a tunnel immediately after the empty |
| 2926 | * line that concludes the header fields. A client MUST ignore any |
| 2927 | * Content-Length or Transfer-Encoding header fields received in |
| 2928 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2929 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2930 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 2931 | * transfer coding (Section 4.1) is the final encoding, the message |
| 2932 | * body length is determined by reading and decoding the chunked |
| 2933 | * data until the transfer coding indicates the data is complete. |
| 2934 | * |
| 2935 | * If a Transfer-Encoding header field is present in a response and |
| 2936 | * the chunked transfer coding is not the final encoding, the |
| 2937 | * message body length is determined by reading the connection until |
| 2938 | * it is closed by the server. If a Transfer-Encoding header field |
| 2939 | * is present in a request and the chunked transfer coding is not |
| 2940 | * the final encoding, the message body length cannot be determined |
| 2941 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 2942 | * status code and then close the connection. |
| 2943 | * |
| 2944 | * If a message is received with both a Transfer-Encoding and a |
| 2945 | * Content-Length header field, the Transfer-Encoding overrides the |
| 2946 | * Content-Length. Such a message might indicate an attempt to |
| 2947 | * perform request smuggling (Section 9.5) or response splitting |
| 2948 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 2949 | * remove the received Content-Length field prior to forwarding such |
| 2950 | * a message downstream. |
| 2951 | * |
| 2952 | * 4. If a message is received without Transfer-Encoding and with |
| 2953 | * either multiple Content-Length header fields having differing |
| 2954 | * field-values or a single Content-Length header field having an |
| 2955 | * invalid value, then the message framing is invalid and the |
| 2956 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 2957 | * request message, the server MUST respond with a 400 (Bad Request) |
| 2958 | * status code and then close the connection. If this is a response |
| 2959 | * message received by a proxy, the proxy MUST close the connection |
| 2960 | * to the server, discard the received response, and send a 502 (Bad |
| 2961 | * Gateway) response to the client. If this is a response message |
| 2962 | * received by a user agent, the user agent MUST close the |
| 2963 | * connection to the server and discard the received response. |
| 2964 | * |
| 2965 | * 5. If a valid Content-Length header field is present without |
| 2966 | * Transfer-Encoding, its decimal value defines the expected message |
| 2967 | * body length in octets. If the sender closes the connection or |
| 2968 | * the recipient times out before the indicated number of octets are |
| 2969 | * received, the recipient MUST consider the message to be |
| 2970 | * incomplete and close the connection. |
| 2971 | * |
| 2972 | * 6. If this is a request message and none of the above are true, then |
| 2973 | * the message body length is zero (no message body is present). |
| 2974 | * |
| 2975 | * 7. Otherwise, this is a response message without a declared message |
| 2976 | * body length, so the message body length is determined by the |
| 2977 | * number of octets received prior to the server closing the |
| 2978 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2979 | */ |
| 2980 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2981 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2982 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | e77bc17 | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 2983 | 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] | 2984 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2985 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2986 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 49efa26 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2987 | /* chunked not last, return badreq */ |
| 2988 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2989 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2990 | } |
| 2991 | |
Willy Tarreau | dfa3d92 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2992 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2993 | ctx.idx = 0; |
Willy Tarreau | dfa3d92 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2994 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 2995 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 2996 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2997 | } |
Willy Tarreau | 49efa26 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2998 | 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] | 2999 | signed long long cl; |
| 3000 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3001 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3002 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3003 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3004 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3005 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3006 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3007 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3008 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3009 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3010 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3011 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3012 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3013 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3014 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3015 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3016 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3017 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3018 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3019 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3020 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3021 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3022 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3023 | } |
| 3024 | |
Willy Tarreau | 49efa26 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3025 | /* even bodyless requests have a known length */ |
| 3026 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3027 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3028 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 3029 | * only change if both the request and the config reference something else. |
| 3030 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3031 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3032 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3033 | * if FE and BE have the same settings (common). The method consists in |
| 3034 | * checking if options changed between the two calls (implying that either |
| 3035 | * one is non-null, or one of them is non-null and we are there for the first |
| 3036 | * time. |
| 3037 | */ |
| 3038 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | 2e47a3a | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 3039 | ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) |
| 3040 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3041 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3042 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3043 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3044 | req->analyse_exp = TICK_ETERNITY; |
| 3045 | return 1; |
| 3046 | |
| 3047 | return_bad_req: |
| 3048 | /* We centralize bad requests processing here */ |
| 3049 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3050 | /* we detected a parsing error. We want to archive this request |
| 3051 | * in the dedicated proxy area for later troubleshooting. |
| 3052 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3053 | 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] | 3054 | } |
| 3055 | |
| 3056 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3057 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3058 | 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] | 3059 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3060 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3061 | if (s->listener->counters) |
| 3062 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3063 | |
| 3064 | return_prx_cond: |
| 3065 | if (!(s->flags & SN_ERR_MASK)) |
| 3066 | s->flags |= SN_ERR_PRXCOND; |
| 3067 | if (!(s->flags & SN_FINST_MASK)) |
| 3068 | s->flags |= SN_FINST_R; |
| 3069 | |
| 3070 | req->analysers = 0; |
| 3071 | req->analyse_exp = TICK_ETERNITY; |
| 3072 | return 0; |
| 3073 | } |
| 3074 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3075 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3076 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3077 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3078 | * and program a response message if something was unexpected. It cannot fail |
| 3079 | * 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] | 3080 | * 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] | 3081 | * s->target which is supposed to already point to the stats applet. The caller |
| 3082 | * is expected to have already assigned an appctx to the session. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3083 | */ |
| 3084 | int http_handle_stats(struct session *s, struct channel *req) |
| 3085 | { |
| 3086 | struct stats_admin_rule *stats_admin_rule; |
| 3087 | struct stream_interface *si = s->rep->prod; |
| 3088 | struct http_txn *txn = &s->txn; |
| 3089 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3090 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3091 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3092 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3093 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3094 | appctx = si_appctx(si); |
| 3095 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3096 | appctx->st1 = appctx->st2 = 0; |
| 3097 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3098 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | af3cf70 | 2014-04-22 22:19:53 +0200 | [diff] [blame] | 3099 | if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD)) |
| 3100 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3101 | |
| 3102 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3103 | lookup = uri + uri_auth->uri_len; |
| 3104 | |
| 3105 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3106 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3107 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3108 | break; |
| 3109 | } |
| 3110 | } |
| 3111 | |
| 3112 | if (uri_auth->refresh) { |
| 3113 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3114 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3115 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3116 | break; |
| 3117 | } |
| 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3122 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3123 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3124 | break; |
| 3125 | } |
| 3126 | } |
| 3127 | |
| 3128 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3129 | if (memcmp(h, ";st=", 4) == 0) { |
| 3130 | int i; |
| 3131 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3132 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3133 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3134 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3135 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3136 | break; |
| 3137 | } |
| 3138 | } |
| 3139 | break; |
| 3140 | } |
| 3141 | } |
| 3142 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3143 | appctx->ctx.stats.scope_str = 0; |
| 3144 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3145 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3146 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3147 | int itx = 0; |
| 3148 | const char *h2; |
| 3149 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3150 | const char *err; |
| 3151 | |
| 3152 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3153 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3154 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3155 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3156 | itx++; |
| 3157 | h++; |
| 3158 | } |
| 3159 | |
| 3160 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3161 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3162 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3163 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3164 | /* 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] | 3165 | memcpy(scope_txt, h2, itx); |
| 3166 | scope_txt[itx] = '\0'; |
| 3167 | err = invalid_char(scope_txt); |
| 3168 | if (err) { |
| 3169 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3170 | appctx->ctx.stats.scope_str = 0; |
| 3171 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3172 | } |
| 3173 | break; |
| 3174 | } |
| 3175 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3176 | |
| 3177 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3178 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3179 | int ret = 1; |
| 3180 | |
| 3181 | if (stats_admin_rule->cond) { |
| 3182 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 3183 | ret = acl_pass(ret); |
| 3184 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3185 | ret = !ret; |
| 3186 | } |
| 3187 | |
| 3188 | if (ret) { |
| 3189 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3190 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3191 | break; |
| 3192 | } |
| 3193 | } |
| 3194 | |
| 3195 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3196 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3197 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3198 | /* we'll need the request body, possibly after sending 100-continue */ |
| 3199 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3200 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3201 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3202 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3203 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3204 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3205 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3206 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3207 | else { |
| 3208 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3209 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3210 | } |
| 3211 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3212 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3213 | return 1; |
| 3214 | } |
| 3215 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3216 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3217 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3218 | */ |
| 3219 | static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
| 3220 | { |
| 3221 | #ifdef IP_TOS |
| 3222 | if (from.ss_family == AF_INET) |
| 3223 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3224 | #endif |
| 3225 | #ifdef IPV6_TCLASS |
| 3226 | if (from.ss_family == AF_INET6) { |
| 3227 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3228 | /* v4-mapped addresses need IP_TOS */ |
| 3229 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3230 | else |
| 3231 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3232 | } |
| 3233 | #endif |
| 3234 | } |
| 3235 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3236 | 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] | 3237 | 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] | 3238 | struct hdr_ctx* ctx, int action) |
| 3239 | { |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3240 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 3241 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
| 3242 | struct chunk *replace = get_trash_chunk(); |
| 3243 | struct chunk *output = get_trash_chunk(); |
| 3244 | |
| 3245 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 3246 | if (replace->len >= replace->size - 1) |
| 3247 | return -1; |
| 3248 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3249 | ctx->idx = 0; |
| 3250 | |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3251 | /* Choose the header browsing function. */ |
| 3252 | switch (action) { |
| 3253 | case HTTP_REQ_ACT_REPLACE_VAL: |
| 3254 | case HTTP_RES_ACT_REPLACE_VAL: |
| 3255 | http_find_hdr_func = http_find_header2; |
| 3256 | break; |
| 3257 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3258 | case HTTP_RES_ACT_REPLACE_HDR: |
| 3259 | http_find_hdr_func = http_find_full_header2; |
| 3260 | break; |
| 3261 | default: /* impossible */ |
| 3262 | return -1; |
| 3263 | } |
| 3264 | |
| 3265 | while (http_find_hdr_func(name, name_len, buf, idx, ctx)) { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3266 | struct hdr_idx_elem *hdr = idx->v + ctx->idx; |
| 3267 | int delta; |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3268 | char *val = ctx->line + ctx->val; |
| 3269 | char* val_end = val + ctx->vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3270 | |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3271 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch)) |
| 3272 | continue; |
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 | output->len = exp_replace(output->str, output->size, val, replace->str, pmatch); |
| 3275 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3276 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3277 | |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3278 | 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] | 3279 | |
| 3280 | hdr->len += delta; |
| 3281 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 06170c5 | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3282 | |
| 3283 | /* Adjust the length of the current value of the index. */ |
| 3284 | ctx->vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3285 | } |
| 3286 | |
| 3287 | return 0; |
| 3288 | } |
| 3289 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3290 | /* Executes the http-request rules <rules> for session <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3291 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3292 | * further processing of the request (auth, deny, ...), and defaults to |
| 3293 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3294 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 3295 | * on txn->flags if it encounters a tarpit rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3296 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3297 | enum rule_result |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3298 | 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] | 3299 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3300 | struct connection *cli_conn; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3301 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3302 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3303 | const char *auth_realm; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3304 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3305 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3306 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3307 | continue; |
| 3308 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3309 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3310 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3311 | int ret; |
| 3312 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3313 | 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] | 3314 | ret = acl_pass(ret); |
| 3315 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3316 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3317 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3318 | |
| 3319 | if (!ret) /* condition not matched */ |
| 3320 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3321 | } |
| 3322 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3323 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3324 | switch (rule->action) { |
| 3325 | case HTTP_REQ_ACT_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3326 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3327 | |
| 3328 | case HTTP_REQ_ACT_DENY: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3329 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3330 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3331 | case HTTP_REQ_ACT_TARPIT: |
| 3332 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3333 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3334 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3335 | case HTTP_REQ_ACT_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3336 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3337 | auth_realm = rule->arg.auth.realm; |
| 3338 | if (!auth_realm) { |
| 3339 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3340 | auth_realm = STATS_DEFAULT_REALM; |
| 3341 | else |
| 3342 | auth_realm = px->id; |
| 3343 | } |
| 3344 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3345 | * count one error, because normal browsing won't significantly |
| 3346 | * increase the counter but brute force attempts will. |
| 3347 | */ |
| 3348 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3349 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
| 3350 | stream_int_retnclose(&s->si[0], &trash); |
| 3351 | session_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3352 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3353 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3354 | case HTTP_REQ_ACT_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3355 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3356 | return HTTP_RULE_RES_BADREQ; |
| 3357 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3358 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3359 | case HTTP_REQ_ACT_SET_NICE: |
| 3360 | s->task->nice = rule->arg.nice; |
| 3361 | break; |
| 3362 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3363 | case HTTP_REQ_ACT_SET_TOS: |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3364 | 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] | 3365 | 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] | 3366 | break; |
| 3367 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3368 | case HTTP_REQ_ACT_SET_MARK: |
| 3369 | #ifdef SO_MARK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3370 | 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] | 3371 | 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] | 3372 | #endif |
| 3373 | break; |
| 3374 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3375 | case HTTP_REQ_ACT_SET_LOGL: |
| 3376 | s->logs.level = rule->arg.loglevel; |
| 3377 | break; |
| 3378 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3379 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3380 | case HTTP_REQ_ACT_REPLACE_VAL: |
| 3381 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3382 | 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] | 3383 | &rule->arg.hdr_add.re, &ctx, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3384 | return HTTP_RULE_RES_BADREQ; |
| 3385 | break; |
| 3386 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3387 | case HTTP_REQ_ACT_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3388 | ctx.idx = 0; |
| 3389 | /* remove all occurrences of the header */ |
| 3390 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3391 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3392 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3393 | } |
Willy Tarreau | fcf75d2 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3394 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3395 | |
Willy Tarreau | fcf75d2 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3396 | case HTTP_REQ_ACT_SET_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3397 | case HTTP_REQ_ACT_ADD_HDR: |
| 3398 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3399 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3400 | trash.len = rule->arg.hdr_add.name_len; |
| 3401 | trash.str[trash.len++] = ':'; |
| 3402 | trash.str[trash.len++] = ' '; |
| 3403 | 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] | 3404 | |
| 3405 | if (rule->action == HTTP_REQ_ACT_SET_HDR) { |
| 3406 | /* remove all occurrences of the header */ |
| 3407 | ctx.idx = 0; |
| 3408 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3409 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3410 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3411 | } |
| 3412 | } |
| 3413 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3414 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3415 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3416 | |
| 3417 | case HTTP_REQ_ACT_DEL_ACL: |
| 3418 | case HTTP_REQ_ACT_DEL_MAP: { |
| 3419 | struct pat_ref *ref; |
| 3420 | char *key; |
| 3421 | int len; |
| 3422 | |
| 3423 | /* collect reference */ |
| 3424 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3425 | if (!ref) |
| 3426 | continue; |
| 3427 | |
| 3428 | /* collect key */ |
| 3429 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3430 | key = trash.str; |
| 3431 | key[len] = '\0'; |
| 3432 | |
| 3433 | /* perform update */ |
| 3434 | /* returned code: 1=ok, 0=ko */ |
| 3435 | pat_ref_delete(ref, key); |
| 3436 | |
| 3437 | break; |
| 3438 | } |
| 3439 | |
| 3440 | case HTTP_REQ_ACT_ADD_ACL: { |
| 3441 | struct pat_ref *ref; |
| 3442 | char *key; |
| 3443 | struct chunk *trash_key; |
| 3444 | int len; |
| 3445 | |
| 3446 | trash_key = get_trash_chunk(); |
| 3447 | |
| 3448 | /* collect reference */ |
| 3449 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3450 | if (!ref) |
| 3451 | continue; |
| 3452 | |
| 3453 | /* collect key */ |
| 3454 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3455 | key = trash_key->str; |
| 3456 | key[len] = '\0'; |
| 3457 | |
| 3458 | /* perform update */ |
| 3459 | /* add entry only if it does not already exist */ |
| 3460 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3461 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3462 | |
| 3463 | break; |
| 3464 | } |
| 3465 | |
| 3466 | case HTTP_REQ_ACT_SET_MAP: { |
| 3467 | struct pat_ref *ref; |
| 3468 | char *key, *value; |
| 3469 | struct chunk *trash_key, *trash_value; |
| 3470 | int len; |
| 3471 | |
| 3472 | trash_key = get_trash_chunk(); |
| 3473 | trash_value = get_trash_chunk(); |
| 3474 | |
| 3475 | /* collect reference */ |
| 3476 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3477 | if (!ref) |
| 3478 | continue; |
| 3479 | |
| 3480 | /* collect key */ |
| 3481 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3482 | key = trash_key->str; |
| 3483 | key[len] = '\0'; |
| 3484 | |
| 3485 | /* collect value */ |
| 3486 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3487 | value = trash_value->str; |
| 3488 | value[len] = '\0'; |
| 3489 | |
| 3490 | /* perform update */ |
| 3491 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3492 | /* update entry if it exists */ |
| 3493 | pat_ref_set(ref, key, value, NULL); |
| 3494 | else |
| 3495 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3496 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3497 | |
| 3498 | break; |
| 3499 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3500 | |
| 3501 | case HTTP_REQ_ACT_CUSTOM_CONT: |
| 3502 | rule->action_ptr(rule, px, s, txn); |
| 3503 | break; |
| 3504 | |
| 3505 | case HTTP_REQ_ACT_CUSTOM_STOP: |
| 3506 | rule->action_ptr(rule, px, s, txn); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3507 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3508 | } |
| 3509 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3510 | |
| 3511 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3512 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3513 | } |
| 3514 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3515 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3516 | /* Executes the http-response rules <rules> for session <s>, proxy <px> and |
| 3517 | * transaction <txn>. Returns the first rule that prevents further processing |
| 3518 | * of the response (deny, ...) or NULL if it executed all rules or stopped |
| 3519 | * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny |
| 3520 | * rule. |
| 3521 | */ |
| 3522 | static struct http_res_rule * |
| 3523 | http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 3524 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3525 | struct connection *cli_conn; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3526 | struct http_res_rule *rule; |
| 3527 | struct hdr_ctx ctx; |
| 3528 | |
| 3529 | list_for_each_entry(rule, rules, list) { |
| 3530 | if (rule->action >= HTTP_RES_ACT_MAX) |
| 3531 | continue; |
| 3532 | |
| 3533 | /* check optional condition */ |
| 3534 | if (rule->cond) { |
| 3535 | int ret; |
| 3536 | |
| 3537 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 3538 | ret = acl_pass(ret); |
| 3539 | |
| 3540 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3541 | ret = !ret; |
| 3542 | |
| 3543 | if (!ret) /* condition not matched */ |
| 3544 | continue; |
| 3545 | } |
| 3546 | |
| 3547 | |
| 3548 | switch (rule->action) { |
| 3549 | case HTTP_RES_ACT_ALLOW: |
| 3550 | return NULL; /* "allow" rules are OK */ |
| 3551 | |
| 3552 | case HTTP_RES_ACT_DENY: |
| 3553 | txn->flags |= TX_SVDENY; |
| 3554 | return rule; |
| 3555 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3556 | case HTTP_RES_ACT_SET_NICE: |
| 3557 | s->task->nice = rule->arg.nice; |
| 3558 | break; |
| 3559 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3560 | case HTTP_RES_ACT_SET_TOS: |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3561 | 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] | 3562 | 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] | 3563 | break; |
| 3564 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3565 | case HTTP_RES_ACT_SET_MARK: |
| 3566 | #ifdef SO_MARK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3567 | 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] | 3568 | 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] | 3569 | #endif |
| 3570 | break; |
| 3571 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3572 | case HTTP_RES_ACT_SET_LOGL: |
| 3573 | s->logs.level = rule->arg.loglevel; |
| 3574 | break; |
| 3575 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3576 | case HTTP_RES_ACT_REPLACE_HDR: |
| 3577 | case HTTP_RES_ACT_REPLACE_VAL: |
| 3578 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3579 | 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] | 3580 | &rule->arg.hdr_add.re, &ctx, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3581 | return NULL; /* note: we should report an error here */ |
| 3582 | break; |
| 3583 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3584 | case HTTP_RES_ACT_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3585 | ctx.idx = 0; |
| 3586 | /* remove all occurrences of the header */ |
| 3587 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3588 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3589 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3590 | } |
Willy Tarreau | fcf75d2 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3591 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3592 | |
Willy Tarreau | fcf75d2 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3593 | case HTTP_RES_ACT_SET_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3594 | case HTTP_RES_ACT_ADD_HDR: |
| 3595 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3596 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3597 | trash.len = rule->arg.hdr_add.name_len; |
| 3598 | trash.str[trash.len++] = ':'; |
| 3599 | trash.str[trash.len++] = ' '; |
| 3600 | 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] | 3601 | |
| 3602 | if (rule->action == HTTP_RES_ACT_SET_HDR) { |
| 3603 | /* remove all occurrences of the header */ |
| 3604 | ctx.idx = 0; |
| 3605 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3606 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3607 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3608 | } |
| 3609 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3610 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3611 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3612 | |
| 3613 | case HTTP_RES_ACT_DEL_ACL: |
| 3614 | case HTTP_RES_ACT_DEL_MAP: { |
| 3615 | struct pat_ref *ref; |
| 3616 | char *key; |
| 3617 | int len; |
| 3618 | |
| 3619 | /* collect reference */ |
| 3620 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3621 | if (!ref) |
| 3622 | continue; |
| 3623 | |
| 3624 | /* collect key */ |
| 3625 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3626 | key = trash.str; |
| 3627 | key[len] = '\0'; |
| 3628 | |
| 3629 | /* perform update */ |
| 3630 | /* returned code: 1=ok, 0=ko */ |
| 3631 | pat_ref_delete(ref, key); |
| 3632 | |
| 3633 | break; |
| 3634 | } |
| 3635 | |
| 3636 | case HTTP_RES_ACT_ADD_ACL: { |
| 3637 | struct pat_ref *ref; |
| 3638 | char *key; |
| 3639 | struct chunk *trash_key; |
| 3640 | int len; |
| 3641 | |
| 3642 | trash_key = get_trash_chunk(); |
| 3643 | |
| 3644 | /* collect reference */ |
| 3645 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3646 | if (!ref) |
| 3647 | continue; |
| 3648 | |
| 3649 | /* collect key */ |
| 3650 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3651 | key = trash_key->str; |
| 3652 | key[len] = '\0'; |
| 3653 | |
| 3654 | /* perform update */ |
| 3655 | /* check if the entry already exists */ |
| 3656 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3657 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3658 | |
| 3659 | break; |
| 3660 | } |
| 3661 | |
| 3662 | case HTTP_RES_ACT_SET_MAP: { |
| 3663 | struct pat_ref *ref; |
| 3664 | char *key, *value; |
| 3665 | struct chunk *trash_key, *trash_value; |
| 3666 | int len; |
| 3667 | |
| 3668 | trash_key = get_trash_chunk(); |
| 3669 | trash_value = get_trash_chunk(); |
| 3670 | |
| 3671 | /* collect reference */ |
| 3672 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3673 | if (!ref) |
| 3674 | continue; |
| 3675 | |
| 3676 | /* collect key */ |
| 3677 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3678 | key = trash_key->str; |
| 3679 | key[len] = '\0'; |
| 3680 | |
| 3681 | /* collect value */ |
| 3682 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3683 | value = trash_value->str; |
| 3684 | value[len] = '\0'; |
| 3685 | |
| 3686 | /* perform update */ |
| 3687 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3688 | /* update entry if it exists */ |
| 3689 | pat_ref_set(ref, key, value, NULL); |
| 3690 | else |
| 3691 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3692 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3693 | |
| 3694 | break; |
| 3695 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3696 | |
| 3697 | case HTTP_RES_ACT_CUSTOM_CONT: |
| 3698 | rule->action_ptr(rule, px, s, txn); |
| 3699 | break; |
| 3700 | |
| 3701 | case HTTP_RES_ACT_CUSTOM_STOP: |
| 3702 | rule->action_ptr(rule, px, s, txn); |
| 3703 | return rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3704 | } |
| 3705 | } |
| 3706 | |
| 3707 | /* we reached the end of the rules, nothing to report */ |
| 3708 | return NULL; |
| 3709 | } |
| 3710 | |
| 3711 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3712 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3713 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3714 | * as too large a request to build a valid response. |
| 3715 | */ |
| 3716 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn) |
| 3717 | { |
| 3718 | struct http_msg *msg = &txn->req; |
| 3719 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3720 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3721 | |
| 3722 | /* build redirect message */ |
| 3723 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3724 | case 308: |
| 3725 | msg_fmt = HTTP_308; |
| 3726 | break; |
| 3727 | case 307: |
| 3728 | msg_fmt = HTTP_307; |
| 3729 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3730 | case 303: |
| 3731 | msg_fmt = HTTP_303; |
| 3732 | break; |
| 3733 | case 301: |
| 3734 | msg_fmt = HTTP_301; |
| 3735 | break; |
| 3736 | case 302: |
| 3737 | default: |
| 3738 | msg_fmt = HTTP_302; |
| 3739 | break; |
| 3740 | } |
| 3741 | |
| 3742 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3743 | return 0; |
| 3744 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3745 | location = trash.str + trash.len; |
| 3746 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3747 | switch(rule->type) { |
| 3748 | case REDIRECT_TYPE_SCHEME: { |
| 3749 | const char *path; |
| 3750 | const char *host; |
| 3751 | struct hdr_ctx ctx; |
| 3752 | int pathlen; |
| 3753 | int hostlen; |
| 3754 | |
| 3755 | host = ""; |
| 3756 | hostlen = 0; |
| 3757 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3758 | 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] | 3759 | host = ctx.line + ctx.val; |
| 3760 | hostlen = ctx.vlen; |
| 3761 | } |
| 3762 | |
| 3763 | path = http_get_path(txn); |
| 3764 | /* build message using path */ |
| 3765 | if (path) { |
| 3766 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3767 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3768 | int qs = 0; |
| 3769 | while (qs < pathlen) { |
| 3770 | if (path[qs] == '?') { |
| 3771 | pathlen = qs; |
| 3772 | break; |
| 3773 | } |
| 3774 | qs++; |
| 3775 | } |
| 3776 | } |
| 3777 | } else { |
| 3778 | path = "/"; |
| 3779 | pathlen = 1; |
| 3780 | } |
| 3781 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3782 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3783 | /* check if we can add scheme + "://" + host + path */ |
| 3784 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3785 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3786 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3787 | /* add scheme */ |
| 3788 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3789 | trash.len += rule->rdr_len; |
| 3790 | } |
| 3791 | else { |
| 3792 | /* add scheme with executing log format */ |
| 3793 | 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] | 3794 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3795 | /* check if we can add scheme + "://" + host + path */ |
| 3796 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 3797 | return 0; |
| 3798 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3799 | /* add "://" */ |
| 3800 | memcpy(trash.str + trash.len, "://", 3); |
| 3801 | trash.len += 3; |
| 3802 | |
| 3803 | /* add host */ |
| 3804 | memcpy(trash.str + trash.len, host, hostlen); |
| 3805 | trash.len += hostlen; |
| 3806 | |
| 3807 | /* add path */ |
| 3808 | memcpy(trash.str + trash.len, path, pathlen); |
| 3809 | trash.len += pathlen; |
| 3810 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3811 | /* 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] | 3812 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3813 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3814 | if (trash.len > trash.size - 5) |
| 3815 | return 0; |
| 3816 | trash.str[trash.len] = '/'; |
| 3817 | trash.len++; |
| 3818 | } |
| 3819 | |
| 3820 | break; |
| 3821 | } |
| 3822 | case REDIRECT_TYPE_PREFIX: { |
| 3823 | const char *path; |
| 3824 | int pathlen; |
| 3825 | |
| 3826 | path = http_get_path(txn); |
| 3827 | /* build message using path */ |
| 3828 | if (path) { |
| 3829 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3830 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3831 | int qs = 0; |
| 3832 | while (qs < pathlen) { |
| 3833 | if (path[qs] == '?') { |
| 3834 | pathlen = qs; |
| 3835 | break; |
| 3836 | } |
| 3837 | qs++; |
| 3838 | } |
| 3839 | } |
| 3840 | } else { |
| 3841 | path = "/"; |
| 3842 | pathlen = 1; |
| 3843 | } |
| 3844 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3845 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3846 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 3847 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3848 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3849 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3850 | * add anything, otherwise it makes it hard for the user to |
| 3851 | * configure a self-redirection. |
| 3852 | */ |
| 3853 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 3854 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3855 | trash.len += rule->rdr_len; |
| 3856 | } |
| 3857 | } |
| 3858 | else { |
| 3859 | /* add prefix with executing log format */ |
| 3860 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 3861 | |
| 3862 | /* Check length */ |
| 3863 | if (trash.len + pathlen > trash.size - 4) |
| 3864 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | /* add path */ |
| 3868 | memcpy(trash.str + trash.len, path, pathlen); |
| 3869 | trash.len += pathlen; |
| 3870 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3871 | /* 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] | 3872 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3873 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3874 | if (trash.len > trash.size - 5) |
| 3875 | return 0; |
| 3876 | trash.str[trash.len] = '/'; |
| 3877 | trash.len++; |
| 3878 | } |
| 3879 | |
| 3880 | break; |
| 3881 | } |
| 3882 | case REDIRECT_TYPE_LOCATION: |
| 3883 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3884 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3885 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 3886 | return 0; |
| 3887 | |
| 3888 | /* add location */ |
| 3889 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3890 | trash.len += rule->rdr_len; |
| 3891 | } |
| 3892 | else { |
| 3893 | /* add location with executing log format */ |
| 3894 | 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] | 3895 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3896 | /* Check left length */ |
| 3897 | if (trash.len > trash.size - 4) |
| 3898 | return 0; |
| 3899 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3900 | break; |
| 3901 | } |
| 3902 | |
| 3903 | if (rule->cookie_len) { |
| 3904 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 3905 | trash.len += 14; |
| 3906 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 3907 | trash.len += rule->cookie_len; |
| 3908 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 3909 | trash.len += 2; |
| 3910 | } |
| 3911 | |
| 3912 | /* add end of headers and the keep-alive/close status. |
| 3913 | * We may choose to set keep-alive if the Location begins |
| 3914 | * with a slash, because the client will come back to the |
| 3915 | * same server. |
| 3916 | */ |
| 3917 | txn->status = rule->code; |
| 3918 | /* let's log the request time */ |
| 3919 | s->logs.tv_request = now; |
| 3920 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3921 | if (*location == '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3922 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3923 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
| 3924 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3925 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3926 | /* keep-alive possible */ |
| 3927 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
| 3928 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3929 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3930 | trash.len += 30; |
| 3931 | } else { |
| 3932 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 3933 | trash.len += 24; |
| 3934 | } |
| 3935 | } |
| 3936 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 3937 | trash.len += 4; |
| 3938 | bo_inject(txn->rsp.chn, trash.str, trash.len); |
| 3939 | /* "eat" the request */ |
| 3940 | bi_fast_delete(txn->req.chn->buf, msg->sov); |
Willy Tarreau | 6d8bac7 | 2014-04-25 12:19:32 +0200 | [diff] [blame] | 3941 | msg->next -= msg->sov; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3942 | msg->sov = 0; |
| 3943 | txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY; |
| 3944 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3945 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3946 | txn->rsp.msg_state = HTTP_MSG_DONE; |
| 3947 | } else { |
| 3948 | /* keep-alive not possible */ |
| 3949 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3950 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3951 | trash.len += 29; |
| 3952 | } else { |
| 3953 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3954 | trash.len += 23; |
| 3955 | } |
| 3956 | stream_int_retnclose(txn->req.chn->prod, &trash); |
| 3957 | txn->req.chn->analysers = 0; |
| 3958 | } |
| 3959 | |
| 3960 | if (!(s->flags & SN_ERR_MASK)) |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 3961 | s->flags |= SN_ERR_LOCAL; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3962 | if (!(s->flags & SN_FINST_MASK)) |
| 3963 | s->flags |= SN_FINST_R; |
| 3964 | |
| 3965 | return 1; |
| 3966 | } |
| 3967 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3968 | /* This stream analyser runs all HTTP request processing which is common to |
| 3969 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3970 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3971 | * 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] | 3972 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3973 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3974 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3975 | 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] | 3976 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3977 | struct http_txn *txn = &s->txn; |
| 3978 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3979 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3980 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3981 | enum rule_result verdict; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3982 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3983 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3984 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3985 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3986 | return 0; |
| 3987 | } |
| 3988 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3989 | 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] | 3990 | now_ms, __FUNCTION__, |
| 3991 | s, |
| 3992 | req, |
| 3993 | req->rex, req->wex, |
| 3994 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3995 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3996 | req->analysers); |
| 3997 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 3998 | /* just in case we have some per-backend tracking */ |
| 3999 | session_inc_be_http_req_ctr(s); |
| 4000 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4001 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4002 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
| 4003 | 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] | 4004 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4005 | switch (verdict) { |
| 4006 | case HTTP_RULE_RES_CONT: |
| 4007 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4008 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4009 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4010 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4011 | if (txn->flags & TX_CLTARPIT) |
| 4012 | goto tarpit; |
| 4013 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4014 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4015 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4016 | goto return_prx_cond; |
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_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4019 | goto done; |
| 4020 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4021 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4022 | goto return_bad_req; |
| 4023 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4024 | } |
| 4025 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4026 | /* OK at this stage, we know that the request was accepted according to |
| 4027 | * the http-request rules, we can check for the stats. Note that the |
| 4028 | * URI is detected *before* the req* rules in order not to be affected |
| 4029 | * by a possible reqrep, while they are processed *after* so that a |
| 4030 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4031 | */ |
| 4032 | if (stats_check_uri(s->rep->prod, txn, px)) { |
| 4033 | s->target = &http_stats_applet.obj_type; |
| 4034 | if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) { |
| 4035 | txn->status = 500; |
| 4036 | s->logs.tv_request = now; |
| 4037 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 4038 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4039 | if (!(s->flags & SN_ERR_MASK)) |
| 4040 | s->flags |= SN_ERR_RESOURCE; |
| 4041 | goto return_prx_cond; |
| 4042 | } |
| 4043 | |
| 4044 | /* parse the whole stats request and extract the relevant information */ |
| 4045 | http_handle_stats(s, req); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4046 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn); |
| 4047 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4048 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4049 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4050 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4051 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4052 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4053 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4054 | } |
| 4055 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4056 | /* evaluate the req* rules except reqadd */ |
| 4057 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4058 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4059 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4060 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4061 | if (txn->flags & TX_CLDENY) |
| 4062 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4063 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4064 | if (txn->flags & TX_CLTARPIT) |
| 4065 | goto tarpit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4066 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4067 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4068 | /* add request headers from the rule sets in the same order */ |
| 4069 | list_for_each_entry(wl, &px->req_add, list) { |
| 4070 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 4071 | 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] | 4072 | ret = acl_pass(ret); |
| 4073 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4074 | ret = !ret; |
| 4075 | if (!ret) |
| 4076 | continue; |
| 4077 | } |
| 4078 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4079 | 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] | 4080 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4081 | } |
| 4082 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4083 | |
| 4084 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4085 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4086 | /* process the stats request now */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4087 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4088 | s->fe->fe_counters.intercepted_req++; |
| 4089 | |
| 4090 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 4091 | s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy |
| 4092 | if (!(s->flags & SN_FINST_MASK)) |
| 4093 | s->flags |= SN_FINST_R; |
| 4094 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4095 | /* we may want to compress the stats page */ |
| 4096 | if (s->fe->comp || s->be->comp) |
| 4097 | select_compression_request_header(s, req->buf); |
| 4098 | |
| 4099 | /* 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] | 4100 | 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] | 4101 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4102 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4103 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4104 | /* check whether we have some ACLs set to redirect this request */ |
| 4105 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4106 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4107 | int ret; |
| 4108 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 4109 | 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] | 4110 | ret = acl_pass(ret); |
| 4111 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4112 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4113 | if (!ret) |
| 4114 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4115 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4116 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4117 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4118 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4119 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4120 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4121 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4122 | * If this happens, then the data will not come immediately, so we must |
| 4123 | * send all what we have without waiting. Note that due to the small gain |
| 4124 | * 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] | 4125 | * 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] | 4126 | * itself once used. |
| 4127 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4128 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4129 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4130 | done: /* done with this analyser, continue with next ones that the calling |
| 4131 | * points will have set, if any. |
| 4132 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4133 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | fc566b5 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4134 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4135 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4136 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4137 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4138 | tarpit: |
| 4139 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4140 | * which may be the same as the connect timeout if unspecified. |
| 4141 | * If unset, then set it to zero because we really want it to |
| 4142 | * eventually expire. We build the tarpit as an analyser. |
| 4143 | */ |
| 4144 | channel_erase(s->req); |
| 4145 | |
| 4146 | /* wipe the request out so that we can drop the connection early |
| 4147 | * if the client closes first. |
| 4148 | */ |
| 4149 | channel_dont_connect(req); |
| 4150 | req->analysers = 0; /* remove switching rules etc... */ |
| 4151 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4152 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4153 | if (!req->analyse_exp) |
| 4154 | req->analyse_exp = tick_add(now_ms, 0); |
| 4155 | session_inc_http_err_ctr(s); |
| 4156 | s->fe->fe_counters.denied_req++; |
| 4157 | if (s->fe != s->be) |
| 4158 | s->be->be_counters.denied_req++; |
| 4159 | if (s->listener->counters) |
| 4160 | s->listener->counters->denied_req++; |
Thierry FOURNIER | fc566b5 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4161 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4162 | |
| 4163 | deny: /* this request was blocked (denied) */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4164 | txn->flags |= TX_CLDENY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4165 | txn->status = 403; |
| 4166 | s->logs.tv_request = now; |
| 4167 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
| 4168 | session_inc_http_err_ctr(s); |
| 4169 | s->fe->fe_counters.denied_req++; |
| 4170 | if (s->fe != s->be) |
| 4171 | s->be->be_counters.denied_req++; |
| 4172 | if (s->listener->counters) |
| 4173 | s->listener->counters->denied_req++; |
| 4174 | goto return_prx_cond; |
| 4175 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4176 | return_bad_req: |
| 4177 | /* We centralize bad requests processing here */ |
| 4178 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4179 | /* we detected a parsing error. We want to archive this request |
| 4180 | * in the dedicated proxy area for later troubleshooting. |
| 4181 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4182 | 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] | 4183 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4184 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4185 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4186 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4187 | 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] | 4188 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4189 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4190 | if (s->listener->counters) |
| 4191 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4192 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4193 | return_prx_cond: |
| 4194 | if (!(s->flags & SN_ERR_MASK)) |
| 4195 | s->flags |= SN_ERR_PRXCOND; |
| 4196 | if (!(s->flags & SN_FINST_MASK)) |
| 4197 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4198 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4199 | req->analysers = 0; |
| 4200 | req->analyse_exp = TICK_ETERNITY; |
| 4201 | return 0; |
| 4202 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4203 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4204 | /* This function performs all the processing enabled for the current request. |
| 4205 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4206 | * needs more data, encounters an error, or wants to immediately abort the |
| 4207 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 4208 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4209 | 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] | 4210 | { |
| 4211 | struct http_txn *txn = &s->txn; |
| 4212 | struct http_msg *msg = &txn->req; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4213 | struct connection *cli_conn = objt_conn(req->prod->end); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4214 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4215 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4216 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4217 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4218 | return 0; |
| 4219 | } |
| 4220 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4221 | 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] | 4222 | now_ms, __FUNCTION__, |
| 4223 | s, |
| 4224 | req, |
| 4225 | req->rex, req->wex, |
| 4226 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4227 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4228 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4229 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 4230 | if (s->fe->comp || s->be->comp) |
| 4231 | select_compression_request_header(s, req->buf); |
| 4232 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4233 | /* |
| 4234 | * Right now, we know that we have processed the entire headers |
| 4235 | * and that unwanted requests have been filtered out. We can do |
| 4236 | * whatever we want with the remaining request. Also, now we |
| 4237 | * may have separate values for ->fe, ->be. |
| 4238 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4239 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4240 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4241 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4242 | * incoming request. Note that this requires that a connection is |
| 4243 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4244 | */ |
| 4245 | 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] | 4246 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4247 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4248 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4249 | /* Note that for now we don't reuse existing proxy connections */ |
| 4250 | if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4251 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4252 | txn->status = 500; |
| 4253 | req->analysers = 0; |
| 4254 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
| 4255 | |
| 4256 | if (!(s->flags & SN_ERR_MASK)) |
| 4257 | s->flags |= SN_ERR_RESOURCE; |
| 4258 | if (!(s->flags & SN_FINST_MASK)) |
| 4259 | s->flags |= SN_FINST_R; |
| 4260 | |
| 4261 | return 0; |
| 4262 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4263 | |
| 4264 | path = http_get_path(txn); |
| 4265 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4266 | 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] | 4267 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4268 | /* if the path was found, we have to remove everything between |
| 4269 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4270 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4271 | * u_l characters by a single "/". |
| 4272 | */ |
| 4273 | if (path) { |
| 4274 | char *cur_ptr = req->buf->p; |
| 4275 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4276 | int delta; |
| 4277 | |
| 4278 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4279 | http_msg_move_end(&txn->req, delta); |
| 4280 | cur_end += delta; |
| 4281 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4282 | goto return_bad_req; |
| 4283 | } |
| 4284 | else { |
| 4285 | char *cur_ptr = req->buf->p; |
| 4286 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4287 | int delta; |
| 4288 | |
| 4289 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4290 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4291 | http_msg_move_end(&txn->req, delta); |
| 4292 | cur_end += delta; |
| 4293 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4294 | goto return_bad_req; |
| 4295 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4296 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4297 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4298 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4299 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4300 | * Note that doing so might move headers in the request, but |
| 4301 | * the fields will stay coherent and the URI will not move. |
| 4302 | * This should only be performed in the backend. |
| 4303 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 4304 | 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] | 4305 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 4306 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4307 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4308 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4309 | * 8: the appsession cookie was looked up very early in 1.2, |
| 4310 | * so let's do the same now. |
| 4311 | */ |
| 4312 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4313 | /* It needs to look into the URI unless persistence must be ignored */ |
| 4314 | 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] | 4315 | 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] | 4316 | } |
| 4317 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4318 | /* add unique-id if "header-unique-id" is specified */ |
| 4319 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4320 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 4321 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4322 | goto return_bad_req; |
| 4323 | s->unique_id[0] = '\0'; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4324 | 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] | 4325 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4326 | |
| 4327 | if (s->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4328 | chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 4329 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4330 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4331 | 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] | 4332 | goto return_bad_req; |
| 4333 | } |
| 4334 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4335 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4336 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4337 | * asks for it. |
| 4338 | */ |
| 4339 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4340 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4341 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Cyril Bonté | a32d275 | 2012-05-29 23:27:41 +0200 | [diff] [blame] | 4342 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name, |
| 4343 | 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] | 4344 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4345 | /* The header is set to be added only if none is present |
| 4346 | * and we found it, so don't do anything. |
| 4347 | */ |
| 4348 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4349 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4350 | /* Add an X-Forwarded-For header unless the source IP is |
| 4351 | * in the 'except' network range. |
| 4352 | */ |
| 4353 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4354 | (((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] | 4355 | != s->fe->except_net.s_addr) && |
| 4356 | (!s->be->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->be->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4358 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4359 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4360 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4361 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4362 | |
| 4363 | /* Note: we rely on the backend to get the header name to be used for |
| 4364 | * x-forwarded-for, because the header is really meant for the backends. |
| 4365 | * However, if the backend did not specify any option, we have to rely |
| 4366 | * on the frontend's header name. |
| 4367 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4368 | if (s->be->fwdfor_hdr_len) { |
| 4369 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4370 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4371 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4372 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4373 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4374 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4375 | 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] | 4376 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4377 | 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] | 4378 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4379 | } |
| 4380 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4381 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4382 | /* FIXME: for the sake of completeness, we should also support |
| 4383 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4384 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4385 | int len; |
| 4386 | char pn[INET6_ADDRSTRLEN]; |
| 4387 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4388 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4389 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4390 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4391 | /* Note: we rely on the backend to get the header name to be used for |
| 4392 | * x-forwarded-for, because the header is really meant for the backends. |
| 4393 | * However, if the backend did not specify any option, we have to rely |
| 4394 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4395 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4396 | if (s->be->fwdfor_hdr_len) { |
| 4397 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4398 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4399 | } else { |
| 4400 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4401 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4402 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4403 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4404 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4405 | 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] | 4406 | goto return_bad_req; |
| 4407 | } |
| 4408 | } |
| 4409 | |
| 4410 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4411 | * 10: add X-Original-To if either the frontend or the backend |
| 4412 | * asks for it. |
| 4413 | */ |
| 4414 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 4415 | |
| 4416 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4417 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4418 | /* Add an X-Original-To header unless the destination IP is |
| 4419 | * in the 'except' network range. |
| 4420 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4421 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4422 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4423 | if (cli_conn->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4424 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4425 | (((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] | 4426 | != s->fe->except_to.s_addr) && |
| 4427 | (!s->be->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->be->except_mask_to.s_addr) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4429 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4430 | int len; |
| 4431 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4432 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4433 | |
| 4434 | /* Note: we rely on the backend to get the header name to be used for |
| 4435 | * x-original-to, because the header is really meant for the backends. |
| 4436 | * However, if the backend did not specify any option, we have to rely |
| 4437 | * on the frontend's header name. |
| 4438 | */ |
| 4439 | if (s->be->orgto_hdr_len) { |
| 4440 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4441 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4442 | } else { |
| 4443 | len = s->fe->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4444 | memcpy(trash.str, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4445 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4446 | 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] | 4447 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4448 | 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] | 4449 | goto return_bad_req; |
| 4450 | } |
| 4451 | } |
| 4452 | } |
| 4453 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4454 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4455 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4456 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4457 | * "Upgrade" is present in the Connection header. |
| 4458 | */ |
| 4459 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4460 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4461 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4462 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4463 | unsigned int want_flags = 0; |
| 4464 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4465 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4466 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4467 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4468 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4469 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4470 | want_flags |= TX_CON_CLO_SET; |
| 4471 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4472 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4473 | ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
| 4474 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4475 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4476 | want_flags |= TX_CON_KAL_SET; |
| 4477 | } |
| 4478 | |
| 4479 | 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] | 4480 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4481 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4482 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4483 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4484 | /* If we have no server assigned yet and we're balancing on url_param |
| 4485 | * with a POST request, we may be interested in checking the body for |
| 4486 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4487 | */ |
| 4488 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 4489 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4490 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4491 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4492 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4493 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4494 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4495 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4496 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4497 | #ifdef TCP_QUICKACK |
| 4498 | /* We expect some data from the client. Unless we know for sure |
| 4499 | * we already have a full request, we have to re-enable quick-ack |
| 4500 | * in case we previously disabled it, otherwise we might cause |
| 4501 | * the client to delay further data. |
| 4502 | */ |
| 4503 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4504 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4505 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4506 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4507 | 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] | 4508 | #endif |
| 4509 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4510 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4511 | /************************************************************* |
| 4512 | * OK, that's finished for the headers. We have done what we * |
| 4513 | * could. Let's switch to the DATA state. * |
| 4514 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4515 | req->analyse_exp = TICK_ETERNITY; |
| 4516 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4517 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4518 | /* if the server closes the connection, we want to immediately react |
| 4519 | * and close the socket to save packets and syscalls. |
| 4520 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4521 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
| 4522 | req->cons->flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4523 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4524 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4525 | /* OK let's go on with the BODY now */ |
| 4526 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4527 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4528 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4529 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4530 | /* we detected a parsing error. We want to archive this request |
| 4531 | * in the dedicated proxy area for later troubleshooting. |
| 4532 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4533 | 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] | 4534 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4535 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4536 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4537 | txn->status = 400; |
| 4538 | req->analysers = 0; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4539 | 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] | 4540 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4541 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4542 | if (s->listener->counters) |
| 4543 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4544 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4545 | if (!(s->flags & SN_ERR_MASK)) |
| 4546 | s->flags |= SN_ERR_PRXCOND; |
| 4547 | if (!(s->flags & SN_FINST_MASK)) |
| 4548 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4549 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4550 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4551 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4552 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4553 | * returns zero, at the beginning because it prevents any other processing |
| 4554 | * from occurring, and at the end because it terminates the request. |
| 4555 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4556 | 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] | 4557 | { |
| 4558 | struct http_txn *txn = &s->txn; |
| 4559 | |
| 4560 | /* This connection is being tarpitted. The CLIENT side has |
| 4561 | * already set the connect expiration date to the right |
| 4562 | * timeout. We just have to check that the client is still |
| 4563 | * there and that the timeout has not expired. |
| 4564 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4565 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4566 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4567 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4568 | return 0; |
| 4569 | |
| 4570 | /* We will set the queue timer to the time spent, just for |
| 4571 | * logging purposes. We fake a 500 server error, so that the |
| 4572 | * attacker will not suspect his connection has been tarpitted. |
| 4573 | * It will not cause trouble to the logs because we can exclude |
| 4574 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4575 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4576 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4577 | |
| 4578 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4579 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4580 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4581 | |
| 4582 | req->analysers = 0; |
| 4583 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4584 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4585 | if (!(s->flags & SN_ERR_MASK)) |
| 4586 | s->flags |= SN_ERR_PRXCOND; |
| 4587 | if (!(s->flags & SN_FINST_MASK)) |
| 4588 | s->flags |= SN_FINST_T; |
| 4589 | return 0; |
| 4590 | } |
| 4591 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4592 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4593 | * for either the buffer to be full, or the full advertised contents to have |
| 4594 | * reached the buffer. It must only be called after the standard HTTP request |
| 4595 | * processing has occurred, because it expects the request to be parsed and will |
| 4596 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4597 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4598 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4599 | * 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] | 4600 | */ |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4601 | 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] | 4602 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4603 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4604 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4605 | |
| 4606 | /* We have to parse the HTTP request body to find any required data. |
| 4607 | * "balance url_param check_post" should have been the only way to get |
| 4608 | * into this. We were brought here after HTTP header analysis, so all |
| 4609 | * related structures are ready. |
| 4610 | */ |
| 4611 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4612 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4613 | /* This is the first call */ |
| 4614 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4615 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4616 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4617 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4618 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4619 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4620 | */ |
| 4621 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4622 | struct hdr_ctx ctx; |
| 4623 | ctx.idx = 0; |
| 4624 | /* Expect is allowed in 1.1, look for it */ |
| 4625 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4626 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 4627 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 4628 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4629 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4630 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4631 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4632 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4633 | /* 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] | 4634 | * req->buf->p still points to the beginning of the message. We |
| 4635 | * must save the body in msg->next because it survives buffer |
| 4636 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4637 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4638 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4639 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4640 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4641 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4642 | else |
| 4643 | msg->msg_state = HTTP_MSG_DATA; |
| 4644 | } |
| 4645 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4646 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4647 | /* 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] | 4648 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4649 | goto missing_data; |
| 4650 | |
| 4651 | /* OK we have everything we need now */ |
| 4652 | goto http_end; |
| 4653 | } |
| 4654 | |
| 4655 | /* OK here we're parsing a chunked-encoded message */ |
| 4656 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4657 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4658 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4659 | * 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] | 4660 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4661 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4662 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4663 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4664 | if (!ret) |
| 4665 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4666 | else if (ret < 0) { |
| 4667 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4668 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4669 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4670 | } |
| 4671 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4672 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 5b7f924 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4673 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 4674 | * for at least a whole chunk or the whole content length bytes after |
| 4675 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4676 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4677 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4678 | goto http_end; |
| 4679 | |
Willy Tarreau | 5b7f924 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4680 | 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] | 4681 | goto http_end; |
| 4682 | |
| 4683 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4684 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4685 | * we have the maximum we can expect. |
| 4686 | */ |
| 4687 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4688 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4689 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4690 | 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] | 4691 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4692 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4693 | |
| 4694 | if (!(s->flags & SN_ERR_MASK)) |
| 4695 | s->flags |= SN_ERR_CLITO; |
| 4696 | if (!(s->flags & SN_FINST_MASK)) |
| 4697 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4698 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4699 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4700 | |
| 4701 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4702 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4703 | /* Not enough data. We'll re-use the http-request |
| 4704 | * timeout here. Ideally, we should set the timeout |
| 4705 | * relative to the accept() date. We just set the |
| 4706 | * request timeout once at the beginning of the |
| 4707 | * request. |
| 4708 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4709 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4710 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4711 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4712 | return 0; |
| 4713 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4714 | |
| 4715 | http_end: |
| 4716 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4717 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4718 | req->analysers &= ~an_bit; |
| 4719 | req->analyse_exp = TICK_ETERNITY; |
| 4720 | return 1; |
| 4721 | |
| 4722 | return_bad_req: /* let's centralize all bad requests */ |
| 4723 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4724 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4725 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4726 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4727 | if (!(s->flags & SN_ERR_MASK)) |
| 4728 | s->flags |= SN_ERR_PRXCOND; |
| 4729 | if (!(s->flags & SN_FINST_MASK)) |
| 4730 | s->flags |= SN_FINST_R; |
| 4731 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4732 | return_err_msg: |
| 4733 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4734 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4735 | if (s->listener->counters) |
| 4736 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4737 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4738 | } |
| 4739 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4740 | /* send a server's name with an outgoing request over an established connection. |
| 4741 | * Note: this function is designed to be called once the request has been scheduled |
| 4742 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4743 | * proceeding. |
| 4744 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4745 | 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] | 4746 | |
| 4747 | struct hdr_ctx ctx; |
| 4748 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4749 | char *hdr_name = be->server_id_hdr_name; |
| 4750 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4751 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4752 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4753 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4754 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4755 | ctx.idx = 0; |
| 4756 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4757 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4758 | if (old_o) { |
| 4759 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4760 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4761 | txn->req.next += old_o; |
| 4762 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4763 | } |
| 4764 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4765 | old_i = chn->buf->i; |
| 4766 | 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] | 4767 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4768 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4769 | } |
| 4770 | |
| 4771 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4772 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4773 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4774 | hdr_val += hdr_name_len; |
| 4775 | *hdr_val++ = ':'; |
| 4776 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4777 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4778 | 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] | 4779 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4780 | if (old_o) { |
| 4781 | /* If this was a forwarded request, we must readjust the amount of |
| 4782 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4783 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4784 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4785 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4786 | old_o += chn->buf->i - old_i; |
| 4787 | b_adv(chn->buf, old_o); |
| 4788 | txn->req.next -= old_o; |
| 4789 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4790 | } |
| 4791 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4792 | return 0; |
| 4793 | } |
| 4794 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4795 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4796 | * right now but it works. |
| 4797 | */ |
| 4798 | void http_end_txn_clean_session(struct session *s) |
| 4799 | { |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4800 | int prev_status = s->txn.status; |
| 4801 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4802 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4803 | * server's connections. We need a safer way to reinitialize buffer |
| 4804 | * flags. We also need a more accurate method for computing per-request |
| 4805 | * data. |
| 4806 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4807 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4808 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4809 | * to the server. |
| 4810 | */ |
| 4811 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4812 | !si_conn_ready(s->req->cons)) { |
| 4813 | s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4814 | si_shutr(s->req->cons); |
| 4815 | si_shutw(s->req->cons); |
| 4816 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4817 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4818 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4819 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4820 | if (unlikely(s->srv_conn)) |
| 4821 | sess_change_server(s, NULL); |
| 4822 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4823 | |
| 4824 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4825 | session_process_counters(s); |
| 4826 | |
| 4827 | if (s->txn.status) { |
| 4828 | int n; |
| 4829 | |
| 4830 | n = s->txn.status / 100; |
| 4831 | if (n < 1 || n > 5) |
| 4832 | n = 0; |
| 4833 | |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4834 | if (s->fe->mode == PR_MODE_HTTP) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4835 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4836 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4837 | s->fe->fe_counters.p.http.comp_rsp++; |
| 4838 | } |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 4839 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4840 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4841 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4842 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4843 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4844 | s->be->be_counters.p.http.comp_rsp++; |
| 4845 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4846 | } |
| 4847 | |
| 4848 | /* don't count other requests' data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4849 | s->logs.bytes_in -= s->req->buf->i; |
| 4850 | s->logs.bytes_out -= s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4851 | |
| 4852 | /* let's do a final log if we need it */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 4853 | if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4854 | !(s->flags & SN_MONITOR) && |
| 4855 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 4856 | s->do_log(s); |
| 4857 | } |
| 4858 | |
Willy Tarreau | c177ea7 | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 4859 | /* stop tracking content-based counters */ |
| 4860 | session_stop_content_counters(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 4861 | session_update_time_stats(s); |
| 4862 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4863 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4864 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4865 | tv_zero(&s->logs.tv_request); |
| 4866 | s->logs.t_queue = -1; |
| 4867 | s->logs.t_connect = -1; |
| 4868 | s->logs.t_data = -1; |
| 4869 | s->logs.t_close = 0; |
| 4870 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4871 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4872 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4873 | s->logs.bytes_in = s->req->total = s->req->buf->i; |
| 4874 | s->logs.bytes_out = s->rep->total = s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4875 | |
| 4876 | if (s->pend_pos) |
| 4877 | pendconn_free(s->pend_pos); |
| 4878 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4879 | if (objt_server(s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4880 | if (s->flags & SN_CURR_SESS) { |
| 4881 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4882 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4883 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4884 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 4885 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4886 | } |
| 4887 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4888 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4889 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4890 | /* only release our endpoint if we don't intend to reuse the |
| 4891 | * connection. |
| 4892 | */ |
| 4893 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4894 | !si_conn_ready(s->req->cons)) { |
| 4895 | si_release_endpoint(s->req->cons); |
| 4896 | } |
| 4897 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4898 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4899 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 4900 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4901 | s->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | c920096 | 2013-12-31 23:03:09 +0100 | [diff] [blame] | 4902 | 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] | 4903 | 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); |
| 4904 | 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] | 4905 | 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] | 4906 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED); |
Cyril Bonté | 17f7707 | 2014-10-22 22:30:13 +0200 | [diff] [blame] | 4907 | s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4908 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4909 | s->txn.meth = 0; |
| 4910 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4911 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4912 | |
| 4913 | if (prev_status == 401 || prev_status == 407) { |
| 4914 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 4915 | * a chance of being able to send the visitor again to the same |
| 4916 | * server over the same connection. This is required by some |
| 4917 | * broken protocols such as NTLM, and anyway whenever there is |
| 4918 | * an opportunity for sending the challenge to the proper place, |
| 4919 | * it's better to do it (at least it helps with debugging). |
| 4920 | */ |
| 4921 | s->txn.flags |= TX_PREFER_LAST; |
| 4922 | } |
| 4923 | |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 4924 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4925 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 4926 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4927 | if (s->fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4928 | s->req->flags |= CF_NEVER_WAIT; |
| 4929 | s->rep->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4930 | } |
| 4931 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4932 | /* if the request buffer is not empty, it means we're |
| 4933 | * about to process another request, so send pending |
| 4934 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4935 | * Just don't do this if the buffer is close to be full, |
| 4936 | * because the request will wait for it to flush a little |
| 4937 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4938 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4939 | if (s->req->buf->i) { |
| 4940 | if (s->rep->buf->o && |
| 4941 | !buffer_full(s->rep->buf, global.tune.maxrewrite) && |
| 4942 | 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] | 4943 | s->rep->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4944 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4945 | |
| 4946 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4947 | channel_auto_read(s->req); |
| 4948 | channel_auto_close(s->req); |
| 4949 | channel_auto_read(s->rep); |
| 4950 | channel_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4951 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 4952 | /* we're in keep-alive with an idle connection, monitor it */ |
| 4953 | si_idle_conn(s->req->cons); |
| 4954 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 4955 | s->req->analysers = s->listener->analysers; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4956 | s->rep->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4957 | } |
| 4958 | |
| 4959 | |
| 4960 | /* This function updates the request state machine according to the response |
| 4961 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4962 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4963 | * it is only used to find when a request/response couple is complete. Both |
| 4964 | * this function and its equivalent should loop until both return zero. It |
| 4965 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4966 | */ |
| 4967 | int http_sync_req_state(struct session *s) |
| 4968 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4969 | struct channel *chn = s->req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4970 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4971 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4972 | unsigned int old_state = txn->req.msg_state; |
| 4973 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4974 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 4975 | return 0; |
| 4976 | |
| 4977 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4978 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4979 | * We can shut the read side unless we want to abort_on_close, |
| 4980 | * or we have a POST request. The issue with POST requests is |
| 4981 | * that some browsers still send a CRLF after the request, and |
| 4982 | * this CRLF must be read so that it does not remain in the kernel |
| 4983 | * buffers, otherwise a close could cause an RST on some systems |
| 4984 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4985 | * Note that if we're using keep-alive on the client side, we'd |
| 4986 | * rather poll now and keep the polling enabled for the whole |
| 4987 | * session's life than enabling/disabling it between each |
| 4988 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4989 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4990 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4991 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4992 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 4993 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4994 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4995 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4996 | /* if the server closes the connection, we want to immediately react |
| 4997 | * and close the socket to save packets and syscalls. |
| 4998 | */ |
| 4999 | chn->cons->flags |= SI_FL_NOHALF; |
| 5000 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5001 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5002 | goto wait_other_side; |
| 5003 | |
| 5004 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5005 | /* The server has not finished to respond, so we |
| 5006 | * don't want to move in order not to upset it. |
| 5007 | */ |
| 5008 | goto wait_other_side; |
| 5009 | } |
| 5010 | |
| 5011 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5012 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5013 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5014 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5015 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5016 | goto wait_other_side; |
| 5017 | } |
| 5018 | |
| 5019 | /* When we get here, it means that both the request and the |
| 5020 | * response have finished receiving. Depending on the connection |
| 5021 | * mode, we'll have to wait for the last bytes to leave in either |
| 5022 | * direction, and sometimes for a close to be effective. |
| 5023 | */ |
| 5024 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5025 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5026 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5027 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5028 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5029 | } |
| 5030 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5031 | /* Option forceclose is set, or either side wants to close, |
| 5032 | * let's enforce it now that we're not expecting any new |
| 5033 | * data to come. The caller knows the session is complete |
| 5034 | * once both states are CLOSED. |
| 5035 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5036 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5037 | channel_shutr_now(chn); |
| 5038 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5039 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5040 | } |
| 5041 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5042 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5043 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5044 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5045 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5046 | channel_auto_read(chn); |
| 5047 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5048 | chn->flags |= CF_NEVER_WAIT; |
| 5049 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5050 | } |
| 5051 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5052 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5053 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5054 | chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5055 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5056 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5057 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5058 | goto http_msg_closing; |
| 5059 | } |
| 5060 | else { |
| 5061 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5062 | goto http_msg_closed; |
| 5063 | } |
| 5064 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5065 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5069 | http_msg_closing: |
| 5070 | /* nothing else to forward, just waiting for the output buffer |
| 5071 | * to be empty and for the shutw_now to take effect. |
| 5072 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5073 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5074 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5075 | goto http_msg_closed; |
| 5076 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5077 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5078 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5079 | goto wait_other_side; |
| 5080 | } |
| 5081 | } |
| 5082 | |
| 5083 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5084 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5085 | /* see above in MSG_DONE why we only do this in these states */ |
| 5086 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5087 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5088 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5089 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5090 | goto wait_other_side; |
| 5091 | } |
| 5092 | |
| 5093 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5094 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | |
| 5098 | /* This function updates the response state machine according to the request |
| 5099 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5100 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5101 | * it is only used to find when a request/response couple is complete. Both |
| 5102 | * this function and its equivalent should loop until both return zero. It |
| 5103 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5104 | */ |
| 5105 | int http_sync_res_state(struct session *s) |
| 5106 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5107 | struct channel *chn = s->rep; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5108 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5109 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5110 | unsigned int old_state = txn->rsp.msg_state; |
| 5111 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5112 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5113 | return 0; |
| 5114 | |
| 5115 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5116 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5117 | * still monitor the server connection for a possible close |
| 5118 | * while the request is being uploaded, so we don't disable |
| 5119 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5120 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5121 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5122 | |
| 5123 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5124 | goto wait_other_side; |
| 5125 | |
| 5126 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5127 | /* The client seems to still be sending data, probably |
| 5128 | * because we got an error response during an upload. |
| 5129 | * We have the choice of either breaking the connection |
| 5130 | * or letting it pass through. Let's do the later. |
| 5131 | */ |
| 5132 | goto wait_other_side; |
| 5133 | } |
| 5134 | |
| 5135 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5136 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5137 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5138 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5139 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5140 | goto wait_other_side; |
| 5141 | } |
| 5142 | |
| 5143 | /* When we get here, it means that both the request and the |
| 5144 | * response have finished receiving. Depending on the connection |
| 5145 | * mode, we'll have to wait for the last bytes to leave in either |
| 5146 | * direction, and sometimes for a close to be effective. |
| 5147 | */ |
| 5148 | |
| 5149 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5150 | /* Server-close mode : shut read and wait for the request |
| 5151 | * side to close its output buffer. The caller will detect |
| 5152 | * when we're in DONE and the other is in CLOSED and will |
| 5153 | * catch that for the final cleanup. |
| 5154 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5155 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5156 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5157 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5158 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5159 | /* Option forceclose is set, or either side wants to close, |
| 5160 | * let's enforce it now that we're not expecting any new |
| 5161 | * data to come. The caller knows the session is complete |
| 5162 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5163 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5164 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5165 | channel_shutr_now(chn); |
| 5166 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5167 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5168 | } |
| 5169 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5170 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5171 | * need to forward remaining data. Keep-alive will need to monitor |
| 5172 | * for connection closing. |
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 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5175 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5176 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5177 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5178 | } |
| 5179 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5180 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5181 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5182 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5183 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5184 | goto http_msg_closing; |
| 5185 | } |
| 5186 | else { |
| 5187 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5188 | goto http_msg_closed; |
| 5189 | } |
| 5190 | } |
| 5191 | goto wait_other_side; |
| 5192 | } |
| 5193 | |
| 5194 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5195 | http_msg_closing: |
| 5196 | /* nothing else to forward, just waiting for the output buffer |
| 5197 | * to be empty and for the shutw_now to take effect. |
| 5198 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5199 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5200 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5201 | goto http_msg_closed; |
| 5202 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5203 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5204 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5205 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5206 | if (objt_server(s->target)) |
| 5207 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5208 | goto wait_other_side; |
| 5209 | } |
| 5210 | } |
| 5211 | |
| 5212 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5213 | http_msg_closed: |
| 5214 | /* drop any pending data */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5215 | bi_erase(chn); |
| 5216 | channel_auto_close(chn); |
| 5217 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5218 | goto wait_other_side; |
| 5219 | } |
| 5220 | |
| 5221 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5222 | /* We force the response to leave immediately if we're waiting for the |
| 5223 | * other side, since there is no pending shutdown to push it out. |
| 5224 | */ |
| 5225 | if (!channel_is_empty(chn)) |
| 5226 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5227 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5228 | } |
| 5229 | |
| 5230 | |
| 5231 | /* Resync the request and response state machines. Return 1 if either state |
| 5232 | * changes. |
| 5233 | */ |
| 5234 | int http_resync_states(struct session *s) |
| 5235 | { |
| 5236 | struct http_txn *txn = &s->txn; |
| 5237 | int old_req_state = txn->req.msg_state; |
| 5238 | int old_res_state = txn->rsp.msg_state; |
| 5239 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5240 | http_sync_req_state(s); |
| 5241 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5242 | if (!http_sync_res_state(s)) |
| 5243 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5244 | if (!http_sync_req_state(s)) |
| 5245 | break; |
| 5246 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5247 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5248 | /* OK, both state machines agree on a compatible state. |
| 5249 | * There are a few cases we're interested in : |
| 5250 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5251 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5252 | * directions, so let's simply disable both analysers. |
| 5253 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5254 | * request. |
| 5255 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5256 | * with server-close mode means we've completed one request and we |
| 5257 | * must re-initialize the server connection. |
| 5258 | */ |
| 5259 | |
| 5260 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5261 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5262 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5263 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 5264 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5265 | channel_auto_close(s->req); |
| 5266 | channel_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5267 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5268 | channel_auto_close(s->rep); |
| 5269 | channel_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5270 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5271 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
| 5272 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5273 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5274 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5275 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5276 | channel_auto_close(s->rep); |
| 5277 | channel_auto_read(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5278 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5279 | channel_abort(s->req); |
| 5280 | channel_auto_close(s->req); |
| 5281 | channel_auto_read(s->req); |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5282 | bi_erase(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5283 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5284 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5285 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5286 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5287 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5288 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5289 | /* server-close/keep-alive: terminate this transaction, |
| 5290 | * possibly killing the server connection and reinitialize |
| 5291 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5292 | */ |
| 5293 | http_end_txn_clean_session(s); |
| 5294 | } |
| 5295 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5296 | return txn->req.msg_state != old_req_state || |
| 5297 | txn->rsp.msg_state != old_res_state; |
| 5298 | } |
| 5299 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5300 | /* This function is an analyser which forwards request body (including chunk |
| 5301 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5302 | * zero byte. The only situation where it must not be called is when we're in |
| 5303 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5304 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5305 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5306 | * 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] | 5307 | * 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] | 5308 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5309 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5310 | 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] | 5311 | { |
| 5312 | struct http_txn *txn = &s->txn; |
| 5313 | struct http_msg *msg = &s->txn.req; |
| 5314 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5315 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5316 | return 0; |
| 5317 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5318 | 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] | 5319 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5320 | /* Output closed while we were sending data. We must abort and |
| 5321 | * wake the other side up. |
| 5322 | */ |
| 5323 | msg->msg_state = HTTP_MSG_ERROR; |
| 5324 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5325 | return 1; |
| 5326 | } |
| 5327 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5328 | /* Note that we don't have to send 100-continue back because we don't |
| 5329 | * need the data to complete our job, and it's up to the server to |
| 5330 | * decide whether to return 100, 417 or anything else in return of |
| 5331 | * an "Expect: 100-continue" header. |
| 5332 | */ |
| 5333 | |
Willy Tarreau | 5bebcd0 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5334 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5335 | /* we have msg->sov which points to the first byte of message |
| 5336 | * body, and req->buf.p still points to the beginning of the |
| 5337 | * message. We forward the headers now, as we don't need them |
| 5338 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5339 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5340 | b_adv(req->buf, msg->sov); |
| 5341 | msg->next -= msg->sov; |
| 5342 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5343 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5344 | /* The previous analysers guarantee that the state is somewhere |
| 5345 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 5346 | * msg->next are always correct. |
| 5347 | */ |
| 5348 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5349 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 5350 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5351 | else |
| 5352 | msg->msg_state = HTTP_MSG_DATA; |
| 5353 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5354 | } |
| 5355 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5356 | /* Some post-connect processing might want us to refrain from starting to |
| 5357 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5358 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5359 | */ |
| 5360 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
| 5361 | if (!(s->rep->flags & CF_READ_ATTACHED)) { |
| 5362 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5363 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5364 | goto missing_data; |
| 5365 | } |
| 5366 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5367 | } |
| 5368 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5369 | /* in most states, we should abort in case of early close */ |
| 5370 | channel_auto_close(req); |
| 5371 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5372 | if (req->to_forward) { |
| 5373 | /* We can't process the buffer's contents yet */ |
| 5374 | req->flags |= CF_WAKE_WRITE; |
| 5375 | goto missing_data; |
| 5376 | } |
| 5377 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5378 | while (1) { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5379 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5380 | /* must still forward */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5381 | /* we may have some pending data starting at req->buf->p */ |
| 5382 | if (msg->chunk_len > req->buf->i - msg->next) { |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5383 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5384 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5385 | } |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5386 | msg->next += msg->chunk_len; |
| 5387 | msg->chunk_len = 0; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5388 | |
| 5389 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5390 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5391 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5392 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5393 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5394 | } |
| 5395 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5396 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 5397 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5398 | * TRAILERS state. |
| 5399 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5400 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5401 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5402 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5403 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5404 | else if (ret < 0) { |
| 5405 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5406 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5407 | 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] | 5408 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5409 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5410 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
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 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5413 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5414 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5415 | |
| 5416 | if (ret == 0) |
| 5417 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5418 | else if (ret < 0) { |
| 5419 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5420 | if (msg->err_pos >= 0) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5421 | 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] | 5422 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5423 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5424 | /* we're in MSG_CHUNK_SIZE now */ |
| 5425 | } |
| 5426 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5427 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5428 | |
| 5429 | if (ret == 0) |
| 5430 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5431 | else if (ret < 0) { |
| 5432 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5433 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5434 | 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] | 5435 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5436 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5437 | /* we're in HTTP_MSG_DONE now */ |
| 5438 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5439 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5440 | int old_state = msg->msg_state; |
| 5441 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5442 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5443 | |
| 5444 | /* we may have some pending data starting at req->buf->p |
| 5445 | * such as last chunk of data or trailers. |
| 5446 | */ |
| 5447 | b_adv(req->buf, msg->next); |
Willy Tarreau | b4d0509 | 2014-09-01 20:35:55 +0200 | [diff] [blame] | 5448 | if (unlikely(!(s->req->flags & CF_WROTE_DATA))) |
Willy Tarreau | 5bebcd0 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5449 | msg->sov -= msg->next; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5450 | msg->next = 0; |
| 5451 | |
Willy Tarreau | 294e467 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5452 | /* we don't want to forward closes on DONE except in |
| 5453 | * tunnel mode. |
| 5454 | */ |
| 5455 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5456 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5457 | if (http_resync_states(s)) { |
| 5458 | /* some state changes occurred, maybe the analyser |
| 5459 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 5460 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5461 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5462 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5463 | /* request errors are most likely due to |
| 5464 | * the server aborting the transfer. |
| 5465 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5466 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5467 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5468 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5469 | 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] | 5470 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5471 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5472 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5473 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5474 | |
| 5475 | /* If "option abortonclose" is set on the backend, we |
| 5476 | * want to monitor the client's connection and forward |
| 5477 | * any shutdown notification to the server, which will |
| 5478 | * decide whether to close or to go on processing the |
Willy Tarreau | 294e467 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5479 | * request. We only do that in tunnel mode, and not in |
| 5480 | * other modes since it can be abused to exhaust source |
| 5481 | * ports. |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5482 | */ |
| 5483 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5484 | channel_auto_read(req); |
Willy Tarreau | 294e467 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5485 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 5486 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 5487 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5488 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5489 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5490 | else if (s->txn.meth == HTTP_METH_POST) { |
| 5491 | /* POST requests may require to read extra CRLF |
| 5492 | * sent by broken browsers and which could cause |
| 5493 | * an RST to be sent upon close on some systems |
| 5494 | * (eg: Linux). |
| 5495 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5496 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5497 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5498 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5499 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5500 | } |
| 5501 | } |
| 5502 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5503 | missing_data: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5504 | /* we may have some pending data starting at req->buf->p */ |
| 5505 | b_adv(req->buf, msg->next); |
Willy Tarreau | b4d0509 | 2014-09-01 20:35:55 +0200 | [diff] [blame] | 5506 | if (unlikely(!(s->req->flags & CF_WROTE_DATA))) |
Willy Tarreau | 5bebcd0 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5507 | msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i); |
| 5508 | |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5509 | msg->next = 0; |
| 5510 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
| 5511 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5512 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5513 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5514 | if (!(s->flags & SN_ERR_MASK)) |
| 5515 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5516 | if (!(s->flags & SN_FINST_MASK)) { |
| 5517 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5518 | s->flags |= SN_FINST_H; |
| 5519 | else |
| 5520 | s->flags |= SN_FINST_D; |
| 5521 | } |
| 5522 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5523 | s->fe->fe_counters.cli_aborts++; |
| 5524 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5525 | if (objt_server(s->target)) |
| 5526 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5527 | |
| 5528 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5529 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5530 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5531 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5532 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5533 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5534 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5535 | /* 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] | 5536 | * 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] | 5537 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5538 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5539 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5540 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5541 | /* 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] | 5542 | * 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] | 5543 | * 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] | 5544 | * modes are already handled by the stream sock layer. We must not do |
| 5545 | * this in content-length mode because it could present the MSG_MORE |
| 5546 | * flag with the last block of forwarded data, which would cause an |
| 5547 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5548 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5549 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5550 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5551 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5552 | return 0; |
| 5553 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5554 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5555 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5556 | if (s->listener->counters) |
| 5557 | s->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5558 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5559 | return_bad_req_stats_ok: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5560 | /* we may have some pending data starting at req->buf->p */ |
| 5561 | b_adv(req->buf, msg->next); |
| 5562 | msg->next = 0; |
| 5563 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5564 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5565 | if (txn->status) { |
| 5566 | /* Note: we don't send any error if some data were already sent */ |
| 5567 | stream_int_retnclose(req->prod, NULL); |
| 5568 | } else { |
| 5569 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5570 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5571 | } |
| 5572 | req->analysers = 0; |
| 5573 | 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] | 5574 | |
| 5575 | if (!(s->flags & SN_ERR_MASK)) |
| 5576 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5577 | if (!(s->flags & SN_FINST_MASK)) { |
| 5578 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5579 | s->flags |= SN_FINST_H; |
| 5580 | else |
| 5581 | s->flags |= SN_FINST_D; |
| 5582 | } |
| 5583 | return 0; |
| 5584 | |
| 5585 | aborted_xfer: |
| 5586 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5587 | if (txn->status) { |
| 5588 | /* Note: we don't send any error if some data were already sent */ |
| 5589 | stream_int_retnclose(req->prod, NULL); |
| 5590 | } else { |
| 5591 | txn->status = 502; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5592 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5593 | } |
| 5594 | req->analysers = 0; |
| 5595 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5596 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5597 | s->fe->fe_counters.srv_aborts++; |
| 5598 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5599 | if (objt_server(s->target)) |
| 5600 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5601 | |
| 5602 | if (!(s->flags & SN_ERR_MASK)) |
| 5603 | s->flags |= SN_ERR_SRVCL; |
| 5604 | if (!(s->flags & SN_FINST_MASK)) { |
| 5605 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5606 | s->flags |= SN_FINST_H; |
| 5607 | else |
| 5608 | s->flags |= SN_FINST_D; |
| 5609 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5610 | return 0; |
| 5611 | } |
| 5612 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5613 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5614 | * processing can continue on next analysers, or zero if it either needs more |
| 5615 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 5616 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 5617 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5618 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5619 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5620 | 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] | 5621 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5622 | struct http_txn *txn = &s->txn; |
| 5623 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5624 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5625 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5626 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5627 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5628 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5629 | 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] | 5630 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5631 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5632 | rep, |
| 5633 | rep->rex, rep->wex, |
| 5634 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5635 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5636 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5637 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5638 | /* |
| 5639 | * Now parse the partial (or complete) lines. |
| 5640 | * We will check the response syntax, and also join multi-line |
| 5641 | * headers. An index of all the lines will be elaborated while |
| 5642 | * parsing. |
| 5643 | * |
| 5644 | * For the parsing, we use a 28 states FSM. |
| 5645 | * |
| 5646 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5647 | * rep->buf->p = beginning of response |
| 5648 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5649 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5650 | * msg->eol = end of current header or line (LF or CRLF) |
| 5651 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5652 | */ |
| 5653 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5654 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5655 | /* There's a protected area at the end of the buffer for rewriting |
| 5656 | * purposes. We don't want to start to parse the request if the |
| 5657 | * protected area is affected, because we may have to move processed |
| 5658 | * data later, which is much more complicated. |
| 5659 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5660 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5661 | if (unlikely(!channel_reserved(rep))) { |
| 5662 | /* some data has still not left the buffer, wake us once that's done */ |
| 5663 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5664 | goto abort_response; |
| 5665 | channel_dont_close(rep); |
| 5666 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5667 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5668 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5669 | } |
| 5670 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5671 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5672 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5673 | buffer_slow_realign(rep->buf); |
| 5674 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5675 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5676 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5677 | } |
| 5678 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5679 | /* 1: we might have to print this header in debug mode */ |
| 5680 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5681 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 8767b13 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5682 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5683 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5684 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5685 | sol = rep->buf->p; |
| 5686 | 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] | 5687 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5688 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5689 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5690 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5691 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5692 | while (cur_idx) { |
| 5693 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5694 | debug_hdr("srvhdr", s, sol, eol); |
| 5695 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5696 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5697 | } |
| 5698 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5699 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5700 | /* |
| 5701 | * Now we quickly check if we have found a full valid response. |
| 5702 | * If not so, we check the FD and buffer states before leaving. |
| 5703 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5704 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5705 | * responses are checked first. |
| 5706 | * |
| 5707 | * Depending on whether the client is still there or not, we |
| 5708 | * may send an error response back or not. Note that normally |
| 5709 | * we should only check for HTTP status there, and check I/O |
| 5710 | * errors somewhere else. |
| 5711 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5712 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5713 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5714 | /* Invalid response */ |
| 5715 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5716 | /* we detected a parsing error. We want to archive this response |
| 5717 | * in the dedicated proxy area for later troubleshooting. |
| 5718 | */ |
| 5719 | hdr_response_bad: |
| 5720 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5721 | 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] | 5722 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5723 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5724 | if (objt_server(s->target)) { |
| 5725 | objt_server(s->target)->counters.failed_resp++; |
| 5726 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5727 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5728 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5729 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5730 | rep->analysers = 0; |
| 5731 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5732 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5733 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5734 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5735 | |
| 5736 | if (!(s->flags & SN_ERR_MASK)) |
| 5737 | s->flags |= SN_ERR_PRXCOND; |
| 5738 | if (!(s->flags & SN_FINST_MASK)) |
| 5739 | s->flags |= SN_FINST_H; |
| 5740 | |
| 5741 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5742 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5743 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5744 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5745 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5746 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5747 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5748 | goto hdr_response_bad; |
| 5749 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5750 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5751 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5752 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5753 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5754 | 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] | 5755 | else if (txn->flags & TX_NOT_FIRST) |
| 5756 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5757 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5758 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5759 | if (objt_server(s->target)) { |
| 5760 | objt_server(s->target)->counters.failed_resp++; |
| 5761 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5762 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5763 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5764 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5765 | rep->analysers = 0; |
| 5766 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5767 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5768 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5769 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5770 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5771 | if (!(s->flags & SN_ERR_MASK)) |
| 5772 | s->flags |= SN_ERR_SRVCL; |
| 5773 | if (!(s->flags & SN_FINST_MASK)) |
| 5774 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5775 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5776 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5777 | |
Willy Tarreau | 2a4f511 | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 5778 | /* read timeout : return a 504 to the client. */ |
| 5779 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5780 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5781 | 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] | 5782 | else if (txn->flags & TX_NOT_FIRST) |
| 5783 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5784 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5785 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5786 | if (objt_server(s->target)) { |
| 5787 | objt_server(s->target)->counters.failed_resp++; |
| 5788 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5789 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5790 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5791 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5792 | rep->analysers = 0; |
| 5793 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5794 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5795 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5796 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5797 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5798 | if (!(s->flags & SN_ERR_MASK)) |
| 5799 | s->flags |= SN_ERR_SRVTO; |
| 5800 | if (!(s->flags & SN_FINST_MASK)) |
| 5801 | s->flags |= SN_FINST_H; |
| 5802 | return 0; |
| 5803 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5804 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5805 | /* client abort with an abortonclose */ |
| 5806 | else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
| 5807 | s->fe->fe_counters.cli_aborts++; |
| 5808 | s->be->be_counters.cli_aborts++; |
| 5809 | if (objt_server(s->target)) |
| 5810 | objt_server(s->target)->counters.cli_aborts++; |
| 5811 | |
| 5812 | rep->analysers = 0; |
| 5813 | channel_auto_close(rep); |
| 5814 | |
| 5815 | txn->status = 400; |
| 5816 | bi_erase(rep); |
| 5817 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400)); |
| 5818 | |
| 5819 | if (!(s->flags & SN_ERR_MASK)) |
| 5820 | s->flags |= SN_ERR_CLICL; |
| 5821 | if (!(s->flags & SN_FINST_MASK)) |
| 5822 | s->flags |= SN_FINST_H; |
| 5823 | |
| 5824 | /* process_session() will take care of the error */ |
| 5825 | return 0; |
| 5826 | } |
| 5827 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5828 | /* 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] | 5829 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5830 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5831 | 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] | 5832 | else if (txn->flags & TX_NOT_FIRST) |
| 5833 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5834 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5835 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5836 | if (objt_server(s->target)) { |
| 5837 | objt_server(s->target)->counters.failed_resp++; |
| 5838 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5839 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5840 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5841 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5842 | rep->analysers = 0; |
| 5843 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5844 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5845 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5846 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5847 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5848 | if (!(s->flags & SN_ERR_MASK)) |
| 5849 | s->flags |= SN_ERR_SRVCL; |
| 5850 | if (!(s->flags & SN_FINST_MASK)) |
| 5851 | s->flags |= SN_FINST_H; |
| 5852 | return 0; |
| 5853 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5854 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5855 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5856 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5857 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5858 | 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] | 5859 | else if (txn->flags & TX_NOT_FIRST) |
| 5860 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5861 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5862 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5863 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5864 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5865 | |
| 5866 | if (!(s->flags & SN_ERR_MASK)) |
| 5867 | s->flags |= SN_ERR_CLICL; |
| 5868 | if (!(s->flags & SN_FINST_MASK)) |
| 5869 | s->flags |= SN_FINST_H; |
| 5870 | |
| 5871 | /* process_session() will take care of the error */ |
| 5872 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5873 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5874 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5875 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5876 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5877 | return 0; |
| 5878 | } |
| 5879 | |
| 5880 | /* More interesting part now : we know that we have a complete |
| 5881 | * response which at least looks like HTTP. We have an indicator |
| 5882 | * of each header's length, so we can parse them quickly. |
| 5883 | */ |
| 5884 | |
| 5885 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5886 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5887 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5888 | /* |
| 5889 | * 1: get the status code |
| 5890 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5891 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5892 | if (n < 1 || n > 5) |
| 5893 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5894 | /* when the client triggers a 4xx from the server, it's most often due |
| 5895 | * to a missing object or permission. These events should be tracked |
| 5896 | * because if they happen often, it may indicate a brute force or a |
| 5897 | * vulnerability scan. |
| 5898 | */ |
| 5899 | if (n == 4) |
| 5900 | session_inc_http_err_ctr(s); |
| 5901 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5902 | if (objt_server(s->target)) |
| 5903 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5904 | |
Willy Tarreau | 5564555 | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 5905 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 5906 | * exactly one digit "." one digit. This check may be disabled using |
| 5907 | * option accept-invalid-http-response. |
| 5908 | */ |
| 5909 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 5910 | if (msg->sl.st.v_l != 8) { |
| 5911 | msg->err_pos = 0; |
| 5912 | goto hdr_response_bad; |
| 5913 | } |
| 5914 | |
| 5915 | if (rep->buf->p[4] != '/' || |
| 5916 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 5917 | rep->buf->p[6] != '.' || |
| 5918 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 5919 | msg->err_pos = 4; |
| 5920 | goto hdr_response_bad; |
| 5921 | } |
| 5922 | } |
| 5923 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5924 | /* check if the response is HTTP/1.1 or above */ |
| 5925 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5926 | ((rep->buf->p[5] > '1') || |
| 5927 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5928 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5929 | |
| 5930 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5931 | 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] | 5932 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5933 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5934 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5935 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5936 | 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] | 5937 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5938 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5939 | * and 505 are triggered on demand by client request, so we must not |
| 5940 | * count them as server failures. |
| 5941 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5942 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5943 | 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] | 5944 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5945 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5946 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5947 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5948 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5949 | /* |
| 5950 | * 2: check for cacheability. |
| 5951 | */ |
| 5952 | |
| 5953 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5954 | case 100: |
| 5955 | /* |
| 5956 | * We may be facing a 100-continue response, in which case this |
| 5957 | * is not the right response, and we're waiting for the next one. |
| 5958 | * Let's allow this response to go to the client and wait for the |
| 5959 | * next one. |
| 5960 | */ |
| 5961 | hdr_idx_init(&txn->hdr_idx); |
| 5962 | msg->next -= channel_forward(rep, msg->next); |
| 5963 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5964 | txn->status = 0; |
| 5965 | s->logs.t_data = -1; /* was not a response yet */ |
| 5966 | goto next_one; |
| 5967 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5968 | case 200: |
| 5969 | case 203: |
| 5970 | case 206: |
| 5971 | case 300: |
| 5972 | case 301: |
| 5973 | case 410: |
| 5974 | /* RFC2616 @13.4: |
| 5975 | * "A response received with a status code of |
| 5976 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 5977 | * by a cache (...) unless a cache-control |
| 5978 | * directive prohibits caching." |
| 5979 | * |
| 5980 | * RFC2616 @9.5: POST method : |
| 5981 | * "Responses to this method are not cacheable, |
| 5982 | * unless the response includes appropriate |
| 5983 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5984 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5985 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5986 | ((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] | 5987 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 5988 | break; |
| 5989 | default: |
| 5990 | break; |
| 5991 | } |
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 | /* |
| 5994 | * 3: we may need to capture headers |
| 5995 | */ |
| 5996 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 5997 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5998 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5999 | txn->rsp.cap, s->fe->rsp_cap); |
| 6000 | |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6001 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 6002 | * by RFC7230#3.3.3 : |
| 6003 | * |
| 6004 | * The length of a message body is determined by one of the following |
| 6005 | * (in order of precedence): |
| 6006 | * |
| 6007 | * 1. Any response to a HEAD request and any response with a 1xx |
| 6008 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 6009 | * code is always terminated by the first empty line after the |
| 6010 | * header fields, regardless of the header fields present in the |
| 6011 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6012 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6013 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 6014 | * the connection will become a tunnel immediately after the empty |
| 6015 | * line that concludes the header fields. A client MUST ignore any |
| 6016 | * Content-Length or Transfer-Encoding header fields received in |
| 6017 | * such a message. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6018 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6019 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 6020 | * transfer coding (Section 4.1) is the final encoding, the message |
| 6021 | * body length is determined by reading and decoding the chunked |
| 6022 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6023 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6024 | * If a Transfer-Encoding header field is present in a response and |
| 6025 | * the chunked transfer coding is not the final encoding, the |
| 6026 | * message body length is determined by reading the connection until |
| 6027 | * it is closed by the server. If a Transfer-Encoding header field |
| 6028 | * is present in a request and the chunked transfer coding is not |
| 6029 | * the final encoding, the message body length cannot be determined |
| 6030 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 6031 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6032 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6033 | * If a message is received with both a Transfer-Encoding and a |
| 6034 | * Content-Length header field, the Transfer-Encoding overrides the |
| 6035 | * Content-Length. Such a message might indicate an attempt to |
| 6036 | * perform request smuggling (Section 9.5) or response splitting |
| 6037 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 6038 | * remove the received Content-Length field prior to forwarding such |
| 6039 | * a message downstream. |
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 | * 4. If a message is received without Transfer-Encoding and with |
| 6042 | * either multiple Content-Length header fields having differing |
| 6043 | * field-values or a single Content-Length header field having an |
| 6044 | * invalid value, then the message framing is invalid and the |
| 6045 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 6046 | * request message, the server MUST respond with a 400 (Bad Request) |
| 6047 | * status code and then close the connection. If this is a response |
| 6048 | * message received by a proxy, the proxy MUST close the connection |
| 6049 | * to the server, discard the received response, and send a 502 (Bad |
| 6050 | * Gateway) response to the client. If this is a response message |
| 6051 | * received by a user agent, the user agent MUST close the |
| 6052 | * connection to the server and discard the received response. |
| 6053 | * |
| 6054 | * 5. If a valid Content-Length header field is present without |
| 6055 | * Transfer-Encoding, its decimal value defines the expected message |
| 6056 | * body length in octets. If the sender closes the connection or |
| 6057 | * the recipient times out before the indicated number of octets are |
| 6058 | * received, the recipient MUST consider the message to be |
| 6059 | * incomplete and close the connection. |
| 6060 | * |
| 6061 | * 6. If this is a request message and none of the above are true, then |
| 6062 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6063 | * |
Willy Tarreau | 4db603d | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6064 | * 7. Otherwise, this is a response message without a declared message |
| 6065 | * body length, so the message body length is determined by the |
| 6066 | * number of octets received prior to the server closing the |
| 6067 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6068 | */ |
| 6069 | |
| 6070 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6071 | * 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] | 6072 | * 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] | 6073 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6074 | */ |
| 6075 | if (txn->meth == HTTP_METH_HEAD || |
| 6076 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6077 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6078 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 6079 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6080 | goto skip_content_length; |
| 6081 | } |
| 6082 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6083 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6084 | ctx.idx = 0; |
Willy Tarreau | e77bc17 | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 6085 | 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] | 6086 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6087 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6088 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6089 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6090 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6091 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6092 | break; |
| 6093 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6094 | } |
| 6095 | |
Willy Tarreau | dfa3d92 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6096 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6097 | ctx.idx = 0; |
Willy Tarreau | 660418d | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6098 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | dfa3d92 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6099 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 6100 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 6101 | } |
Willy Tarreau | 660418d | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6102 | 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] | 6103 | signed long long cl; |
| 6104 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6105 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6106 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6107 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6108 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6109 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6110 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6111 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6112 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6113 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6114 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6115 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6116 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6117 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6118 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6119 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6120 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6121 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6122 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6123 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6124 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6125 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6126 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6127 | } |
| 6128 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6129 | if (s->fe->comp || s->be->comp) |
| 6130 | select_compression_response_header(s, rep->buf); |
| 6131 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6132 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6133 | /* Now we have to check if we need to modify the Connection header. |
| 6134 | * This is more difficult on the response than it is on the request, |
| 6135 | * because we can have two different HTTP versions and we don't know |
| 6136 | * how the client will interprete a response. For instance, let's say |
| 6137 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6138 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6139 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6140 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6141 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6142 | * indicating how a request MAY be understood by the client. In case |
| 6143 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6144 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6145 | * will fall back to explicit close. Note that we won't take risks with |
| 6146 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6147 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6148 | */ |
| 6149 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6150 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6151 | txn->status == 101)) { |
| 6152 | /* Either we've established an explicit tunnel, or we're |
| 6153 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6154 | * to understand the next protocols. We have to switch to tunnel |
| 6155 | * mode, so that we transfer the request and responses then let |
| 6156 | * this protocol pass unmodified. When we later implement specific |
| 6157 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6158 | * header which contains information about that protocol for |
| 6159 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6160 | */ |
| 6161 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6162 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6163 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6164 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6165 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6166 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6167 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6168 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6169 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6170 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6171 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6172 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6173 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6174 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6175 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6176 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6177 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6178 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6179 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6180 | /* now adjust header transformations depending on current state */ |
| 6181 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6182 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6183 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6184 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6185 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6186 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6187 | else { /* SCL / KAL */ |
| 6188 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6189 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6190 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6191 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6192 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6193 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6194 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6195 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6196 | /* Some keep-alive responses are converted to Server-close if |
| 6197 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6198 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6199 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6200 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6201 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6202 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6203 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6204 | } |
| 6205 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6206 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6207 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6208 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6209 | /* end of job, return OK */ |
| 6210 | rep->analysers &= ~an_bit; |
| 6211 | rep->analyse_exp = TICK_ETERNITY; |
| 6212 | channel_auto_close(rep); |
| 6213 | return 1; |
| 6214 | |
| 6215 | abort_keep_alive: |
| 6216 | /* A keep-alive request to the server failed on a network error. |
| 6217 | * The client is required to retry. We need to close without returning |
| 6218 | * any other information so that the client retries. |
| 6219 | */ |
| 6220 | txn->status = 0; |
| 6221 | rep->analysers = 0; |
| 6222 | s->req->analysers = 0; |
| 6223 | channel_auto_close(rep); |
| 6224 | s->logs.logwait = 0; |
| 6225 | s->logs.level = 0; |
| 6226 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 6227 | bi_erase(rep); |
| 6228 | stream_int_retnclose(rep->cons, NULL); |
| 6229 | return 0; |
| 6230 | } |
| 6231 | |
| 6232 | /* This function performs all the processing enabled for the current response. |
| 6233 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 6234 | * and updates s->rep->analysers. It might make sense to explode it into several |
| 6235 | * other functions. It works like process_request (see indications above). |
| 6236 | */ |
| 6237 | int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px) |
| 6238 | { |
| 6239 | struct http_txn *txn = &s->txn; |
| 6240 | struct http_msg *msg = &txn->rsp; |
| 6241 | struct proxy *cur_proxy; |
| 6242 | struct cond_wordlist *wl; |
| 6243 | struct http_res_rule *http_res_last_rule = NULL; |
| 6244 | |
| 6245 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 6246 | now_ms, __FUNCTION__, |
| 6247 | s, |
| 6248 | rep, |
| 6249 | rep->rex, rep->wex, |
| 6250 | rep->flags, |
| 6251 | rep->buf->i, |
| 6252 | rep->analysers); |
| 6253 | |
| 6254 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6255 | return 0; |
| 6256 | |
| 6257 | rep->analysers &= ~an_bit; |
| 6258 | rep->analyse_exp = TICK_ETERNITY; |
| 6259 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6260 | /* The stats applet needs to adjust the Connection header but we don't |
| 6261 | * apply any filter there. |
| 6262 | */ |
| 6263 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) |
| 6264 | goto skip_filters; |
| 6265 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6266 | /* |
| 6267 | * We will have to evaluate the filters. |
| 6268 | * As opposed to version 1.2, now they will be evaluated in the |
| 6269 | * filters order and not in the header order. This means that |
| 6270 | * each filter has to be validated among all headers. |
| 6271 | * |
| 6272 | * Filters are tried with ->be first, then with ->fe if it is |
| 6273 | * different from ->be. |
| 6274 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6275 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6276 | cur_proxy = s->be; |
| 6277 | while (1) { |
| 6278 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6279 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6280 | /* evaluate http-response rules */ |
| 6281 | if (!http_res_last_rule) |
| 6282 | 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] | 6283 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6284 | /* try headers filters */ |
| 6285 | if (rule_set->rsp_exp != NULL) { |
| 6286 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6287 | return_bad_resp: |
| 6288 | if (objt_server(s->target)) { |
| 6289 | objt_server(s->target)->counters.failed_resp++; |
| 6290 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6291 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6292 | s->be->be_counters.failed_resp++; |
| 6293 | return_srv_prx_502: |
| 6294 | rep->analysers = 0; |
| 6295 | txn->status = 502; |
| 6296 | s->logs.t_data = -1; /* was not a valid response */ |
| 6297 | rep->prod->flags |= SI_FL_NOLINGER; |
| 6298 | bi_erase(rep); |
| 6299 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
| 6300 | if (!(s->flags & SN_ERR_MASK)) |
| 6301 | s->flags |= SN_ERR_PRXCOND; |
| 6302 | if (!(s->flags & SN_FINST_MASK)) |
| 6303 | s->flags |= SN_FINST_H; |
| 6304 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6305 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6306 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6307 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6308 | /* has the response been denied ? */ |
| 6309 | if (txn->flags & TX_SVDENY) { |
| 6310 | if (objt_server(s->target)) |
| 6311 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6312 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6313 | s->be->be_counters.denied_resp++; |
| 6314 | s->fe->fe_counters.denied_resp++; |
| 6315 | if (s->listener->counters) |
| 6316 | s->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6317 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6318 | goto return_srv_prx_502; |
| 6319 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6320 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6321 | /* add response headers from the rule sets in the same order */ |
| 6322 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 0cb4b89 | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6323 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6324 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6325 | if (wl->cond) { |
| 6326 | int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 6327 | ret = acl_pass(ret); |
| 6328 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6329 | ret = !ret; |
| 6330 | if (!ret) |
| 6331 | continue; |
| 6332 | } |
| 6333 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6334 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6335 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6336 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6337 | /* check whether we're already working on the frontend */ |
| 6338 | if (cur_proxy == s->fe) |
| 6339 | break; |
| 6340 | cur_proxy = s->fe; |
| 6341 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6342 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6343 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | 0cb4b89 | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6344 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6345 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6346 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6347 | /* |
| 6348 | * Now check for a server cookie. |
| 6349 | */ |
| 6350 | if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name || |
| 6351 | (s->be->options & PR_O_CHK_CACHE)) |
| 6352 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6353 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6354 | /* |
| 6355 | * Check for cache-control or pragma headers if required. |
| 6356 | */ |
Willy Tarreau | 0cb4b89 | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6357 | 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] | 6358 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6359 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6360 | /* |
| 6361 | * Add server cookie in the response if needed |
| 6362 | */ |
| 6363 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6364 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 6365 | (!(s->flags & SN_DIRECT) || |
| 6366 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6367 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6368 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6369 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6370 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 6371 | !(s->flags & SN_IGNORE_PRST)) { |
| 6372 | /* the server is known, it's not the one the client requested, or the |
| 6373 | * cookie's last seen date needs to be refreshed. We have to |
| 6374 | * insert a set-cookie here, except if we want to insert only on POST |
| 6375 | * requests and this one isn't. Note that servers which don't have cookies |
| 6376 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6377 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6378 | if (!objt_server(s->target)->cookie) { |
| 6379 | chunk_printf(&trash, |
| 6380 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6381 | s->be->cookie_name); |
| 6382 | } |
| 6383 | else { |
| 6384 | 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] | 6385 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6386 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6387 | /* emit last_date, which is mandatory */ |
| 6388 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6389 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6390 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6391 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6392 | if (s->be->cookie_maxlife) { |
| 6393 | /* emit first_date, which is either the original one or |
| 6394 | * the current date. |
| 6395 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6396 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6397 | s30tob64(txn->cookie_first_date ? |
| 6398 | txn->cookie_first_date >> 2 : |
| 6399 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6400 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6401 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6402 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6403 | chunk_appendf(&trash, "; path=/"); |
| 6404 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6405 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6406 | if (s->be->cookie_domain) |
| 6407 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6408 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6409 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6410 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6411 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6412 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6413 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6414 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6415 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6416 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6417 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6418 | txn->flags &= ~TX_SCK_MASK; |
| 6419 | if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT)) |
| 6420 | /* the server did not change, only the date was updated */ |
| 6421 | txn->flags |= TX_SCK_UPDATED; |
| 6422 | else |
| 6423 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6424 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6425 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6426 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6427 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6428 | * 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] | 6429 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6430 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6431 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6432 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6433 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6434 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6435 | "Cache-control: private", 22) < 0)) |
| 6436 | goto return_bad_resp; |
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 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6439 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6440 | /* |
| 6441 | * Check if result will be cacheable with a cookie. |
| 6442 | * We'll block the response if security checks have caught |
| 6443 | * nasty things such as a cacheable cookie. |
| 6444 | */ |
| 6445 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6446 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6447 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6448 | /* we're in presence of a cacheable response containing |
| 6449 | * a set-cookie header. We'll block it as requested by |
| 6450 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6451 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6452 | if (objt_server(s->target)) |
| 6453 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6454 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6455 | s->be->be_counters.denied_resp++; |
| 6456 | s->fe->fe_counters.denied_resp++; |
| 6457 | if (s->listener->counters) |
| 6458 | s->listener->counters->denied_resp++; |
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 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6461 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6462 | send_log(s->be, LOG_ALERT, |
| 6463 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6464 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6465 | goto return_srv_prx_502; |
| 6466 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6467 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6468 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6469 | /* |
| 6470 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6471 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6472 | * 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] | 6473 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6474 | * want to touch any 101 response either since it's switching to another |
| 6475 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6476 | */ |
Willy Tarreau | 0cb4b89 | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6477 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6478 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 6479 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6480 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6481 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6482 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6483 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6484 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6485 | /* we want a keep-alive response here. Keep-alive header |
| 6486 | * required if either side is not 1.1. |
| 6487 | */ |
| 6488 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6489 | want_flags |= TX_CON_KAL_SET; |
| 6490 | } |
| 6491 | else { |
| 6492 | /* we want a close response here. Close header required if |
| 6493 | * the server is 1.1, regardless of the client. |
| 6494 | */ |
| 6495 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6496 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6497 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6498 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6499 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6500 | http_change_connection_header(txn, msg, want_flags); |
| 6501 | } |
| 6502 | |
| 6503 | skip_header_mangling: |
| 6504 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
| 6505 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 6506 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6507 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6508 | /* if the user wants to log as soon as possible, without counting |
| 6509 | * bytes from the server, then this is the right moment. We have |
| 6510 | * to temporarily assign bytes_out to log what we currently have. |
| 6511 | */ |
| 6512 | if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 6513 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6514 | s->logs.bytes_out = txn->rsp.eoh; |
| 6515 | s->do_log(s); |
| 6516 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6517 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6518 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6519 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6520 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6521 | /* This function is an analyser which forwards response body (including chunk |
| 6522 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6523 | * zero byte. The only situation where it must not be called is when we're in |
| 6524 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6525 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6526 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 6527 | * 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] | 6528 | * |
| 6529 | * It is capable of compressing response data both in content-length mode and |
| 6530 | * in chunked mode. The state machines follows different flows depending on |
| 6531 | * whether content-length and chunked modes are used, since there are no |
| 6532 | * trailers in content-length : |
| 6533 | * |
| 6534 | * chk-mode cl-mode |
| 6535 | * ,----- BODY -----. |
| 6536 | * / \ |
| 6537 | * V size > 0 V chk-mode |
| 6538 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6539 | * | | size == 0 | last byte | |
| 6540 | * | v final crlf v inspected | |
| 6541 | * | TRAILERS -----------> DONE | |
| 6542 | * | | |
| 6543 | * `----------------------------------------------' |
| 6544 | * |
| 6545 | * Compression only happens in the DATA state, and must be flushed in final |
| 6546 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6547 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6548 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6549 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6550 | 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] | 6551 | { |
| 6552 | struct http_txn *txn = &s->txn; |
| 6553 | struct http_msg *msg = &s->txn.rsp; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6554 | static struct buffer *tmpbuf = NULL; |
| 6555 | int compressing = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6556 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6557 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6558 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6559 | return 0; |
| 6560 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6561 | 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] | 6562 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 6563 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6564 | /* Output closed while we were sending data. We must abort and |
| 6565 | * wake the other side up. |
| 6566 | */ |
| 6567 | msg->msg_state = HTTP_MSG_ERROR; |
| 6568 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6569 | return 1; |
| 6570 | } |
| 6571 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6572 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6573 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6574 | |
Willy Tarreau | 5bebcd0 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 6575 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6576 | /* we have msg->sov which points to the first byte of message |
| 6577 | * body, and res->buf.p still points to the beginning of the |
| 6578 | * message. We forward the headers now, as we don't need them |
| 6579 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6580 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6581 | b_adv(res->buf, msg->sov); |
| 6582 | msg->next -= msg->sov; |
| 6583 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6584 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6585 | /* The previous analysers guarantee that the state is somewhere |
| 6586 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 6587 | * msg->next are always correct. |
| 6588 | */ |
| 6589 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 6590 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 6591 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6592 | else |
| 6593 | msg->msg_state = HTTP_MSG_DATA; |
| 6594 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6595 | } |
| 6596 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6597 | if (res->to_forward) { |
| 6598 | /* We can't process the buffer's contents yet */ |
| 6599 | res->flags |= CF_WAKE_WRITE; |
| 6600 | goto missing_data; |
| 6601 | } |
| 6602 | |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6603 | if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) { |
| 6604 | /* We need a compression buffer in the DATA state to put the |
| 6605 | * output of compressed data, and in CRLF state to let the |
| 6606 | * TRAILERS state finish the job of removing the trailing CRLF. |
| 6607 | */ |
| 6608 | if (unlikely(tmpbuf == NULL)) { |
| 6609 | /* this is the first time we need the compression buffer */ |
| 6610 | tmpbuf = pool_alloc2(pool2_buffer); |
| 6611 | if (tmpbuf == NULL) |
| 6612 | goto aborted_xfer; /* no memory */ |
| 6613 | } |
| 6614 | |
| 6615 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6616 | if (ret < 0) { |
| 6617 | res->flags |= CF_WAKE_WRITE; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6618 | goto missing_data; /* not enough spaces in buffers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6619 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6620 | compressing = 1; |
| 6621 | } |
| 6622 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6623 | while (1) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6624 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6625 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6626 | /* we may have some pending data starting at res->buf->p */ |
| 6627 | if (unlikely(s->comp_algo)) { |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 6628 | ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6629 | if (ret < 0) |
| 6630 | goto aborted_xfer; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6631 | |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6632 | if (msg->chunk_len) { |
| 6633 | /* input empty or output full */ |
| 6634 | if (res->buf->i > msg->next) |
| 6635 | res->flags |= CF_WAKE_WRITE; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6636 | goto missing_data; |
| 6637 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6638 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6639 | else { |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6640 | if (msg->chunk_len > res->buf->i - msg->next) { |
| 6641 | /* output full */ |
| 6642 | res->flags |= CF_WAKE_WRITE; |
| 6643 | goto missing_data; |
| 6644 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6645 | msg->next += msg->chunk_len; |
| 6646 | msg->chunk_len = 0; |
| 6647 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6648 | |
| 6649 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6650 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6651 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6652 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6653 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6654 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6655 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6656 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6657 | |
| 6658 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 6659 | /* we want the CRLF after the data */ |
| 6660 | |
| 6661 | ret = http_skip_chunk_crlf(msg); |
| 6662 | if (ret == 0) |
| 6663 | goto missing_data; |
| 6664 | else if (ret < 0) { |
| 6665 | if (msg->err_pos >= 0) |
| 6666 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe); |
| 6667 | goto return_bad_res; |
| 6668 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6669 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 6670 | |
| 6671 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6672 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 6673 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6674 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6675 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6676 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6677 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6678 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6679 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6680 | else if (ret < 0) { |
| 6681 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6682 | 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] | 6683 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6684 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 6685 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6686 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6687 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6688 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6689 | if (unlikely(compressing)) { |
| 6690 | /* we need to flush output contents before syncing FSMs */ |
| 6691 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6692 | compressing = 0; |
| 6693 | } |
| 6694 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6695 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6696 | if (ret == 0) |
| 6697 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6698 | else if (ret < 0) { |
| 6699 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6700 | 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] | 6701 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6702 | } |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6703 | /* we're in HTTP_MSG_DONE now, fall through */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6704 | |
| 6705 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6706 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6707 | if (unlikely(compressing)) { |
| 6708 | /* we need to flush output contents before syncing FSMs */ |
| 6709 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6710 | compressing = 0; |
| 6711 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6712 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6713 | /* we may have some pending data starting at res->buf->p |
| 6714 | * such as a last chunk of data or trailers. |
| 6715 | */ |
| 6716 | b_adv(res->buf, msg->next); |
| 6717 | msg->next = 0; |
| 6718 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6719 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6720 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6721 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6722 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6723 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6724 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6725 | if (http_resync_states(s)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6726 | /* some state changes occurred, maybe the analyser |
| 6727 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6728 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6729 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6730 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6731 | /* response errors are most likely due to |
| 6732 | * the client aborting the transfer. |
| 6733 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6734 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6735 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6736 | if (msg->err_pos >= 0) |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6737 | 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] | 6738 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6739 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6740 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6741 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6742 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6743 | } |
| 6744 | } |
| 6745 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6746 | missing_data: |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6747 | /* we may have some pending data starting at res->buf->p */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6748 | if (unlikely(compressing)) { |
| 6749 | 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] | 6750 | compressing = 0; |
| 6751 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6752 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6753 | if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) { |
| 6754 | b_adv(res->buf, msg->next); |
| 6755 | msg->next = 0; |
| 6756 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6757 | } |
| 6758 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6759 | if (res->flags & CF_SHUTW) |
| 6760 | goto aborted_xfer; |
| 6761 | |
| 6762 | /* stop waiting for data if the input is closed before the end. If the |
| 6763 | * client side was already closed, it means that the client has aborted, |
| 6764 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6765 | * server abort. |
| 6766 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6767 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | b2c6a78 | 2014-04-23 20:29:01 +0200 | [diff] [blame] | 6768 | if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6769 | goto aborted_xfer; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6770 | if (!(s->flags & SN_ERR_MASK)) |
| 6771 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6772 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6773 | if (objt_server(s->target)) |
| 6774 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6775 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6776 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6777 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6778 | /* 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] | 6779 | if (!s->req->analysers) |
| 6780 | goto return_bad_res; |
| 6781 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6782 | /* 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] | 6783 | * 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] | 6784 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 6785 | * close. |
| 6786 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6787 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6788 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6789 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6790 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6791 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6792 | /* 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] | 6793 | * 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] | 6794 | * 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] | 6795 | * modes are already handled by the stream sock layer. We must not do |
| 6796 | * this in content-length mode because it could present the MSG_MORE |
| 6797 | * flag with the last block of forwarded data, which would cause an |
| 6798 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6799 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6800 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6801 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6802 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6803 | /* the session handler will take care of timeouts and errors */ |
| 6804 | return 0; |
| 6805 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6806 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6807 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6808 | if (objt_server(s->target)) |
| 6809 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6810 | |
| 6811 | return_bad_res_stats_ok: |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 6812 | if (unlikely(compressing)) { |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6813 | 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] | 6814 | compressing = 0; |
| 6815 | } |
| 6816 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6817 | /* we may have some pending data starting at res->buf->p */ |
| 6818 | if (s->comp_algo == NULL) { |
| 6819 | b_adv(res->buf, msg->next); |
| 6820 | msg->next = 0; |
| 6821 | } |
| 6822 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6823 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6824 | /* don't send any error message as we're in the body */ |
| 6825 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6826 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6827 | 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] | 6828 | if (objt_server(s->target)) |
| 6829 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6830 | |
| 6831 | if (!(s->flags & SN_ERR_MASK)) |
| 6832 | s->flags |= SN_ERR_PRXCOND; |
| 6833 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6834 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6835 | return 0; |
| 6836 | |
| 6837 | aborted_xfer: |
Willy Tarreau | 6fef8ae | 2014-04-22 21:22:06 +0200 | [diff] [blame] | 6838 | if (unlikely(compressing)) { |
| 6839 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
| 6840 | compressing = 0; |
| 6841 | } |
| 6842 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6843 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6844 | /* don't send any error message as we're in the body */ |
| 6845 | stream_int_retnclose(res->cons, NULL); |
| 6846 | res->analysers = 0; |
| 6847 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 6848 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6849 | s->fe->fe_counters.cli_aborts++; |
| 6850 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6851 | if (objt_server(s->target)) |
| 6852 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6853 | |
| 6854 | if (!(s->flags & SN_ERR_MASK)) |
| 6855 | s->flags |= SN_ERR_CLICL; |
| 6856 | if (!(s->flags & SN_FINST_MASK)) |
| 6857 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6858 | return 0; |
| 6859 | } |
| 6860 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6861 | /* Iterate the same filter through all request headers. |
| 6862 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6863 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6864 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6865 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6866 | 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] | 6867 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6868 | char *cur_ptr, *cur_end, *cur_next; |
| 6869 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6870 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6871 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6872 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6873 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6874 | last_hdr = 0; |
| 6875 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6876 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6877 | old_idx = 0; |
| 6878 | |
| 6879 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6880 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6881 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6882 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6883 | (exp->action == ACT_ALLOW || |
| 6884 | exp->action == ACT_DENY || |
| 6885 | exp->action == ACT_TARPIT)) |
| 6886 | return 0; |
| 6887 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6888 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6889 | if (!cur_idx) |
| 6890 | break; |
| 6891 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6892 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6893 | cur_ptr = cur_next; |
| 6894 | cur_end = cur_ptr + cur_hdr->len; |
| 6895 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6896 | |
| 6897 | /* Now we have one header between cur_ptr and cur_end, |
| 6898 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6899 | */ |
| 6900 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 6901 | 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] | 6902 | switch (exp->action) { |
| 6903 | case ACT_SETBE: |
| 6904 | /* It is not possible to jump a second time. |
| 6905 | * FIXME: should we return an HTTP/500 here so that |
| 6906 | * the admin knows there's a problem ? |
| 6907 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6908 | if (s->be != s->fe) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6909 | break; |
| 6910 | |
| 6911 | /* Swithing Proxy */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6912 | session_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6913 | last_hdr = 1; |
| 6914 | break; |
| 6915 | |
| 6916 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6917 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6918 | last_hdr = 1; |
| 6919 | break; |
| 6920 | |
| 6921 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6922 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6923 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6924 | break; |
| 6925 | |
| 6926 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6927 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6928 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6929 | break; |
| 6930 | |
| 6931 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6932 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6933 | if (trash.len < 0) |
| 6934 | return -1; |
| 6935 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6936 | 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] | 6937 | /* FIXME: if the user adds a newline in the replacement, the |
| 6938 | * index will not be recalculated for now, and the new line |
| 6939 | * will not be counted as a new header. |
| 6940 | */ |
| 6941 | |
| 6942 | cur_end += delta; |
| 6943 | cur_next += delta; |
| 6944 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6945 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6946 | break; |
| 6947 | |
| 6948 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6949 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6950 | cur_next += delta; |
| 6951 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6952 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6953 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6954 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6955 | cur_hdr->len = 0; |
| 6956 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6957 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6958 | break; |
| 6959 | |
| 6960 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6961 | } |
| 6962 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6963 | /* keep the link from this header to next one in case of later |
| 6964 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6965 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6966 | old_idx = cur_idx; |
| 6967 | } |
| 6968 | return 0; |
| 6969 | } |
| 6970 | |
| 6971 | |
| 6972 | /* Apply the filter to the request line. |
| 6973 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6974 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6975 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6976 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6977 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6978 | 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] | 6979 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6980 | char *cur_ptr, *cur_end; |
| 6981 | int done; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6982 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6983 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6984 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6985 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6986 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6987 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6988 | (exp->action == ACT_ALLOW || |
| 6989 | exp->action == ACT_DENY || |
| 6990 | exp->action == ACT_TARPIT)) |
| 6991 | return 0; |
| 6992 | else if (exp->action == ACT_REMOVE) |
| 6993 | return 0; |
| 6994 | |
| 6995 | done = 0; |
| 6996 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6997 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6998 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6999 | |
| 7000 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7001 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 7002 | 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] | 7003 | switch (exp->action) { |
| 7004 | case ACT_SETBE: |
| 7005 | /* It is not possible to jump a second time. |
| 7006 | * FIXME: should we return an HTTP/500 here so that |
| 7007 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7008 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7009 | if (s->be != s->fe) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7010 | break; |
| 7011 | |
| 7012 | /* Swithing Proxy */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7013 | session_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7014 | done = 1; |
| 7015 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7016 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7017 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7018 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7019 | done = 1; |
| 7020 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7021 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7022 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7023 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7024 | done = 1; |
| 7025 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7026 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7027 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7028 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7029 | done = 1; |
| 7030 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7031 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7032 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7033 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7034 | if (trash.len < 0) |
| 7035 | return -1; |
| 7036 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7037 | 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] | 7038 | /* FIXME: if the user adds a newline in the replacement, the |
| 7039 | * index will not be recalculated for now, and the new line |
| 7040 | * will not be counted as a new header. |
| 7041 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7042 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7043 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7044 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7045 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7046 | HTTP_MSG_RQMETH, |
| 7047 | cur_ptr, cur_end + 1, |
| 7048 | NULL, NULL); |
| 7049 | if (unlikely(!cur_end)) |
| 7050 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7051 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7052 | /* we have a full request and we know that we have either a CR |
| 7053 | * or an LF at <ptr>. |
| 7054 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7055 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7056 | 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] | 7057 | /* there is no point trying this regex on headers */ |
| 7058 | return 1; |
| 7059 | } |
| 7060 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7061 | return done; |
| 7062 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7063 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7064 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7065 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7066 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7067 | * 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] | 7068 | * 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] | 7069 | * unparsable request. Since it can manage the switch to another backend, it |
| 7070 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7071 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7072 | 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] | 7073 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7074 | struct http_txn *txn = &s->txn; |
| 7075 | struct hdr_exp *exp; |
| 7076 | |
| 7077 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7078 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7079 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7080 | /* |
| 7081 | * The interleaving of transformations and verdicts |
| 7082 | * makes it difficult to decide to continue or stop |
| 7083 | * the evaluation. |
| 7084 | */ |
| 7085 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7086 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7087 | break; |
| 7088 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7089 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7090 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7091 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7092 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7093 | |
| 7094 | /* if this filter had a condition, evaluate it now and skip to |
| 7095 | * next filter if the condition does not match. |
| 7096 | */ |
| 7097 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7098 | 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] | 7099 | ret = acl_pass(ret); |
| 7100 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7101 | ret = !ret; |
| 7102 | |
| 7103 | if (!ret) |
| 7104 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7105 | } |
| 7106 | |
| 7107 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7108 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7109 | if (unlikely(ret < 0)) |
| 7110 | return -1; |
| 7111 | |
| 7112 | if (likely(ret == 0)) { |
| 7113 | /* The filter did not match the request, it can be |
| 7114 | * iterated through all headers. |
| 7115 | */ |
Willy Tarreau | a256fae | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7116 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7117 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7118 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7119 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7120 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7121 | } |
| 7122 | |
| 7123 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7124 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7125 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7126 | * Try to retrieve the server associated to the appsession. |
| 7127 | * If the server is found, it's assigned to the session. |
| 7128 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7129 | void manage_client_side_appsession(struct session *s, const char *buf, int len) { |
| 7130 | struct http_txn *txn = &s->txn; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7131 | appsess *asession = NULL; |
| 7132 | char *sessid_temp = NULL; |
| 7133 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7134 | if (len > s->be->appsession_len) { |
| 7135 | len = s->be->appsession_len; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7136 | } |
| 7137 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7138 | if (s->be->options2 & PR_O2_AS_REQL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7139 | /* 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] | 7140 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7141 | /* 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] | 7142 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7143 | } |
| 7144 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7145 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7146 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7147 | 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] | 7148 | return; |
| 7149 | } |
| 7150 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7151 | memcpy(txn->sessid, buf, len); |
| 7152 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7153 | } |
| 7154 | |
| 7155 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 7156 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7157 | 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] | 7158 | return; |
| 7159 | } |
| 7160 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7161 | memcpy(sessid_temp, buf, len); |
| 7162 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7163 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7164 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7165 | /* free previously allocated memory */ |
| 7166 | pool_free2(apools.sessid, sessid_temp); |
| 7167 | |
| 7168 | if (asession != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7169 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
| 7170 | if (!(s->be->options2 & PR_O2_AS_REQL)) |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7171 | asession->request_count++; |
| 7172 | |
| 7173 | if (asession->serverid != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7174 | struct server *srv = s->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7175 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7176 | while (srv) { |
| 7177 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7178 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7179 | (s->be->options & PR_O_PERSIST) || |
| 7180 | (s->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7181 | /* we found the server and it's usable */ |
| 7182 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7183 | 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] | 7184 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 7185 | s->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 7186 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7187 | break; |
| 7188 | } else { |
| 7189 | txn->flags &= ~TX_CK_MASK; |
| 7190 | txn->flags |= TX_CK_DOWN; |
| 7191 | } |
| 7192 | } |
| 7193 | srv = srv->next; |
| 7194 | } |
| 7195 | } |
| 7196 | } |
| 7197 | } |
| 7198 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7199 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7200 | * same way as with headers above except that the semi-colon also ends a token. |
| 7201 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7202 | * return a valid result. |
| 7203 | */ |
| 7204 | char *find_cookie_value_end(char *s, const char *e) |
| 7205 | { |
| 7206 | int quoted, qdpair; |
| 7207 | |
| 7208 | quoted = qdpair = 0; |
| 7209 | for (; s < e; s++) { |
| 7210 | if (qdpair) qdpair = 0; |
| 7211 | else if (quoted) { |
| 7212 | if (*s == '\\') qdpair = 1; |
| 7213 | else if (*s == '"') quoted = 0; |
| 7214 | } |
| 7215 | else if (*s == '"') quoted = 1; |
| 7216 | else if (*s == ',' || *s == ';') return s; |
| 7217 | } |
| 7218 | return s; |
| 7219 | } |
| 7220 | |
| 7221 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7222 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7223 | * the first delimiter is updated if required. The function tries as much as |
| 7224 | * possible to respect the following principles : |
| 7225 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7226 | * colon, in which case <next> is simply removed |
| 7227 | * - set exactly one space character after the new first delimiter, unless |
| 7228 | * there are not enough characters in the block being moved to do so. |
| 7229 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7230 | * one. |
| 7231 | * |
| 7232 | * It is the caller's responsibility to ensure that : |
| 7233 | * - <from> points to a valid delimiter or the colon ; |
| 7234 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7235 | * - there are non-space chars before <from> ; |
| 7236 | * - there is a CR/LF at or after <next>. |
| 7237 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7238 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7239 | { |
| 7240 | char *prev = *from; |
| 7241 | |
| 7242 | if (*prev == ':') { |
| 7243 | /* We're removing the first value, preserve the colon and add a |
| 7244 | * space if possible. |
| 7245 | */ |
| 7246 | if (!http_is_crlf[(unsigned char)*next]) |
| 7247 | next++; |
| 7248 | prev++; |
| 7249 | if (prev < next) |
| 7250 | *prev++ = ' '; |
| 7251 | |
| 7252 | while (http_is_spht[(unsigned char)*next]) |
| 7253 | next++; |
| 7254 | } else { |
| 7255 | /* Remove useless spaces before the old delimiter. */ |
| 7256 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 7257 | prev--; |
| 7258 | *from = prev; |
| 7259 | |
| 7260 | /* copy the delimiter and if possible a space if we're |
| 7261 | * not at the end of the line. |
| 7262 | */ |
| 7263 | if (!http_is_crlf[(unsigned char)*next]) { |
| 7264 | *prev++ = *next++; |
| 7265 | if (prev + 1 < next) |
| 7266 | *prev++ = ' '; |
| 7267 | while (http_is_spht[(unsigned char)*next]) |
| 7268 | next++; |
| 7269 | } |
| 7270 | } |
| 7271 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7272 | } |
| 7273 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7274 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7275 | * 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] | 7276 | * desirable to call it only when needed. This code is quite complex because |
| 7277 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7278 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7279 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7280 | void manage_client_side_cookies(struct session *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7281 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7282 | struct http_txn *txn = &s->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7283 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7284 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7285 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7286 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7287 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7288 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7289 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7290 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7291 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7292 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7293 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7294 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7295 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7296 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7297 | hdr_beg = hdr_next; |
| 7298 | hdr_end = hdr_beg + cur_hdr->len; |
| 7299 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7300 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7301 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7302 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7303 | * "Cookie:" headers. |
| 7304 | */ |
| 7305 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7306 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7307 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7308 | old_idx = cur_idx; |
| 7309 | continue; |
| 7310 | } |
| 7311 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7312 | del_from = NULL; /* nothing to be deleted */ |
| 7313 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7314 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7315 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7316 | * attributes whose name begin with a '$', and associate them with |
| 7317 | * the right cookie, if we want to delete this cookie. |
| 7318 | * So there are 3 cases for each cookie read : |
| 7319 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7320 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7321 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7322 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7323 | * "special" cookie. |
| 7324 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7325 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7326 | * *MUST* delete it. |
| 7327 | * |
| 7328 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7329 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7330 | * the RFC explicitly allows spaces before it, and not within the |
| 7331 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7332 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7333 | * after the equal sign too, resulting in some (rare) buggy |
| 7334 | * implementations trying to do that. So let's do what servers do. |
| 7335 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7336 | * allowed quoted strings in values, with any possible character |
| 7337 | * after a backslash, including control chars and delimitors, which |
| 7338 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7339 | * within values even without quotes. |
| 7340 | * |
| 7341 | * We have to keep multiple pointers in order to support cookie |
| 7342 | * removal at the beginning, middle or end of header without |
| 7343 | * corrupting the header. All of these headers are valid : |
| 7344 | * |
| 7345 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7346 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7347 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7348 | * | | | | | | | | | |
| 7349 | * | | | | | | | | hdr_end <--+ |
| 7350 | * | | | | | | | +--> next |
| 7351 | * | | | | | | +----> val_end |
| 7352 | * | | | | | +-----------> val_beg |
| 7353 | * | | | | +--------------> equal |
| 7354 | * | | | +----------------> att_end |
| 7355 | * | | +---------------------> att_beg |
| 7356 | * | +--------------------------> prev |
| 7357 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7358 | */ |
| 7359 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7360 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7361 | /* Iterate through all cookies on this line */ |
| 7362 | |
| 7363 | /* find att_beg */ |
| 7364 | att_beg = prev + 1; |
| 7365 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7366 | att_beg++; |
| 7367 | |
| 7368 | /* find att_end : this is the first character after the last non |
| 7369 | * space before the equal. It may be equal to hdr_end. |
| 7370 | */ |
| 7371 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7372 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7373 | while (equal < hdr_end) { |
| 7374 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7375 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7376 | if (http_is_spht[(unsigned char)*equal++]) |
| 7377 | continue; |
| 7378 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7379 | } |
| 7380 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7381 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7382 | * is between <att_beg> and <equal>, both may be identical. |
| 7383 | */ |
| 7384 | |
| 7385 | /* look for end of cookie if there is an equal sign */ |
| 7386 | if (equal < hdr_end && *equal == '=') { |
| 7387 | /* look for the beginning of the value */ |
| 7388 | val_beg = equal + 1; |
| 7389 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7390 | val_beg++; |
| 7391 | |
| 7392 | /* find the end of the value, respecting quotes */ |
| 7393 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7394 | |
| 7395 | /* make val_end point to the first white space or delimitor after the value */ |
| 7396 | val_end = next; |
| 7397 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7398 | val_end--; |
| 7399 | } else { |
| 7400 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7401 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7402 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7403 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7404 | * they will automatically be removed if a header before them is removed, |
| 7405 | * since they're supposed to be linked together. |
| 7406 | */ |
| 7407 | if (*att_beg == '$') |
| 7408 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7409 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7410 | /* Ignore cookies with no equal sign */ |
| 7411 | if (equal == next) { |
| 7412 | /* This is not our cookie, so we must preserve it. But if we already |
| 7413 | * scheduled another cookie for removal, we cannot remove the |
| 7414 | * complete header, but we can remove the previous block itself. |
| 7415 | */ |
| 7416 | preserve_hdr = 1; |
| 7417 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7418 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7419 | val_end += delta; |
| 7420 | next += delta; |
| 7421 | hdr_end += delta; |
| 7422 | hdr_next += delta; |
| 7423 | cur_hdr->len += delta; |
| 7424 | http_msg_move_end(&txn->req, delta); |
| 7425 | prev = del_from; |
| 7426 | del_from = NULL; |
| 7427 | } |
| 7428 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7429 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7430 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7431 | /* if there are spaces around the equal sign, we need to |
| 7432 | * strip them otherwise we'll get trouble for cookie captures, |
| 7433 | * or even for rewrites. Since this happens extremely rarely, |
| 7434 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7435 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7436 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7437 | int stripped_before = 0; |
| 7438 | int stripped_after = 0; |
| 7439 | |
| 7440 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7441 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7442 | equal += stripped_before; |
| 7443 | val_beg += stripped_before; |
| 7444 | } |
| 7445 | |
| 7446 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7447 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7448 | val_beg += stripped_after; |
| 7449 | stripped_before += stripped_after; |
| 7450 | } |
| 7451 | |
| 7452 | val_end += stripped_before; |
| 7453 | next += stripped_before; |
| 7454 | hdr_end += stripped_before; |
| 7455 | hdr_next += stripped_before; |
| 7456 | cur_hdr->len += stripped_before; |
| 7457 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7458 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7459 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7460 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7461 | /* First, let's see if we want to capture this cookie. We check |
| 7462 | * that we don't already have a client side cookie, because we |
| 7463 | * can only capture one. Also as an optimisation, we ignore |
| 7464 | * cookies shorter than the declared name. |
| 7465 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7466 | if (s->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7467 | (val_end - att_beg >= s->fe->capture_namelen) && |
| 7468 | memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7469 | int log_len = val_end - att_beg; |
| 7470 | |
| 7471 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7472 | Alert("HTTP logging : out of memory.\n"); |
| 7473 | } else { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7474 | if (log_len > s->fe->capture_len) |
| 7475 | log_len = s->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7476 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7477 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7478 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7479 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7480 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7481 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7482 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7483 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7484 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7485 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7486 | * For cookies in prefix mode, the form is : |
| 7487 | * |
| 7488 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7489 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7490 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7491 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7492 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7493 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7494 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7495 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7496 | * have the server ID between val_beg and delim, and the original cookie between |
| 7497 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7498 | * |
| 7499 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7500 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7501 | * | || || | |+-> next |
| 7502 | * | || || | +--> val_end |
| 7503 | * | || || +---------> delim |
| 7504 | * | || |+------------> val_beg |
| 7505 | * | || +-------------> att_end = equal |
| 7506 | * | |+-----------------> att_beg |
| 7507 | * | +------------------> prev |
| 7508 | * +-------------------------> hdr_beg |
| 7509 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7510 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7511 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7512 | for (delim = val_beg; delim < val_end; delim++) |
| 7513 | if (*delim == COOKIE_DELIM) |
| 7514 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7515 | } else { |
| 7516 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7517 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7518 | /* Now check if the cookie contains a date field, which would |
| 7519 | * appear after a vertical bar ('|') just after the server name |
| 7520 | * and before the delimiter. |
| 7521 | */ |
| 7522 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7523 | if (vbar1) { |
| 7524 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7525 | * right is the last seen date. It is a base64 encoded |
| 7526 | * 30-bit value representing the UNIX date since the |
| 7527 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7528 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7529 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7530 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7531 | if (val_end - vbar1 >= 5) { |
| 7532 | val = b64tos30(vbar1); |
| 7533 | if (val > 0) |
| 7534 | txn->cookie_last_date = val << 2; |
| 7535 | } |
| 7536 | /* look for a second vertical bar */ |
| 7537 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7538 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7539 | val = b64tos30(vbar1 + 1); |
| 7540 | if (val > 0) |
| 7541 | txn->cookie_first_date = val << 2; |
| 7542 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7543 | } |
| 7544 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7545 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7546 | /* if the cookie has an expiration date and the proxy wants to check |
| 7547 | * it, then we do that now. We first check if the cookie is too old, |
| 7548 | * then only if it has expired. We detect strict overflow because the |
| 7549 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7550 | * in the future are ignored if their offset is beyond one day. This |
| 7551 | * allows an admin to fix timezone issues without expiring everyone |
| 7552 | * and at the same time avoids keeping unwanted side effects for too |
| 7553 | * long. |
| 7554 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7555 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7556 | (((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] | 7557 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7558 | txn->flags &= ~TX_CK_MASK; |
| 7559 | txn->flags |= TX_CK_OLD; |
| 7560 | delim = val_beg; // let's pretend we have not found the cookie |
| 7561 | txn->cookie_first_date = 0; |
| 7562 | txn->cookie_last_date = 0; |
| 7563 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7564 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7565 | (((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] | 7566 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7567 | txn->flags &= ~TX_CK_MASK; |
| 7568 | txn->flags |= TX_CK_EXPIRED; |
| 7569 | delim = val_beg; // let's pretend we have not found the cookie |
| 7570 | txn->cookie_first_date = 0; |
| 7571 | txn->cookie_last_date = 0; |
| 7572 | } |
| 7573 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7574 | /* Here, we'll look for the first running server which supports the cookie. |
| 7575 | * This allows to share a same cookie between several servers, for example |
| 7576 | * to dedicate backup servers to specific servers only. |
| 7577 | * However, to prevent clients from sticking to cookie-less backup server |
| 7578 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7579 | * empty cookies and mark them as invalid. |
| 7580 | * The same behaviour is applied when persistence must be ignored. |
| 7581 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7582 | if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7583 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7584 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7585 | while (srv) { |
| 7586 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7587 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7588 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7589 | (s->be->options & PR_O_PERSIST) || |
| 7590 | (s->flags & SN_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7591 | /* we found the server and we can use it */ |
| 7592 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7593 | 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] | 7594 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 7595 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7596 | break; |
| 7597 | } else { |
| 7598 | /* we found a server, but it's down, |
| 7599 | * mark it as such and go on in case |
| 7600 | * another one is available. |
| 7601 | */ |
| 7602 | txn->flags &= ~TX_CK_MASK; |
| 7603 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7604 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7605 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7606 | srv = srv->next; |
| 7607 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7608 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7609 | 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] | 7610 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7611 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7612 | if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7613 | txn->flags |= TX_CK_UNUSED; |
| 7614 | else |
| 7615 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7616 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7617 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7618 | /* depending on the cookie mode, we may have to either : |
| 7619 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7620 | * the server never sees it ; |
| 7621 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7622 | * application cookie so that it does not get accidentely removed later, |
| 7623 | * if we're in cookie prefix mode |
| 7624 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7625 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7626 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7627 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7628 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7629 | val_end += delta; |
| 7630 | next += delta; |
| 7631 | hdr_end += delta; |
| 7632 | hdr_next += delta; |
| 7633 | cur_hdr->len += delta; |
| 7634 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7635 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7636 | del_from = NULL; |
| 7637 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7638 | } |
| 7639 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7640 | (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] | 7641 | del_from = prev; |
| 7642 | } |
| 7643 | } else { |
| 7644 | /* This is not our cookie, so we must preserve it. But if we already |
| 7645 | * scheduled another cookie for removal, we cannot remove the |
| 7646 | * complete header, but we can remove the previous block itself. |
| 7647 | */ |
| 7648 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7649 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7650 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7651 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7652 | if (att_beg >= del_from) |
| 7653 | att_beg += delta; |
| 7654 | if (att_end >= del_from) |
| 7655 | att_end += delta; |
| 7656 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7657 | val_end += delta; |
| 7658 | next += delta; |
| 7659 | hdr_end += delta; |
| 7660 | hdr_next += delta; |
| 7661 | cur_hdr->len += delta; |
| 7662 | http_msg_move_end(&txn->req, delta); |
| 7663 | prev = del_from; |
| 7664 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7665 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7666 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7667 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7668 | /* Look for the appsession cookie unless persistence must be ignored */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7669 | if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7670 | int cmp_len, value_len; |
| 7671 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7672 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7673 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 7674 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 7675 | value_begin = att_beg + s->be->appsession_name_len; |
| 7676 | value_len = val_end - att_beg - s->be->appsession_name_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7677 | } else { |
| 7678 | cmp_len = att_end - att_beg; |
| 7679 | value_begin = val_beg; |
| 7680 | value_len = val_end - val_beg; |
| 7681 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7682 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7683 | /* let's see if the cookie is our appcookie */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7684 | if (cmp_len == s->be->appsession_name_len && |
| 7685 | memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) { |
| 7686 | manage_client_side_appsession(s, value_begin, value_len); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7687 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7688 | } |
| 7689 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7690 | /* continue with next cookie on this header line */ |
| 7691 | att_beg = next; |
| 7692 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7693 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7694 | /* There are no more cookies on this line. |
| 7695 | * We may still have one (or several) marked for deletion at the |
| 7696 | * end of the line. We must do this now in two ways : |
| 7697 | * - if some cookies must be preserved, we only delete from the |
| 7698 | * mark to the end of line ; |
| 7699 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7700 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7701 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7702 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7703 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7704 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7705 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7706 | cur_hdr->len += delta; |
| 7707 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7708 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7709 | |
| 7710 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7711 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7712 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7713 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7714 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7715 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7716 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7717 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7718 | } |
| 7719 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7720 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7721 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7722 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7723 | } |
| 7724 | |
| 7725 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7726 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7727 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7728 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7729 | 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] | 7730 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7731 | char *cur_ptr, *cur_end, *cur_next; |
| 7732 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7733 | struct http_txn *txn = &s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7734 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7735 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7736 | |
| 7737 | last_hdr = 0; |
| 7738 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7739 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7740 | old_idx = 0; |
| 7741 | |
| 7742 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7743 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7744 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7745 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7746 | (exp->action == ACT_ALLOW || |
| 7747 | exp->action == ACT_DENY)) |
| 7748 | return 0; |
| 7749 | |
| 7750 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7751 | if (!cur_idx) |
| 7752 | break; |
| 7753 | |
| 7754 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7755 | cur_ptr = cur_next; |
| 7756 | cur_end = cur_ptr + cur_hdr->len; |
| 7757 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7758 | |
| 7759 | /* Now we have one header between cur_ptr and cur_end, |
| 7760 | * and the next header starts at cur_next. |
| 7761 | */ |
| 7762 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 7763 | 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] | 7764 | switch (exp->action) { |
| 7765 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7766 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7767 | last_hdr = 1; |
| 7768 | break; |
| 7769 | |
| 7770 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7771 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7772 | last_hdr = 1; |
| 7773 | break; |
| 7774 | |
| 7775 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7776 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7777 | if (trash.len < 0) |
| 7778 | return -1; |
| 7779 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7780 | 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] | 7781 | /* FIXME: if the user adds a newline in the replacement, the |
| 7782 | * index will not be recalculated for now, and the new line |
| 7783 | * will not be counted as a new header. |
| 7784 | */ |
| 7785 | |
| 7786 | cur_end += delta; |
| 7787 | cur_next += delta; |
| 7788 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7789 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7790 | break; |
| 7791 | |
| 7792 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7793 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7794 | cur_next += delta; |
| 7795 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7796 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7797 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7798 | txn->hdr_idx.used--; |
| 7799 | cur_hdr->len = 0; |
| 7800 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7801 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7802 | break; |
| 7803 | |
| 7804 | } |
| 7805 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7806 | |
| 7807 | /* keep the link from this header to next one in case of later |
| 7808 | * removal of next header. |
| 7809 | */ |
| 7810 | old_idx = cur_idx; |
| 7811 | } |
| 7812 | return 0; |
| 7813 | } |
| 7814 | |
| 7815 | |
| 7816 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7817 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7818 | * or -1 if a replacement resulted in an invalid status line. |
| 7819 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7820 | 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] | 7821 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7822 | char *cur_ptr, *cur_end; |
| 7823 | int done; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7824 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7825 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7826 | |
| 7827 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7828 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7829 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7830 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7831 | (exp->action == ACT_ALLOW || |
| 7832 | exp->action == ACT_DENY)) |
| 7833 | return 0; |
| 7834 | else if (exp->action == ACT_REMOVE) |
| 7835 | return 0; |
| 7836 | |
| 7837 | done = 0; |
| 7838 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7839 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7840 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7841 | |
| 7842 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7843 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 7844 | 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] | 7845 | switch (exp->action) { |
| 7846 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7847 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7848 | done = 1; |
| 7849 | break; |
| 7850 | |
| 7851 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7852 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7853 | done = 1; |
| 7854 | break; |
| 7855 | |
| 7856 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7857 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7858 | if (trash.len < 0) |
| 7859 | return -1; |
| 7860 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7861 | 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] | 7862 | /* FIXME: if the user adds a newline in the replacement, the |
| 7863 | * index will not be recalculated for now, and the new line |
| 7864 | * will not be counted as a new header. |
| 7865 | */ |
| 7866 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7867 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7868 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7869 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7870 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7871 | cur_ptr, cur_end + 1, |
| 7872 | NULL, NULL); |
| 7873 | if (unlikely(!cur_end)) |
| 7874 | return -1; |
| 7875 | |
| 7876 | /* we have a full respnse and we know that we have either a CR |
| 7877 | * or an LF at <ptr>. |
| 7878 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7879 | 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] | 7880 | 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] | 7881 | /* there is no point trying this regex on headers */ |
| 7882 | return 1; |
| 7883 | } |
| 7884 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7885 | return done; |
| 7886 | } |
| 7887 | |
| 7888 | |
| 7889 | |
| 7890 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7891 | * 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] | 7892 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7893 | * unparsable response. |
| 7894 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7895 | 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] | 7896 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7897 | struct http_txn *txn = &s->txn; |
| 7898 | struct hdr_exp *exp; |
| 7899 | |
| 7900 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7901 | int ret; |
| 7902 | |
| 7903 | /* |
| 7904 | * The interleaving of transformations and verdicts |
| 7905 | * makes it difficult to decide to continue or stop |
| 7906 | * the evaluation. |
| 7907 | */ |
| 7908 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7909 | if (txn->flags & TX_SVDENY) |
| 7910 | break; |
| 7911 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7912 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7913 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7914 | exp->action == ACT_PASS)) { |
| 7915 | exp = exp->next; |
| 7916 | continue; |
| 7917 | } |
| 7918 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7919 | /* if this filter had a condition, evaluate it now and skip to |
| 7920 | * next filter if the condition does not match. |
| 7921 | */ |
| 7922 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7923 | 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] | 7924 | ret = acl_pass(ret); |
| 7925 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7926 | ret = !ret; |
| 7927 | if (!ret) |
| 7928 | continue; |
| 7929 | } |
| 7930 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7931 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7932 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7933 | if (unlikely(ret < 0)) |
| 7934 | return -1; |
| 7935 | |
| 7936 | if (likely(ret == 0)) { |
| 7937 | /* The filter did not match the response, it can be |
| 7938 | * iterated through all headers. |
| 7939 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7940 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 7941 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7942 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7943 | } |
| 7944 | return 0; |
| 7945 | } |
| 7946 | |
| 7947 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7948 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7949 | * 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] | 7950 | * desirable to call it only when needed. This function is also used when we |
| 7951 | * 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] | 7952 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7953 | void manage_server_side_cookies(struct session *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7954 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7955 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7956 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7957 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7958 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7959 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7960 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7961 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7962 | /* Iterate through the headers. |
| 7963 | * we start with the start line. |
| 7964 | */ |
| 7965 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7966 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7967 | |
| 7968 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7969 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7970 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7971 | |
| 7972 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7973 | hdr_beg = hdr_next; |
| 7974 | hdr_end = hdr_beg + cur_hdr->len; |
| 7975 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7976 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7977 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7978 | * next header starts at hdr_next. We're only interested in |
| 7979 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7980 | */ |
| 7981 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7982 | is_cookie2 = 0; |
| 7983 | prev = hdr_beg + 10; |
| 7984 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7985 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7986 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7987 | if (!val) { |
| 7988 | old_idx = cur_idx; |
| 7989 | continue; |
| 7990 | } |
| 7991 | is_cookie2 = 1; |
| 7992 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7993 | } |
| 7994 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7995 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7996 | * <prev> points to the colon. |
| 7997 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7998 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7999 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8000 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8001 | * check-cache is enabled) and we are not interested in checking |
| 8002 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8003 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8004 | if (s->be->cookie_name == NULL && |
| 8005 | s->be->appsession_name == NULL && |
| 8006 | s->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8007 | return; |
| 8008 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8009 | /* OK so now we know we have to process this response cookie. |
| 8010 | * The format of the Set-Cookie header is slightly different |
| 8011 | * from the format of the Cookie header in that it does not |
| 8012 | * support the comma as a cookie delimiter (thus the header |
| 8013 | * cannot be folded) because the Expires attribute described in |
| 8014 | * the original Netscape's spec may contain an unquoted date |
| 8015 | * with a comma inside. We have to live with this because |
| 8016 | * many browsers don't support Max-Age and some browsers don't |
| 8017 | * support quoted strings. However the Set-Cookie2 header is |
| 8018 | * clean. |
| 8019 | * |
| 8020 | * We have to keep multiple pointers in order to support cookie |
| 8021 | * removal at the beginning, middle or end of header without |
| 8022 | * corrupting the header (in case of set-cookie2). A special |
| 8023 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8024 | * fields after the first semi-colon. The <next> pointer points |
| 8025 | * either to the end of line (set-cookie) or next unquoted comma |
| 8026 | * (set-cookie2). All of these headers are valid : |
| 8027 | * |
| 8028 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8029 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8030 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8031 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8032 | * | | | | | | | | | | |
| 8033 | * | | | | | | | | +-> next hdr_end <--+ |
| 8034 | * | | | | | | | +------------> scav |
| 8035 | * | | | | | | +--------------> val_end |
| 8036 | * | | | | | +--------------------> val_beg |
| 8037 | * | | | | +----------------------> equal |
| 8038 | * | | | +------------------------> att_end |
| 8039 | * | | +----------------------------> att_beg |
| 8040 | * | +------------------------------> prev |
| 8041 | * +-----------------------------------------> hdr_beg |
| 8042 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8043 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8044 | for (; prev < hdr_end; prev = next) { |
| 8045 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8046 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8047 | /* find att_beg */ |
| 8048 | att_beg = prev + 1; |
| 8049 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8050 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8051 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8052 | /* find att_end : this is the first character after the last non |
| 8053 | * space before the equal. It may be equal to hdr_end. |
| 8054 | */ |
| 8055 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8056 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8057 | while (equal < hdr_end) { |
| 8058 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8059 | break; |
| 8060 | if (http_is_spht[(unsigned char)*equal++]) |
| 8061 | continue; |
| 8062 | att_end = equal; |
| 8063 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8064 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8065 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8066 | * is between <att_beg> and <equal>, both may be identical. |
| 8067 | */ |
| 8068 | |
| 8069 | /* look for end of cookie if there is an equal sign */ |
| 8070 | if (equal < hdr_end && *equal == '=') { |
| 8071 | /* look for the beginning of the value */ |
| 8072 | val_beg = equal + 1; |
| 8073 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8074 | val_beg++; |
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 | /* find the end of the value, respecting quotes */ |
| 8077 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8078 | |
| 8079 | /* make val_end point to the first white space or delimitor after the value */ |
| 8080 | val_end = next; |
| 8081 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8082 | val_end--; |
| 8083 | } else { |
| 8084 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8085 | val_beg = val_end = next = equal; |
| 8086 | } |
| 8087 | |
| 8088 | if (next < hdr_end) { |
| 8089 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8090 | * a colon or semi-colon before the end. So skip all attr-value |
| 8091 | * pairs and look for the next comma. For Set-Cookie, since |
| 8092 | * commas are permitted in values, skip to the end. |
| 8093 | */ |
| 8094 | if (is_cookie2) |
| 8095 | next = find_hdr_value_end(next, hdr_end); |
| 8096 | else |
| 8097 | next = hdr_end; |
| 8098 | } |
| 8099 | |
| 8100 | /* Now everything is as on the diagram above */ |
| 8101 | |
| 8102 | /* Ignore cookies with no equal sign */ |
| 8103 | if (equal == val_end) |
| 8104 | continue; |
| 8105 | |
| 8106 | /* If there are spaces around the equal sign, we need to |
| 8107 | * strip them otherwise we'll get trouble for cookie captures, |
| 8108 | * or even for rewrites. Since this happens extremely rarely, |
| 8109 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8110 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8111 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8112 | int stripped_before = 0; |
| 8113 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8114 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8115 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8116 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8117 | equal += stripped_before; |
| 8118 | val_beg += stripped_before; |
| 8119 | } |
| 8120 | |
| 8121 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8122 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8123 | val_beg += stripped_after; |
| 8124 | stripped_before += stripped_after; |
| 8125 | } |
| 8126 | |
| 8127 | val_end += stripped_before; |
| 8128 | next += stripped_before; |
| 8129 | hdr_end += stripped_before; |
| 8130 | hdr_next += stripped_before; |
| 8131 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8132 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8133 | } |
| 8134 | |
| 8135 | /* First, let's see if we want to capture this cookie. We check |
| 8136 | * that we don't already have a server side cookie, because we |
| 8137 | * can only capture one. Also as an optimisation, we ignore |
| 8138 | * cookies shorter than the declared name. |
| 8139 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8140 | if (s->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8141 | txn->srv_cookie == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8142 | (val_end - att_beg >= s->fe->capture_namelen) && |
| 8143 | memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8144 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8145 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8146 | Alert("HTTP logging : out of memory.\n"); |
| 8147 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8148 | else { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8149 | if (log_len > s->fe->capture_len) |
| 8150 | log_len = s->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8151 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8152 | txn->srv_cookie[log_len] = 0; |
| 8153 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8154 | } |
| 8155 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8156 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8157 | /* now check if we need to process it for persistence */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8158 | if (!(s->flags & SN_IGNORE_PRST) && |
| 8159 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8160 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8161 | /* assume passive cookie by default */ |
| 8162 | txn->flags &= ~TX_SCK_MASK; |
| 8163 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8164 | |
| 8165 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8166 | * this occurrence because we'll insert another one later. |
| 8167 | * 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] | 8168 | * a direct access. |
| 8169 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8170 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8171 | /* The "preserve" flag was set, we don't want to touch the |
| 8172 | * server's cookie. |
| 8173 | */ |
| 8174 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8175 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 8176 | ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8177 | /* this cookie must be deleted */ |
| 8178 | if (*prev == ':' && next == hdr_end) { |
| 8179 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8180 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8181 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8182 | txn->hdr_idx.used--; |
| 8183 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8184 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8185 | hdr_next += delta; |
| 8186 | http_msg_move_end(&txn->rsp, delta); |
| 8187 | /* note: while both invalid now, <next> and <hdr_end> |
| 8188 | * are still equal, so the for() will stop as expected. |
| 8189 | */ |
| 8190 | } else { |
| 8191 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8192 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8193 | next = prev; |
| 8194 | hdr_end += delta; |
| 8195 | hdr_next += delta; |
| 8196 | cur_hdr->len += delta; |
| 8197 | http_msg_move_end(&txn->rsp, delta); |
| 8198 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8199 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8200 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8201 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8202 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8203 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8204 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8205 | * with this server since we know it. |
| 8206 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8207 | 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] | 8208 | next += delta; |
| 8209 | hdr_end += delta; |
| 8210 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8211 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8212 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8213 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8214 | txn->flags &= ~TX_SCK_MASK; |
| 8215 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8216 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8217 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8218 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8219 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8220 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8221 | 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] | 8222 | next += delta; |
| 8223 | hdr_end += delta; |
| 8224 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8225 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8226 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8227 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8228 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8229 | txn->flags &= ~TX_SCK_MASK; |
| 8230 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8231 | } |
| 8232 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 8233 | /* 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] | 8234 | else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8235 | int cmp_len, value_len; |
| 8236 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8237 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8238 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 8239 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 8240 | value_begin = att_beg + s->be->appsession_name_len; |
| 8241 | 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] | 8242 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8243 | cmp_len = att_end - att_beg; |
| 8244 | value_begin = val_beg; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8245 | value_len = MIN(s->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8246 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8247 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8248 | if ((cmp_len == s->be->appsession_name_len) && |
| 8249 | (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] | 8250 | /* free a possibly previously allocated memory */ |
| 8251 | pool_free2(apools.sessid, txn->sessid); |
| 8252 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8253 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8254 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8255 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8256 | 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] | 8257 | return; |
| 8258 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8259 | memcpy(txn->sessid, value_begin, value_len); |
| 8260 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8261 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8262 | } |
| 8263 | /* that's done for this cookie, check the next one on the same |
| 8264 | * line when next != hdr_end (only if is_cookie2). |
| 8265 | */ |
| 8266 | } |
| 8267 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8268 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8269 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8270 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8271 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8272 | appsess *asession = NULL; |
| 8273 | /* only do insert, if lookup fails */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8274 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8275 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 8276 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8277 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 8278 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8279 | 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] | 8280 | return; |
| 8281 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8282 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 8283 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8284 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 8285 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8286 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8287 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8288 | return; |
| 8289 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8290 | memcpy(asession->sessid, txn->sessid, s->be->appsession_len); |
| 8291 | asession->sessid[s->be->appsession_len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8292 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8293 | server_id_len = strlen(objt_server(s->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8294 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8295 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8296 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8297 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8298 | return; |
| 8299 | } |
| 8300 | asession->serverid[0] = '\0'; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8301 | memcpy(asession->serverid, objt_server(s->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8302 | |
| 8303 | asession->request_count = 0; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8304 | appsession_hash_insert(&(s->be->htbl_proxy), asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8305 | } |
| 8306 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8307 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8308 | asession->request_count++; |
| 8309 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8310 | } |
| 8311 | |
| 8312 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8313 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8314 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8315 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8316 | void check_response_for_cacheability(struct session *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8317 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8318 | struct http_txn *txn = &s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8319 | char *p1, *p2; |
| 8320 | |
| 8321 | char *cur_ptr, *cur_end, *cur_next; |
| 8322 | int cur_idx; |
| 8323 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8324 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8325 | return; |
| 8326 | |
| 8327 | /* Iterate through the headers. |
| 8328 | * we start with the start line. |
| 8329 | */ |
| 8330 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8331 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8332 | |
| 8333 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8334 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8335 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8336 | |
| 8337 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8338 | cur_ptr = cur_next; |
| 8339 | cur_end = cur_ptr + cur_hdr->len; |
| 8340 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8341 | |
| 8342 | /* We have one full header between cur_ptr and cur_end, and the |
| 8343 | * next header starts at cur_next. We're only interested in |
| 8344 | * "Cookie:" headers. |
| 8345 | */ |
| 8346 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8347 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8348 | if (val) { |
| 8349 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8350 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8351 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8352 | return; |
| 8353 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8354 | } |
| 8355 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8356 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8357 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8358 | continue; |
| 8359 | |
| 8360 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8361 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8362 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8363 | |
| 8364 | if (p1 >= cur_end) /* no more info */ |
| 8365 | continue; |
| 8366 | |
| 8367 | /* p1 is at the beginning of the value */ |
| 8368 | p2 = p1; |
| 8369 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8370 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8371 | p2++; |
| 8372 | |
| 8373 | /* we have a complete value between p1 and p2 */ |
| 8374 | if (p2 < cur_end && *p2 == '=') { |
| 8375 | /* we have something of the form no-cache="set-cookie" */ |
| 8376 | if ((cur_end - p1 >= 21) && |
| 8377 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8378 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8379 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8380 | continue; |
| 8381 | } |
| 8382 | |
| 8383 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8384 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8385 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8386 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8387 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8388 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8389 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8390 | return; |
| 8391 | } |
| 8392 | |
| 8393 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8394 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8395 | continue; |
| 8396 | } |
| 8397 | } |
| 8398 | } |
| 8399 | |
| 8400 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8401 | /* |
| 8402 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 8403 | * If the server is found, it's assigned to the session. |
| 8404 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8405 | 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] | 8406 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8407 | char *end_params, *first_param, *cur_param, *next_param; |
| 8408 | char separator; |
| 8409 | int value_len; |
| 8410 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8411 | int mode = s->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8412 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8413 | if (s->be->appsession_name == NULL || |
| 8414 | (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] | 8415 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8416 | } |
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 | first_param = NULL; |
| 8419 | switch (mode) { |
| 8420 | case PR_O2_AS_M_PP: |
| 8421 | first_param = memchr(begin, ';', len); |
| 8422 | break; |
| 8423 | case PR_O2_AS_M_QS: |
| 8424 | first_param = memchr(begin, '?', len); |
| 8425 | break; |
| 8426 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8427 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8428 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8429 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8430 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8431 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8432 | switch (mode) { |
| 8433 | case PR_O2_AS_M_PP: |
| 8434 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 8435 | end_params = (char *) begin + len; |
| 8436 | } |
| 8437 | separator = ';'; |
| 8438 | break; |
| 8439 | case PR_O2_AS_M_QS: |
| 8440 | end_params = (char *) begin + len; |
| 8441 | separator = '&'; |
| 8442 | break; |
| 8443 | default: |
| 8444 | /* unknown mode, shouldn't happen */ |
| 8445 | return; |
| 8446 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8447 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8448 | cur_param = next_param = end_params; |
| 8449 | while (cur_param > first_param) { |
| 8450 | cur_param--; |
| 8451 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 8452 | /* let's see if this is the appsession parameter */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8453 | if ((cur_param + s->be->appsession_name_len + 1 < next_param) && |
| 8454 | ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') && |
| 8455 | (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] | 8456 | /* Cool... it's the right one */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8457 | cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 8458 | value_len = MIN(s->be->appsession_len, next_param - cur_param); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8459 | if (value_len > 0) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8460 | manage_client_side_appsession(s, cur_param, value_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8461 | } |
| 8462 | break; |
| 8463 | } |
| 8464 | next_param = cur_param; |
| 8465 | } |
| 8466 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8467 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 8468 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8469 | appsession_hash_dump(&(s->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8470 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8471 | } |
| 8472 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8473 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8474 | * 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] | 8475 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8476 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8477 | * 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] | 8478 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8479 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8480 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8481 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8482 | 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] | 8483 | { |
| 8484 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8485 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8486 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8487 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8488 | if (!uri_auth) |
| 8489 | return 0; |
| 8490 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8491 | 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] | 8492 | return 0; |
| 8493 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8494 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8495 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8496 | return 0; |
| 8497 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8498 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8499 | return 0; |
| 8500 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8501 | return 1; |
| 8502 | } |
| 8503 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8504 | /* |
| 8505 | * 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] | 8506 | * By default it tries to report the error position as msg->err_pos. However if |
| 8507 | * this one is not set, it will then report msg->next, which is the last known |
| 8508 | * 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] | 8509 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8510 | */ |
| 8511 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8512 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8513 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8514 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8515 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8516 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8517 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8518 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8519 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8520 | len1 = MIN(len1, es->len); |
| 8521 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8522 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8523 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8524 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8525 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8526 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8527 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8528 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8529 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8530 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8531 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8532 | es->when = date; // user-visible date |
| 8533 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8534 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8535 | es->oe = other_end; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8536 | if (objt_conn(s->req->prod->end)) |
| 8537 | es->src = __objt_conn(s->req->prod->end)->addr.from; |
| 8538 | else |
| 8539 | memset(&es->src, 0, sizeof(es->src)); |
| 8540 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8541 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8542 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8543 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8544 | es->s_flags = s->flags; |
| 8545 | es->t_flags = s->txn.flags; |
| 8546 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8547 | es->b_out = chn->buf->o; |
| 8548 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8549 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8550 | es->m_clen = msg->chunk_len; |
| 8551 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8552 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8553 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8554 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8555 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8556 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8557 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8558 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8559 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8560 | * 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] | 8561 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8562 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8563 | * 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] | 8564 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8565 | 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] | 8566 | struct hdr_idx *idx, int occ, |
| 8567 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8568 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8569 | struct hdr_ctx local_ctx; |
| 8570 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8571 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8572 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8573 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8574 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8575 | if (!ctx) { |
| 8576 | local_ctx.idx = 0; |
| 8577 | ctx = &local_ctx; |
| 8578 | } |
| 8579 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8580 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8581 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8582 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8583 | occ--; |
| 8584 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8585 | *vptr = ctx->line + ctx->val; |
| 8586 | *vlen = ctx->vlen; |
| 8587 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8588 | } |
| 8589 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8590 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8591 | } |
| 8592 | |
| 8593 | /* negative occurrence, we scan all the list then walk back */ |
| 8594 | if (-occ > MAX_HDR_HISTORY) |
| 8595 | return 0; |
| 8596 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8597 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8598 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8599 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8600 | len_hist[hist_ptr] = ctx->vlen; |
| 8601 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8602 | hist_ptr = 0; |
| 8603 | found++; |
| 8604 | } |
| 8605 | if (-occ > found) |
| 8606 | return 0; |
| 8607 | /* 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] | 8608 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8609 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8610 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8611 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8612 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8613 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8614 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8615 | *vptr = ptr_hist[hist_ptr]; |
| 8616 | *vlen = len_hist[hist_ptr]; |
| 8617 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8618 | } |
| 8619 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8620 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8621 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8622 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8623 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8624 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8625 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8626 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8627 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8628 | * line header values and does not stop at commas. |
| 8629 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8630 | */ |
| 8631 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8632 | struct hdr_idx *idx, int occ, |
| 8633 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8634 | { |
| 8635 | struct hdr_ctx local_ctx; |
| 8636 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8637 | int len_hist[MAX_HDR_HISTORY]; |
| 8638 | unsigned int hist_ptr; |
| 8639 | int found; |
| 8640 | |
| 8641 | if (!ctx) { |
| 8642 | local_ctx.idx = 0; |
| 8643 | ctx = &local_ctx; |
| 8644 | } |
| 8645 | |
| 8646 | if (occ >= 0) { |
| 8647 | /* search from the beginning */ |
| 8648 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8649 | occ--; |
| 8650 | if (occ <= 0) { |
| 8651 | *vptr = ctx->line + ctx->val; |
| 8652 | *vlen = ctx->vlen; |
| 8653 | return 1; |
| 8654 | } |
| 8655 | } |
| 8656 | return 0; |
| 8657 | } |
| 8658 | |
| 8659 | /* negative occurrence, we scan all the list then walk back */ |
| 8660 | if (-occ > MAX_HDR_HISTORY) |
| 8661 | return 0; |
| 8662 | |
| 8663 | found = hist_ptr = 0; |
| 8664 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8665 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8666 | len_hist[hist_ptr] = ctx->vlen; |
| 8667 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8668 | hist_ptr = 0; |
| 8669 | found++; |
| 8670 | } |
| 8671 | if (-occ > found) |
| 8672 | return 0; |
| 8673 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8674 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8675 | */ |
| 8676 | hist_ptr += occ; |
| 8677 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8678 | hist_ptr -= MAX_HDR_HISTORY; |
| 8679 | *vptr = ptr_hist[hist_ptr]; |
| 8680 | *vlen = len_hist[hist_ptr]; |
| 8681 | return 1; |
| 8682 | } |
| 8683 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8684 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8685 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8686 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8687 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8688 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8689 | 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] | 8690 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8691 | int max; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8692 | 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] | 8693 | dir, |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8694 | objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1, |
| 8695 | 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] | 8696 | |
| 8697 | for (max = 0; start + max < end; max++) |
| 8698 | if (start[max] == '\r' || start[max] == '\n') |
| 8699 | break; |
| 8700 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8701 | UBOUND(max, trash.size - trash.len - 3); |
| 8702 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8703 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8704 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8705 | } |
| 8706 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8707 | /* |
| 8708 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 8709 | * the required fields are properly allocated and that we only need to (re)init |
| 8710 | * them. This should be used before processing any new request. |
| 8711 | */ |
| 8712 | void http_init_txn(struct session *s) |
| 8713 | { |
| 8714 | struct http_txn *txn = &s->txn; |
| 8715 | struct proxy *fe = s->fe; |
| 8716 | |
| 8717 | txn->flags = 0; |
| 8718 | txn->status = -1; |
| 8719 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8720 | txn->cookie_first_date = 0; |
| 8721 | txn->cookie_last_date = 0; |
| 8722 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8723 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8724 | 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] | 8725 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8726 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8727 | 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] | 8728 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 8729 | txn->req.chunk_len = 0LL; |
| 8730 | txn->req.body_len = 0LL; |
| 8731 | txn->rsp.chunk_len = 0LL; |
| 8732 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8733 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8734 | 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] | 8735 | txn->req.chn = s->req; |
| 8736 | txn->rsp.chn = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8737 | |
| 8738 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8739 | |
| 8740 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8741 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8742 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8743 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8744 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8745 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 8746 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8747 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8748 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 8749 | |
| 8750 | if (txn->hdr_idx.v) |
| 8751 | hdr_idx_init(&txn->hdr_idx); |
| 8752 | } |
| 8753 | |
| 8754 | /* to be used at the end of a transaction */ |
| 8755 | void http_end_txn(struct session *s) |
| 8756 | { |
| 8757 | struct http_txn *txn = &s->txn; |
| 8758 | |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8759 | /* release any possible compression context */ |
| 8760 | if (s->flags & SN_COMP_READY) |
| 8761 | s->comp_algo->end(&s->comp_ctx); |
| 8762 | s->comp_algo = NULL; |
| 8763 | s->flags &= ~SN_COMP_READY; |
| 8764 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8765 | /* these ones will have been dynamically allocated */ |
| 8766 | pool_free2(pool2_requri, txn->uri); |
| 8767 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8768 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8769 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8770 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8771 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8772 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8773 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8774 | txn->uri = NULL; |
| 8775 | txn->srv_cookie = NULL; |
| 8776 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8777 | |
| 8778 | if (txn->req.cap) { |
| 8779 | struct cap_hdr *h; |
| 8780 | for (h = s->fe->req_cap; h; h = h->next) |
| 8781 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 8782 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 8783 | } |
| 8784 | |
| 8785 | if (txn->rsp.cap) { |
| 8786 | struct cap_hdr *h; |
| 8787 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 8788 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 8789 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 8790 | } |
| 8791 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8792 | } |
| 8793 | |
| 8794 | /* to be used at the end of a transaction to prepare a new one */ |
| 8795 | void http_reset_txn(struct session *s) |
| 8796 | { |
| 8797 | http_end_txn(s); |
| 8798 | http_init_txn(s); |
| 8799 | |
| 8800 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8801 | s->logs.logwait = s->fe->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8802 | s->logs.level = 0; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 8803 | session_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8804 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8805 | /* re-init store persistence */ |
| 8806 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8807 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8808 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8809 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8810 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 8811 | s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8812 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8813 | /* We must trim any excess data from the response buffer, because we |
| 8814 | * may have blocked an invalid response from a server that we don't |
| 8815 | * want to accidentely forward once we disable the analysers, nor do |
| 8816 | * we want those data to come along with next response. A typical |
| 8817 | * example of such data would be from a buggy server responding to |
| 8818 | * a HEAD with some data, or sending more than the advertised |
| 8819 | * content-length. |
| 8820 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8821 | if (unlikely(s->rep->buf->i)) |
| 8822 | s->rep->buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8823 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8824 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8825 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8826 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8827 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8828 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8829 | |
| 8830 | s->req->rex = TICK_ETERNITY; |
| 8831 | s->req->wex = TICK_ETERNITY; |
| 8832 | s->req->analyse_exp = TICK_ETERNITY; |
| 8833 | s->rep->rex = TICK_ETERNITY; |
| 8834 | s->rep->wex = TICK_ETERNITY; |
| 8835 | s->rep->analyse_exp = TICK_ETERNITY; |
| 8836 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8837 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8838 | void free_http_res_rules(struct list *r) |
| 8839 | { |
| 8840 | struct http_res_rule *tr, *pr; |
| 8841 | |
| 8842 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8843 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8844 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8845 | free(pr); |
| 8846 | } |
| 8847 | } |
| 8848 | |
| 8849 | void free_http_req_rules(struct list *r) |
| 8850 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8851 | struct http_req_rule *tr, *pr; |
| 8852 | |
| 8853 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8854 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8855 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8856 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8857 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8858 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8859 | free(pr); |
| 8860 | } |
| 8861 | } |
| 8862 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8863 | /* parse an "http-request" rule */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8864 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8865 | { |
| 8866 | struct http_req_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8867 | struct http_req_action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8868 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8869 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8870 | |
| 8871 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 8872 | if (!rule) { |
| 8873 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8874 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8875 | } |
| 8876 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8877 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8878 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 8879 | cur_arg = 1; |
Willy Tarreau | 5bd6759 | 2014-04-28 22:00:46 +0200 | [diff] [blame] | 8880 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8881 | rule->action = HTTP_REQ_ACT_DENY; |
| 8882 | cur_arg = 1; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8883 | } else if (!strcmp(args[0], "tarpit")) { |
| 8884 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 8885 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8886 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8887 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8888 | cur_arg = 1; |
| 8889 | |
| 8890 | while(*args[cur_arg]) { |
| 8891 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8892 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8893 | cur_arg+=2; |
| 8894 | continue; |
| 8895 | } else |
| 8896 | break; |
| 8897 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8898 | } else if (!strcmp(args[0], "set-nice")) { |
| 8899 | rule->action = HTTP_REQ_ACT_SET_NICE; |
| 8900 | cur_arg = 1; |
| 8901 | |
| 8902 | if (!*args[cur_arg] || |
| 8903 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8904 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8905 | file, linenum, args[0]); |
| 8906 | goto out_err; |
| 8907 | } |
| 8908 | rule->arg.nice = atoi(args[cur_arg]); |
| 8909 | if (rule->arg.nice < -1024) |
| 8910 | rule->arg.nice = -1024; |
| 8911 | else if (rule->arg.nice > 1024) |
| 8912 | rule->arg.nice = 1024; |
| 8913 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8914 | } else if (!strcmp(args[0], "set-tos")) { |
| 8915 | #ifdef IP_TOS |
| 8916 | char *err; |
| 8917 | rule->action = HTTP_REQ_ACT_SET_TOS; |
| 8918 | cur_arg = 1; |
| 8919 | |
| 8920 | if (!*args[cur_arg] || |
| 8921 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8922 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8923 | file, linenum, args[0]); |
| 8924 | goto out_err; |
| 8925 | } |
| 8926 | |
| 8927 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8928 | if (err && *err != '\0') { |
| 8929 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8930 | file, linenum, err, args[0]); |
| 8931 | goto out_err; |
| 8932 | } |
| 8933 | cur_arg++; |
| 8934 | #else |
| 8935 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8936 | goto out_err; |
| 8937 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8938 | } else if (!strcmp(args[0], "set-mark")) { |
| 8939 | #ifdef SO_MARK |
| 8940 | char *err; |
| 8941 | rule->action = HTTP_REQ_ACT_SET_MARK; |
| 8942 | cur_arg = 1; |
| 8943 | |
| 8944 | if (!*args[cur_arg] || |
| 8945 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8946 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8947 | file, linenum, args[0]); |
| 8948 | goto out_err; |
| 8949 | } |
| 8950 | |
| 8951 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8952 | if (err && *err != '\0') { |
| 8953 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8954 | file, linenum, err, args[0]); |
| 8955 | goto out_err; |
| 8956 | } |
| 8957 | cur_arg++; |
| 8958 | global.last_checks |= LSTCHK_NETADM; |
| 8959 | #else |
| 8960 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8961 | goto out_err; |
| 8962 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8963 | } else if (!strcmp(args[0], "set-log-level")) { |
| 8964 | rule->action = HTTP_REQ_ACT_SET_LOGL; |
| 8965 | cur_arg = 1; |
| 8966 | |
| 8967 | if (!*args[cur_arg] || |
| 8968 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8969 | bad_log_level: |
| 8970 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8971 | file, linenum, args[0]); |
| 8972 | goto out_err; |
| 8973 | } |
| 8974 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8975 | rule->arg.loglevel = -1; |
| 8976 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8977 | goto bad_log_level; |
| 8978 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8979 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 8980 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 8981 | cur_arg = 1; |
| 8982 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 8983 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8984 | (*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] | 8985 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8986 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8987 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8988 | } |
| 8989 | |
| 8990 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8991 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8992 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 8993 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8994 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 8995 | 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] | 8996 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 8997 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8998 | free(proxy->conf.lfs_file); |
| 8999 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9000 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9001 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9002 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
| 9003 | 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] | 9004 | cur_arg = 1; |
| 9005 | |
| 9006 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 8f249f4 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 9007 | (*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] | 9008 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 9009 | file, linenum, args[0]); |
| 9010 | goto out_err; |
| 9011 | } |
| 9012 | |
| 9013 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9014 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9015 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9016 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9017 | error = NULL; |
| 9018 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9019 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9020 | args[cur_arg + 1], error); |
| 9021 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9022 | goto out_err; |
| 9023 | } |
| 9024 | |
| 9025 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9026 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9027 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9028 | file, linenum); |
| 9029 | |
| 9030 | free(proxy->conf.lfs_file); |
| 9031 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9032 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9033 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9034 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9035 | rule->action = HTTP_REQ_ACT_DEL_HDR; |
| 9036 | cur_arg = 1; |
| 9037 | |
| 9038 | if (!*args[cur_arg] || |
| 9039 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9040 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9041 | file, linenum, args[0]); |
| 9042 | goto out_err; |
| 9043 | } |
| 9044 | |
| 9045 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9046 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9047 | |
| 9048 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9049 | free(proxy->conf.lfs_file); |
| 9050 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9051 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9052 | cur_arg += 1; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9053 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9054 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9055 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9056 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9057 | 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] | 9058 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9059 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9060 | goto out_err; |
| 9061 | } |
| 9062 | |
| 9063 | /* this redirect rule might already contain a parsed condition which |
| 9064 | * we'll pass to the http-request rule. |
| 9065 | */ |
| 9066 | rule->action = HTTP_REQ_ACT_REDIR; |
| 9067 | rule->arg.redir = redir; |
| 9068 | rule->cond = redir->cond; |
| 9069 | redir->cond = NULL; |
| 9070 | cur_arg = 2; |
| 9071 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9072 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9073 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9074 | rule->action = HTTP_REQ_ACT_ADD_ACL; |
| 9075 | /* |
| 9076 | * '+ 8' for 'add-acl(' |
| 9077 | * '- 9' for 'add-acl(' + trailing ')' |
| 9078 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9079 | 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] | 9080 | |
| 9081 | cur_arg = 1; |
| 9082 | |
| 9083 | if (!*args[cur_arg] || |
| 9084 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9085 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9086 | file, linenum, args[0]); |
| 9087 | goto out_err; |
| 9088 | } |
| 9089 | |
| 9090 | LIST_INIT(&rule->arg.map.key); |
| 9091 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9092 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9093 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9094 | file, linenum); |
| 9095 | free(proxy->conf.lfs_file); |
| 9096 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9097 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9098 | cur_arg += 1; |
| 9099 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9100 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
| 9101 | rule->action = HTTP_REQ_ACT_DEL_ACL; |
| 9102 | /* |
| 9103 | * '+ 8' for 'del-acl(' |
| 9104 | * '- 9' for 'del-acl(' + trailing ')' |
| 9105 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9106 | 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] | 9107 | |
| 9108 | cur_arg = 1; |
| 9109 | |
| 9110 | if (!*args[cur_arg] || |
| 9111 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9112 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9113 | file, linenum, args[0]); |
| 9114 | goto out_err; |
| 9115 | } |
| 9116 | |
| 9117 | LIST_INIT(&rule->arg.map.key); |
| 9118 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9119 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9120 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9121 | file, linenum); |
| 9122 | free(proxy->conf.lfs_file); |
| 9123 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9124 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9125 | cur_arg += 1; |
| 9126 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9127 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
| 9128 | rule->action = HTTP_REQ_ACT_DEL_MAP; |
| 9129 | /* |
| 9130 | * '+ 8' for 'del-map(' |
| 9131 | * '- 9' for 'del-map(' + trailing ')' |
| 9132 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9133 | 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] | 9134 | |
| 9135 | cur_arg = 1; |
| 9136 | |
| 9137 | if (!*args[cur_arg] || |
| 9138 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9139 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9140 | file, linenum, args[0]); |
| 9141 | goto out_err; |
| 9142 | } |
| 9143 | |
| 9144 | LIST_INIT(&rule->arg.map.key); |
| 9145 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9146 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9147 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9148 | file, linenum); |
| 9149 | free(proxy->conf.lfs_file); |
| 9150 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9151 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9152 | cur_arg += 1; |
| 9153 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9154 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9155 | rule->action = HTTP_REQ_ACT_SET_MAP; |
| 9156 | /* |
| 9157 | * '+ 8' for 'set-map(' |
| 9158 | * '- 9' for 'set-map(' + trailing ')' |
| 9159 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9160 | 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] | 9161 | |
| 9162 | cur_arg = 1; |
| 9163 | |
| 9164 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9165 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9166 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9167 | file, linenum, args[0]); |
| 9168 | goto out_err; |
| 9169 | } |
| 9170 | |
| 9171 | LIST_INIT(&rule->arg.map.key); |
| 9172 | LIST_INIT(&rule->arg.map.value); |
| 9173 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9174 | |
| 9175 | /* key pattern */ |
| 9176 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9177 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9178 | file, linenum); |
| 9179 | |
| 9180 | /* value pattern */ |
| 9181 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9182 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9183 | file, linenum); |
| 9184 | free(proxy->conf.lfs_file); |
| 9185 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9186 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9187 | |
| 9188 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9189 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9190 | char *errmsg = NULL; |
| 9191 | cur_arg = 1; |
| 9192 | /* try in the module list */ |
| 9193 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9194 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9195 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9196 | free(errmsg); |
| 9197 | goto out_err; |
| 9198 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9199 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9200 | 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] | 9201 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9202 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9203 | } |
| 9204 | |
| 9205 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9206 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9207 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9208 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9209 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9210 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9211 | file, linenum, args[0], errmsg); |
| 9212 | free(errmsg); |
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 | rule->cond = cond; |
| 9216 | } |
| 9217 | else if (*args[cur_arg]) { |
| 9218 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9219 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9220 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9221 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9222 | } |
| 9223 | |
| 9224 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9225 | out_err: |
| 9226 | free(rule); |
| 9227 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9228 | } |
| 9229 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9230 | /* parse an "http-respose" rule */ |
| 9231 | struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 9232 | { |
| 9233 | struct http_res_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9234 | struct http_res_action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9235 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9236 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9237 | |
| 9238 | rule = calloc(1, sizeof(*rule)); |
| 9239 | if (!rule) { |
| 9240 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9241 | goto out_err; |
| 9242 | } |
| 9243 | |
| 9244 | if (!strcmp(args[0], "allow")) { |
| 9245 | rule->action = HTTP_RES_ACT_ALLOW; |
| 9246 | cur_arg = 1; |
| 9247 | } else if (!strcmp(args[0], "deny")) { |
| 9248 | rule->action = HTTP_RES_ACT_DENY; |
| 9249 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9250 | } else if (!strcmp(args[0], "set-nice")) { |
| 9251 | rule->action = HTTP_RES_ACT_SET_NICE; |
| 9252 | cur_arg = 1; |
| 9253 | |
| 9254 | if (!*args[cur_arg] || |
| 9255 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9256 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9257 | file, linenum, args[0]); |
| 9258 | goto out_err; |
| 9259 | } |
| 9260 | rule->arg.nice = atoi(args[cur_arg]); |
| 9261 | if (rule->arg.nice < -1024) |
| 9262 | rule->arg.nice = -1024; |
| 9263 | else if (rule->arg.nice > 1024) |
| 9264 | rule->arg.nice = 1024; |
| 9265 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9266 | } else if (!strcmp(args[0], "set-tos")) { |
| 9267 | #ifdef IP_TOS |
| 9268 | char *err; |
| 9269 | rule->action = HTTP_RES_ACT_SET_TOS; |
| 9270 | cur_arg = 1; |
| 9271 | |
| 9272 | if (!*args[cur_arg] || |
| 9273 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9274 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9275 | file, linenum, args[0]); |
| 9276 | goto out_err; |
| 9277 | } |
| 9278 | |
| 9279 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9280 | if (err && *err != '\0') { |
| 9281 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9282 | file, linenum, err, args[0]); |
| 9283 | goto out_err; |
| 9284 | } |
| 9285 | cur_arg++; |
| 9286 | #else |
| 9287 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9288 | goto out_err; |
| 9289 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9290 | } else if (!strcmp(args[0], "set-mark")) { |
| 9291 | #ifdef SO_MARK |
| 9292 | char *err; |
| 9293 | rule->action = HTTP_RES_ACT_SET_MARK; |
| 9294 | cur_arg = 1; |
| 9295 | |
| 9296 | if (!*args[cur_arg] || |
| 9297 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9298 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9299 | file, linenum, args[0]); |
| 9300 | goto out_err; |
| 9301 | } |
| 9302 | |
| 9303 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9304 | if (err && *err != '\0') { |
| 9305 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9306 | file, linenum, err, args[0]); |
| 9307 | goto out_err; |
| 9308 | } |
| 9309 | cur_arg++; |
| 9310 | global.last_checks |= LSTCHK_NETADM; |
| 9311 | #else |
| 9312 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9313 | goto out_err; |
| 9314 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9315 | } else if (!strcmp(args[0], "set-log-level")) { |
| 9316 | rule->action = HTTP_RES_ACT_SET_LOGL; |
| 9317 | cur_arg = 1; |
| 9318 | |
| 9319 | if (!*args[cur_arg] || |
| 9320 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9321 | bad_log_level: |
| 9322 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9323 | file, linenum, args[0]); |
| 9324 | goto out_err; |
| 9325 | } |
| 9326 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9327 | rule->arg.loglevel = -1; |
| 9328 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 9329 | goto bad_log_level; |
| 9330 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9331 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9332 | rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR; |
| 9333 | cur_arg = 1; |
| 9334 | |
| 9335 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9336 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9337 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9338 | file, linenum, args[0]); |
| 9339 | goto out_err; |
| 9340 | } |
| 9341 | |
| 9342 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9343 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9344 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9345 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9346 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9347 | 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] | 9348 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9349 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9350 | free(proxy->conf.lfs_file); |
| 9351 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9352 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9353 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9354 | } 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] | 9355 | 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] | 9356 | cur_arg = 1; |
| 9357 | |
| 9358 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | a772b94 | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9359 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9360 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9361 | file, linenum, args[0]); |
| 9362 | goto out_err; |
| 9363 | } |
| 9364 | |
| 9365 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9366 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9367 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9368 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9369 | error = NULL; |
| 9370 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9371 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9372 | args[cur_arg + 1], error); |
| 9373 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9374 | goto out_err; |
| 9375 | } |
| 9376 | |
| 9377 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9378 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9379 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9380 | file, linenum); |
| 9381 | |
| 9382 | free(proxy->conf.lfs_file); |
| 9383 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9384 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9385 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9386 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9387 | rule->action = HTTP_RES_ACT_DEL_HDR; |
| 9388 | cur_arg = 1; |
| 9389 | |
| 9390 | if (!*args[cur_arg] || |
| 9391 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9392 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9393 | file, linenum, args[0]); |
| 9394 | goto out_err; |
| 9395 | } |
| 9396 | |
| 9397 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9398 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9399 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9400 | proxy->conf.args.ctx = ARGC_HRS; |
| 9401 | free(proxy->conf.lfs_file); |
| 9402 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9403 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9404 | cur_arg += 1; |
| 9405 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9406 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9407 | rule->action = HTTP_RES_ACT_ADD_ACL; |
| 9408 | /* |
| 9409 | * '+ 8' for 'add-acl(' |
| 9410 | * '- 9' for 'add-acl(' + trailing ')' |
| 9411 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9412 | 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] | 9413 | |
| 9414 | cur_arg = 1; |
| 9415 | |
| 9416 | if (!*args[cur_arg] || |
| 9417 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9418 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9419 | file, linenum, args[0]); |
| 9420 | goto out_err; |
| 9421 | } |
| 9422 | |
| 9423 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9424 | proxy->conf.args.ctx = ARGC_HRS; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9425 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9426 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9427 | file, linenum); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9428 | free(proxy->conf.lfs_file); |
| 9429 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9430 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9431 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9432 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9433 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9434 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
| 9435 | rule->action = HTTP_RES_ACT_DEL_ACL; |
| 9436 | /* |
| 9437 | * '+ 8' for 'del-acl(' |
| 9438 | * '- 9' for 'del-acl(' + trailing ')' |
| 9439 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9440 | 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] | 9441 | |
| 9442 | cur_arg = 1; |
| 9443 | |
| 9444 | if (!*args[cur_arg] || |
| 9445 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9446 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9447 | file, linenum, args[0]); |
| 9448 | goto out_err; |
| 9449 | } |
| 9450 | |
| 9451 | LIST_INIT(&rule->arg.map.key); |
| 9452 | proxy->conf.args.ctx = ARGC_HRS; |
| 9453 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9454 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9455 | file, linenum); |
| 9456 | free(proxy->conf.lfs_file); |
| 9457 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9458 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9459 | cur_arg += 1; |
| 9460 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9461 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
| 9462 | rule->action = HTTP_RES_ACT_DEL_MAP; |
| 9463 | /* |
| 9464 | * '+ 8' for 'del-map(' |
| 9465 | * '- 9' for 'del-map(' + trailing ')' |
| 9466 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9467 | 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] | 9468 | |
| 9469 | cur_arg = 1; |
| 9470 | |
| 9471 | if (!*args[cur_arg] || |
| 9472 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9473 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9474 | file, linenum, args[0]); |
| 9475 | goto out_err; |
| 9476 | } |
| 9477 | |
| 9478 | LIST_INIT(&rule->arg.map.key); |
| 9479 | proxy->conf.args.ctx = ARGC_HRS; |
| 9480 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9481 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9482 | file, linenum); |
| 9483 | free(proxy->conf.lfs_file); |
| 9484 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9485 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9486 | cur_arg += 1; |
| 9487 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9488 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9489 | rule->action = HTTP_RES_ACT_SET_MAP; |
| 9490 | /* |
| 9491 | * '+ 8' for 'set-map(' |
| 9492 | * '- 9' for 'set-map(' + trailing ')' |
| 9493 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9494 | 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] | 9495 | |
| 9496 | cur_arg = 1; |
| 9497 | |
| 9498 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9499 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9500 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9501 | file, linenum, args[0]); |
| 9502 | goto out_err; |
| 9503 | } |
| 9504 | |
| 9505 | LIST_INIT(&rule->arg.map.key); |
| 9506 | LIST_INIT(&rule->arg.map.value); |
| 9507 | |
| 9508 | proxy->conf.args.ctx = ARGC_HRS; |
| 9509 | |
| 9510 | /* key pattern */ |
| 9511 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9512 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9513 | file, linenum); |
| 9514 | |
| 9515 | /* value pattern */ |
| 9516 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9517 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9518 | file, linenum); |
| 9519 | |
| 9520 | free(proxy->conf.lfs_file); |
| 9521 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9522 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9523 | |
| 9524 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9525 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9526 | char *errmsg = NULL; |
| 9527 | cur_arg = 1; |
| 9528 | /* try in the module list */ |
| 9529 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9530 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9531 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9532 | free(errmsg); |
| 9533 | goto out_err; |
| 9534 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9535 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9536 | 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] | 9537 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 9538 | goto out_err; |
| 9539 | } |
| 9540 | |
| 9541 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9542 | struct acl_cond *cond; |
| 9543 | char *errmsg = NULL; |
| 9544 | |
| 9545 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9546 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9547 | file, linenum, args[0], errmsg); |
| 9548 | free(errmsg); |
| 9549 | goto out_err; |
| 9550 | } |
| 9551 | rule->cond = cond; |
| 9552 | } |
| 9553 | else if (*args[cur_arg]) { |
| 9554 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9555 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9556 | file, linenum, args[0], args[cur_arg]); |
| 9557 | goto out_err; |
| 9558 | } |
| 9559 | |
| 9560 | return rule; |
| 9561 | out_err: |
| 9562 | free(rule); |
| 9563 | return NULL; |
| 9564 | } |
| 9565 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9566 | /* 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] | 9567 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
| 9568 | * dynamic log-format rule instead of a static string. |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9569 | */ |
| 9570 | 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] | 9571 | const char **args, char **errmsg, int use_fmt) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9572 | { |
| 9573 | struct redirect_rule *rule; |
| 9574 | int cur_arg; |
| 9575 | int type = REDIRECT_TYPE_NONE; |
| 9576 | int code = 302; |
| 9577 | const char *destination = NULL; |
| 9578 | const char *cookie = NULL; |
| 9579 | int cookie_set = 0; |
| 9580 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9581 | struct acl_cond *cond = NULL; |
| 9582 | |
| 9583 | cur_arg = 0; |
| 9584 | while (*(args[cur_arg])) { |
| 9585 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9586 | if (!*args[cur_arg + 1]) |
| 9587 | goto missing_arg; |
| 9588 | |
| 9589 | type = REDIRECT_TYPE_LOCATION; |
| 9590 | cur_arg++; |
| 9591 | destination = args[cur_arg]; |
| 9592 | } |
| 9593 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9594 | if (!*args[cur_arg + 1]) |
| 9595 | goto missing_arg; |
| 9596 | |
| 9597 | type = REDIRECT_TYPE_PREFIX; |
| 9598 | cur_arg++; |
| 9599 | destination = args[cur_arg]; |
| 9600 | } |
| 9601 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9602 | if (!*args[cur_arg + 1]) |
| 9603 | goto missing_arg; |
| 9604 | |
| 9605 | type = REDIRECT_TYPE_SCHEME; |
| 9606 | cur_arg++; |
| 9607 | destination = args[cur_arg]; |
| 9608 | } |
| 9609 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9610 | if (!*args[cur_arg + 1]) |
| 9611 | goto missing_arg; |
| 9612 | |
| 9613 | cur_arg++; |
| 9614 | cookie = args[cur_arg]; |
| 9615 | cookie_set = 1; |
| 9616 | } |
| 9617 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9618 | if (!*args[cur_arg + 1]) |
| 9619 | goto missing_arg; |
| 9620 | |
| 9621 | cur_arg++; |
| 9622 | cookie = args[cur_arg]; |
| 9623 | cookie_set = 0; |
| 9624 | } |
| 9625 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9626 | if (!*args[cur_arg + 1]) |
| 9627 | goto missing_arg; |
| 9628 | |
| 9629 | cur_arg++; |
| 9630 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9631 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9632 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9633 | "'%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] | 9634 | args[cur_arg - 1], args[cur_arg]); |
| 9635 | return NULL; |
| 9636 | } |
| 9637 | } |
| 9638 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9639 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9640 | } |
| 9641 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9642 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9643 | } |
| 9644 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9645 | strcmp(args[cur_arg], "unless") == 0) { |
| 9646 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 9647 | if (!cond) { |
| 9648 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9649 | return NULL; |
| 9650 | } |
| 9651 | break; |
| 9652 | } |
| 9653 | else { |
| 9654 | memprintf(errmsg, |
| 9655 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9656 | args[cur_arg]); |
| 9657 | return NULL; |
| 9658 | } |
| 9659 | cur_arg++; |
| 9660 | } |
| 9661 | |
| 9662 | if (type == REDIRECT_TYPE_NONE) { |
| 9663 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9664 | return NULL; |
| 9665 | } |
| 9666 | |
| 9667 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 9668 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9669 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9670 | |
| 9671 | if (!use_fmt) { |
| 9672 | /* old-style static redirect rule */ |
| 9673 | rule->rdr_str = strdup(destination); |
| 9674 | rule->rdr_len = strlen(destination); |
| 9675 | } |
| 9676 | else { |
| 9677 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9678 | |
| 9679 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9680 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9681 | * makes it hard for the user to configure a self-redirection. |
| 9682 | */ |
Godbach | 543b978 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9683 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9684 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9685 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9686 | (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9687 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9688 | free(curproxy->conf.lfs_file); |
| 9689 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9690 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9691 | } |
| 9692 | } |
| 9693 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9694 | if (cookie) { |
| 9695 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9696 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9697 | */ |
| 9698 | rule->cookie_len = strlen(cookie); |
| 9699 | if (cookie_set) { |
| 9700 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9701 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9702 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9703 | rule->cookie_len += 9; |
| 9704 | } else { |
| 9705 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9706 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9707 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9708 | rule->cookie_len += 20; |
| 9709 | } |
| 9710 | } |
| 9711 | rule->type = type; |
| 9712 | rule->code = code; |
| 9713 | rule->flags = flags; |
| 9714 | LIST_INIT(&rule->list); |
| 9715 | return rule; |
| 9716 | |
| 9717 | missing_arg: |
| 9718 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9719 | return NULL; |
| 9720 | } |
| 9721 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9722 | /************************************************************************/ |
| 9723 | /* The code below is dedicated to ACL parsing and matching */ |
| 9724 | /************************************************************************/ |
| 9725 | |
| 9726 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9727 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9728 | * which means that a request or response is ready. If some data is missing, |
| 9729 | * 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] | 9730 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9731 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9732 | * |
| 9733 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9734 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9735 | * decide whether or not an HTTP message is present ; |
| 9736 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9737 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9738 | * 1 if an HTTP message is ready |
| 9739 | */ |
| 9740 | static int |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9741 | 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] | 9742 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9743 | { |
| 9744 | struct http_txn *txn = l7; |
| 9745 | struct http_msg *msg = &txn->req; |
| 9746 | |
| 9747 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 9748 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 9749 | */ |
| 9750 | |
| 9751 | if (unlikely(!s || !txn)) |
| 9752 | return 0; |
| 9753 | |
| 9754 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9755 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9756 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9757 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9758 | if (unlikely(!s->req)) |
| 9759 | return 0; |
| 9760 | |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9761 | /* If the buffer does not leave enough free space at the end, |
| 9762 | * we must first realign it. |
| 9763 | */ |
| 9764 | if (s->req->buf->p > s->req->buf->data && |
| 9765 | s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite) |
| 9766 | buffer_slow_realign(s->req->buf); |
| 9767 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9768 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9769 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9770 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9771 | |
| 9772 | /* Try to decode HTTP request */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9773 | if (likely(msg->next < s->req->buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9774 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9775 | |
| 9776 | /* Still no valid request ? */ |
| 9777 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9778 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9779 | buffer_full(s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9780 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9781 | } |
| 9782 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9783 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9784 | return 0; |
| 9785 | } |
| 9786 | |
| 9787 | /* OK we just got a valid HTTP request. We have some minor |
| 9788 | * preparation to perform so that further checks can rely |
| 9789 | * on HTTP tests. |
| 9790 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9791 | |
| 9792 | /* If the request was parsed but was too large, we must absolutely |
| 9793 | * return an error so that it is not processed. At the moment this |
| 9794 | * cannot happen, but if the parsers are to change in the future, |
| 9795 | * we want this check to be maintained. |
| 9796 | */ |
| 9797 | if (unlikely(s->req->buf->i + s->req->buf->p > |
| 9798 | s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) { |
| 9799 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9800 | smp->data.uint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9801 | return 1; |
| 9802 | } |
| 9803 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9804 | 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] | 9805 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 9806 | s->flags |= SN_REDIRECTABLE; |
| 9807 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9808 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9809 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9810 | } |
| 9811 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9812 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9813 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9814 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9815 | |
| 9816 | /* otherwise everything's ready for the request */ |
| 9817 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9818 | else { |
| 9819 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9820 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9821 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9822 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9823 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9824 | } |
| 9825 | |
| 9826 | /* everything's OK */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9827 | smp->data.uint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9828 | return 1; |
| 9829 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9830 | |
Willy Tarreau | a4ba9db | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 9831 | /* Note: these functinos *do* modify the sample. Even in case of success, at |
| 9832 | * least the type and uint value are modified. |
| 9833 | */ |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9834 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9835 | 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] | 9836 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9837 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9838 | 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] | 9839 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9840 | |
| 9841 | /* 1. Check on METHOD |
| 9842 | * We use the pre-parsed method if it is known, and store its number as an |
| 9843 | * integer. If it is unknown, we use the pointer and the length. |
| 9844 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9845 | 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] | 9846 | { |
| 9847 | int len, meth; |
| 9848 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9849 | len = strlen(text); |
| 9850 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9851 | |
| 9852 | pattern->val.i = meth; |
| 9853 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 71196f3 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 9854 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9855 | pattern->len = len; |
| 9856 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9857 | else { |
| 9858 | pattern->ptr.str = NULL; |
| 9859 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9860 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9861 | return 1; |
| 9862 | } |
| 9863 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9864 | /* This function fetches the method of current HTTP request and stores |
| 9865 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 9866 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 9867 | * in <len> and <ptr> is NULL ; |
| 9868 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 9869 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9870 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9871 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9872 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9873 | 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] | 9874 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9875 | { |
| 9876 | int meth; |
| 9877 | struct http_txn *txn = l7; |
| 9878 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9879 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9880 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9881 | meth = txn->meth; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9882 | smp->type = SMP_T_METH; |
| 9883 | smp->data.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9884 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9885 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9886 | /* ensure the indexes are not affected */ |
| 9887 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9888 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9889 | smp->data.meth.str.len = txn->req.sl.rq.m_l; |
| 9890 | smp->data.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9891 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9892 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9893 | return 1; |
| 9894 | } |
| 9895 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9896 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9897 | 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] | 9898 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9899 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9900 | struct pattern_list *lst; |
| 9901 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9902 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9903 | list_for_each_entry(lst, &expr->patterns, list) { |
| 9904 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9905 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9906 | /* well-known method */ |
| 9907 | if (pattern->val.i != HTTP_METH_OTHER) { |
| 9908 | if (smp->data.meth.meth == pattern->val.i) |
| 9909 | return pattern; |
| 9910 | else |
| 9911 | continue; |
| 9912 | } |
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 | /* Other method, we must compare the strings */ |
| 9915 | if (pattern->len != smp->data.meth.str.len) |
| 9916 | continue; |
| 9917 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9918 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | c62a0c6 | 2014-08-28 20:42:57 +0200 | [diff] [blame] | 9919 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) || |
| 9920 | (!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] | 9921 | return pattern; |
| 9922 | } |
| 9923 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9924 | } |
| 9925 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9926 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9927 | 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] | 9928 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9929 | { |
| 9930 | struct http_txn *txn = l7; |
| 9931 | char *ptr; |
| 9932 | int len; |
| 9933 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9934 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9935 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9936 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9937 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9938 | |
| 9939 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9940 | if (len <= 0) |
| 9941 | return 0; |
| 9942 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9943 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9944 | smp->data.str.str = ptr; |
| 9945 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9946 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9947 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9948 | return 1; |
| 9949 | } |
| 9950 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9951 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9952 | 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] | 9953 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9954 | { |
| 9955 | struct http_txn *txn = l7; |
| 9956 | char *ptr; |
| 9957 | int len; |
| 9958 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9959 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9960 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9961 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9962 | return 0; |
| 9963 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9964 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9965 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9966 | |
| 9967 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9968 | if (len <= 0) |
| 9969 | return 0; |
| 9970 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9971 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9972 | smp->data.str.str = ptr; |
| 9973 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9974 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9975 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9976 | return 1; |
| 9977 | } |
| 9978 | |
| 9979 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9980 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9981 | 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] | 9982 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9983 | { |
| 9984 | struct http_txn *txn = l7; |
| 9985 | char *ptr; |
| 9986 | int len; |
| 9987 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9988 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9989 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9990 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9991 | return 0; |
| 9992 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9993 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9994 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9995 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9996 | smp->type = SMP_T_UINT; |
| 9997 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9998 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9999 | return 1; |
| 10000 | } |
| 10001 | |
| 10002 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10003 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10004 | 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] | 10005 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10006 | { |
| 10007 | struct http_txn *txn = l7; |
| 10008 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10009 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10010 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10011 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10012 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10013 | 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] | 10014 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10015 | return 1; |
| 10016 | } |
| 10017 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10018 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10019 | 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] | 10020 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10021 | { |
| 10022 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10023 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10024 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10025 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10026 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10027 | 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] | 10028 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10029 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10030 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10031 | smp->type = SMP_T_IPV4; |
| 10032 | smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10033 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10034 | return 1; |
| 10035 | } |
| 10036 | |
| 10037 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10038 | 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] | 10039 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10040 | { |
| 10041 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10042 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10043 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10044 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10045 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10046 | 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] | 10047 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10048 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10049 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10050 | smp->type = SMP_T_UINT; |
| 10051 | smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10052 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10053 | return 1; |
| 10054 | } |
| 10055 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10056 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10057 | * Accepts an optional argument of type string containing the header field name, |
| 10058 | * and an optional argument of type signed or unsigned integer to request an |
| 10059 | * 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] | 10060 | * headers are considered from the first one. It does not stop on commas and |
| 10061 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10062 | */ |
| 10063 | static int |
| 10064 | 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] | 10065 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10066 | { |
| 10067 | struct http_txn *txn = l7; |
| 10068 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10069 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
| 10070 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 10071 | int occ = 0; |
| 10072 | const char *name_str = NULL; |
| 10073 | int name_len = 0; |
| 10074 | |
| 10075 | if (!ctx) { |
| 10076 | /* first call */ |
| 10077 | ctx = &static_hdr_ctx; |
| 10078 | ctx->idx = 0; |
| 10079 | smp->ctx.a[0] = ctx; |
| 10080 | } |
| 10081 | |
| 10082 | if (args) { |
| 10083 | if (args[0].type != ARGT_STR) |
| 10084 | return 0; |
| 10085 | name_str = args[0].data.str.str; |
| 10086 | name_len = args[0].data.str.len; |
| 10087 | |
| 10088 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10089 | occ = args[1].data.uint; |
| 10090 | } |
| 10091 | |
| 10092 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10093 | |
| 10094 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10095 | /* search for header from the beginning */ |
| 10096 | ctx->idx = 0; |
| 10097 | |
| 10098 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10099 | /* no explicit occurrence and single fetch => last header by default */ |
| 10100 | occ = -1; |
| 10101 | |
| 10102 | if (!occ) |
| 10103 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10104 | smp->flags |= SMP_F_NOT_LAST; |
| 10105 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10106 | smp->type = SMP_T_STR; |
| 10107 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10108 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
| 10109 | return 1; |
| 10110 | |
| 10111 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10112 | return 0; |
| 10113 | } |
| 10114 | |
| 10115 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10116 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10117 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10118 | */ |
| 10119 | static int |
| 10120 | 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] | 10121 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10122 | { |
| 10123 | struct http_txn *txn = l7; |
| 10124 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10125 | struct hdr_ctx ctx; |
| 10126 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 10127 | int cnt; |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10128 | const char *name = NULL; |
| 10129 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10130 | |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10131 | if (args && args->type == ARGT_STR) { |
| 10132 | name = args->data.str.str; |
| 10133 | len = args->data.str.len; |
| 10134 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10135 | |
| 10136 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10137 | |
| 10138 | ctx.idx = 0; |
| 10139 | cnt = 0; |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10140 | 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] | 10141 | cnt++; |
| 10142 | |
| 10143 | smp->type = SMP_T_UINT; |
| 10144 | smp->data.uint = cnt; |
| 10145 | smp->flags = SMP_F_VOL_HDR; |
| 10146 | return 1; |
| 10147 | } |
| 10148 | |
| 10149 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10150 | * Accepts an optional argument of type string containing the header field name, |
| 10151 | * and an optional argument of type signed or unsigned integer to request an |
| 10152 | * 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] | 10153 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10154 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10155 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10156 | 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] | 10157 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10158 | { |
| 10159 | struct http_txn *txn = l7; |
| 10160 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10161 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10162 | 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] | 10163 | int occ = 0; |
| 10164 | const char *name_str = NULL; |
| 10165 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10166 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10167 | if (!ctx) { |
| 10168 | /* first call */ |
| 10169 | ctx = &static_hdr_ctx; |
| 10170 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10171 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10172 | } |
| 10173 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10174 | if (args) { |
| 10175 | if (args[0].type != ARGT_STR) |
| 10176 | return 0; |
| 10177 | name_str = args[0].data.str.str; |
| 10178 | name_len = args[0].data.str.len; |
| 10179 | |
| 10180 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10181 | occ = args[1].data.uint; |
| 10182 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10183 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10184 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10185 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10186 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10187 | /* search for header from the beginning */ |
| 10188 | ctx->idx = 0; |
| 10189 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10190 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10191 | /* no explicit occurrence and single fetch => last header by default */ |
| 10192 | occ = -1; |
| 10193 | |
| 10194 | if (!occ) |
| 10195 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10196 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10197 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10198 | smp->type = SMP_T_STR; |
| 10199 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10200 | 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] | 10201 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10202 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10203 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10204 | return 0; |
| 10205 | } |
| 10206 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10207 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10208 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10209 | */ |
| 10210 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10211 | 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] | 10212 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10213 | { |
| 10214 | struct http_txn *txn = l7; |
| 10215 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10216 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10217 | 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] | 10218 | int cnt; |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10219 | const char *name = NULL; |
| 10220 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10221 | |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10222 | if (args && args->type == ARGT_STR) { |
| 10223 | name = args->data.str.str; |
| 10224 | len = args->data.str.len; |
| 10225 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10226 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10227 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10228 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10229 | ctx.idx = 0; |
| 10230 | cnt = 0; |
Willy Tarreau | 2943734 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10231 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10232 | cnt++; |
| 10233 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10234 | smp->type = SMP_T_UINT; |
| 10235 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10236 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10237 | return 1; |
| 10238 | } |
| 10239 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10240 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10241 | * takes a mandatory argument of type string and an optional one of type int |
| 10242 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10243 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10244 | */ |
| 10245 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10246 | 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] | 10247 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10248 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10249 | int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10250 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10251 | if (ret > 0) { |
| 10252 | smp->type = SMP_T_UINT; |
| 10253 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10254 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10255 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10256 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10257 | } |
| 10258 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10259 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10260 | * and an optional one of type int to designate a specific occurrence. |
| 10261 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10262 | */ |
| 10263 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10264 | 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] | 10265 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10266 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10267 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10268 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10269 | 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] | 10270 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 10271 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10272 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10273 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10274 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10275 | if (smp->data.str.len < temp->size - 1) { |
| 10276 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 10277 | temp->str[smp->data.str.len] = '\0'; |
| 10278 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 10279 | smp->type = SMP_T_IPV6; |
| 10280 | break; |
| 10281 | } |
| 10282 | } |
| 10283 | } |
| 10284 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10285 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10286 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10287 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10288 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10289 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10290 | } |
| 10291 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10292 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10293 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10294 | */ |
| 10295 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10296 | 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] | 10297 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10298 | { |
| 10299 | struct http_txn *txn = l7; |
| 10300 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10301 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10302 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10303 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10304 | 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] | 10305 | ptr = http_get_path(txn); |
| 10306 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10307 | return 0; |
| 10308 | |
| 10309 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10310 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10311 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10312 | |
| 10313 | while (ptr < end && *ptr != '?') |
| 10314 | ptr++; |
| 10315 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10316 | smp->data.str.len = ptr - smp->data.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10317 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10318 | return 1; |
| 10319 | } |
| 10320 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10321 | /* This produces a concatenation of the first occurrence of the Host header |
| 10322 | * followed by the path component if it begins with a slash ('/'). This means |
| 10323 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10324 | * If no Host header is found, then the path is returned as-is. The returned |
| 10325 | * 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] | 10326 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10327 | */ |
| 10328 | static int |
| 10329 | 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] | 10330 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10331 | { |
| 10332 | struct http_txn *txn = l7; |
| 10333 | char *ptr, *end, *beg; |
| 10334 | struct hdr_ctx ctx; |
Willy Tarreau | c1fbbd4 | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10335 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10336 | |
| 10337 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10338 | |
| 10339 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10340 | 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] | 10341 | return smp_fetch_path(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10342 | |
| 10343 | /* 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] | 10344 | temp = get_trash_chunk(); |
| 10345 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10346 | smp->type = SMP_T_STR; |
Willy Tarreau | c1fbbd4 | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10347 | smp->data.str.str = temp->str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10348 | smp->data.str.len = ctx.vlen; |
| 10349 | |
| 10350 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10351 | 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] | 10352 | beg = http_get_path(txn); |
| 10353 | if (!beg) |
| 10354 | beg = end; |
| 10355 | |
| 10356 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10357 | |
| 10358 | if (beg < ptr && *beg == '/') { |
| 10359 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 10360 | smp->data.str.len += ptr - beg; |
| 10361 | } |
| 10362 | |
| 10363 | smp->flags = SMP_F_VOL_1ST; |
| 10364 | return 1; |
| 10365 | } |
| 10366 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10367 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10368 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10369 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10370 | * 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] | 10371 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10372 | * 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] | 10373 | * high-traffic sites without having to store whole paths. |
| 10374 | */ |
| 10375 | static int |
| 10376 | 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] | 10377 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10378 | { |
| 10379 | struct http_txn *txn = l7; |
| 10380 | struct hdr_ctx ctx; |
| 10381 | unsigned int hash = 0; |
| 10382 | char *ptr, *beg, *end; |
| 10383 | int len; |
| 10384 | |
| 10385 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10386 | |
| 10387 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10388 | 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] | 10389 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10390 | ptr = ctx.line + ctx.val; |
| 10391 | len = ctx.vlen; |
| 10392 | while (len--) |
| 10393 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10394 | } |
| 10395 | |
| 10396 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10397 | 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] | 10398 | beg = http_get_path(txn); |
| 10399 | if (!beg) |
| 10400 | beg = end; |
| 10401 | |
| 10402 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10403 | |
| 10404 | if (beg < ptr && *beg == '/') { |
| 10405 | while (beg < ptr) |
| 10406 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10407 | } |
| 10408 | hash = full_hash(hash); |
| 10409 | |
| 10410 | smp->type = SMP_T_UINT; |
| 10411 | smp->data.uint = hash; |
| 10412 | smp->flags = SMP_F_VOL_1ST; |
| 10413 | return 1; |
| 10414 | } |
| 10415 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10416 | /* 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] | 10417 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10418 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10419 | * 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] | 10420 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10421 | * 8 bytes would still work. |
| 10422 | */ |
| 10423 | static int |
| 10424 | 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] | 10425 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10426 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10427 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10428 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
| 10429 | |
| 10430 | if (!cli_conn) |
| 10431 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10432 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10433 | if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10434 | return 0; |
| 10435 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10436 | temp = get_trash_chunk(); |
Willy Tarreau | 0dff81c | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10437 | *(unsigned int *)temp->str = htonl(smp->data.uint); |
| 10438 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10439 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10440 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10441 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10442 | 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] | 10443 | temp->len += 4; |
| 10444 | break; |
| 10445 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10446 | 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] | 10447 | temp->len += 16; |
| 10448 | break; |
| 10449 | default: |
| 10450 | return 0; |
| 10451 | } |
| 10452 | |
| 10453 | smp->data.str = *temp; |
| 10454 | smp->type = SMP_T_BIN; |
| 10455 | return 1; |
| 10456 | } |
| 10457 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10458 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10459 | 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] | 10460 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10461 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10462 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10463 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10464 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10465 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10466 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10467 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10468 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 10469 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10470 | return 1; |
| 10471 | } |
| 10472 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10473 | /* return a valid test if the current request is the first one on the connection */ |
| 10474 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10475 | 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] | 10476 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10477 | { |
| 10478 | if (!s) |
| 10479 | return 0; |
| 10480 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10481 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 10482 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10483 | return 1; |
| 10484 | } |
| 10485 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10486 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10487 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10488 | 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] | 10489 | const struct arg *args, struct sample *smp, const char *kw) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10490 | { |
| 10491 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10492 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10493 | return 0; |
| 10494 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10495 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10496 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10497 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10498 | return 0; |
| 10499 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10500 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10501 | 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] | 10502 | return 1; |
| 10503 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10504 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10505 | /* Accepts exactly 1 argument of type userlist */ |
| 10506 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10507 | 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] | 10508 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10509 | { |
| 10510 | |
| 10511 | if (!args || args->type != ARGT_USR) |
| 10512 | return 0; |
| 10513 | |
| 10514 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10515 | |
| 10516 | if (!get_http_auth(l4)) |
| 10517 | return 0; |
| 10518 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10519 | /* if the user does not belong to the userlist or has a wrong password, |
| 10520 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10521 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10522 | */ |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10523 | if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass)) |
| 10524 | return 0; |
| 10525 | |
| 10526 | /* pat_match_auth() will need the user list */ |
| 10527 | smp->ctx.a[0] = args->data.usr; |
| 10528 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10529 | smp->type = SMP_T_STR; |
| 10530 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10531 | smp->data.str.str = l4->txn.auth.user; |
| 10532 | smp->data.str.len = strlen(l4->txn.auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10533 | |
| 10534 | return 1; |
| 10535 | } |
| 10536 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10537 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10538 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10539 | * the cookie value is returned into *value and *value_l, and the function |
| 10540 | * returns a pointer to the next pointer to search from if the value was found. |
| 10541 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10542 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10543 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10544 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10545 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10546 | * list is expected. |
| 10547 | */ |
| 10548 | static char * |
| 10549 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10550 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10551 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10552 | { |
| 10553 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10554 | char *next; |
| 10555 | |
| 10556 | /* we search at least a cookie name followed by an equal, and more |
| 10557 | * generally something like this : |
| 10558 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10559 | */ |
| 10560 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10561 | /* Iterate through all cookies on this line */ |
| 10562 | |
| 10563 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 10564 | att_beg++; |
| 10565 | |
| 10566 | /* find att_end : this is the first character after the last non |
| 10567 | * space before the equal. It may be equal to hdr_end. |
| 10568 | */ |
| 10569 | equal = att_end = att_beg; |
| 10570 | |
| 10571 | while (equal < hdr_end) { |
| 10572 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10573 | break; |
| 10574 | if (http_is_spht[(unsigned char)*equal++]) |
| 10575 | continue; |
| 10576 | att_end = equal; |
| 10577 | } |
| 10578 | |
| 10579 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10580 | * is between <att_beg> and <equal>, both may be identical. |
| 10581 | */ |
| 10582 | |
| 10583 | /* look for end of cookie if there is an equal sign */ |
| 10584 | if (equal < hdr_end && *equal == '=') { |
| 10585 | /* look for the beginning of the value */ |
| 10586 | val_beg = equal + 1; |
| 10587 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 10588 | val_beg++; |
| 10589 | |
| 10590 | /* find the end of the value, respecting quotes */ |
| 10591 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10592 | |
| 10593 | /* make val_end point to the first white space or delimitor after the value */ |
| 10594 | val_end = next; |
| 10595 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 10596 | val_end--; |
| 10597 | } else { |
| 10598 | val_beg = val_end = next = equal; |
| 10599 | } |
| 10600 | |
| 10601 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10602 | * they will automatically be removed if a header before them is removed, |
| 10603 | * since they're supposed to be linked together. |
| 10604 | */ |
| 10605 | if (*att_beg == '$') |
| 10606 | continue; |
| 10607 | |
| 10608 | /* Ignore cookies with no equal sign */ |
| 10609 | if (equal == next) |
| 10610 | continue; |
| 10611 | |
| 10612 | /* Now we have the cookie name between att_beg and att_end, and |
| 10613 | * its value between val_beg and val_end. |
| 10614 | */ |
| 10615 | |
| 10616 | if (att_end - att_beg == cookie_name_l && |
| 10617 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10618 | /* let's return this value and indicate where to go on from */ |
| 10619 | *value = val_beg; |
| 10620 | *value_l = val_end - val_beg; |
| 10621 | return next + 1; |
| 10622 | } |
| 10623 | |
| 10624 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10625 | if (!list) |
| 10626 | break; |
| 10627 | } |
| 10628 | |
| 10629 | return NULL; |
| 10630 | } |
| 10631 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10632 | /* Fetch a captured HTTP request header. The index is the position of |
| 10633 | * the "capture" option in the configuration file |
| 10634 | */ |
| 10635 | static int |
| 10636 | smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10637 | const struct arg *args, struct sample *smp, const char *kw) |
| 10638 | { |
| 10639 | struct proxy *fe = l4->fe; |
| 10640 | struct http_txn *txn = l7; |
| 10641 | int idx; |
| 10642 | |
| 10643 | if (!args || args->type != ARGT_UINT) |
| 10644 | return 0; |
| 10645 | |
| 10646 | idx = args->data.uint; |
| 10647 | |
| 10648 | if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL) |
| 10649 | return 0; |
| 10650 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10651 | smp->type = SMP_T_STR; |
| 10652 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10653 | smp->data.str.str = txn->req.cap[idx]; |
| 10654 | smp->data.str.len = strlen(txn->req.cap[idx]); |
| 10655 | |
| 10656 | return 1; |
| 10657 | } |
| 10658 | |
| 10659 | /* Fetch a captured HTTP response header. The index is the position of |
| 10660 | * the "capture" option in the configuration file |
| 10661 | */ |
| 10662 | static int |
| 10663 | smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10664 | const struct arg *args, struct sample *smp, const char *kw) |
| 10665 | { |
| 10666 | struct proxy *fe = l4->fe; |
| 10667 | struct http_txn *txn = l7; |
| 10668 | int idx; |
| 10669 | |
| 10670 | if (!args || args->type != ARGT_UINT) |
| 10671 | return 0; |
| 10672 | |
| 10673 | idx = args->data.uint; |
| 10674 | |
| 10675 | if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL) |
| 10676 | return 0; |
| 10677 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10678 | smp->type = SMP_T_STR; |
| 10679 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10680 | smp->data.str.str = txn->rsp.cap[idx]; |
| 10681 | smp->data.str.len = strlen(txn->rsp.cap[idx]); |
| 10682 | |
| 10683 | return 1; |
| 10684 | } |
| 10685 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10686 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 10687 | static int |
| 10688 | smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10689 | const struct arg *args, struct sample *smp, const char *kw) |
| 10690 | { |
| 10691 | struct chunk *temp; |
| 10692 | struct http_txn *txn = l7; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10693 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10694 | |
| 10695 | if (!txn->uri) |
| 10696 | return 0; |
| 10697 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10698 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10699 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10700 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10701 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10702 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10703 | temp = get_trash_chunk(); |
| 10704 | temp->str = txn->uri; |
| 10705 | temp->len = ptr - txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10706 | smp->data.str = *temp; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10707 | smp->type = SMP_T_STR; |
| 10708 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10709 | |
| 10710 | return 1; |
| 10711 | |
| 10712 | } |
| 10713 | |
| 10714 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 10715 | static int |
| 10716 | smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10717 | const struct arg *args, struct sample *smp, const char *kw) |
| 10718 | { |
| 10719 | struct chunk *temp; |
| 10720 | struct http_txn *txn = l7; |
| 10721 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10722 | |
| 10723 | if (!txn->uri) |
| 10724 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10725 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10726 | ptr = txn->uri; |
| 10727 | |
| 10728 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10729 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10730 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10731 | if (!*ptr) |
| 10732 | return 0; |
| 10733 | |
| 10734 | ptr++; /* skip the space */ |
| 10735 | |
| 10736 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10737 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10738 | if (!ptr) |
| 10739 | return 0; |
| 10740 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 10741 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10742 | |
| 10743 | smp->data.str = *temp; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10744 | smp->data.str.len = ptr - temp->str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10745 | smp->type = SMP_T_STR; |
| 10746 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10747 | |
| 10748 | return 1; |
| 10749 | } |
| 10750 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10751 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 10752 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10753 | */ |
| 10754 | static int |
| 10755 | smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10756 | const struct arg *args, struct sample *smp, const char *kw) |
| 10757 | { |
| 10758 | struct http_txn *txn = l7; |
| 10759 | |
| 10760 | if (txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
| 10761 | return 0; |
| 10762 | |
| 10763 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 10764 | smp->data.str.str = "HTTP/1.1"; |
| 10765 | else |
| 10766 | smp->data.str.str = "HTTP/1.0"; |
| 10767 | |
| 10768 | smp->data.str.len = 8; |
| 10769 | smp->type = SMP_T_STR; |
| 10770 | smp->flags = SMP_F_CONST; |
| 10771 | return 1; |
| 10772 | |
| 10773 | } |
| 10774 | |
| 10775 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 10776 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10777 | */ |
| 10778 | static int |
| 10779 | smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10780 | const struct arg *args, struct sample *smp, const char *kw) |
| 10781 | { |
| 10782 | struct http_txn *txn = l7; |
| 10783 | |
| 10784 | if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
| 10785 | return 0; |
| 10786 | |
| 10787 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
| 10788 | smp->data.str.str = "HTTP/1.1"; |
| 10789 | else |
| 10790 | smp->data.str.str = "HTTP/1.0"; |
| 10791 | |
| 10792 | smp->data.str.len = 8; |
| 10793 | smp->type = SMP_T_STR; |
| 10794 | smp->flags = SMP_F_CONST; |
| 10795 | return 1; |
| 10796 | |
| 10797 | } |
| 10798 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10799 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10800 | /* 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] | 10801 | * 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] | 10802 | * 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] | 10803 | * 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] | 10804 | * 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] | 10805 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 10806 | * 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] | 10807 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 10808 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10809 | */ |
William Lallemand | 07c8b24 | 2014-05-02 17:11:07 +0200 | [diff] [blame] | 10810 | 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] | 10811 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10812 | { |
| 10813 | struct http_txn *txn = l7; |
| 10814 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10815 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10816 | const struct http_msg *msg; |
| 10817 | const char *hdr_name; |
| 10818 | int hdr_name_len; |
| 10819 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10820 | int occ = 0; |
| 10821 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10822 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10823 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10824 | return 0; |
| 10825 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10826 | if (!ctx) { |
| 10827 | /* first call */ |
| 10828 | ctx = &static_hdr_ctx; |
| 10829 | ctx->idx = 0; |
| 10830 | smp->ctx.a[2] = ctx; |
| 10831 | } |
| 10832 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10833 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10834 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10835 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10836 | msg = &txn->req; |
| 10837 | hdr_name = "Cookie"; |
| 10838 | hdr_name_len = 6; |
| 10839 | } else { |
| 10840 | msg = &txn->rsp; |
| 10841 | hdr_name = "Set-Cookie"; |
| 10842 | hdr_name_len = 10; |
| 10843 | } |
| 10844 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10845 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10846 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 10847 | occ = -1; |
| 10848 | |
| 10849 | /* OK so basically here, either we want only one value and it's the |
| 10850 | * last one, or we want to iterate over all of them and we fetch the |
| 10851 | * next one. |
| 10852 | */ |
| 10853 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10854 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10855 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10856 | /* search for the header from the beginning, we must first initialize |
| 10857 | * the search parameters. |
| 10858 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10859 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10860 | ctx->idx = 0; |
| 10861 | } |
| 10862 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10863 | smp->flags |= SMP_F_VOL_HDR; |
| 10864 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10865 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10866 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 10867 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10868 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 10869 | goto out; |
| 10870 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10871 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10872 | continue; |
| 10873 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10874 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 10875 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10876 | } |
| 10877 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10878 | smp->type = SMP_T_STR; |
| 10879 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10880 | 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] | 10881 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10882 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10883 | &smp->data.str.str, |
| 10884 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10885 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10886 | found = 1; |
| 10887 | if (occ >= 0) { |
| 10888 | /* one value was returned into smp->data.str.{str,len} */ |
| 10889 | smp->flags |= SMP_F_NOT_LAST; |
| 10890 | return 1; |
| 10891 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10892 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10893 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10894 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10895 | /* all cookie headers and values were scanned. If we're looking for the |
| 10896 | * last occurrence, we may return it now. |
| 10897 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10898 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10899 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10900 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10901 | } |
| 10902 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10903 | /* 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] | 10904 | * 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] | 10905 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 10906 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10907 | */ |
| 10908 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10909 | 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] | 10910 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10911 | { |
| 10912 | struct http_txn *txn = l7; |
| 10913 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10914 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10915 | const struct http_msg *msg; |
| 10916 | const char *hdr_name; |
| 10917 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10918 | int cnt; |
| 10919 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10920 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10921 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10922 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10923 | return 0; |
| 10924 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10925 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10926 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10927 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10928 | msg = &txn->req; |
| 10929 | hdr_name = "Cookie"; |
| 10930 | hdr_name_len = 6; |
| 10931 | } else { |
| 10932 | msg = &txn->rsp; |
| 10933 | hdr_name = "Set-Cookie"; |
| 10934 | hdr_name_len = 10; |
| 10935 | } |
| 10936 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10937 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 10938 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10939 | ctx.idx = 0; |
| 10940 | cnt = 0; |
| 10941 | |
| 10942 | while (1) { |
| 10943 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 10944 | if (!val_beg) { |
| 10945 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 10946 | break; |
| 10947 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10948 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10949 | continue; |
| 10950 | |
| 10951 | val_beg = ctx.line + ctx.val; |
| 10952 | val_end = val_beg + ctx.vlen; |
| 10953 | } |
| 10954 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10955 | smp->type = SMP_T_STR; |
| 10956 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10957 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10958 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10959 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10960 | &smp->data.str.str, |
| 10961 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10962 | cnt++; |
| 10963 | } |
| 10964 | } |
| 10965 | |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 10966 | smp->type = SMP_T_UINT; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10967 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10968 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10969 | return 1; |
| 10970 | } |
| 10971 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10972 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 10973 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 10974 | */ |
| 10975 | static int |
| 10976 | 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] | 10977 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10978 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10979 | int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10980 | |
| 10981 | if (ret > 0) { |
| 10982 | smp->type = SMP_T_UINT; |
| 10983 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10984 | } |
| 10985 | |
| 10986 | return ret; |
| 10987 | } |
| 10988 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10989 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 10990 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10991 | /************************************************************************/ |
| 10992 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10993 | /* |
| 10994 | * Given a path string and its length, find the position of beginning of the |
| 10995 | * query string. Returns NULL if no query string is found in the path. |
| 10996 | * |
| 10997 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 10998 | * |
| 10999 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 11000 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11001 | 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] | 11002 | { |
| 11003 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11004 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11005 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11006 | return p ? p + 1 : NULL; |
| 11007 | } |
| 11008 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11009 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11010 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11011 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11012 | } |
| 11013 | |
| 11014 | /* |
| 11015 | * Given a url parameter, find the starting position of the first occurence, |
| 11016 | * or NULL if the parameter is not found. |
| 11017 | * |
| 11018 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11019 | * the function will return query_string+8. |
| 11020 | */ |
| 11021 | static char* |
| 11022 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11023 | char* url_param_name, size_t url_param_name_l, |
| 11024 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11025 | { |
| 11026 | char *pos, *last; |
| 11027 | |
| 11028 | pos = query_string; |
| 11029 | last = query_string + query_string_l - url_param_name_l - 1; |
| 11030 | |
| 11031 | while (pos <= last) { |
| 11032 | if (pos[url_param_name_l] == '=') { |
| 11033 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 11034 | return pos; |
| 11035 | pos += url_param_name_l + 1; |
| 11036 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11037 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11038 | pos++; |
| 11039 | pos++; |
| 11040 | } |
| 11041 | return NULL; |
| 11042 | } |
| 11043 | |
| 11044 | /* |
| 11045 | * Given a url parameter name, returns its value and size into *value and |
| 11046 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 11047 | * zero is returned and value/value_l are not touched. |
| 11048 | */ |
| 11049 | static int |
| 11050 | find_url_param_value(char* path, size_t path_l, |
| 11051 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11052 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11053 | { |
| 11054 | char *query_string, *qs_end; |
| 11055 | char *arg_start; |
| 11056 | char *value_start, *value_end; |
| 11057 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11058 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11059 | if (!query_string) |
| 11060 | return 0; |
| 11061 | |
| 11062 | qs_end = path + path_l; |
| 11063 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11064 | url_param_name, url_param_name_l, |
| 11065 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11066 | if (!arg_start) |
| 11067 | return 0; |
| 11068 | |
| 11069 | value_start = arg_start + url_param_name_l + 1; |
| 11070 | value_end = value_start; |
| 11071 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11072 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11073 | value_end++; |
| 11074 | |
| 11075 | *value = value_start; |
| 11076 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 11077 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11078 | } |
| 11079 | |
| 11080 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11081 | 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] | 11082 | const struct arg *args, struct sample *smp, const char *kw) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11083 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11084 | char delim = '?'; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11085 | struct http_txn *txn = l7; |
| 11086 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11087 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11088 | if (!args || args[0].type != ARGT_STR || |
| 11089 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11090 | return 0; |
| 11091 | |
| 11092 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11093 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11094 | if (args[1].type) |
| 11095 | delim = *args[1].data.str.str; |
| 11096 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11097 | 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] | 11098 | args->data.str.str, args->data.str.len, |
| 11099 | &smp->data.str.str, &smp->data.str.len, |
| 11100 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11101 | return 0; |
| 11102 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11103 | smp->type = SMP_T_STR; |
| 11104 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11105 | return 1; |
| 11106 | } |
| 11107 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11108 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11109 | * above). |
| 11110 | */ |
| 11111 | static int |
| 11112 | 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] | 11113 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11114 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 11115 | 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] | 11116 | |
| 11117 | if (ret > 0) { |
| 11118 | smp->type = SMP_T_UINT; |
| 11119 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 11120 | } |
| 11121 | |
| 11122 | return ret; |
| 11123 | } |
| 11124 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11125 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11126 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11127 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11128 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11129 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11130 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11131 | * high-traffic sites without having to store whole paths. |
| 11132 | * this differs from the base32 functions in that it includes the url parameters |
| 11133 | * as well as the path |
| 11134 | */ |
| 11135 | static int |
| 11136 | 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] | 11137 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11138 | { |
| 11139 | struct http_txn *txn = l7; |
| 11140 | struct hdr_ctx ctx; |
| 11141 | unsigned int hash = 0; |
| 11142 | char *ptr, *beg, *end; |
| 11143 | int len; |
| 11144 | |
| 11145 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11146 | |
| 11147 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11148 | 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] | 11149 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11150 | ptr = ctx.line + ctx.val; |
| 11151 | len = ctx.vlen; |
| 11152 | while (len--) |
| 11153 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11154 | } |
| 11155 | |
| 11156 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11157 | 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] | 11158 | beg = http_get_path(txn); |
| 11159 | if (!beg) |
| 11160 | beg = end; |
| 11161 | |
| 11162 | for (ptr = beg; ptr < end ; ptr++); |
| 11163 | |
| 11164 | if (beg < ptr && *beg == '/') { |
| 11165 | while (beg < ptr) |
| 11166 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11167 | } |
| 11168 | hash = full_hash(hash); |
| 11169 | |
| 11170 | smp->type = SMP_T_UINT; |
| 11171 | smp->data.uint = hash; |
| 11172 | smp->flags = SMP_F_VOL_1ST; |
| 11173 | return 1; |
| 11174 | } |
| 11175 | |
| 11176 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11177 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11178 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11179 | * on the source address length. The URL hash is stored before the address so |
| 11180 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11181 | * 8 bytes would still work. |
| 11182 | */ |
| 11183 | static int |
| 11184 | 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] | 11185 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11186 | { |
| 11187 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11188 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11189 | |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 11190 | if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11191 | return 0; |
| 11192 | |
| 11193 | temp = get_trash_chunk(); |
| 11194 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 11195 | temp->len += sizeof(smp->data.uint); |
| 11196 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11197 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11198 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11199 | 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] | 11200 | temp->len += 4; |
| 11201 | break; |
| 11202 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11203 | 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] | 11204 | temp->len += 16; |
| 11205 | break; |
| 11206 | default: |
| 11207 | return 0; |
| 11208 | } |
| 11209 | |
| 11210 | smp->data.str = *temp; |
| 11211 | smp->type = SMP_T_BIN; |
| 11212 | return 1; |
| 11213 | } |
| 11214 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11215 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11216 | * keyword. These keywords support an optional positive or negative occurrence |
| 11217 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11218 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11219 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11220 | * error message in case of error, that the caller is responsible for freeing. |
| 11221 | * The initial location must either be freeable or NULL. |
| 11222 | */ |
| 11223 | static int val_hdr(struct arg *arg, char **err_msg) |
| 11224 | { |
| 11225 | 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] | 11226 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11227 | return 0; |
| 11228 | } |
| 11229 | return 1; |
| 11230 | } |
| 11231 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11232 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11233 | * adds an optional offset found in args[0] and emits a string representing |
| 11234 | * the date in RFC-1123/5322 format. |
| 11235 | */ |
| 11236 | static int sample_conv_http_date(const struct arg *args, struct sample *smp) |
| 11237 | { |
| 11238 | const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; |
| 11239 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11240 | struct chunk *temp; |
| 11241 | struct tm *tm; |
| 11242 | time_t curr_date = smp->data.uint; |
| 11243 | |
| 11244 | /* add offset */ |
| 11245 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 11246 | curr_date += args[0].data.sint; |
| 11247 | |
| 11248 | tm = gmtime(&curr_date); |
Thierry FOURNIER | 9555872 | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 11249 | if (!tm) |
| 11250 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11251 | |
| 11252 | temp = get_trash_chunk(); |
| 11253 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11254 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11255 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11256 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11257 | |
| 11258 | smp->data.str = *temp; |
| 11259 | smp->type = SMP_T_STR; |
| 11260 | return 1; |
| 11261 | } |
| 11262 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11263 | /* Match language range with language tag. RFC2616 14.4: |
| 11264 | * |
| 11265 | * A language-range matches a language-tag if it exactly equals |
| 11266 | * the tag, or if it exactly equals a prefix of the tag such |
| 11267 | * that the first tag character following the prefix is "-". |
| 11268 | * |
| 11269 | * Return 1 if the strings match, else return 0. |
| 11270 | */ |
| 11271 | static inline int language_range_match(const char *range, int range_len, |
| 11272 | const char *tag, int tag_len) |
| 11273 | { |
| 11274 | const char *end = range + range_len; |
| 11275 | const char *tend = tag + tag_len; |
| 11276 | while (range < end) { |
| 11277 | if (*range == '-' && tag == tend) |
| 11278 | return 1; |
| 11279 | if (*range != *tag || tag == tend) |
| 11280 | return 0; |
| 11281 | range++; |
| 11282 | tag++; |
| 11283 | } |
| 11284 | /* Return true only if the last char of the tag is matched. */ |
| 11285 | return tag == tend; |
| 11286 | } |
| 11287 | |
| 11288 | /* Arguments: The list of expected value, the number of parts returned and the separator */ |
| 11289 | static int sample_conv_q_prefered(const struct arg *args, struct sample *smp) |
| 11290 | { |
| 11291 | const char *al = smp->data.str.str; |
| 11292 | const char *end = al + smp->data.str.len; |
| 11293 | const char *token; |
| 11294 | int toklen; |
| 11295 | int qvalue; |
| 11296 | const char *str; |
| 11297 | const char *w; |
| 11298 | int best_q = 0; |
| 11299 | |
| 11300 | /* Set the constant to the sample, because the output of the |
| 11301 | * function will be peek in the constant configuration string. |
| 11302 | */ |
| 11303 | smp->flags |= SMP_F_CONST; |
| 11304 | smp->data.str.size = 0; |
| 11305 | smp->data.str.str = ""; |
| 11306 | smp->data.str.len = 0; |
| 11307 | |
| 11308 | /* Parse the accept language */ |
| 11309 | while (1) { |
| 11310 | |
| 11311 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11312 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11313 | al++; |
| 11314 | if (al >= end) |
| 11315 | break; |
| 11316 | |
| 11317 | /* Start of the fisrt word. */ |
| 11318 | token = al; |
| 11319 | |
| 11320 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11321 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11322 | al++; |
| 11323 | if (al == token) |
| 11324 | goto expect_comma; |
| 11325 | |
| 11326 | /* Length of the token. */ |
| 11327 | toklen = al - token; |
| 11328 | qvalue = 1000; |
| 11329 | |
| 11330 | /* Check if the token exists in the list. If the token not exists, |
| 11331 | * jump to the next token. |
| 11332 | */ |
| 11333 | str = args[0].data.str.str; |
| 11334 | w = str; |
| 11335 | while (1) { |
| 11336 | if (*str == ';' || *str == '\0') { |
| 11337 | if (language_range_match(token, toklen, w, str-w)) |
| 11338 | goto look_for_q; |
| 11339 | if (*str == '\0') |
| 11340 | goto expect_comma; |
| 11341 | w = str + 1; |
| 11342 | } |
| 11343 | str++; |
| 11344 | } |
| 11345 | goto expect_comma; |
| 11346 | |
| 11347 | look_for_q: |
| 11348 | |
| 11349 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11350 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11351 | al++; |
| 11352 | if (al >= end) |
| 11353 | goto process_value; |
| 11354 | |
| 11355 | /* If ',' is found, process the result */ |
| 11356 | if (*al == ',') |
| 11357 | goto process_value; |
| 11358 | |
| 11359 | /* If the character is different from ';', look |
| 11360 | * for the end of the header part in best effort. |
| 11361 | */ |
| 11362 | if (*al != ';') |
| 11363 | goto expect_comma; |
| 11364 | |
| 11365 | /* Assumes that the char is ';', now expect "q=". */ |
| 11366 | al++; |
| 11367 | |
| 11368 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11369 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11370 | al++; |
| 11371 | if (al >= end) |
| 11372 | goto process_value; |
| 11373 | |
| 11374 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11375 | if (*al != 'q') |
| 11376 | goto process_value; |
| 11377 | al++; |
| 11378 | |
| 11379 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11380 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11381 | al++; |
| 11382 | if (al >= end) |
| 11383 | goto process_value; |
| 11384 | |
| 11385 | /* Expect '='. If no '=', continue in best effort */ |
| 11386 | if (*al != '=') |
| 11387 | goto process_value; |
| 11388 | al++; |
| 11389 | |
| 11390 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 463ae6f | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11391 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11392 | al++; |
| 11393 | if (al >= end) |
| 11394 | goto process_value; |
| 11395 | |
| 11396 | /* Parse the q value. */ |
| 11397 | qvalue = parse_qvalue(al, &al); |
| 11398 | |
| 11399 | process_value: |
| 11400 | |
| 11401 | /* If the new q value is the best q value, then store the associated |
| 11402 | * language in the response. If qvalue is the biggest value (1000), |
| 11403 | * break the process. |
| 11404 | */ |
| 11405 | if (qvalue > best_q) { |
| 11406 | smp->data.str.str = (char *)w; |
| 11407 | smp->data.str.len = str - w; |
| 11408 | if (qvalue >= 1000) |
| 11409 | break; |
| 11410 | best_q = qvalue; |
| 11411 | } |
| 11412 | |
| 11413 | expect_comma: |
| 11414 | |
| 11415 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11416 | while (al < end && *al != ',') |
| 11417 | al++; |
| 11418 | if (al >= end) |
| 11419 | break; |
| 11420 | |
| 11421 | /* Comma is found, jump it and restart the analyzer. */ |
| 11422 | al++; |
| 11423 | } |
| 11424 | |
| 11425 | /* Set default value if required. */ |
| 11426 | if (smp->data.str.len == 0 && args[1].type == ARGT_STR) { |
| 11427 | smp->data.str.str = args[1].data.str.str; |
| 11428 | smp->data.str.len = args[1].data.str.len; |
| 11429 | } |
| 11430 | |
| 11431 | /* Return true only if a matching language was found. */ |
| 11432 | return smp->data.str.len != 0; |
| 11433 | } |
| 11434 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 11435 | /* |
| 11436 | * Return the struct http_req_action_kw associated to a keyword. |
| 11437 | */ |
| 11438 | struct http_req_action_kw *action_http_req_custom(const char *kw) |
| 11439 | { |
| 11440 | if (!LIST_ISEMPTY(&http_req_keywords.list)) { |
| 11441 | struct http_req_action_kw_list *kw_list; |
| 11442 | int i; |
| 11443 | |
| 11444 | list_for_each_entry(kw_list, &http_req_keywords.list, list) { |
| 11445 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11446 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11447 | return &kw_list->kw[i]; |
| 11448 | } |
| 11449 | } |
| 11450 | } |
| 11451 | return NULL; |
| 11452 | } |
| 11453 | |
| 11454 | /* |
| 11455 | * Return the struct http_res_action_kw associated to a keyword. |
| 11456 | */ |
| 11457 | struct http_res_action_kw *action_http_res_custom(const char *kw) |
| 11458 | { |
| 11459 | if (!LIST_ISEMPTY(&http_res_keywords.list)) { |
| 11460 | struct http_res_action_kw_list *kw_list; |
| 11461 | int i; |
| 11462 | |
| 11463 | list_for_each_entry(kw_list, &http_res_keywords.list, list) { |
| 11464 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11465 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11466 | return &kw_list->kw[i]; |
| 11467 | } |
| 11468 | } |
| 11469 | } |
| 11470 | return NULL; |
| 11471 | } |
| 11472 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11473 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11474 | /* All supported ACL keywords must be declared here. */ |
| 11475 | /************************************************************************/ |
| 11476 | |
| 11477 | /* Note: must not be declared <const> as its list will be overwritten. |
| 11478 | * Please take care of keeping this list alphabetically sorted. |
| 11479 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11480 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11481 | { "base", "base", PAT_MATCH_STR }, |
| 11482 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 11483 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 11484 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 11485 | { "base_end", "base", PAT_MATCH_END }, |
| 11486 | { "base_len", "base", PAT_MATCH_LEN }, |
| 11487 | { "base_reg", "base", PAT_MATCH_REG }, |
| 11488 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11489 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11490 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 11491 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 11492 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 11493 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 11494 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 11495 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 11496 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 11497 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11498 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11499 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 11500 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 11501 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 11502 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 11503 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 11504 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 11505 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 11506 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11507 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11508 | /* these two declarations uses strings with list storage (in place |
| 11509 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 11510 | * and delete functions are relative to the list management. The parse |
| 11511 | * and match method are related to the corresponding fetch methods. This |
| 11512 | * is very particular ACL declaration mode. |
| 11513 | */ |
| 11514 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 11515 | { "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] | 11516 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11517 | { "path", "path", PAT_MATCH_STR }, |
| 11518 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 11519 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 11520 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 11521 | { "path_end", "path", PAT_MATCH_END }, |
| 11522 | { "path_len", "path", PAT_MATCH_LEN }, |
| 11523 | { "path_reg", "path", PAT_MATCH_REG }, |
| 11524 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11525 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11526 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 11527 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11528 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11529 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 11530 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 11531 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 11532 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 11533 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 11534 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 11535 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 11536 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11537 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11538 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 11539 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 11540 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 11541 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 11542 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 11543 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 11544 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 11545 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11546 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11547 | { "url", "url", PAT_MATCH_STR }, |
| 11548 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 11549 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 11550 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 11551 | { "url_end", "url", PAT_MATCH_END }, |
| 11552 | { "url_len", "url", PAT_MATCH_LEN }, |
| 11553 | { "url_reg", "url", PAT_MATCH_REG }, |
| 11554 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11555 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11556 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 11557 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 11558 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 11559 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 11560 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 11561 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 11562 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 11563 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11564 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 11565 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11566 | }}; |
| 11567 | |
| 11568 | /************************************************************************/ |
| 11569 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11570 | /************************************************************************/ |
| 11571 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11572 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11573 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11574 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11575 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 11576 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11577 | /* capture are allocated and are permanent in the session */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11578 | { "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] | 11579 | |
| 11580 | /* retrieve these captures from the HTTP logs */ |
| 11581 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11582 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11583 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11584 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11585 | { "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] | 11586 | { "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] | 11587 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11588 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 11589 | * are only here to match the ACL's name, are request-only and are used |
| 11590 | * for ACL compatibility only. |
| 11591 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11592 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11593 | { "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] | 11594 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11595 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11596 | |
| 11597 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 11598 | * only here to match the ACL's name, are request-only and are used for |
| 11599 | * ACL compatibility only. |
| 11600 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11601 | { "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] | 11602 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11603 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11604 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11605 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 11606 | { "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] | 11607 | { "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] | 11608 | { "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] | 11609 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11610 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11611 | |
| 11612 | /* HTTP protocol on the request path */ |
| 11613 | { "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] | 11614 | { "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] | 11615 | |
| 11616 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11617 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11618 | { "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] | 11619 | |
| 11620 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11621 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 11622 | { "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] | 11623 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11624 | /* 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] | 11625 | { "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] | 11626 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11627 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11628 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11629 | { "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] | 11630 | { "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] | 11631 | { "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] | 11632 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11633 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11634 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11635 | |
| 11636 | /* 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] | 11637 | { "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] | 11638 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11639 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11640 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11641 | { "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] | 11642 | { "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] | 11643 | { "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] | 11644 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11645 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 11646 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11647 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11648 | /* 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] | 11649 | { "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] | 11650 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11651 | { "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] | 11652 | { "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] | 11653 | |
| 11654 | /* 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] | 11655 | { "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] | 11656 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11657 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 11658 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11659 | |
| 11660 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11661 | { "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] | 11662 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11663 | { "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] | 11664 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11665 | { "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] | 11666 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11667 | { "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] | 11668 | { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11669 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11670 | }}; |
| 11671 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11672 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11673 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 11674 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11675 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR}, |
| 11676 | { "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] | 11677 | { NULL, NULL, 0, 0, 0 }, |
| 11678 | }}; |
| 11679 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11680 | __attribute__((constructor)) |
| 11681 | static void __http_protocol_init(void) |
| 11682 | { |
| 11683 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11684 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11685 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11686 | } |
| 11687 | |
| 11688 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 11689 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 11690 | * Local variables: |
| 11691 | * c-indent-level: 8 |
| 11692 | * c-basic-offset: 8 |
| 11693 | * End: |
| 11694 | */ |