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 | 1f1fae6 | 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 : |
| 364 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 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] = { |
| 369 | [HTTP_METH_NONE] = { "", 0 }, |
| 370 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 371 | [HTTP_METH_GET] = { "GET", 3 }, |
| 372 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 373 | [HTTP_METH_POST] = { "POST", 4 }, |
| 374 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 375 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 376 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 377 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 378 | }; |
| 379 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 380 | /* 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] | 381 | * 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] | 382 | * RFC2616 for those chars. |
| 383 | */ |
| 384 | |
| 385 | const char http_is_spht[256] = { |
| 386 | [' '] = 1, ['\t'] = 1, |
| 387 | }; |
| 388 | |
| 389 | const char http_is_crlf[256] = { |
| 390 | ['\r'] = 1, ['\n'] = 1, |
| 391 | }; |
| 392 | |
| 393 | const char http_is_lws[256] = { |
| 394 | [' '] = 1, ['\t'] = 1, |
| 395 | ['\r'] = 1, ['\n'] = 1, |
| 396 | }; |
| 397 | |
| 398 | const char http_is_sep[256] = { |
| 399 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 400 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 401 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 402 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 403 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 404 | }; |
| 405 | |
| 406 | const char http_is_ctl[256] = { |
| 407 | [0 ... 31] = 1, |
| 408 | [127] = 1, |
| 409 | }; |
| 410 | |
| 411 | /* |
| 412 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 413 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 414 | * them correctly. Instead, define every non-CTL char's status. |
| 415 | */ |
| 416 | const char http_is_token[256] = { |
| 417 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 418 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 419 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 420 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 421 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 422 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 423 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 424 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 425 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 426 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 427 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 428 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 429 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 430 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 431 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 432 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 433 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 434 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 435 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 436 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 437 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 438 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 439 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 440 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 441 | }; |
| 442 | |
| 443 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 444 | /* |
| 445 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 446 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 447 | */ |
| 448 | const char http_is_ver_token[256] = { |
| 449 | ['.'] = 1, ['/'] = 1, |
| 450 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 451 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 452 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 453 | }; |
| 454 | |
| 455 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 456 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 457 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 458 | * 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] | 459 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 460 | * of headers is automatically adjusted. The number of bytes added is returned |
| 461 | * on success, otherwise <0 is returned indicating an error. |
| 462 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 463 | 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] | 464 | { |
| 465 | int bytes, len; |
| 466 | |
| 467 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 468 | 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] | 469 | if (!bytes) |
| 470 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 471 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 472 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 473 | } |
| 474 | |
| 475 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 476 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 477 | * 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] | 478 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 479 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 480 | * of headers is automatically adjusted. The number of bytes added is returned |
| 481 | * on success, otherwise <0 is returned indicating an error. |
| 482 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 483 | int http_header_add_tail2(struct http_msg *msg, |
| 484 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 485 | { |
| 486 | int bytes; |
| 487 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 488 | 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] | 489 | if (!bytes) |
| 490 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 491 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 492 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 493 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 494 | |
| 495 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 496 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 497 | * If so, returns the position of the first non-space character relative to |
| 498 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 499 | * to return a pointer to the place after the first space. Returns 0 if the |
| 500 | * header name does not match. Checks are case-insensitive. |
| 501 | */ |
| 502 | int http_header_match2(const char *hdr, const char *end, |
| 503 | const char *name, int len) |
| 504 | { |
| 505 | const char *val; |
| 506 | |
| 507 | if (hdr + len >= end) |
| 508 | return 0; |
| 509 | if (hdr[len] != ':') |
| 510 | return 0; |
| 511 | if (strncasecmp(hdr, name, len) != 0) |
| 512 | return 0; |
| 513 | val = hdr + len + 1; |
| 514 | while (val < end && HTTP_IS_SPHT(*val)) |
| 515 | val++; |
| 516 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 517 | return len + 2; /* we may replace starting from second space */ |
| 518 | return val - hdr; |
| 519 | } |
| 520 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 521 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 522 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 523 | * structure holds everything necessary to use the header and find next |
| 524 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 525 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 526 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 527 | * http_find_header2() except that it is designed to work with full-line headers |
| 528 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 529 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 530 | * header is rescanned. This allows rescanning after a header deletion. |
| 531 | */ |
| 532 | int http_find_full_header2(const char *name, int len, |
| 533 | char *sol, struct hdr_idx *idx, |
| 534 | struct hdr_ctx *ctx) |
| 535 | { |
| 536 | char *eol, *sov; |
| 537 | int cur_idx, old_idx; |
| 538 | |
| 539 | cur_idx = ctx->idx; |
| 540 | if (cur_idx) { |
| 541 | /* We have previously returned a header, let's search another one */ |
| 542 | sol = ctx->line; |
| 543 | eol = sol + idx->v[cur_idx].len; |
| 544 | goto next_hdr; |
| 545 | } |
| 546 | |
| 547 | /* first request for this header */ |
| 548 | sol += hdr_idx_first_pos(idx); |
| 549 | old_idx = 0; |
| 550 | cur_idx = hdr_idx_first_idx(idx); |
| 551 | while (cur_idx) { |
| 552 | eol = sol + idx->v[cur_idx].len; |
| 553 | |
| 554 | if (len == 0) { |
| 555 | /* No argument was passed, we want any header. |
| 556 | * To achieve this, we simply build a fake request. */ |
| 557 | while (sol + len < eol && sol[len] != ':') |
| 558 | len++; |
| 559 | name = sol; |
| 560 | } |
| 561 | |
| 562 | if ((len < eol - sol) && |
| 563 | (sol[len] == ':') && |
| 564 | (strncasecmp(sol, name, len) == 0)) { |
| 565 | ctx->del = len; |
| 566 | sov = sol + len + 1; |
| 567 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 568 | sov++; |
| 569 | |
| 570 | ctx->line = sol; |
| 571 | ctx->prev = old_idx; |
| 572 | ctx->idx = cur_idx; |
| 573 | ctx->val = sov - sol; |
| 574 | ctx->tws = 0; |
| 575 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 576 | eol--; |
| 577 | ctx->tws++; |
| 578 | } |
| 579 | ctx->vlen = eol - sov; |
| 580 | return 1; |
| 581 | } |
| 582 | next_hdr: |
| 583 | sol = eol + idx->v[cur_idx].cr + 1; |
| 584 | old_idx = cur_idx; |
| 585 | cur_idx = idx->v[cur_idx].next; |
| 586 | } |
| 587 | return 0; |
| 588 | } |
| 589 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 590 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 591 | * par 2.2 for more information. Note that it requires a valid header to return |
| 592 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 593 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 594 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 595 | { |
| 596 | int quoted, qdpair; |
| 597 | |
| 598 | quoted = qdpair = 0; |
| 599 | for (; s < e; s++) { |
| 600 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 601 | else if (quoted) { |
| 602 | if (*s == '\\') qdpair = 1; |
| 603 | else if (*s == '"') quoted = 0; |
| 604 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 605 | else if (*s == '"') quoted = 1; |
| 606 | else if (*s == ',') return s; |
| 607 | } |
| 608 | return s; |
| 609 | } |
| 610 | |
| 611 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 612 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 613 | * structure holds everything necessary to use the header and find next |
| 614 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 615 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 616 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 617 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 618 | * is NULL when searching for a new values of a header, the current header is |
| 619 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 620 | */ |
| 621 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 622 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 623 | struct hdr_ctx *ctx) |
| 624 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 625 | char *eol, *sov; |
| 626 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 627 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 628 | cur_idx = ctx->idx; |
| 629 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 630 | /* We have previously returned a value, let's search |
| 631 | * another one on the same line. |
| 632 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 633 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 634 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 635 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 636 | eol = sol + idx->v[cur_idx].len; |
| 637 | |
| 638 | if (sov >= eol) |
| 639 | /* no more values in this header */ |
| 640 | goto next_hdr; |
| 641 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 642 | /* values remaining for this header, skip the comma but save it |
| 643 | * for later use (eg: for header deletion). |
| 644 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 645 | sov++; |
| 646 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 647 | sov++; |
| 648 | |
| 649 | goto return_hdr; |
| 650 | } |
| 651 | |
| 652 | /* first request for this header */ |
| 653 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 654 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 655 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 656 | while (cur_idx) { |
| 657 | eol = sol + idx->v[cur_idx].len; |
| 658 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 659 | if (len == 0) { |
| 660 | /* No argument was passed, we want any header. |
| 661 | * To achieve this, we simply build a fake request. */ |
| 662 | while (sol + len < eol && sol[len] != ':') |
| 663 | len++; |
| 664 | name = sol; |
| 665 | } |
| 666 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 667 | if ((len < eol - sol) && |
| 668 | (sol[len] == ':') && |
| 669 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 670 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 671 | sov = sol + len + 1; |
| 672 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 673 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 674 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 675 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 676 | ctx->prev = old_idx; |
| 677 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 678 | ctx->idx = cur_idx; |
| 679 | ctx->val = sov - sol; |
| 680 | |
| 681 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 682 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 683 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 684 | eol--; |
| 685 | ctx->tws++; |
| 686 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 687 | ctx->vlen = eol - sov; |
| 688 | return 1; |
| 689 | } |
| 690 | next_hdr: |
| 691 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 692 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 693 | cur_idx = idx->v[cur_idx].next; |
| 694 | } |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 699 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 700 | struct hdr_ctx *ctx) |
| 701 | { |
| 702 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 703 | } |
| 704 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 705 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 706 | * the http_find_header functions. The value is removed, as well as surrounding |
| 707 | * 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] | 708 | * 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] | 709 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 710 | * no more header, so any processing may stop. The ctx is always left in a form |
| 711 | * that can be handled by http_find_header2() to find next occurrence. |
| 712 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 713 | 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] | 714 | { |
| 715 | int cur_idx = ctx->idx; |
| 716 | char *sol = ctx->line; |
| 717 | struct hdr_idx_elem *hdr; |
| 718 | int delta, skip_comma; |
| 719 | |
| 720 | if (!cur_idx) |
| 721 | return 0; |
| 722 | |
| 723 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 724 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 725 | /* 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] | 726 | 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] | 727 | http_msg_move_end(msg, delta); |
| 728 | idx->used--; |
| 729 | hdr->len = 0; /* unused entry */ |
| 730 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 731 | if (idx->tail == ctx->idx) |
| 732 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 733 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 734 | 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] | 735 | 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] | 736 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 737 | return ctx->idx; |
| 738 | } |
| 739 | |
| 740 | /* 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] | 741 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 742 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 743 | */ |
| 744 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 745 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 746 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 747 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 748 | NULL, 0); |
| 749 | hdr->len += delta; |
| 750 | http_msg_move_end(msg, delta); |
| 751 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 752 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 753 | return ctx->idx; |
| 754 | } |
| 755 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 756 | /* This function handles a server error at the stream interface level. The |
| 757 | * stream interface is assumed to be already in a closed state. An optional |
| 758 | * message is copied into the input buffer, and an HTTP status code stored. |
| 759 | * 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] | 760 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 761 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 762 | static void http_server_error(struct session *s, struct stream_interface *si, |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 763 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 764 | { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 765 | channel_auto_read(si->ob); |
| 766 | channel_abort(si->ob); |
| 767 | channel_auto_close(si->ob); |
| 768 | channel_erase(si->ob); |
| 769 | channel_auto_close(si->ib); |
| 770 | channel_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 771 | if (status > 0 && msg) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 772 | s->txn.status = status; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 773 | bo_inject(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 774 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 775 | if (!(s->flags & SN_ERR_MASK)) |
| 776 | s->flags |= err; |
| 777 | if (!(s->flags & SN_FINST_MASK)) |
| 778 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 779 | } |
| 780 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 781 | /* This function returns the appropriate error location for the given session |
| 782 | * and message. |
| 783 | */ |
| 784 | |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 785 | struct chunk *http_error_message(struct session *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 786 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 787 | if (s->be->errmsg[msgnum].str) |
| 788 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 789 | else if (s->fe->errmsg[msgnum].str) |
| 790 | return &s->fe->errmsg[msgnum]; |
| 791 | else |
| 792 | return &http_err_chunks[msgnum]; |
| 793 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 794 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 795 | /* |
| 796 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 797 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 798 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 799 | 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] | 800 | { |
| 801 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 802 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 803 | |
| 804 | m = ((unsigned)*str - 'A'); |
| 805 | |
| 806 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 807 | for (h = http_methods[m]; h->len > 0; h++) { |
| 808 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 809 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 810 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 811 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 812 | }; |
| 813 | return HTTP_METH_OTHER; |
| 814 | } |
| 815 | return HTTP_METH_NONE; |
| 816 | |
| 817 | } |
| 818 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 819 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 820 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 821 | * It is returned otherwise. |
| 822 | */ |
| 823 | static char * |
| 824 | http_get_path(struct http_txn *txn) |
| 825 | { |
| 826 | char *ptr, *end; |
| 827 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 828 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 829 | end = ptr + txn->req.sl.rq.u_l; |
| 830 | |
| 831 | if (ptr >= end) |
| 832 | return NULL; |
| 833 | |
| 834 | /* RFC2616, par. 5.1.2 : |
| 835 | * Request-URI = "*" | absuri | abspath | authority |
| 836 | */ |
| 837 | |
| 838 | if (*ptr == '*') |
| 839 | return NULL; |
| 840 | |
| 841 | if (isalpha((unsigned char)*ptr)) { |
| 842 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 843 | ptr++; |
| 844 | while (ptr < end && |
| 845 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 846 | ptr++; |
| 847 | /* skip '://' */ |
| 848 | if (ptr == end || *ptr++ != ':') |
| 849 | return NULL; |
| 850 | if (ptr == end || *ptr++ != '/') |
| 851 | return NULL; |
| 852 | if (ptr == end || *ptr++ != '/') |
| 853 | return NULL; |
| 854 | } |
| 855 | /* skip [user[:passwd]@]host[:[port]] */ |
| 856 | |
| 857 | while (ptr < end && *ptr != '/') |
| 858 | ptr++; |
| 859 | |
| 860 | if (ptr == end) |
| 861 | return NULL; |
| 862 | |
| 863 | /* OK, we got the '/' ! */ |
| 864 | return ptr; |
| 865 | } |
| 866 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 867 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 868 | * If not found, return NULL. It is returned otherwise. |
| 869 | */ |
| 870 | static char * |
| 871 | http_get_path_from_string(char *str) |
| 872 | { |
| 873 | char *ptr = str; |
| 874 | |
| 875 | /* RFC2616, par. 5.1.2 : |
| 876 | * Request-URI = "*" | absuri | abspath | authority |
| 877 | */ |
| 878 | |
| 879 | if (*ptr == '*') |
| 880 | return NULL; |
| 881 | |
| 882 | if (isalpha((unsigned char)*ptr)) { |
| 883 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 884 | ptr++; |
| 885 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 886 | ptr++; |
| 887 | /* skip '://' */ |
| 888 | if (*ptr == '\0' || *ptr++ != ':') |
| 889 | return NULL; |
| 890 | if (*ptr == '\0' || *ptr++ != '/') |
| 891 | return NULL; |
| 892 | if (*ptr == '\0' || *ptr++ != '/') |
| 893 | return NULL; |
| 894 | } |
| 895 | /* skip [user[:passwd]@]host[:[port]] */ |
| 896 | |
| 897 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 898 | ptr++; |
| 899 | |
| 900 | if (*ptr == '\0' || *ptr == ' ') |
| 901 | return NULL; |
| 902 | |
| 903 | /* OK, we got the '/' ! */ |
| 904 | return ptr; |
| 905 | } |
| 906 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 907 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 908 | * in redirect mode. This may only be called just after the stream interface |
| 909 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 910 | * follow normal proxy processing. NOTE: this function is designed to support |
| 911 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 912 | */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 913 | void http_perform_server_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 914 | { |
| 915 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 916 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 917 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 918 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 919 | |
| 920 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 921 | trash.len = strlen(HTTP_302); |
| 922 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 923 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 924 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 925 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 926 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 927 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 928 | return; |
| 929 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 930 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 931 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 932 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 933 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 934 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 935 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 936 | /* 3: add the request URI. Since it was already forwarded, we need |
| 937 | * to temporarily rewind the buffer. |
| 938 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 939 | txn = &s->txn; |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 940 | b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 941 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 942 | path = http_get_path(txn); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 943 | len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 944 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 945 | b_adv(s->req->buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 946 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 947 | if (!path) |
| 948 | return; |
| 949 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 950 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 951 | return; |
| 952 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 953 | memcpy(trash.str + trash.len, path, len); |
| 954 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 955 | |
| 956 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 957 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 958 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 959 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 960 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 961 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 962 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 963 | |
| 964 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 965 | si_shutr(si); |
| 966 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 967 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 968 | si->state = SI_ST_CLO; |
| 969 | |
| 970 | /* send the message */ |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 971 | http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 972 | |
| 973 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
Willy Tarreau | 4521ba6 | 2013-01-24 01:25:25 +0100 | [diff] [blame] | 974 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 975 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 976 | } |
| 977 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 978 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 979 | * that the server side is closed. Note that err_type is actually a |
| 980 | * bitmask, where almost only aborts may be cumulated with other |
| 981 | * values. We consider that aborted operations are more important |
| 982 | * than timeouts or errors due to the fact that nobody else in the |
| 983 | * logs might explain incomplete retries. All others should avoid |
| 984 | * being cumulated. It should normally not be possible to have multiple |
| 985 | * aborts at once, but just in case, the first one in sequence is reported. |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 986 | * Note that connection errors appearing on the second request of a keep-alive |
| 987 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 988 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 989 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 990 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 991 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 992 | |
| 993 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 994 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 995 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 996 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 997 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 998 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 999 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1000 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1001 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1002 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1003 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1004 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1005 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1006 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1007 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1008 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 1009 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1010 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1011 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
Willy Tarreau | 3634624 | 2014-02-24 18:26:30 +0100 | [diff] [blame] | 1012 | 503, (s->flags & SN_SRV_REUSED) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1013 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1014 | else if (err_type & SI_ET_CONN_RES) |
| 1015 | http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1016 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 1017 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1018 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1019 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1020 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1021 | } |
| 1022 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1023 | extern const char sess_term_cond[8]; |
| 1024 | extern const char sess_fin_state[8]; |
| 1025 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1026 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1027 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1028 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1029 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1030 | /* |
| 1031 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1032 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1033 | */ |
| 1034 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1035 | char **cap, struct cap_hdr *cap_hdr) |
| 1036 | { |
| 1037 | char *eol, *sol, *col, *sov; |
| 1038 | int cur_idx; |
| 1039 | struct cap_hdr *h; |
| 1040 | int len; |
| 1041 | |
| 1042 | sol = som + hdr_idx_first_pos(idx); |
| 1043 | cur_idx = hdr_idx_first_idx(idx); |
| 1044 | |
| 1045 | while (cur_idx) { |
| 1046 | eol = sol + idx->v[cur_idx].len; |
| 1047 | |
| 1048 | col = sol; |
| 1049 | while (col < eol && *col != ':') |
| 1050 | col++; |
| 1051 | |
| 1052 | sov = col + 1; |
| 1053 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1054 | sov++; |
| 1055 | |
| 1056 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1057 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1058 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1059 | if (cap[h->index] == NULL) |
| 1060 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1061 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1062 | |
| 1063 | if (cap[h->index] == NULL) { |
| 1064 | Alert("HTTP capture : out of memory.\n"); |
| 1065 | continue; |
| 1066 | } |
| 1067 | |
| 1068 | len = eol - sov; |
| 1069 | if (len > h->len) |
| 1070 | len = h->len; |
| 1071 | |
| 1072 | memcpy(cap[h->index], sov, len); |
| 1073 | cap[h->index][len]=0; |
| 1074 | } |
| 1075 | } |
| 1076 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1077 | cur_idx = idx->v[cur_idx].next; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1082 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1083 | */ |
| 1084 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1085 | |
| 1086 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1087 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1088 | */ |
| 1089 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1090 | ptr++; \ |
| 1091 | if (likely(ptr < end)) \ |
| 1092 | goto good; \ |
| 1093 | else { \ |
| 1094 | state = (st); \ |
| 1095 | goto http_msg_ood; \ |
| 1096 | } \ |
| 1097 | } while (0) |
| 1098 | |
| 1099 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1100 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1101 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1102 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1103 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1104 | * will give undefined results. |
| 1105 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1106 | * and that msg->sol points to the beginning of the response. |
| 1107 | * If a complete line is found (which implies that at least one CR or LF is |
| 1108 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1109 | * returned indicating an incomplete line (which does not mean that parts have |
| 1110 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1111 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1112 | * upon next call. |
| 1113 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1114 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1115 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1116 | * within its state machine and use the same macros, hence the need for same |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1117 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1118 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1119 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1120 | enum ht_state state, const char *ptr, const char *end, |
| 1121 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1122 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1123 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1124 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1125 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1126 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1127 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1128 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1129 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1130 | |
| 1131 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1132 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1133 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1134 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1135 | state = HTTP_MSG_ERROR; |
| 1136 | break; |
| 1137 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1138 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1139 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1140 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1141 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1142 | goto http_msg_rpcode; |
| 1143 | } |
| 1144 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1145 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1146 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1147 | state = HTTP_MSG_ERROR; |
| 1148 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1149 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1150 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1151 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1152 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1153 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1154 | |
| 1155 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1156 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1157 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1158 | } |
| 1159 | |
| 1160 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1161 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1162 | http_msg_rsp_reason: |
| 1163 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1164 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1165 | msg->sl.st.r_l = 0; |
| 1166 | goto http_msg_rpline_eol; |
| 1167 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1168 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1169 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1170 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1171 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1172 | goto http_msg_rpreason; |
| 1173 | } |
| 1174 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1175 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1176 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1177 | goto http_msg_rsp_reason; |
| 1178 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1179 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1180 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1181 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1182 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1183 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1184 | http_msg_rpline_eol: |
| 1185 | /* We have seen the end of line. Note that we do not |
| 1186 | * necessarily have the \n yet, but at least we know that we |
| 1187 | * have EITHER \r OR \n, otherwise the response would not be |
| 1188 | * complete. We can then record the response length and return |
| 1189 | * to the caller which will be able to register it. |
| 1190 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1191 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1192 | return ptr; |
| 1193 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1194 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1195 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1196 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1197 | exit(1); |
| 1198 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1199 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1203 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1204 | if (ret_state) |
| 1205 | *ret_state = state; |
| 1206 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1207 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1208 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1209 | } |
| 1210 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1211 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1212 | * This function parses a request line between <ptr> and <end>, starting with |
| 1213 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1214 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1215 | * will give undefined results. |
| 1216 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1217 | * and that msg->sol points to the beginning of the request. |
| 1218 | * If a complete line is found (which implies that at least one CR or LF is |
| 1219 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1220 | * returned indicating an incomplete line (which does not mean that parts have |
| 1221 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1222 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1223 | * upon next call. |
| 1224 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1225 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1226 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1227 | * within its state machine and use the same macros, hence the need for same |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1228 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1229 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1230 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1231 | enum ht_state state, const char *ptr, const char *end, |
| 1232 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1233 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1234 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1235 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1236 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1237 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1238 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1239 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1240 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1241 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1242 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1243 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1244 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1245 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1246 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1247 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1248 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1249 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1250 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1251 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1252 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1253 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1254 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1255 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1256 | msg->sl.rq.v_l = 0; |
| 1257 | goto http_msg_rqline_eol; |
| 1258 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1259 | state = HTTP_MSG_ERROR; |
| 1260 | break; |
| 1261 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1262 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1263 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1264 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1265 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1266 | goto http_msg_rquri; |
| 1267 | } |
| 1268 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1269 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1270 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1271 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1272 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1273 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1274 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1275 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1276 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1277 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1278 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1279 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1280 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1281 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1282 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1283 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1284 | /* non-ASCII chars are forbidden unless option |
| 1285 | * accept-invalid-http-request is enabled in the frontend. |
| 1286 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1287 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1288 | if (msg->err_pos < -1) |
| 1289 | goto invalid_char; |
| 1290 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1291 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1292 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1293 | } |
| 1294 | |
| 1295 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1296 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1297 | goto http_msg_req09_uri_e; |
| 1298 | } |
| 1299 | |
| 1300 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1301 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1302 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1303 | state = HTTP_MSG_ERROR; |
| 1304 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1305 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1306 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1307 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1308 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1309 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1310 | goto http_msg_rqver; |
| 1311 | } |
| 1312 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1313 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1314 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1315 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1316 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1317 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1318 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1319 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1320 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1321 | |
| 1322 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1323 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1324 | http_msg_rqline_eol: |
| 1325 | /* We have seen the end of line. Note that we do not |
| 1326 | * necessarily have the \n yet, but at least we know that we |
| 1327 | * have EITHER \r OR \n, otherwise the request would not be |
| 1328 | * complete. We can then record the request length and return |
| 1329 | * to the caller which will be able to register it. |
| 1330 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1331 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1332 | return ptr; |
| 1333 | } |
| 1334 | |
| 1335 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1336 | state = HTTP_MSG_ERROR; |
| 1337 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1338 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1339 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1340 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1341 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1342 | exit(1); |
| 1343 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1344 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1345 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1346 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1347 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1348 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1349 | if (ret_state) |
| 1350 | *ret_state = state; |
| 1351 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1352 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1353 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1354 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1355 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1356 | /* |
| 1357 | * Returns the data from Authorization header. Function may be called more |
| 1358 | * than once so data is stored in txn->auth_data. When no header is found |
| 1359 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
Thierry FOURNIER | 98d9695 | 2014-01-23 12:13:02 +0100 | [diff] [blame] | 1360 | * searching again for something we are unable to find anyway. However, if |
| 1361 | * the result if valid, the cache is not reused because we would risk to |
| 1362 | * have the credentials overwritten by another session in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1363 | */ |
| 1364 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 1365 | /* This bufffer is initialized in the file 'src/haproxy.c'. This length is |
| 1366 | * set according to global.tune.bufsize. |
| 1367 | */ |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1368 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1369 | |
| 1370 | int |
| 1371 | get_http_auth(struct session *s) |
| 1372 | { |
| 1373 | |
| 1374 | struct http_txn *txn = &s->txn; |
| 1375 | struct chunk auth_method; |
| 1376 | struct hdr_ctx ctx; |
| 1377 | char *h, *p; |
| 1378 | int len; |
| 1379 | |
| 1380 | #ifdef DEBUG_AUTH |
| 1381 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1382 | #endif |
| 1383 | |
| 1384 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1385 | return 0; |
| 1386 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1387 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1388 | |
| 1389 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1390 | |
| 1391 | if (txn->flags & TX_USE_PX_CONN) { |
| 1392 | h = "Proxy-Authorization"; |
| 1393 | len = strlen(h); |
| 1394 | } else { |
| 1395 | h = "Authorization"; |
| 1396 | len = strlen(h); |
| 1397 | } |
| 1398 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1399 | if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1400 | return 0; |
| 1401 | |
| 1402 | h = ctx.line + ctx.val; |
| 1403 | |
| 1404 | p = memchr(h, ' ', ctx.vlen); |
| 1405 | if (!p || p == h) |
| 1406 | return 0; |
| 1407 | |
| 1408 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1409 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1410 | |
| 1411 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1412 | |
| 1413 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1414 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1415 | |
| 1416 | if (len < 0) |
| 1417 | return 0; |
| 1418 | |
| 1419 | |
| 1420 | get_http_auth_buff[len] = '\0'; |
| 1421 | |
| 1422 | p = strchr(get_http_auth_buff, ':'); |
| 1423 | |
| 1424 | if (!p) |
| 1425 | return 0; |
| 1426 | |
| 1427 | txn->auth.user = get_http_auth_buff; |
| 1428 | *p = '\0'; |
| 1429 | txn->auth.pass = p+1; |
| 1430 | |
| 1431 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1432 | return 1; |
| 1433 | } |
| 1434 | |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1438 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1439 | /* |
| 1440 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1441 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1442 | * ensuring that the message does not wrap. The function can be preempted |
| 1443 | * everywhere when data are missing and recalled at the exact same location |
| 1444 | * with no information loss. The message may even be realigned between two |
| 1445 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1446 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1447 | * fields. Note that msg->sol will be initialized after completing the first |
| 1448 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1449 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1450 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1451 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1452 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1453 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1454 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1455 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1456 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1457 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1458 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1459 | ptr = buf->p + msg->next; |
| 1460 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1461 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1462 | if (unlikely(ptr >= end)) |
| 1463 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1464 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1465 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1466 | /* |
| 1467 | * First, states that are specific to the response only. |
| 1468 | * We check them first so that request and headers are |
| 1469 | * closer to each other (accessed more often). |
| 1470 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1471 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1472 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1473 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1474 | /* we have a start of message, but we have to check |
| 1475 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1476 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1477 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1478 | if (unlikely(ptr != buf->p)) { |
| 1479 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1480 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1481 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1482 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1483 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1484 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1485 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1486 | hdr_idx_init(idx); |
| 1487 | state = HTTP_MSG_RPVER; |
| 1488 | goto http_msg_rpver; |
| 1489 | } |
| 1490 | |
| 1491 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1492 | goto http_msg_invalid; |
| 1493 | |
| 1494 | if (unlikely(*ptr == '\n')) |
| 1495 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1496 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1497 | /* stop here */ |
| 1498 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1499 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1500 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1501 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1502 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1503 | /* stop here */ |
| 1504 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1505 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1506 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1507 | case HTTP_MSG_RPVER_SP: |
| 1508 | case HTTP_MSG_RPCODE: |
| 1509 | case HTTP_MSG_RPCODE_SP: |
| 1510 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1511 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1512 | state, ptr, end, |
| 1513 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1514 | if (unlikely(!ptr)) |
| 1515 | return; |
| 1516 | |
| 1517 | /* we have a full response and we know that we have either a CR |
| 1518 | * or an LF at <ptr>. |
| 1519 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1520 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1521 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1522 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1523 | if (likely(*ptr == '\r')) |
| 1524 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1525 | goto http_msg_rpline_end; |
| 1526 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1527 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1528 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1529 | /* msg->sol must point to the first of CR or LF. */ |
| 1530 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1531 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1532 | /* stop here */ |
| 1533 | |
| 1534 | /* |
| 1535 | * Second, states that are specific to the request only |
| 1536 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1537 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1538 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1539 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1540 | /* we have a start of message, but we have to check |
| 1541 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1542 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1543 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1544 | if (likely(ptr != buf->p)) { |
| 1545 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1546 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1547 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1548 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1549 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1550 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1551 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1552 | state = HTTP_MSG_RQMETH; |
| 1553 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1554 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1555 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1556 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1557 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1558 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1559 | if (unlikely(*ptr == '\n')) |
| 1560 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1561 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1562 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1563 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1564 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1565 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1566 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1567 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1568 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1569 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1570 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1571 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1572 | case HTTP_MSG_RQMETH_SP: |
| 1573 | case HTTP_MSG_RQURI: |
| 1574 | case HTTP_MSG_RQURI_SP: |
| 1575 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1576 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1577 | state, ptr, end, |
| 1578 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1579 | if (unlikely(!ptr)) |
| 1580 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1581 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1582 | /* we have a full request and we know that we have either a CR |
| 1583 | * or an LF at <ptr>. |
| 1584 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1585 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1586 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1587 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1588 | if (likely(*ptr == '\r')) |
| 1589 | EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1590 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1591 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1592 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1593 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1594 | /* check for HTTP/0.9 request : no version information available. |
| 1595 | * msg->sol must point to the first of CR or LF. |
| 1596 | */ |
| 1597 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1598 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1599 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1600 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1601 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1602 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1603 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1604 | /* |
| 1605 | * Common states below |
| 1606 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1607 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1608 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1609 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1610 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1611 | goto http_msg_hdr_name; |
| 1612 | } |
| 1613 | |
| 1614 | if (likely(*ptr == '\r')) |
| 1615 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1616 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1617 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1618 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1619 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1620 | /* assumes msg->sol points to the first char */ |
| 1621 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1622 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1623 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1624 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1625 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1626 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1627 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1628 | goto http_msg_invalid; |
| 1629 | |
| 1630 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1631 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1632 | |
| 1633 | /* and we still accept this non-token character */ |
| 1634 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1635 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1636 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1637 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1638 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1639 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1640 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1641 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1642 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1643 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1644 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1645 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1646 | goto http_msg_hdr_val; |
| 1647 | } |
| 1648 | |
| 1649 | if (likely(*ptr == '\r')) |
| 1650 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1651 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1652 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1653 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1654 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1655 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1656 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1657 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1658 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1659 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1660 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1661 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1662 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1663 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1664 | goto http_msg_hdr_l1_sp; |
| 1665 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1666 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1667 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1668 | goto http_msg_complete_header; |
| 1669 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1670 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1671 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1672 | /* assumes msg->sol points to the first char, and msg->sov |
| 1673 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1674 | */ |
| 1675 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1676 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1677 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1678 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1679 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1680 | * real header end in case it ends with lots of LWS, but is this |
| 1681 | * really needed ? |
| 1682 | */ |
| 1683 | if (likely(*ptr == '\r')) |
| 1684 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1685 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1686 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1687 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1688 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1689 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1690 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1691 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1692 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1693 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1694 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1695 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1696 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1697 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1698 | goto http_msg_hdr_val; |
| 1699 | } |
| 1700 | http_msg_complete_header: |
| 1701 | /* |
| 1702 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1703 | * Assumes msg->sol points to the first char, msg->sov points |
| 1704 | * to the first character of the value and msg->eol to the |
| 1705 | * first CR or LF so we know how the line ends. We insert last |
| 1706 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1707 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1708 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r', |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1709 | idx, idx->tail) < 0)) |
| 1710 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1711 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1712 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1713 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1714 | goto http_msg_hdr_name; |
| 1715 | } |
| 1716 | |
| 1717 | if (likely(*ptr == '\r')) |
| 1718 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1719 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1720 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1721 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1722 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1723 | /* Assumes msg->sol points to the first of either CR or LF. |
| 1724 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 1725 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 1726 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1727 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1728 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1729 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1730 | msg->eoh = msg->sol; |
| 1731 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1732 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1733 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1734 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1735 | |
| 1736 | case HTTP_MSG_ERROR: |
| 1737 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1738 | break; |
| 1739 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1740 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1741 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1742 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1743 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1744 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1745 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1746 | } |
| 1747 | http_msg_ood: |
| 1748 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1749 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1750 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1751 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1752 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1753 | http_msg_invalid: |
| 1754 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1755 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1756 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1757 | return; |
| 1758 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1759 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1760 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1761 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1762 | * nothing is done and 1 is returned. |
| 1763 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1764 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1765 | { |
| 1766 | int delta; |
| 1767 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1768 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1769 | |
| 1770 | if (msg->sl.rq.v_l != 0) |
| 1771 | return 1; |
| 1772 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1773 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1774 | if (txn->meth != HTTP_METH_GET) |
| 1775 | return 0; |
| 1776 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1777 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1778 | delta = 0; |
| 1779 | |
| 1780 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1781 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1782 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1783 | } |
| 1784 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1785 | delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1786 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1787 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1788 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1789 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1790 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1791 | NULL, NULL); |
| 1792 | if (unlikely(!cur_end)) |
| 1793 | return 0; |
| 1794 | |
| 1795 | /* we have a full HTTP/1.0 request now and we know that |
| 1796 | * we have either a CR or an LF at <ptr>. |
| 1797 | */ |
| 1798 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1799 | return 1; |
| 1800 | } |
| 1801 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1802 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1803 | * and "keep-alive" values. If we already know that some headers may safely |
| 1804 | * be removed, we remove them now. The <to_del> flags are used for that : |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1805 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1806 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1807 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1808 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1809 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1810 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1811 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1812 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1813 | void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1814 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1815 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1816 | const char *hdr_val = "Connection"; |
| 1817 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1818 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1819 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1820 | return; |
| 1821 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1822 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1823 | hdr_val = "Proxy-Connection"; |
| 1824 | hdr_len = 16; |
| 1825 | } |
| 1826 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1827 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1828 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1829 | while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1830 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1831 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1832 | if (to_del & 2) |
| 1833 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1834 | else |
| 1835 | txn->flags |= TX_CON_KAL_SET; |
| 1836 | } |
| 1837 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1838 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1839 | if (to_del & 1) |
| 1840 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1841 | else |
| 1842 | txn->flags |= TX_CON_CLO_SET; |
| 1843 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1844 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1845 | txn->flags |= TX_HDR_CONN_UPG; |
| 1846 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1847 | } |
| 1848 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1849 | txn->flags |= TX_HDR_CONN_PRS; |
| 1850 | return; |
| 1851 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1852 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1853 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1854 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1855 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1856 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1857 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1858 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1859 | { |
| 1860 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1861 | const char *hdr_val = "Connection"; |
| 1862 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1863 | |
| 1864 | ctx.idx = 0; |
| 1865 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1866 | |
| 1867 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1868 | hdr_val = "Proxy-Connection"; |
| 1869 | hdr_len = 16; |
| 1870 | } |
| 1871 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1872 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1873 | while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1874 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1875 | if (wanted & TX_CON_KAL_SET) |
| 1876 | txn->flags |= TX_CON_KAL_SET; |
| 1877 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1878 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1879 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1880 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1881 | if (wanted & TX_CON_CLO_SET) |
| 1882 | txn->flags |= TX_CON_CLO_SET; |
| 1883 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1884 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1885 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1886 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1887 | |
| 1888 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1889 | return; |
| 1890 | |
| 1891 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1892 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1893 | hdr_val = "Connection: close"; |
| 1894 | hdr_len = 17; |
| 1895 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1896 | hdr_val = "Proxy-Connection: close"; |
| 1897 | hdr_len = 23; |
| 1898 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1899 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1903 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1904 | hdr_val = "Connection: keep-alive"; |
| 1905 | hdr_len = 22; |
| 1906 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1907 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1908 | hdr_len = 28; |
| 1909 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1910 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1911 | } |
| 1912 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1913 | } |
| 1914 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 1915 | /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to |
| 1916 | * the first byte of data after the chunk size, so that we know we can forward |
| 1917 | * exactly msg->next bytes. msg->sol contains the exact number of bytes forming |
| 1918 | * the chunk size. That way it is always possible to differentiate between the |
| 1919 | * start of the body and the start of the data. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1920 | * Return >0 on success, 0 when some data is missing, <0 on error. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1921 | * Note: this function is designed to parse wrapped CRLF at the end of the buffer. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1922 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1923 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1924 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1925 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1926 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1927 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1928 | const char *end = buf->data + buf->size; |
| 1929 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1930 | unsigned int chunk = 0; |
| 1931 | |
| 1932 | /* The chunk size is in the following form, though we are only |
| 1933 | * interested in the size and CRLF : |
| 1934 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1935 | */ |
| 1936 | while (1) { |
| 1937 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1938 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1939 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1940 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1941 | if (c < 0) /* not a hex digit anymore */ |
| 1942 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1943 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1944 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1945 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1946 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1947 | chunk = (chunk << 4) + c; |
| 1948 | } |
| 1949 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1950 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1951 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1952 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1953 | |
| 1954 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1955 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1956 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1957 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1958 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1959 | } |
| 1960 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1961 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1962 | * for the end of chunk size. |
| 1963 | */ |
| 1964 | while (1) { |
| 1965 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1966 | /* we now have a CR or an LF at ptr */ |
| 1967 | if (likely(*ptr == '\r')) { |
| 1968 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1969 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1970 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1971 | return 0; |
| 1972 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1973 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1974 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1975 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1976 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1977 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1978 | /* done */ |
| 1979 | break; |
| 1980 | } |
| 1981 | else if (*ptr == ';') { |
| 1982 | /* chunk extension, ends at next CRLF */ |
| 1983 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1984 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1985 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1986 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1987 | |
| 1988 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1989 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1990 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1991 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1992 | return 0; |
| 1993 | } |
| 1994 | /* we have a CRLF now, loop above */ |
| 1995 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1996 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1997 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1998 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1999 | } |
| 2000 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2001 | /* OK we found our CRLF and now <ptr> points to the next byte, |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2002 | * which may or may not be present. We save that into ->next, |
| 2003 | * and the number of bytes parsed into msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2004 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2005 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2006 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2007 | msg->sol += buf->size; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2008 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2009 | msg->chunk_len = chunk; |
| 2010 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2011 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2012 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2013 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2014 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2015 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2016 | } |
| 2017 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2018 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2019 | * message <msg>. The first visited position is msg->next. If the end of |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2020 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2021 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2022 | * If not enough data are available, the function does not change anything |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2023 | * except maybe msg->next if it could parse some lines, and returns zero. |
| 2024 | * If a parse error is encountered, the function returns < 0 and does not |
| 2025 | * change anything except maybe msg->next. Note that the message must |
| 2026 | * already be in HTTP_MSG_TRAILERS state before calling this function, |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2027 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2028 | * forwarding, and that msg->next exactly matches the length of trailers |
| 2029 | * already parsed and not forwarded. It is also important to note that this |
| 2030 | * function is designed to be able to parse wrapped headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2031 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2032 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2033 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2034 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2035 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2036 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2037 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2038 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2039 | const char *ptr = b_ptr(buf, msg->next); |
| 2040 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2041 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2042 | |
| 2043 | /* scan current line and stop at LF or CRLF */ |
| 2044 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2045 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2046 | return 0; |
| 2047 | |
| 2048 | if (*ptr == '\n') { |
| 2049 | if (!p1) |
| 2050 | p1 = ptr; |
| 2051 | p2 = ptr; |
| 2052 | break; |
| 2053 | } |
| 2054 | |
| 2055 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2056 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2057 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2058 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2059 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2060 | p1 = ptr; |
| 2061 | } |
| 2062 | |
| 2063 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2064 | if (ptr >= buf->data + buf->size) |
| 2065 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | /* after LF; point to beginning of next line */ |
| 2069 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2070 | if (p2 >= buf->data + buf->size) |
| 2071 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2072 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2073 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2074 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2075 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2076 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2077 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2078 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2079 | * Everything was scheduled for forwarding, there's nothing |
| 2080 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2081 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2082 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2083 | msg->msg_state = HTTP_MSG_DONE; |
| 2084 | return 1; |
| 2085 | } |
| 2086 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2087 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2088 | } |
| 2089 | } |
| 2090 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2091 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF |
| 2092 | * or a possible LF alone at the end of a chunk. It automatically adjusts |
| 2093 | * msg->next in order to include this part into the next forwarding phase. |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2094 | * Note that the caller must ensure that ->p points to the first byte to parse. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2095 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2096 | * not enough data are available, the function does not change anything and |
| 2097 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2098 | * does not change anything. Note: this function is designed to parse wrapped |
| 2099 | * CRLF at the end of the buffer. |
| 2100 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2101 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2102 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2103 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2104 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2105 | int bytes; |
| 2106 | |
| 2107 | /* NB: we'll check data availabilty at the end. It's not a |
| 2108 | * problem because whatever we match first will be checked |
| 2109 | * against the correct length. |
| 2110 | */ |
| 2111 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2112 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2113 | if (*ptr == '\r') { |
| 2114 | bytes++; |
| 2115 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2116 | if (ptr >= buf->data + buf->size) |
| 2117 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2118 | } |
| 2119 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2120 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2121 | return 0; |
| 2122 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2123 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2124 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2125 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2126 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2127 | |
| 2128 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2129 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2130 | ptr = buf->data; |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2131 | /* Advance ->next to allow the CRLF to be forwarded */ |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2132 | msg->next += bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2133 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2134 | return 1; |
| 2135 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2136 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2137 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2138 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2139 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2140 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2141 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2142 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2143 | { |
| 2144 | int q = 1000; |
| 2145 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2146 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2147 | goto out; |
| 2148 | q = (*qvalue++ - '0') * 1000; |
| 2149 | |
| 2150 | if (*qvalue++ != '.') |
| 2151 | goto out; |
| 2152 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2153 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2154 | goto out; |
| 2155 | q += (*qvalue++ - '0') * 100; |
| 2156 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2157 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2158 | goto out; |
| 2159 | q += (*qvalue++ - '0') * 10; |
| 2160 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2161 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2162 | goto out; |
| 2163 | q += (*qvalue++ - '0') * 1; |
| 2164 | out: |
| 2165 | if (q > 1000) |
| 2166 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2167 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2168 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2169 | return q; |
| 2170 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2171 | |
| 2172 | /* |
| 2173 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2174 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2175 | int select_compression_request_header(struct session *s, struct buffer *req) |
| 2176 | { |
| 2177 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2178 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2179 | struct hdr_ctx ctx; |
| 2180 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2181 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2182 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2183 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 2184 | * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later). |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2185 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 2186 | */ |
| 2187 | ctx.idx = 0; |
| 2188 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2189 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2190 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2191 | (ctx.vlen < 31 || |
| 2192 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2193 | ctx.line[ctx.val + 30] < '6' || |
| 2194 | (ctx.line[ctx.val + 30] == '6' && |
| 2195 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2196 | s->comp_algo = NULL; |
| 2197 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2198 | } |
| 2199 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2200 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2201 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2202 | int best_q = 0; |
| 2203 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2204 | ctx.idx = 0; |
| 2205 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2206 | const char *qval; |
| 2207 | int q; |
| 2208 | int toklen; |
| 2209 | |
| 2210 | /* try to isolate the token from the optional q-value */ |
| 2211 | toklen = 0; |
| 2212 | while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)]) |
| 2213 | toklen++; |
| 2214 | |
| 2215 | qval = ctx.line + ctx.val + toklen; |
| 2216 | while (1) { |
| 2217 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2218 | qval++; |
| 2219 | |
| 2220 | if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') { |
| 2221 | qval = NULL; |
| 2222 | break; |
| 2223 | } |
| 2224 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2225 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2226 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2227 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2228 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2229 | if (qval >= ctx.line + ctx.val + ctx.vlen) { |
| 2230 | qval = NULL; |
| 2231 | break; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2232 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2233 | if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0) |
| 2234 | break; |
| 2235 | |
| 2236 | while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';') |
| 2237 | qval++; |
| 2238 | } |
| 2239 | |
| 2240 | /* here we have qval pointing to the first "q=" attribute or NULL if not found */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2241 | q = qval ? parse_qvalue(qval + 2, NULL) : 1000; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2242 | |
| 2243 | if (q <= best_q) |
| 2244 | continue; |
| 2245 | |
| 2246 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 2247 | if (*(ctx.line + ctx.val) == '*' || |
| 2248 | word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) { |
| 2249 | s->comp_algo = comp_algo; |
| 2250 | best_q = q; |
| 2251 | break; |
| 2252 | } |
| 2253 | } |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2258 | if (s->comp_algo) { |
| 2259 | if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) { |
| 2260 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2261 | ctx.idx = 0; |
| 2262 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2263 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2264 | } |
| 2265 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2266 | return 1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | /* identity is implicit does not require headers */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2270 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
| 2271 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2272 | if (comp_algo->add_data == identity_add_data) { |
| 2273 | s->comp_algo = comp_algo; |
| 2274 | return 1; |
| 2275 | } |
| 2276 | } |
| 2277 | } |
| 2278 | |
| 2279 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2280 | return 0; |
| 2281 | } |
| 2282 | |
| 2283 | /* |
| 2284 | * Selects a comression algorithm depending of the server response. |
| 2285 | */ |
| 2286 | int select_compression_response_header(struct session *s, struct buffer *res) |
| 2287 | { |
| 2288 | struct http_txn *txn = &s->txn; |
| 2289 | struct http_msg *msg = &txn->rsp; |
| 2290 | struct hdr_ctx ctx; |
| 2291 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2292 | |
| 2293 | /* no common compression algorithm was found in request header */ |
| 2294 | if (s->comp_algo == NULL) |
| 2295 | goto fail; |
| 2296 | |
| 2297 | /* HTTP < 1.1 should not be compressed */ |
Willy Tarreau | 7257550 | 2013-12-24 14:41:35 +0100 | [diff] [blame] | 2298 | if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11)) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2299 | goto fail; |
| 2300 | |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2301 | /* 200 only */ |
| 2302 | if (txn->status != 200) |
| 2303 | goto fail; |
| 2304 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2305 | /* Content-Length is null */ |
| 2306 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2307 | goto fail; |
| 2308 | |
| 2309 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2310 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2311 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2312 | goto fail; |
| 2313 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2314 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2315 | ctx.idx = 0; |
| 2316 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2317 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2318 | goto fail; |
| 2319 | } |
| 2320 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2321 | comp_type = NULL; |
| 2322 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2323 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2324 | * not listed in the "compression type" directive if any. If no content-type was |
| 2325 | * found but configuration requires one, we don't compress either. Backend has |
| 2326 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2327 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2328 | ctx.idx = 0; |
| 2329 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2330 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2331 | goto fail; |
| 2332 | |
| 2333 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 2334 | (s->fe->comp && (comp_type = s->fe->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2335 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2336 | if (ctx.vlen >= comp_type->name_len && |
| 2337 | strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2338 | /* this Content-Type should be compressed */ |
| 2339 | break; |
| 2340 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2341 | /* this Content-Type should not be compressed */ |
| 2342 | if (comp_type == NULL) |
| 2343 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2344 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2345 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2346 | else { /* no content-type header */ |
| 2347 | if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types)) |
| 2348 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2349 | } |
| 2350 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2351 | /* limit compression rate */ |
| 2352 | if (global.comp_rate_lim > 0) |
| 2353 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2354 | goto fail; |
| 2355 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2356 | /* limit cpu usage */ |
| 2357 | if (idle_pct < compress_min_idle) |
| 2358 | goto fail; |
| 2359 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2360 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2361 | if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0) |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2362 | goto fail; |
| 2363 | |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2364 | s->flags |= SN_COMP_READY; |
| 2365 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2366 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2367 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2368 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2369 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2370 | |
| 2371 | /* add Transfer-Encoding header */ |
| 2372 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2373 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2374 | |
| 2375 | /* |
| 2376 | * Add Content-Encoding header when it's not identity encoding. |
| 2377 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2378 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2379 | * header. |
| 2380 | */ |
| 2381 | if (s->comp_algo->add_data != identity_add_data) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2382 | trash.len = 18; |
| 2383 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
| 2384 | memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len); |
| 2385 | trash.len += s->comp_algo->name_len; |
| 2386 | trash.str[trash.len] = '\0'; |
| 2387 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2388 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2389 | return 1; |
| 2390 | |
| 2391 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2392 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2393 | return 0; |
| 2394 | } |
| 2395 | |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2396 | void http_adjust_conn_mode(struct session *s, struct http_txn *txn, struct http_msg *msg) |
| 2397 | { |
| 2398 | int tmp = TX_CON_WANT_KAL; |
| 2399 | |
| 2400 | if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 2401 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
| 2402 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2403 | tmp = TX_CON_WANT_TUN; |
| 2404 | |
| 2405 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 2406 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2407 | tmp = TX_CON_WANT_TUN; |
| 2408 | } |
| 2409 | |
| 2410 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 2411 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 2412 | /* option httpclose + server_close => forceclose */ |
| 2413 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 2414 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2415 | tmp = TX_CON_WANT_CLO; |
| 2416 | else |
| 2417 | tmp = TX_CON_WANT_SCL; |
| 2418 | } |
| 2419 | |
| 2420 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
| 2421 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 2422 | tmp = TX_CON_WANT_CLO; |
| 2423 | |
| 2424 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2425 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2426 | |
| 2427 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 2428 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2429 | /* parse the Connection header and possibly clean it */ |
| 2430 | int to_del = 0; |
| 2431 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 2432 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 2433 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
| 2434 | to_del |= 2; /* remove "keep-alive" */ |
| 2435 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2436 | to_del |= 1; /* remove "close" */ |
| 2437 | http_parse_connection_header(txn, msg, to_del); |
| 2438 | } |
| 2439 | |
| 2440 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2441 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2442 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2443 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2444 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 2445 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
| 2446 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
| 2447 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2448 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2449 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2450 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2451 | * processing can continue on next analysers, or zero if it either needs more |
| 2452 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2453 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2454 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2455 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2456 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2457 | int http_wait_for_request(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2458 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2459 | /* |
| 2460 | * We will parse the partial (or complete) lines. |
| 2461 | * We will check the request syntax, and also join multi-line |
| 2462 | * headers. An index of all the lines will be elaborated while |
| 2463 | * parsing. |
| 2464 | * |
| 2465 | * For the parsing, we use a 28 states FSM. |
| 2466 | * |
| 2467 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2468 | * req->buf->p = beginning of request |
| 2469 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2470 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2471 | * msg->eol = end of current header or line (LF or CRLF) |
| 2472 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2473 | * |
| 2474 | * At end of parsing, we may perform a capture of the error (if any), and |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 2475 | * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2476 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2477 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2478 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2479 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2480 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2481 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2482 | struct http_txn *txn = &s->txn; |
| 2483 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2484 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2485 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2486 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2487 | now_ms, __FUNCTION__, |
| 2488 | s, |
| 2489 | req, |
| 2490 | req->rex, req->wex, |
| 2491 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2492 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2493 | req->analysers); |
| 2494 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2495 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2496 | s->srv_error = http_return_srv_error; |
| 2497 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2498 | /* There's a protected area at the end of the buffer for rewriting |
| 2499 | * purposes. We don't want to start to parse the request if the |
| 2500 | * protected area is affected, because we may have to move processed |
| 2501 | * data later, which is much more complicated. |
| 2502 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2503 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2504 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 2505 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2506 | if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2507 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2508 | /* some data has still not left the buffer, wake us once that's done */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2509 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2510 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2511 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2512 | return 0; |
| 2513 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2514 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2515 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2516 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2517 | } |
| 2518 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2519 | /* Note that we have the same problem with the response ; we |
| 2520 | * may want to send a redirect, error or anything which requires |
| 2521 | * some spare space. So we'll ensure that we have at least |
| 2522 | * maxrewrite bytes available in the response buffer before |
| 2523 | * processing that one. This will only affect pipelined |
| 2524 | * keep-alive requests. |
| 2525 | */ |
| 2526 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 2527 | unlikely(!channel_is_rewritable(s->rep) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2528 | bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) || |
| 2529 | bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) { |
| 2530 | if (s->rep->buf->o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2531 | if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2532 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2533 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2534 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2535 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2536 | s->rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2537 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2538 | return 0; |
| 2539 | } |
| 2540 | } |
| 2541 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2542 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2543 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2544 | } |
| 2545 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2546 | /* 1: we might have to print this header in debug mode */ |
| 2547 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2548 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2549 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2550 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2551 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2552 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2553 | /* this is a bit complex : in case of error on the request line, |
| 2554 | * we know that rq.l is still zero, so we display only the part |
| 2555 | * up to the end of the line (truncated by debug_hdr). |
| 2556 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2557 | eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2558 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2559 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2560 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2561 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2562 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2563 | while (cur_idx) { |
| 2564 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2565 | debug_hdr("clihdr", s, sol, eol); |
| 2566 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2567 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2568 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2569 | } |
| 2570 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2571 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2572 | /* |
| 2573 | * Now we quickly check if we have found a full valid request. |
| 2574 | * If not so, we check the FD and buffer states before leaving. |
| 2575 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2576 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2577 | * requests are checked first. When waiting for a second request |
| 2578 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2579 | * we note the error in the session flags but don't set any state. |
| 2580 | * Since the error will be noted there, it will not be counted by |
| 2581 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2582 | * Last, we may increase some tracked counters' http request errors on |
| 2583 | * the cases that are deliberately the client's fault. For instance, |
| 2584 | * a timeout or connection reset is not counted as an error. However |
| 2585 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2586 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2587 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2588 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2589 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2590 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2591 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2592 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2593 | session_inc_http_req_ctr(s); |
| 2594 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2595 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2596 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2597 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2598 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2599 | /* 1: Since we are in header mode, if there's no space |
| 2600 | * left for headers, we won't be able to free more |
| 2601 | * later, so the session will never terminate. We |
| 2602 | * must terminate it now. |
| 2603 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2604 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2605 | /* FIXME: check if URI is set and return Status |
| 2606 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2607 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2608 | session_inc_http_req_ctr(s); |
| 2609 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2610 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2611 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2612 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2613 | goto return_bad_req; |
| 2614 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2615 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2616 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2617 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2618 | if (!(s->flags & SN_ERR_MASK)) |
| 2619 | s->flags |= SN_ERR_CLICL; |
| 2620 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2621 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2622 | goto failed_keep_alive; |
| 2623 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2624 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2625 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2626 | 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] | 2627 | session_inc_http_err_ctr(s); |
| 2628 | } |
| 2629 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2630 | txn->status = 400; |
| 2631 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2632 | msg->msg_state = HTTP_MSG_ERROR; |
| 2633 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2634 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2635 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2636 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2637 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2638 | if (s->listener->counters) |
| 2639 | s->listener->counters->failed_req++; |
| 2640 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2641 | if (!(s->flags & SN_FINST_MASK)) |
| 2642 | s->flags |= SN_FINST_R; |
| 2643 | return 0; |
| 2644 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2645 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2646 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2647 | 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] | 2648 | if (!(s->flags & SN_ERR_MASK)) |
| 2649 | s->flags |= SN_ERR_CLITO; |
| 2650 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2651 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2652 | goto failed_keep_alive; |
| 2653 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2654 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2655 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2656 | 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] | 2657 | session_inc_http_err_ctr(s); |
| 2658 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2659 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2660 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2661 | msg->msg_state = HTTP_MSG_ERROR; |
| 2662 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2663 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2664 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2665 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2666 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2667 | if (s->listener->counters) |
| 2668 | s->listener->counters->failed_req++; |
| 2669 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2670 | if (!(s->flags & SN_FINST_MASK)) |
| 2671 | s->flags |= SN_FINST_R; |
| 2672 | return 0; |
| 2673 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2674 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2675 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2676 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2677 | if (!(s->flags & SN_ERR_MASK)) |
| 2678 | s->flags |= SN_ERR_CLICL; |
| 2679 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2680 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2681 | goto failed_keep_alive; |
| 2682 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2683 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2684 | 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] | 2685 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2686 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2687 | msg->msg_state = HTTP_MSG_ERROR; |
| 2688 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2689 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2690 | session_inc_http_err_ctr(s); |
| 2691 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2692 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2693 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2694 | if (s->listener->counters) |
| 2695 | s->listener->counters->failed_req++; |
| 2696 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2697 | if (!(s->flags & SN_FINST_MASK)) |
| 2698 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2699 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2700 | } |
| 2701 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2702 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2703 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2704 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2705 | #ifdef TCP_QUICKACK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 2706 | 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] | 2707 | /* We need more data, we have to re-enable quick-ack in case we |
| 2708 | * previously disabled it, otherwise we might cause the client |
| 2709 | * to delay next data. |
| 2710 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2711 | 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] | 2712 | } |
| 2713 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2714 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2715 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2716 | /* If the client starts to talk, let's fall back to |
| 2717 | * request timeout processing. |
| 2718 | */ |
| 2719 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2720 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2721 | } |
| 2722 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2723 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2724 | if (!tick_isset(req->analyse_exp)) { |
| 2725 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2726 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2727 | tick_isset(s->be->timeout.httpka)) |
| 2728 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2729 | else |
| 2730 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2731 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2732 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2733 | /* we're not ready yet */ |
| 2734 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2735 | |
| 2736 | failed_keep_alive: |
| 2737 | /* Here we process low-level errors for keep-alive requests. In |
| 2738 | * short, if the request is not the first one and it experiences |
| 2739 | * a timeout, read error or shutdown, we just silently close so |
| 2740 | * that the client can try again. |
| 2741 | */ |
| 2742 | txn->status = 0; |
| 2743 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2744 | req->analysers = 0; |
| 2745 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2746 | s->logs.level = 0; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2747 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2748 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2749 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2750 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2751 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2752 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2753 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2754 | * 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] | 2755 | * 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] | 2756 | * 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] | 2757 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2758 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2759 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2760 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2761 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2762 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2763 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2764 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2765 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2766 | /* kill the pending keep-alive timeout */ |
| 2767 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2768 | req->analyse_exp = TICK_ETERNITY; |
| 2769 | } |
| 2770 | |
| 2771 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2772 | /* Maybe we found in invalid header name while we were configured not |
| 2773 | * to block on that, so we have to capture it now. |
| 2774 | */ |
| 2775 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2776 | 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] | 2777 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2778 | /* |
| 2779 | * 1: identify the method |
| 2780 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2781 | 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] | 2782 | |
| 2783 | /* we can make use of server redirect on GET and HEAD */ |
| 2784 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2785 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2786 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2787 | /* |
| 2788 | * 2: check if the URI matches the monitor_uri. |
| 2789 | * We have to do this for every request which gets in, because |
| 2790 | * the monitor-uri is defined by the frontend. |
| 2791 | */ |
| 2792 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2793 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2794 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2795 | s->fe->monitor_uri, |
| 2796 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2797 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2798 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2799 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2800 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2801 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2802 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2803 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2804 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2805 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2806 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2807 | 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] | 2808 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2809 | ret = acl_pass(ret); |
| 2810 | if (cond->pol == ACL_COND_UNLESS) |
| 2811 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2812 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2813 | if (ret) { |
| 2814 | /* we fail this request, let's return 503 service unavail */ |
| 2815 | txn->status = 503; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2816 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2817 | if (!(s->flags & SN_ERR_MASK)) |
| 2818 | 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] | 2819 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2820 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2821 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2822 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2823 | /* nothing to fail, let's reply normaly */ |
| 2824 | txn->status = 200; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2825 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2826 | if (!(s->flags & SN_ERR_MASK)) |
| 2827 | s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2828 | goto return_prx_cond; |
| 2829 | } |
| 2830 | |
| 2831 | /* |
| 2832 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2833 | * Note: we cannot log anymore if the request has been |
| 2834 | * classified as invalid. |
| 2835 | */ |
| 2836 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2837 | /* we have a complete HTTP request that we must log */ |
| 2838 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2839 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2840 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2841 | if (urilen >= REQURI_LEN) |
| 2842 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2843 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2844 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2845 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2846 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2847 | s->do_log(s); |
| 2848 | } else { |
| 2849 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2850 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2851 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2852 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2853 | /* 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] | 2854 | 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] | 2855 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2856 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2857 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2858 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2859 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2860 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2861 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2862 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2863 | |
| 2864 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2865 | 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] | 2866 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2867 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2868 | * we have what looks like a proxied connection instead of a connection, |
| 2869 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2870 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2871 | * happen to do that despite being non-standard :-( |
| 2872 | * We consider that a request not beginning with either '/' or '*' is |
| 2873 | * a proxied connection, which covers both "scheme://location" and |
| 2874 | * CONNECT ip:port. |
| 2875 | */ |
| 2876 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2877 | 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] | 2878 | txn->flags |= TX_USE_PX_CONN; |
| 2879 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2880 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2881 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2882 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2883 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2884 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2885 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2886 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2887 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2888 | /* 6: determine the transfer-length. |
| 2889 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2890 | * the presence of a message-body in a REQUEST and its transfer length |
| 2891 | * must be determined that way (in order of precedence) : |
| 2892 | * 1. The presence of a message-body in a request is signaled by the |
| 2893 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2894 | * in the request's header fields. When a request message contains |
| 2895 | * both a message-body of non-zero length and a method that does |
| 2896 | * not define any semantics for that request message-body, then an |
| 2897 | * origin server SHOULD either ignore the message-body or respond |
| 2898 | * with an appropriate error message (e.g., 413). A proxy or |
| 2899 | * gateway, when presented the same request, SHOULD either forward |
| 2900 | * the request inbound with the message- body or ignore the |
| 2901 | * message-body when determining a response. |
| 2902 | * |
| 2903 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2904 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2905 | * transfer-length is defined by the use of this transfer-coding. |
| 2906 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2907 | * transfer-coding is not present, the transfer-length is defined |
| 2908 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2909 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2910 | * 3. If a Content-Length header field is present, its decimal value in |
| 2911 | * OCTETs represents both the entity-length and the transfer-length. |
| 2912 | * If a message is received with both a Transfer-Encoding header |
| 2913 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2914 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2915 | * 4. By the server closing the connection. (Closing the connection |
| 2916 | * cannot be used to indicate the end of a request body, since that |
| 2917 | * would leave no possibility for the server to send back a response.) |
| 2918 | * |
| 2919 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2920 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2921 | * it is terminated by closing the connection. When the "chunked" |
| 2922 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2923 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2924 | */ |
| 2925 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2926 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2927 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2928 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2929 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2930 | 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] | 2931 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2932 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2933 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2934 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2935 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2936 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2937 | break; |
| 2938 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2939 | } |
| 2940 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2941 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2942 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2943 | 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] | 2944 | signed long long cl; |
| 2945 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2946 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2947 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2948 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2949 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2950 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2951 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2952 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2953 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2954 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2955 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2956 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2957 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2958 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2959 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2960 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2961 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2962 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2963 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2964 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2965 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2966 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2967 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2968 | } |
| 2969 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2970 | /* bodyless requests have a known length */ |
| 2971 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2972 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2973 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 2974 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 2975 | * only change if both the request and the config reference something else. |
| 2976 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 2977 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 2978 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 2979 | * if FE and BE have the same settings (common). The method consists in |
| 2980 | * checking if options changed between the two calls (implying that either |
| 2981 | * one is non-null, or one of them is non-null and we are there for the first |
| 2982 | * time. |
| 2983 | */ |
| 2984 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2985 | ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) |
| 2986 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 2987 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2988 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2989 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2990 | req->analyse_exp = TICK_ETERNITY; |
| 2991 | return 1; |
| 2992 | |
| 2993 | return_bad_req: |
| 2994 | /* We centralize bad requests processing here */ |
| 2995 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2996 | /* we detected a parsing error. We want to archive this request |
| 2997 | * in the dedicated proxy area for later troubleshooting. |
| 2998 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2999 | 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] | 3000 | } |
| 3001 | |
| 3002 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3003 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3004 | 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] | 3005 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3006 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3007 | if (s->listener->counters) |
| 3008 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3009 | |
| 3010 | return_prx_cond: |
| 3011 | if (!(s->flags & SN_ERR_MASK)) |
| 3012 | s->flags |= SN_ERR_PRXCOND; |
| 3013 | if (!(s->flags & SN_FINST_MASK)) |
| 3014 | s->flags |= SN_FINST_R; |
| 3015 | |
| 3016 | req->analysers = 0; |
| 3017 | req->analyse_exp = TICK_ETERNITY; |
| 3018 | return 0; |
| 3019 | } |
| 3020 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3021 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3022 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3023 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3024 | * and program a response message if something was unexpected. It cannot fail |
| 3025 | * 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] | 3026 | * 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] | 3027 | * s->target which is supposed to already point to the stats applet. The caller |
| 3028 | * is expected to have already assigned an appctx to the session. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3029 | */ |
| 3030 | int http_handle_stats(struct session *s, struct channel *req) |
| 3031 | { |
| 3032 | struct stats_admin_rule *stats_admin_rule; |
| 3033 | struct stream_interface *si = s->rep->prod; |
| 3034 | struct http_txn *txn = &s->txn; |
| 3035 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3036 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3037 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3038 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3039 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3040 | appctx = si_appctx(si); |
| 3041 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3042 | appctx->st1 = appctx->st2 = 0; |
| 3043 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3044 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | af3cf70 | 2014-04-22 22:19:53 +0200 | [diff] [blame] | 3045 | if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD)) |
| 3046 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3047 | |
| 3048 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3049 | lookup = uri + uri_auth->uri_len; |
| 3050 | |
| 3051 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3052 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3053 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3054 | break; |
| 3055 | } |
| 3056 | } |
| 3057 | |
| 3058 | if (uri_auth->refresh) { |
| 3059 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3060 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3061 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3062 | break; |
| 3063 | } |
| 3064 | } |
| 3065 | } |
| 3066 | |
| 3067 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3068 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3069 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3070 | break; |
| 3071 | } |
| 3072 | } |
| 3073 | |
| 3074 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3075 | if (memcmp(h, ";st=", 4) == 0) { |
| 3076 | int i; |
| 3077 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3078 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3079 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3080 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3081 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3082 | break; |
| 3083 | } |
| 3084 | } |
| 3085 | break; |
| 3086 | } |
| 3087 | } |
| 3088 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3089 | appctx->ctx.stats.scope_str = 0; |
| 3090 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3091 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3092 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3093 | int itx = 0; |
| 3094 | const char *h2; |
| 3095 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3096 | const char *err; |
| 3097 | |
| 3098 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3099 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3100 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3101 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3102 | itx++; |
| 3103 | h++; |
| 3104 | } |
| 3105 | |
| 3106 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3107 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3108 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3109 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3110 | /* 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] | 3111 | memcpy(scope_txt, h2, itx); |
| 3112 | scope_txt[itx] = '\0'; |
| 3113 | err = invalid_char(scope_txt); |
| 3114 | if (err) { |
| 3115 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3116 | appctx->ctx.stats.scope_str = 0; |
| 3117 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3118 | } |
| 3119 | break; |
| 3120 | } |
| 3121 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3122 | |
| 3123 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3124 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3125 | int ret = 1; |
| 3126 | |
| 3127 | if (stats_admin_rule->cond) { |
| 3128 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 3129 | ret = acl_pass(ret); |
| 3130 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3131 | ret = !ret; |
| 3132 | } |
| 3133 | |
| 3134 | if (ret) { |
| 3135 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3136 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3137 | break; |
| 3138 | } |
| 3139 | } |
| 3140 | |
| 3141 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3142 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3143 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3144 | /* we'll need the request body, possibly after sending 100-continue */ |
| 3145 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3146 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3147 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3148 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3149 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3150 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3151 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3152 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3153 | else { |
| 3154 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3155 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3156 | } |
| 3157 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3158 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3159 | return 1; |
| 3160 | } |
| 3161 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3162 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3163 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3164 | */ |
| 3165 | static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
| 3166 | { |
| 3167 | #ifdef IP_TOS |
| 3168 | if (from.ss_family == AF_INET) |
| 3169 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3170 | #endif |
| 3171 | #ifdef IPV6_TCLASS |
| 3172 | if (from.ss_family == AF_INET6) { |
| 3173 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3174 | /* v4-mapped addresses need IP_TOS */ |
| 3175 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3176 | else |
| 3177 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3178 | } |
| 3179 | #endif |
| 3180 | } |
| 3181 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3182 | /* Returns the number of characters written to destination, |
| 3183 | * -1 on internal error and -2 if no replacement took place. |
| 3184 | */ |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3185 | static int http_replace_header(struct my_regex *re, char *dst, uint dst_size, char *val, int len, |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 3186 | const char *rep_str) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3187 | { |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3188 | if (!regex_exec_match2(re, val, len, MAX_MATCH, pmatch)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3189 | return -2; |
| 3190 | |
| 3191 | return exp_replace(dst, dst_size, val, rep_str, pmatch); |
| 3192 | } |
| 3193 | |
| 3194 | /* Returns the number of characters written to destination, |
| 3195 | * -1 on internal error and -2 if no replacement took place. |
| 3196 | */ |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3197 | static int http_replace_value(struct my_regex *re, char *dst, uint dst_size, char *val, int len, char delim, |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 3198 | const char *rep_str) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3199 | { |
| 3200 | char* p = val; |
| 3201 | char* dst_end = dst + dst_size; |
| 3202 | char* dst_p = dst; |
| 3203 | |
| 3204 | for (;;) { |
| 3205 | char *p_delim; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3206 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3207 | /* look for delim. */ |
| 3208 | p_delim = p; |
| 3209 | while (p_delim < p + len && *p_delim != delim) |
| 3210 | p_delim++; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3211 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3212 | if (regex_exec_match2(re, p, p_delim-p, MAX_MATCH, pmatch)) { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3213 | int replace_n = exp_replace(dst_p, dst_end - dst_p, p, rep_str, pmatch); |
| 3214 | |
| 3215 | if (replace_n < 0) |
| 3216 | return -1; |
| 3217 | |
| 3218 | dst_p += replace_n; |
| 3219 | } else { |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3220 | uint len = p_delim - p; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3221 | |
| 3222 | if (dst_p + len >= dst_end) |
| 3223 | return -1; |
| 3224 | |
| 3225 | memcpy(dst_p, p, len); |
| 3226 | dst_p += len; |
| 3227 | } |
| 3228 | |
| 3229 | if (dst_p >= dst_end) |
| 3230 | return -1; |
| 3231 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3232 | /* end of the replacements. */ |
| 3233 | if (p_delim >= p + len) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3234 | break; |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3235 | |
| 3236 | /* Next part. */ |
| 3237 | *dst_p++ = delim; |
| 3238 | p = p_delim + 1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | return dst_p - dst; |
| 3242 | } |
| 3243 | |
| 3244 | 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] | 3245 | 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] | 3246 | struct hdr_ctx* ctx, int action) |
| 3247 | { |
| 3248 | ctx->idx = 0; |
| 3249 | |
| 3250 | while (http_find_full_header2(name, name_len, buf, idx, ctx)) { |
| 3251 | struct hdr_idx_elem *hdr = idx->v + ctx->idx; |
| 3252 | int delta; |
| 3253 | char* val = (char*)ctx->line + name_len + 2; |
| 3254 | char* val_end = (char*)ctx->line + hdr->len; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3255 | char* reg_dst_buf; |
| 3256 | uint reg_dst_buf_size; |
| 3257 | int n_replaced; |
| 3258 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3259 | trash.len = build_logline(s, trash.str, trash.size, fmt); |
| 3260 | |
| 3261 | if (trash.len >= trash.size - 1) |
| 3262 | return -1; |
| 3263 | |
| 3264 | reg_dst_buf = trash.str + trash.len + 1; |
| 3265 | reg_dst_buf_size = trash.size - trash.len - 1; |
| 3266 | |
| 3267 | switch (action) { |
| 3268 | case HTTP_REQ_ACT_REPLACE_VAL: |
| 3269 | case HTTP_RES_ACT_REPLACE_VAL: |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3270 | n_replaced = http_replace_value(re, reg_dst_buf, reg_dst_buf_size, val, val_end-val, ',', trash.str); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3271 | break; |
| 3272 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3273 | case HTTP_RES_ACT_REPLACE_HDR: |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3274 | n_replaced = http_replace_header(re, reg_dst_buf, reg_dst_buf_size, val, val_end-val, trash.str); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3275 | break; |
| 3276 | default: /* impossible */ |
| 3277 | return -1; |
| 3278 | } |
| 3279 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3280 | switch (n_replaced) { |
| 3281 | case -1: return -1; |
| 3282 | case -2: continue; |
| 3283 | } |
| 3284 | |
| 3285 | delta = buffer_replace2(msg->chn->buf, val, val_end, reg_dst_buf, n_replaced); |
| 3286 | |
| 3287 | hdr->len += delta; |
| 3288 | http_msg_move_end(msg, delta); |
| 3289 | } |
| 3290 | |
| 3291 | return 0; |
| 3292 | } |
| 3293 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3294 | /* Executes the http-request rules <rules> for session <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3295 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3296 | * further processing of the request (auth, deny, ...), and defaults to |
| 3297 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3298 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 3299 | * on txn->flags if it encounters a tarpit rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3300 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3301 | enum rule_result |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3302 | http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3303 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3304 | struct connection *cli_conn; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3305 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3306 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3307 | const char *auth_realm; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3308 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3309 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3310 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3311 | continue; |
| 3312 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3313 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3314 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3315 | int ret; |
| 3316 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3317 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3318 | ret = acl_pass(ret); |
| 3319 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3320 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3321 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3322 | |
| 3323 | if (!ret) /* condition not matched */ |
| 3324 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3325 | } |
| 3326 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3327 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3328 | switch (rule->action) { |
| 3329 | case HTTP_REQ_ACT_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3330 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3331 | |
| 3332 | case HTTP_REQ_ACT_DENY: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3333 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3334 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3335 | case HTTP_REQ_ACT_TARPIT: |
| 3336 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3337 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3338 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3339 | case HTTP_REQ_ACT_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3340 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3341 | auth_realm = rule->arg.auth.realm; |
| 3342 | if (!auth_realm) { |
| 3343 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3344 | auth_realm = STATS_DEFAULT_REALM; |
| 3345 | else |
| 3346 | auth_realm = px->id; |
| 3347 | } |
| 3348 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3349 | * count one error, because normal browsing won't significantly |
| 3350 | * increase the counter but brute force attempts will. |
| 3351 | */ |
| 3352 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3353 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
| 3354 | stream_int_retnclose(&s->si[0], &trash); |
| 3355 | session_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3356 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3357 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3358 | case HTTP_REQ_ACT_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3359 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3360 | return HTTP_RULE_RES_BADREQ; |
| 3361 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3362 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3363 | case HTTP_REQ_ACT_SET_NICE: |
| 3364 | s->task->nice = rule->arg.nice; |
| 3365 | break; |
| 3366 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3367 | case HTTP_REQ_ACT_SET_TOS: |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3368 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3369 | inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3370 | break; |
| 3371 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3372 | case HTTP_REQ_ACT_SET_MARK: |
| 3373 | #ifdef SO_MARK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3374 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3375 | setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark)); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3376 | #endif |
| 3377 | break; |
| 3378 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3379 | case HTTP_REQ_ACT_SET_LOGL: |
| 3380 | s->logs.level = rule->arg.loglevel; |
| 3381 | break; |
| 3382 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3383 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3384 | case HTTP_REQ_ACT_REPLACE_VAL: |
| 3385 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3386 | txn->req.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt, |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 3387 | &rule->arg.hdr_add.re, &ctx, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3388 | return HTTP_RULE_RES_BADREQ; |
| 3389 | break; |
| 3390 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3391 | case HTTP_REQ_ACT_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3392 | ctx.idx = 0; |
| 3393 | /* remove all occurrences of the header */ |
| 3394 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3395 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3396 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3397 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3398 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3399 | |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3400 | case HTTP_REQ_ACT_SET_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3401 | case HTTP_REQ_ACT_ADD_HDR: |
| 3402 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3403 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3404 | trash.len = rule->arg.hdr_add.name_len; |
| 3405 | trash.str[trash.len++] = ':'; |
| 3406 | trash.str[trash.len++] = ' '; |
| 3407 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3408 | |
| 3409 | if (rule->action == HTTP_REQ_ACT_SET_HDR) { |
| 3410 | /* remove all occurrences of the header */ |
| 3411 | ctx.idx = 0; |
| 3412 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3413 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3414 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3415 | } |
| 3416 | } |
| 3417 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3418 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3419 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3420 | |
| 3421 | case HTTP_REQ_ACT_DEL_ACL: |
| 3422 | case HTTP_REQ_ACT_DEL_MAP: { |
| 3423 | struct pat_ref *ref; |
| 3424 | char *key; |
| 3425 | int len; |
| 3426 | |
| 3427 | /* collect reference */ |
| 3428 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3429 | if (!ref) |
| 3430 | continue; |
| 3431 | |
| 3432 | /* collect key */ |
| 3433 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3434 | key = trash.str; |
| 3435 | key[len] = '\0'; |
| 3436 | |
| 3437 | /* perform update */ |
| 3438 | /* returned code: 1=ok, 0=ko */ |
| 3439 | pat_ref_delete(ref, key); |
| 3440 | |
| 3441 | break; |
| 3442 | } |
| 3443 | |
| 3444 | case HTTP_REQ_ACT_ADD_ACL: { |
| 3445 | struct pat_ref *ref; |
| 3446 | char *key; |
| 3447 | struct chunk *trash_key; |
| 3448 | int len; |
| 3449 | |
| 3450 | trash_key = get_trash_chunk(); |
| 3451 | |
| 3452 | /* collect reference */ |
| 3453 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3454 | if (!ref) |
| 3455 | continue; |
| 3456 | |
| 3457 | /* collect key */ |
| 3458 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3459 | key = trash_key->str; |
| 3460 | key[len] = '\0'; |
| 3461 | |
| 3462 | /* perform update */ |
| 3463 | /* add entry only if it does not already exist */ |
| 3464 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3465 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3466 | |
| 3467 | break; |
| 3468 | } |
| 3469 | |
| 3470 | case HTTP_REQ_ACT_SET_MAP: { |
| 3471 | struct pat_ref *ref; |
| 3472 | char *key, *value; |
| 3473 | struct chunk *trash_key, *trash_value; |
| 3474 | int len; |
| 3475 | |
| 3476 | trash_key = get_trash_chunk(); |
| 3477 | trash_value = get_trash_chunk(); |
| 3478 | |
| 3479 | /* collect reference */ |
| 3480 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3481 | if (!ref) |
| 3482 | continue; |
| 3483 | |
| 3484 | /* collect key */ |
| 3485 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3486 | key = trash_key->str; |
| 3487 | key[len] = '\0'; |
| 3488 | |
| 3489 | /* collect value */ |
| 3490 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3491 | value = trash_value->str; |
| 3492 | value[len] = '\0'; |
| 3493 | |
| 3494 | /* perform update */ |
| 3495 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3496 | /* update entry if it exists */ |
| 3497 | pat_ref_set(ref, key, value, NULL); |
| 3498 | else |
| 3499 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3500 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3501 | |
| 3502 | break; |
| 3503 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3504 | |
| 3505 | case HTTP_REQ_ACT_CUSTOM_CONT: |
| 3506 | rule->action_ptr(rule, px, s, txn); |
| 3507 | break; |
| 3508 | |
| 3509 | case HTTP_REQ_ACT_CUSTOM_STOP: |
| 3510 | rule->action_ptr(rule, px, s, txn); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3511 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3512 | |
| 3513 | case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX: |
| 3514 | /* Note: only the first valid tracking parameter of each |
| 3515 | * applies. |
| 3516 | */ |
| 3517 | |
| 3518 | if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) { |
| 3519 | struct stktable *t; |
| 3520 | struct stksess *ts; |
| 3521 | struct stktable_key *key; |
| 3522 | void *ptr; |
| 3523 | |
| 3524 | t = rule->act_prm.trk_ctr.table.t; |
| 3525 | key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL); |
| 3526 | |
| 3527 | if (key && (ts = stktable_get_entry(t, key))) { |
| 3528 | session_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts); |
| 3529 | |
| 3530 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3531 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3532 | if (ptr) |
| 3533 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3534 | |
| 3535 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3536 | if (ptr) |
| 3537 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3538 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3539 | |
| 3540 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 3541 | if (s->fe != s->be) |
| 3542 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3543 | } |
| 3544 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3545 | } |
| 3546 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3547 | |
| 3548 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3549 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3550 | } |
| 3551 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3552 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3553 | /* Executes the http-response rules <rules> for session <s>, proxy <px> and |
| 3554 | * transaction <txn>. Returns the first rule that prevents further processing |
| 3555 | * of the response (deny, ...) or NULL if it executed all rules or stopped |
| 3556 | * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny |
| 3557 | * rule. |
| 3558 | */ |
| 3559 | static struct http_res_rule * |
| 3560 | http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 3561 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3562 | struct connection *cli_conn; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3563 | struct http_res_rule *rule; |
| 3564 | struct hdr_ctx ctx; |
| 3565 | |
| 3566 | list_for_each_entry(rule, rules, list) { |
| 3567 | if (rule->action >= HTTP_RES_ACT_MAX) |
| 3568 | continue; |
| 3569 | |
| 3570 | /* check optional condition */ |
| 3571 | if (rule->cond) { |
| 3572 | int ret; |
| 3573 | |
| 3574 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 3575 | ret = acl_pass(ret); |
| 3576 | |
| 3577 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3578 | ret = !ret; |
| 3579 | |
| 3580 | if (!ret) /* condition not matched */ |
| 3581 | continue; |
| 3582 | } |
| 3583 | |
| 3584 | |
| 3585 | switch (rule->action) { |
| 3586 | case HTTP_RES_ACT_ALLOW: |
| 3587 | return NULL; /* "allow" rules are OK */ |
| 3588 | |
| 3589 | case HTTP_RES_ACT_DENY: |
| 3590 | txn->flags |= TX_SVDENY; |
| 3591 | return rule; |
| 3592 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3593 | case HTTP_RES_ACT_SET_NICE: |
| 3594 | s->task->nice = rule->arg.nice; |
| 3595 | break; |
| 3596 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3597 | case HTTP_RES_ACT_SET_TOS: |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3598 | 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] | 3599 | 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] | 3600 | break; |
| 3601 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3602 | case HTTP_RES_ACT_SET_MARK: |
| 3603 | #ifdef SO_MARK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3604 | 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] | 3605 | 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] | 3606 | #endif |
| 3607 | break; |
| 3608 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3609 | case HTTP_RES_ACT_SET_LOGL: |
| 3610 | s->logs.level = rule->arg.loglevel; |
| 3611 | break; |
| 3612 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3613 | case HTTP_RES_ACT_REPLACE_HDR: |
| 3614 | case HTTP_RES_ACT_REPLACE_VAL: |
| 3615 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3616 | 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] | 3617 | &rule->arg.hdr_add.re, &ctx, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3618 | return NULL; /* note: we should report an error here */ |
| 3619 | break; |
| 3620 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3621 | case HTTP_RES_ACT_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3622 | ctx.idx = 0; |
| 3623 | /* remove all occurrences of the header */ |
| 3624 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3625 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3626 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3627 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3628 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3629 | |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3630 | case HTTP_RES_ACT_SET_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3631 | case HTTP_RES_ACT_ADD_HDR: |
| 3632 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3633 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3634 | trash.len = rule->arg.hdr_add.name_len; |
| 3635 | trash.str[trash.len++] = ':'; |
| 3636 | trash.str[trash.len++] = ' '; |
| 3637 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3638 | |
| 3639 | if (rule->action == HTTP_RES_ACT_SET_HDR) { |
| 3640 | /* remove all occurrences of the header */ |
| 3641 | ctx.idx = 0; |
| 3642 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3643 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3644 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3645 | } |
| 3646 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3647 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3648 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3649 | |
| 3650 | case HTTP_RES_ACT_DEL_ACL: |
| 3651 | case HTTP_RES_ACT_DEL_MAP: { |
| 3652 | struct pat_ref *ref; |
| 3653 | char *key; |
| 3654 | int len; |
| 3655 | |
| 3656 | /* collect reference */ |
| 3657 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3658 | if (!ref) |
| 3659 | continue; |
| 3660 | |
| 3661 | /* collect key */ |
| 3662 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3663 | key = trash.str; |
| 3664 | key[len] = '\0'; |
| 3665 | |
| 3666 | /* perform update */ |
| 3667 | /* returned code: 1=ok, 0=ko */ |
| 3668 | pat_ref_delete(ref, key); |
| 3669 | |
| 3670 | break; |
| 3671 | } |
| 3672 | |
| 3673 | case HTTP_RES_ACT_ADD_ACL: { |
| 3674 | struct pat_ref *ref; |
| 3675 | char *key; |
| 3676 | struct chunk *trash_key; |
| 3677 | int len; |
| 3678 | |
| 3679 | trash_key = get_trash_chunk(); |
| 3680 | |
| 3681 | /* collect reference */ |
| 3682 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3683 | if (!ref) |
| 3684 | continue; |
| 3685 | |
| 3686 | /* collect key */ |
| 3687 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3688 | key = trash_key->str; |
| 3689 | key[len] = '\0'; |
| 3690 | |
| 3691 | /* perform update */ |
| 3692 | /* check if the entry already exists */ |
| 3693 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3694 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3695 | |
| 3696 | break; |
| 3697 | } |
| 3698 | |
| 3699 | case HTTP_RES_ACT_SET_MAP: { |
| 3700 | struct pat_ref *ref; |
| 3701 | char *key, *value; |
| 3702 | struct chunk *trash_key, *trash_value; |
| 3703 | int len; |
| 3704 | |
| 3705 | trash_key = get_trash_chunk(); |
| 3706 | trash_value = get_trash_chunk(); |
| 3707 | |
| 3708 | /* collect reference */ |
| 3709 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3710 | if (!ref) |
| 3711 | continue; |
| 3712 | |
| 3713 | /* collect key */ |
| 3714 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3715 | key = trash_key->str; |
| 3716 | key[len] = '\0'; |
| 3717 | |
| 3718 | /* collect value */ |
| 3719 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3720 | value = trash_value->str; |
| 3721 | value[len] = '\0'; |
| 3722 | |
| 3723 | /* perform update */ |
| 3724 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3725 | /* update entry if it exists */ |
| 3726 | pat_ref_set(ref, key, value, NULL); |
| 3727 | else |
| 3728 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3729 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3730 | |
| 3731 | break; |
| 3732 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3733 | |
| 3734 | case HTTP_RES_ACT_CUSTOM_CONT: |
| 3735 | rule->action_ptr(rule, px, s, txn); |
| 3736 | break; |
| 3737 | |
| 3738 | case HTTP_RES_ACT_CUSTOM_STOP: |
| 3739 | rule->action_ptr(rule, px, s, txn); |
| 3740 | return rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3741 | } |
| 3742 | } |
| 3743 | |
| 3744 | /* we reached the end of the rules, nothing to report */ |
| 3745 | return NULL; |
| 3746 | } |
| 3747 | |
| 3748 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3749 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3750 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3751 | * as too large a request to build a valid response. |
| 3752 | */ |
| 3753 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn) |
| 3754 | { |
| 3755 | struct http_msg *msg = &txn->req; |
| 3756 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3757 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3758 | |
| 3759 | /* build redirect message */ |
| 3760 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3761 | case 308: |
| 3762 | msg_fmt = HTTP_308; |
| 3763 | break; |
| 3764 | case 307: |
| 3765 | msg_fmt = HTTP_307; |
| 3766 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3767 | case 303: |
| 3768 | msg_fmt = HTTP_303; |
| 3769 | break; |
| 3770 | case 301: |
| 3771 | msg_fmt = HTTP_301; |
| 3772 | break; |
| 3773 | case 302: |
| 3774 | default: |
| 3775 | msg_fmt = HTTP_302; |
| 3776 | break; |
| 3777 | } |
| 3778 | |
| 3779 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3780 | return 0; |
| 3781 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3782 | location = trash.str + trash.len; |
| 3783 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3784 | switch(rule->type) { |
| 3785 | case REDIRECT_TYPE_SCHEME: { |
| 3786 | const char *path; |
| 3787 | const char *host; |
| 3788 | struct hdr_ctx ctx; |
| 3789 | int pathlen; |
| 3790 | int hostlen; |
| 3791 | |
| 3792 | host = ""; |
| 3793 | hostlen = 0; |
| 3794 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3795 | 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] | 3796 | host = ctx.line + ctx.val; |
| 3797 | hostlen = ctx.vlen; |
| 3798 | } |
| 3799 | |
| 3800 | path = http_get_path(txn); |
| 3801 | /* build message using path */ |
| 3802 | if (path) { |
| 3803 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3804 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3805 | int qs = 0; |
| 3806 | while (qs < pathlen) { |
| 3807 | if (path[qs] == '?') { |
| 3808 | pathlen = qs; |
| 3809 | break; |
| 3810 | } |
| 3811 | qs++; |
| 3812 | } |
| 3813 | } |
| 3814 | } else { |
| 3815 | path = "/"; |
| 3816 | pathlen = 1; |
| 3817 | } |
| 3818 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3819 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3820 | /* check if we can add scheme + "://" + host + path */ |
| 3821 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3822 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3823 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3824 | /* add scheme */ |
| 3825 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3826 | trash.len += rule->rdr_len; |
| 3827 | } |
| 3828 | else { |
| 3829 | /* add scheme with executing log format */ |
| 3830 | 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] | 3831 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3832 | /* check if we can add scheme + "://" + host + path */ |
| 3833 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 3834 | return 0; |
| 3835 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3836 | /* add "://" */ |
| 3837 | memcpy(trash.str + trash.len, "://", 3); |
| 3838 | trash.len += 3; |
| 3839 | |
| 3840 | /* add host */ |
| 3841 | memcpy(trash.str + trash.len, host, hostlen); |
| 3842 | trash.len += hostlen; |
| 3843 | |
| 3844 | /* add path */ |
| 3845 | memcpy(trash.str + trash.len, path, pathlen); |
| 3846 | trash.len += pathlen; |
| 3847 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3848 | /* 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] | 3849 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3850 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3851 | if (trash.len > trash.size - 5) |
| 3852 | return 0; |
| 3853 | trash.str[trash.len] = '/'; |
| 3854 | trash.len++; |
| 3855 | } |
| 3856 | |
| 3857 | break; |
| 3858 | } |
| 3859 | case REDIRECT_TYPE_PREFIX: { |
| 3860 | const char *path; |
| 3861 | int pathlen; |
| 3862 | |
| 3863 | path = http_get_path(txn); |
| 3864 | /* build message using path */ |
| 3865 | if (path) { |
| 3866 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3867 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3868 | int qs = 0; |
| 3869 | while (qs < pathlen) { |
| 3870 | if (path[qs] == '?') { |
| 3871 | pathlen = qs; |
| 3872 | break; |
| 3873 | } |
| 3874 | qs++; |
| 3875 | } |
| 3876 | } |
| 3877 | } else { |
| 3878 | path = "/"; |
| 3879 | pathlen = 1; |
| 3880 | } |
| 3881 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3882 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3883 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 3884 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3885 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3886 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3887 | * add anything, otherwise it makes it hard for the user to |
| 3888 | * configure a self-redirection. |
| 3889 | */ |
| 3890 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 3891 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3892 | trash.len += rule->rdr_len; |
| 3893 | } |
| 3894 | } |
| 3895 | else { |
| 3896 | /* add prefix with executing log format */ |
| 3897 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 3898 | |
| 3899 | /* Check length */ |
| 3900 | if (trash.len + pathlen > trash.size - 4) |
| 3901 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | /* add path */ |
| 3905 | memcpy(trash.str + trash.len, path, pathlen); |
| 3906 | trash.len += pathlen; |
| 3907 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3908 | /* 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] | 3909 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3910 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3911 | if (trash.len > trash.size - 5) |
| 3912 | return 0; |
| 3913 | trash.str[trash.len] = '/'; |
| 3914 | trash.len++; |
| 3915 | } |
| 3916 | |
| 3917 | break; |
| 3918 | } |
| 3919 | case REDIRECT_TYPE_LOCATION: |
| 3920 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3921 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3922 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 3923 | return 0; |
| 3924 | |
| 3925 | /* add location */ |
| 3926 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3927 | trash.len += rule->rdr_len; |
| 3928 | } |
| 3929 | else { |
| 3930 | /* add location with executing log format */ |
| 3931 | 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] | 3932 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3933 | /* Check left length */ |
| 3934 | if (trash.len > trash.size - 4) |
| 3935 | return 0; |
| 3936 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3937 | break; |
| 3938 | } |
| 3939 | |
| 3940 | if (rule->cookie_len) { |
| 3941 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 3942 | trash.len += 14; |
| 3943 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 3944 | trash.len += rule->cookie_len; |
| 3945 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 3946 | trash.len += 2; |
| 3947 | } |
| 3948 | |
| 3949 | /* add end of headers and the keep-alive/close status. |
| 3950 | * We may choose to set keep-alive if the Location begins |
| 3951 | * with a slash, because the client will come back to the |
| 3952 | * same server. |
| 3953 | */ |
| 3954 | txn->status = rule->code; |
| 3955 | /* let's log the request time */ |
| 3956 | s->logs.tv_request = now; |
| 3957 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3958 | if (*location == '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3959 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3960 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
| 3961 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3962 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3963 | /* keep-alive possible */ |
| 3964 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
| 3965 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3966 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3967 | trash.len += 30; |
| 3968 | } else { |
| 3969 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 3970 | trash.len += 24; |
| 3971 | } |
| 3972 | } |
| 3973 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 3974 | trash.len += 4; |
| 3975 | bo_inject(txn->rsp.chn, trash.str, trash.len); |
| 3976 | /* "eat" the request */ |
| 3977 | bi_fast_delete(txn->req.chn->buf, msg->sov); |
Willy Tarreau | 6d8bac7 | 2014-04-25 12:19:32 +0200 | [diff] [blame] | 3978 | msg->next -= msg->sov; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3979 | msg->sov = 0; |
| 3980 | txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY; |
| 3981 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3982 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3983 | txn->rsp.msg_state = HTTP_MSG_DONE; |
| 3984 | } else { |
| 3985 | /* keep-alive not possible */ |
| 3986 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3987 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3988 | trash.len += 29; |
| 3989 | } else { |
| 3990 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3991 | trash.len += 23; |
| 3992 | } |
| 3993 | stream_int_retnclose(txn->req.chn->prod, &trash); |
| 3994 | txn->req.chn->analysers = 0; |
| 3995 | } |
| 3996 | |
| 3997 | if (!(s->flags & SN_ERR_MASK)) |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 3998 | s->flags |= SN_ERR_LOCAL; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3999 | if (!(s->flags & SN_FINST_MASK)) |
| 4000 | s->flags |= SN_FINST_R; |
| 4001 | |
| 4002 | return 1; |
| 4003 | } |
| 4004 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4005 | /* This stream analyser runs all HTTP request processing which is common to |
| 4006 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 4007 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4008 | * 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] | 4009 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 4010 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4011 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4012 | 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] | 4013 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4014 | struct http_txn *txn = &s->txn; |
| 4015 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4016 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4017 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4018 | enum rule_result verdict; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4019 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4020 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4021 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4022 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4023 | return 0; |
| 4024 | } |
| 4025 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4026 | 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] | 4027 | now_ms, __FUNCTION__, |
| 4028 | s, |
| 4029 | req, |
| 4030 | req->rex, req->wex, |
| 4031 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4032 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4033 | req->analysers); |
| 4034 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4035 | /* just in case we have some per-backend tracking */ |
| 4036 | session_inc_be_http_req_ctr(s); |
| 4037 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4038 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4039 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
| 4040 | 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] | 4041 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4042 | switch (verdict) { |
| 4043 | case HTTP_RULE_RES_CONT: |
| 4044 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4045 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4046 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4047 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4048 | if (txn->flags & TX_CLTARPIT) |
| 4049 | goto tarpit; |
| 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 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4053 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4054 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4055 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4056 | goto done; |
| 4057 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4058 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4059 | goto return_bad_req; |
| 4060 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4061 | } |
| 4062 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4063 | /* OK at this stage, we know that the request was accepted according to |
| 4064 | * the http-request rules, we can check for the stats. Note that the |
| 4065 | * URI is detected *before* the req* rules in order not to be affected |
| 4066 | * by a possible reqrep, while they are processed *after* so that a |
| 4067 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4068 | */ |
| 4069 | if (stats_check_uri(s->rep->prod, txn, px)) { |
| 4070 | s->target = &http_stats_applet.obj_type; |
| 4071 | if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) { |
| 4072 | txn->status = 500; |
| 4073 | s->logs.tv_request = now; |
| 4074 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 4075 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4076 | if (!(s->flags & SN_ERR_MASK)) |
| 4077 | s->flags |= SN_ERR_RESOURCE; |
| 4078 | goto return_prx_cond; |
| 4079 | } |
| 4080 | |
| 4081 | /* parse the whole stats request and extract the relevant information */ |
| 4082 | http_handle_stats(s, req); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4083 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn); |
| 4084 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4085 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4086 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4087 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4088 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4089 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4090 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4091 | } |
| 4092 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4093 | /* evaluate the req* rules except reqadd */ |
| 4094 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4095 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4096 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4097 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4098 | if (txn->flags & TX_CLDENY) |
| 4099 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4100 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4101 | if (txn->flags & TX_CLTARPIT) |
| 4102 | goto tarpit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4103 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4104 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4105 | /* add request headers from the rule sets in the same order */ |
| 4106 | list_for_each_entry(wl, &px->req_add, list) { |
| 4107 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 4108 | 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] | 4109 | ret = acl_pass(ret); |
| 4110 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4111 | ret = !ret; |
| 4112 | if (!ret) |
| 4113 | continue; |
| 4114 | } |
| 4115 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4116 | 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] | 4117 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4118 | } |
| 4119 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4120 | |
| 4121 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4122 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4123 | /* process the stats request now */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4124 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4125 | s->fe->fe_counters.intercepted_req++; |
| 4126 | |
| 4127 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 4128 | s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy |
| 4129 | if (!(s->flags & SN_FINST_MASK)) |
| 4130 | s->flags |= SN_FINST_R; |
| 4131 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4132 | /* we may want to compress the stats page */ |
| 4133 | if (s->fe->comp || s->be->comp) |
| 4134 | select_compression_request_header(s, req->buf); |
| 4135 | |
| 4136 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
Willy Tarreau | 5506e3f | 2014-11-20 22:23:10 +0100 | [diff] [blame] | 4137 | 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] | 4138 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4139 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4140 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4141 | /* check whether we have some ACLs set to redirect this request */ |
| 4142 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4143 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4144 | int ret; |
| 4145 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 4146 | 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] | 4147 | ret = acl_pass(ret); |
| 4148 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4149 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4150 | if (!ret) |
| 4151 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4152 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4153 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4154 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4155 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4156 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4157 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4158 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4159 | * If this happens, then the data will not come immediately, so we must |
| 4160 | * send all what we have without waiting. Note that due to the small gain |
| 4161 | * 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] | 4162 | * 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] | 4163 | * itself once used. |
| 4164 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4165 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4166 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4167 | done: /* done with this analyser, continue with next ones that the calling |
| 4168 | * points will have set, if any. |
| 4169 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4170 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4171 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4172 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4173 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4174 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4175 | tarpit: |
| 4176 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4177 | * which may be the same as the connect timeout if unspecified. |
| 4178 | * If unset, then set it to zero because we really want it to |
| 4179 | * eventually expire. We build the tarpit as an analyser. |
| 4180 | */ |
| 4181 | channel_erase(s->req); |
| 4182 | |
| 4183 | /* wipe the request out so that we can drop the connection early |
| 4184 | * if the client closes first. |
| 4185 | */ |
| 4186 | channel_dont_connect(req); |
| 4187 | req->analysers = 0; /* remove switching rules etc... */ |
| 4188 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4189 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4190 | if (!req->analyse_exp) |
| 4191 | req->analyse_exp = tick_add(now_ms, 0); |
| 4192 | session_inc_http_err_ctr(s); |
| 4193 | s->fe->fe_counters.denied_req++; |
| 4194 | if (s->fe != s->be) |
| 4195 | s->be->be_counters.denied_req++; |
| 4196 | if (s->listener->counters) |
| 4197 | s->listener->counters->denied_req++; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4198 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4199 | |
| 4200 | deny: /* this request was blocked (denied) */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4201 | txn->flags |= TX_CLDENY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4202 | txn->status = 403; |
| 4203 | s->logs.tv_request = now; |
| 4204 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
| 4205 | session_inc_http_err_ctr(s); |
| 4206 | s->fe->fe_counters.denied_req++; |
| 4207 | if (s->fe != s->be) |
| 4208 | s->be->be_counters.denied_req++; |
| 4209 | if (s->listener->counters) |
| 4210 | s->listener->counters->denied_req++; |
| 4211 | goto return_prx_cond; |
| 4212 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4213 | return_bad_req: |
| 4214 | /* We centralize bad requests processing here */ |
| 4215 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4216 | /* we detected a parsing error. We want to archive this request |
| 4217 | * in the dedicated proxy area for later troubleshooting. |
| 4218 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4219 | 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] | 4220 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4221 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4222 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4223 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4224 | 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] | 4225 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4226 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4227 | if (s->listener->counters) |
| 4228 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4229 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4230 | return_prx_cond: |
| 4231 | if (!(s->flags & SN_ERR_MASK)) |
| 4232 | s->flags |= SN_ERR_PRXCOND; |
| 4233 | if (!(s->flags & SN_FINST_MASK)) |
| 4234 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4235 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4236 | req->analysers = 0; |
| 4237 | req->analyse_exp = TICK_ETERNITY; |
| 4238 | return 0; |
| 4239 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4240 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4241 | /* This function performs all the processing enabled for the current request. |
| 4242 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4243 | * needs more data, encounters an error, or wants to immediately abort the |
| 4244 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 4245 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4246 | 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] | 4247 | { |
| 4248 | struct http_txn *txn = &s->txn; |
| 4249 | struct http_msg *msg = &txn->req; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4250 | struct connection *cli_conn = objt_conn(req->prod->end); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4251 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4252 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4253 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4254 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4255 | return 0; |
| 4256 | } |
| 4257 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4258 | 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] | 4259 | now_ms, __FUNCTION__, |
| 4260 | s, |
| 4261 | req, |
| 4262 | req->rex, req->wex, |
| 4263 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4264 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4265 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4266 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 4267 | if (s->fe->comp || s->be->comp) |
| 4268 | select_compression_request_header(s, req->buf); |
| 4269 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4270 | /* |
| 4271 | * Right now, we know that we have processed the entire headers |
| 4272 | * and that unwanted requests have been filtered out. We can do |
| 4273 | * whatever we want with the remaining request. Also, now we |
| 4274 | * may have separate values for ->fe, ->be. |
| 4275 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4276 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4277 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4278 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4279 | * incoming request. Note that this requires that a connection is |
| 4280 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4281 | */ |
| 4282 | 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] | 4283 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4284 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4285 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4286 | /* Note that for now we don't reuse existing proxy connections */ |
| 4287 | if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4288 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4289 | txn->status = 500; |
| 4290 | req->analysers = 0; |
| 4291 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
| 4292 | |
| 4293 | if (!(s->flags & SN_ERR_MASK)) |
| 4294 | s->flags |= SN_ERR_RESOURCE; |
| 4295 | if (!(s->flags & SN_FINST_MASK)) |
| 4296 | s->flags |= SN_FINST_R; |
| 4297 | |
| 4298 | return 0; |
| 4299 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4300 | |
| 4301 | path = http_get_path(txn); |
| 4302 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4303 | 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] | 4304 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4305 | /* if the path was found, we have to remove everything between |
| 4306 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4307 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4308 | * u_l characters by a single "/". |
| 4309 | */ |
| 4310 | if (path) { |
| 4311 | char *cur_ptr = req->buf->p; |
| 4312 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4313 | int delta; |
| 4314 | |
| 4315 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4316 | http_msg_move_end(&txn->req, delta); |
| 4317 | cur_end += delta; |
| 4318 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4319 | goto return_bad_req; |
| 4320 | } |
| 4321 | else { |
| 4322 | char *cur_ptr = req->buf->p; |
| 4323 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4324 | int delta; |
| 4325 | |
| 4326 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4327 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4328 | http_msg_move_end(&txn->req, delta); |
| 4329 | cur_end += delta; |
| 4330 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4331 | goto return_bad_req; |
| 4332 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4333 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4334 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4335 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4336 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4337 | * Note that doing so might move headers in the request, but |
| 4338 | * the fields will stay coherent and the URI will not move. |
| 4339 | * This should only be performed in the backend. |
| 4340 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 4341 | 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] | 4342 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 4343 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4344 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4345 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4346 | * 8: the appsession cookie was looked up very early in 1.2, |
| 4347 | * so let's do the same now. |
| 4348 | */ |
| 4349 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4350 | /* It needs to look into the URI unless persistence must be ignored */ |
| 4351 | 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] | 4352 | 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] | 4353 | } |
| 4354 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4355 | /* add unique-id if "header-unique-id" is specified */ |
| 4356 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4357 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 4358 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4359 | goto return_bad_req; |
| 4360 | s->unique_id[0] = '\0'; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4361 | 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] | 4362 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4363 | |
| 4364 | if (s->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4365 | chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 4366 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4367 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4368 | 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] | 4369 | goto return_bad_req; |
| 4370 | } |
| 4371 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4372 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4373 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4374 | * asks for it. |
| 4375 | */ |
| 4376 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4377 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4378 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Cyril Bonté | a32d275 | 2012-05-29 23:27:41 +0200 | [diff] [blame] | 4379 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name, |
| 4380 | 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] | 4381 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4382 | /* The header is set to be added only if none is present |
| 4383 | * and we found it, so don't do anything. |
| 4384 | */ |
| 4385 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4386 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4387 | /* Add an X-Forwarded-For header unless the source IP is |
| 4388 | * in the 'except' network range. |
| 4389 | */ |
| 4390 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4391 | (((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] | 4392 | != s->fe->except_net.s_addr) && |
| 4393 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4394 | (((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] | 4395 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4396 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4397 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4398 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4399 | |
| 4400 | /* Note: we rely on the backend to get the header name to be used for |
| 4401 | * x-forwarded-for, because the header is really meant for the backends. |
| 4402 | * However, if the backend did not specify any option, we have to rely |
| 4403 | * on the frontend's header name. |
| 4404 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4405 | if (s->be->fwdfor_hdr_len) { |
| 4406 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4407 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4408 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4409 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4410 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4411 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4412 | 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] | 4413 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4414 | 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] | 4415 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4416 | } |
| 4417 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4418 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4419 | /* FIXME: for the sake of completeness, we should also support |
| 4420 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4421 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4422 | int len; |
| 4423 | char pn[INET6_ADDRSTRLEN]; |
| 4424 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4425 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4426 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4427 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4428 | /* Note: we rely on the backend to get the header name to be used for |
| 4429 | * x-forwarded-for, because the header is really meant for the backends. |
| 4430 | * However, if the backend did not specify any option, we have to rely |
| 4431 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4432 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4433 | if (s->be->fwdfor_hdr_len) { |
| 4434 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4435 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4436 | } else { |
| 4437 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4438 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4439 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4440 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4441 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4442 | 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] | 4443 | goto return_bad_req; |
| 4444 | } |
| 4445 | } |
| 4446 | |
| 4447 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4448 | * 10: add X-Original-To if either the frontend or the backend |
| 4449 | * asks for it. |
| 4450 | */ |
| 4451 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 4452 | |
| 4453 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4454 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4455 | /* Add an X-Original-To header unless the destination IP is |
| 4456 | * in the 'except' network range. |
| 4457 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4458 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4459 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4460 | if (cli_conn->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4461 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4462 | (((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] | 4463 | != s->fe->except_to.s_addr) && |
| 4464 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4465 | (((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] | 4466 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4467 | int len; |
| 4468 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4469 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4470 | |
| 4471 | /* Note: we rely on the backend to get the header name to be used for |
| 4472 | * x-original-to, because the header is really meant for the backends. |
| 4473 | * However, if the backend did not specify any option, we have to rely |
| 4474 | * on the frontend's header name. |
| 4475 | */ |
| 4476 | if (s->be->orgto_hdr_len) { |
| 4477 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4478 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4479 | } else { |
| 4480 | len = s->fe->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4481 | memcpy(trash.str, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4482 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4483 | 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] | 4484 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4485 | 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] | 4486 | goto return_bad_req; |
| 4487 | } |
| 4488 | } |
| 4489 | } |
| 4490 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4491 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4492 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4493 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4494 | * "Upgrade" is present in the Connection header. |
| 4495 | */ |
| 4496 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4497 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4498 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4499 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4500 | unsigned int want_flags = 0; |
| 4501 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4502 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4503 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4504 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4505 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4506 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4507 | want_flags |= TX_CON_CLO_SET; |
| 4508 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4509 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4510 | ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
| 4511 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4512 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4513 | want_flags |= TX_CON_KAL_SET; |
| 4514 | } |
| 4515 | |
| 4516 | 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] | 4517 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4518 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4519 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4520 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4521 | /* If we have no server assigned yet and we're balancing on url_param |
| 4522 | * with a POST request, we may be interested in checking the body for |
| 4523 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4524 | */ |
| 4525 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 4526 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4527 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4528 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4529 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4530 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4531 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4532 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4533 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4534 | #ifdef TCP_QUICKACK |
| 4535 | /* We expect some data from the client. Unless we know for sure |
| 4536 | * we already have a full request, we have to re-enable quick-ack |
| 4537 | * in case we previously disabled it, otherwise we might cause |
| 4538 | * the client to delay further data. |
| 4539 | */ |
| 4540 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4541 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4542 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4543 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4544 | 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] | 4545 | #endif |
| 4546 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4547 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4548 | /************************************************************* |
| 4549 | * OK, that's finished for the headers. We have done what we * |
| 4550 | * could. Let's switch to the DATA state. * |
| 4551 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4552 | req->analyse_exp = TICK_ETERNITY; |
| 4553 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4554 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4555 | /* if the server closes the connection, we want to immediately react |
| 4556 | * and close the socket to save packets and syscalls. |
| 4557 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4558 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
| 4559 | req->cons->flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4560 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4561 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4562 | /* OK let's go on with the BODY now */ |
| 4563 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4564 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4565 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4566 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4567 | /* we detected a parsing error. We want to archive this request |
| 4568 | * in the dedicated proxy area for later troubleshooting. |
| 4569 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4570 | 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] | 4571 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4572 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4573 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4574 | txn->status = 400; |
| 4575 | req->analysers = 0; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4576 | 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] | 4577 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4578 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4579 | if (s->listener->counters) |
| 4580 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4581 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4582 | if (!(s->flags & SN_ERR_MASK)) |
| 4583 | s->flags |= SN_ERR_PRXCOND; |
| 4584 | if (!(s->flags & SN_FINST_MASK)) |
| 4585 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4586 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4587 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4588 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4589 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4590 | * returns zero, at the beginning because it prevents any other processing |
| 4591 | * from occurring, and at the end because it terminates the request. |
| 4592 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4593 | 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] | 4594 | { |
| 4595 | struct http_txn *txn = &s->txn; |
| 4596 | |
| 4597 | /* This connection is being tarpitted. The CLIENT side has |
| 4598 | * already set the connect expiration date to the right |
| 4599 | * timeout. We just have to check that the client is still |
| 4600 | * there and that the timeout has not expired. |
| 4601 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4602 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4603 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4604 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4605 | return 0; |
| 4606 | |
| 4607 | /* We will set the queue timer to the time spent, just for |
| 4608 | * logging purposes. We fake a 500 server error, so that the |
| 4609 | * attacker will not suspect his connection has been tarpitted. |
| 4610 | * It will not cause trouble to the logs because we can exclude |
| 4611 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4612 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4613 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4614 | |
| 4615 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4616 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4617 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4618 | |
| 4619 | req->analysers = 0; |
| 4620 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4621 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4622 | if (!(s->flags & SN_ERR_MASK)) |
| 4623 | s->flags |= SN_ERR_PRXCOND; |
| 4624 | if (!(s->flags & SN_FINST_MASK)) |
| 4625 | s->flags |= SN_FINST_T; |
| 4626 | return 0; |
| 4627 | } |
| 4628 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4629 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4630 | * for either the buffer to be full, or the full advertised contents to have |
| 4631 | * reached the buffer. It must only be called after the standard HTTP request |
| 4632 | * processing has occurred, because it expects the request to be parsed and will |
| 4633 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4634 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4635 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4636 | * 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] | 4637 | */ |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4638 | 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] | 4639 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4640 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4641 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4642 | |
| 4643 | /* We have to parse the HTTP request body to find any required data. |
| 4644 | * "balance url_param check_post" should have been the only way to get |
| 4645 | * into this. We were brought here after HTTP header analysis, so all |
| 4646 | * related structures are ready. |
| 4647 | */ |
| 4648 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4649 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4650 | /* This is the first call */ |
| 4651 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4652 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4653 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4654 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4655 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4656 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4657 | */ |
| 4658 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4659 | struct hdr_ctx ctx; |
| 4660 | ctx.idx = 0; |
| 4661 | /* Expect is allowed in 1.1, look for it */ |
| 4662 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4663 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 4664 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 4665 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4666 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4667 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4668 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4669 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4670 | /* 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] | 4671 | * req->buf->p still points to the beginning of the message. We |
| 4672 | * must save the body in msg->next because it survives buffer |
| 4673 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4674 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4675 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4676 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4677 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4678 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4679 | else |
| 4680 | msg->msg_state = HTTP_MSG_DATA; |
| 4681 | } |
| 4682 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4683 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4684 | /* We're in content-length mode, we just have to wait for enough data. */ |
| 4685 | if (req->buf->i - msg->sov < msg->body_len) |
| 4686 | goto missing_data; |
| 4687 | |
| 4688 | /* OK we have everything we need now */ |
| 4689 | goto http_end; |
| 4690 | } |
| 4691 | |
| 4692 | /* OK here we're parsing a chunked-encoded message */ |
| 4693 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4694 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4695 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4696 | * 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] | 4697 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4698 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4699 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4700 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4701 | if (!ret) |
| 4702 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4703 | else if (ret < 0) { |
| 4704 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4705 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4706 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4707 | } |
| 4708 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4709 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4710 | * We have the first data byte is in msg->sov. We're waiting for at |
Willy Tarreau | 226071e | 2014-04-10 11:55:45 +0200 | [diff] [blame] | 4711 | * least a whole chunk or the whole content length bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4712 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4713 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4714 | goto http_end; |
| 4715 | |
Willy Tarreau | 226071e | 2014-04-10 11:55:45 +0200 | [diff] [blame] | 4716 | if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4717 | goto http_end; |
| 4718 | |
| 4719 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4720 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4721 | * we have the maximum we can expect. |
| 4722 | */ |
| 4723 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4724 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4725 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4726 | 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] | 4727 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4728 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4729 | |
| 4730 | if (!(s->flags & SN_ERR_MASK)) |
| 4731 | s->flags |= SN_ERR_CLITO; |
| 4732 | if (!(s->flags & SN_FINST_MASK)) |
| 4733 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4734 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4735 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4736 | |
| 4737 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4738 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4739 | /* Not enough data. We'll re-use the http-request |
| 4740 | * timeout here. Ideally, we should set the timeout |
| 4741 | * relative to the accept() date. We just set the |
| 4742 | * request timeout once at the beginning of the |
| 4743 | * request. |
| 4744 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4745 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4746 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4747 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4748 | return 0; |
| 4749 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4750 | |
| 4751 | http_end: |
| 4752 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4753 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4754 | req->analysers &= ~an_bit; |
| 4755 | req->analyse_exp = TICK_ETERNITY; |
| 4756 | return 1; |
| 4757 | |
| 4758 | return_bad_req: /* let's centralize all bad requests */ |
| 4759 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4760 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4761 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4762 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4763 | if (!(s->flags & SN_ERR_MASK)) |
| 4764 | s->flags |= SN_ERR_PRXCOND; |
| 4765 | if (!(s->flags & SN_FINST_MASK)) |
| 4766 | s->flags |= SN_FINST_R; |
| 4767 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4768 | return_err_msg: |
| 4769 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4770 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4771 | if (s->listener->counters) |
| 4772 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4773 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4774 | } |
| 4775 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4776 | /* send a server's name with an outgoing request over an established connection. |
| 4777 | * Note: this function is designed to be called once the request has been scheduled |
| 4778 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4779 | * proceeding. |
| 4780 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4781 | 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] | 4782 | |
| 4783 | struct hdr_ctx ctx; |
| 4784 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4785 | char *hdr_name = be->server_id_hdr_name; |
| 4786 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4787 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4788 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4789 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4790 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4791 | ctx.idx = 0; |
| 4792 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4793 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4794 | if (old_o) { |
| 4795 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4796 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4797 | txn->req.next += old_o; |
| 4798 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4799 | } |
| 4800 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4801 | old_i = chn->buf->i; |
| 4802 | 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] | 4803 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4804 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4805 | } |
| 4806 | |
| 4807 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4808 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4809 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4810 | hdr_val += hdr_name_len; |
| 4811 | *hdr_val++ = ':'; |
| 4812 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4813 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4814 | 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] | 4815 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4816 | if (old_o) { |
| 4817 | /* If this was a forwarded request, we must readjust the amount of |
| 4818 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4819 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4820 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4821 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4822 | old_o += chn->buf->i - old_i; |
| 4823 | b_adv(chn->buf, old_o); |
| 4824 | txn->req.next -= old_o; |
| 4825 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4826 | } |
| 4827 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4828 | return 0; |
| 4829 | } |
| 4830 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4831 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4832 | * right now but it works. |
| 4833 | */ |
| 4834 | void http_end_txn_clean_session(struct session *s) |
| 4835 | { |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4836 | int prev_status = s->txn.status; |
| 4837 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4838 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4839 | * server's connections. We need a safer way to reinitialize buffer |
| 4840 | * flags. We also need a more accurate method for computing per-request |
| 4841 | * data. |
| 4842 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4843 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4844 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4845 | * to the server. |
| 4846 | */ |
| 4847 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4848 | !si_conn_ready(s->req->cons)) { |
| 4849 | s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4850 | si_shutr(s->req->cons); |
| 4851 | si_shutw(s->req->cons); |
| 4852 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4853 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4854 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4855 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4856 | if (unlikely(s->srv_conn)) |
| 4857 | sess_change_server(s, NULL); |
| 4858 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4859 | |
| 4860 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4861 | session_process_counters(s); |
| 4862 | |
| 4863 | if (s->txn.status) { |
| 4864 | int n; |
| 4865 | |
| 4866 | n = s->txn.status / 100; |
| 4867 | if (n < 1 || n > 5) |
| 4868 | n = 0; |
| 4869 | |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4870 | if (s->fe->mode == PR_MODE_HTTP) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4871 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4872 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4873 | s->fe->fe_counters.p.http.comp_rsp++; |
| 4874 | } |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 4875 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4876 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4877 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4878 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4879 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4880 | s->be->be_counters.p.http.comp_rsp++; |
| 4881 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4882 | } |
| 4883 | |
| 4884 | /* don't count other requests' data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4885 | s->logs.bytes_in -= s->req->buf->i; |
| 4886 | s->logs.bytes_out -= s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4887 | |
| 4888 | /* let's do a final log if we need it */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 4889 | if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4890 | !(s->flags & SN_MONITOR) && |
| 4891 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 4892 | s->do_log(s); |
| 4893 | } |
| 4894 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 4895 | /* stop tracking content-based counters */ |
| 4896 | session_stop_content_counters(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 4897 | session_update_time_stats(s); |
| 4898 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4899 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4900 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4901 | tv_zero(&s->logs.tv_request); |
| 4902 | s->logs.t_queue = -1; |
| 4903 | s->logs.t_connect = -1; |
| 4904 | s->logs.t_data = -1; |
| 4905 | s->logs.t_close = 0; |
| 4906 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4907 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4908 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4909 | s->logs.bytes_in = s->req->total = s->req->buf->i; |
| 4910 | s->logs.bytes_out = s->rep->total = s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4911 | |
| 4912 | if (s->pend_pos) |
| 4913 | pendconn_free(s->pend_pos); |
| 4914 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4915 | if (objt_server(s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4916 | if (s->flags & SN_CURR_SESS) { |
| 4917 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4918 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4919 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4920 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 4921 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4922 | } |
| 4923 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4924 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4925 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4926 | /* only release our endpoint if we don't intend to reuse the |
| 4927 | * connection. |
| 4928 | */ |
| 4929 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4930 | !si_conn_ready(s->req->cons)) { |
| 4931 | si_release_endpoint(s->req->cons); |
| 4932 | } |
| 4933 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4934 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4935 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 4936 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4937 | s->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | c920096 | 2013-12-31 23:03:09 +0100 | [diff] [blame] | 4938 | s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */ |
Willy Tarreau | 9dc1c61 | 2014-09-01 20:35:55 +0200 | [diff] [blame] | 4939 | 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); |
| 4940 | 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] | 4941 | 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] | 4942 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED); |
Cyril Bonté | a83a50b | 2014-10-22 22:30:13 +0200 | [diff] [blame] | 4943 | s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4944 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4945 | s->txn.meth = 0; |
| 4946 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4947 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4948 | |
| 4949 | if (prev_status == 401 || prev_status == 407) { |
| 4950 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 4951 | * a chance of being able to send the visitor again to the same |
| 4952 | * server over the same connection. This is required by some |
| 4953 | * broken protocols such as NTLM, and anyway whenever there is |
| 4954 | * an opportunity for sending the challenge to the proper place, |
| 4955 | * it's better to do it (at least it helps with debugging). |
| 4956 | */ |
| 4957 | s->txn.flags |= TX_PREFER_LAST; |
| 4958 | } |
| 4959 | |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 4960 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4961 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 4962 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4963 | if (s->fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4964 | s->req->flags |= CF_NEVER_WAIT; |
| 4965 | s->rep->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4966 | } |
| 4967 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4968 | /* if the request buffer is not empty, it means we're |
| 4969 | * about to process another request, so send pending |
| 4970 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4971 | * Just don't do this if the buffer is close to be full, |
| 4972 | * because the request will wait for it to flush a little |
| 4973 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4974 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4975 | if (s->req->buf->i) { |
| 4976 | if (s->rep->buf->o && |
| 4977 | !buffer_full(s->rep->buf, global.tune.maxrewrite) && |
| 4978 | 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] | 4979 | s->rep->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4980 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4981 | |
| 4982 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4983 | channel_auto_read(s->req); |
| 4984 | channel_auto_close(s->req); |
| 4985 | channel_auto_read(s->rep); |
| 4986 | channel_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4987 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 4988 | /* we're in keep-alive with an idle connection, monitor it */ |
| 4989 | si_idle_conn(s->req->cons); |
| 4990 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 4991 | s->req->analysers = s->listener->analysers; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4992 | s->rep->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4993 | } |
| 4994 | |
| 4995 | |
| 4996 | /* This function updates the request state machine according to the response |
| 4997 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4998 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4999 | * it is only used to find when a request/response couple is complete. Both |
| 5000 | * this function and its equivalent should loop until both return zero. It |
| 5001 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5002 | */ |
| 5003 | int http_sync_req_state(struct session *s) |
| 5004 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5005 | struct channel *chn = s->req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5006 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5007 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5008 | unsigned int old_state = txn->req.msg_state; |
| 5009 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5010 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 5011 | return 0; |
| 5012 | |
| 5013 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5014 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5015 | * We can shut the read side unless we want to abort_on_close, |
| 5016 | * or we have a POST request. The issue with POST requests is |
| 5017 | * that some browsers still send a CRLF after the request, and |
| 5018 | * this CRLF must be read so that it does not remain in the kernel |
| 5019 | * buffers, otherwise a close could cause an RST on some systems |
| 5020 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5021 | * Note that if we're using keep-alive on the client side, we'd |
| 5022 | * rather poll now and keep the polling enabled for the whole |
| 5023 | * session's life than enabling/disabling it between each |
| 5024 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5025 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5026 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5027 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5028 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 5029 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5030 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5031 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5032 | /* if the server closes the connection, we want to immediately react |
| 5033 | * and close the socket to save packets and syscalls. |
| 5034 | */ |
| 5035 | chn->cons->flags |= SI_FL_NOHALF; |
| 5036 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5037 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5038 | goto wait_other_side; |
| 5039 | |
| 5040 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5041 | /* The server has not finished to respond, so we |
| 5042 | * don't want to move in order not to upset it. |
| 5043 | */ |
| 5044 | goto wait_other_side; |
| 5045 | } |
| 5046 | |
| 5047 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5048 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5049 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5050 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5051 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5052 | goto wait_other_side; |
| 5053 | } |
| 5054 | |
| 5055 | /* When we get here, it means that both the request and the |
| 5056 | * response have finished receiving. Depending on the connection |
| 5057 | * mode, we'll have to wait for the last bytes to leave in either |
| 5058 | * direction, and sometimes for a close to be effective. |
| 5059 | */ |
| 5060 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5061 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5062 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5063 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5064 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5065 | } |
| 5066 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5067 | /* Option forceclose is set, or either side wants to close, |
| 5068 | * let's enforce it now that we're not expecting any new |
| 5069 | * data to come. The caller knows the session is complete |
| 5070 | * once both states are CLOSED. |
| 5071 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5072 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5073 | channel_shutr_now(chn); |
| 5074 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5075 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5076 | } |
| 5077 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5078 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5079 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5080 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5081 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5082 | channel_auto_read(chn); |
| 5083 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5084 | chn->flags |= CF_NEVER_WAIT; |
| 5085 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5086 | } |
| 5087 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5088 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5089 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5090 | chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5091 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5092 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5093 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5094 | goto http_msg_closing; |
| 5095 | } |
| 5096 | else { |
| 5097 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5098 | goto http_msg_closed; |
| 5099 | } |
| 5100 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5101 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5102 | } |
| 5103 | |
| 5104 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5105 | http_msg_closing: |
| 5106 | /* nothing else to forward, just waiting for the output buffer |
| 5107 | * to be empty and for the shutw_now to take effect. |
| 5108 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5109 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5110 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5111 | goto http_msg_closed; |
| 5112 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5113 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5114 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5115 | goto wait_other_side; |
| 5116 | } |
| 5117 | } |
| 5118 | |
| 5119 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5120 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5121 | /* see above in MSG_DONE why we only do this in these states */ |
| 5122 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5123 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5124 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5125 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5126 | goto wait_other_side; |
| 5127 | } |
| 5128 | |
| 5129 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5130 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5131 | } |
| 5132 | |
| 5133 | |
| 5134 | /* This function updates the response state machine according to the request |
| 5135 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5136 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5137 | * it is only used to find when a request/response couple is complete. Both |
| 5138 | * this function and its equivalent should loop until both return zero. It |
| 5139 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5140 | */ |
| 5141 | int http_sync_res_state(struct session *s) |
| 5142 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5143 | struct channel *chn = s->rep; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5144 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5145 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5146 | unsigned int old_state = txn->rsp.msg_state; |
| 5147 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5148 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5149 | return 0; |
| 5150 | |
| 5151 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5152 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5153 | * still monitor the server connection for a possible close |
| 5154 | * while the request is being uploaded, so we don't disable |
| 5155 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5156 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5157 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5158 | |
| 5159 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5160 | goto wait_other_side; |
| 5161 | |
| 5162 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5163 | /* The client seems to still be sending data, probably |
| 5164 | * because we got an error response during an upload. |
| 5165 | * We have the choice of either breaking the connection |
| 5166 | * or letting it pass through. Let's do the later. |
| 5167 | */ |
| 5168 | goto wait_other_side; |
| 5169 | } |
| 5170 | |
| 5171 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5172 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5173 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5174 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5175 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5176 | goto wait_other_side; |
| 5177 | } |
| 5178 | |
| 5179 | /* When we get here, it means that both the request and the |
| 5180 | * response have finished receiving. Depending on the connection |
| 5181 | * mode, we'll have to wait for the last bytes to leave in either |
| 5182 | * direction, and sometimes for a close to be effective. |
| 5183 | */ |
| 5184 | |
| 5185 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5186 | /* Server-close mode : shut read and wait for the request |
| 5187 | * side to close its output buffer. The caller will detect |
| 5188 | * when we're in DONE and the other is in CLOSED and will |
| 5189 | * catch that for the final cleanup. |
| 5190 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5191 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5192 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5193 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5194 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5195 | /* Option forceclose is set, or either side wants to close, |
| 5196 | * let's enforce it now that we're not expecting any new |
| 5197 | * data to come. The caller knows the session is complete |
| 5198 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5199 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5200 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5201 | channel_shutr_now(chn); |
| 5202 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5203 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5204 | } |
| 5205 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5206 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5207 | * need to forward remaining data. Keep-alive will need to monitor |
| 5208 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5209 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5210 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5211 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5212 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5213 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5214 | } |
| 5215 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5216 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5217 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5218 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5219 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5220 | goto http_msg_closing; |
| 5221 | } |
| 5222 | else { |
| 5223 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5224 | goto http_msg_closed; |
| 5225 | } |
| 5226 | } |
| 5227 | goto wait_other_side; |
| 5228 | } |
| 5229 | |
| 5230 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5231 | http_msg_closing: |
| 5232 | /* nothing else to forward, just waiting for the output buffer |
| 5233 | * to be empty and for the shutw_now to take effect. |
| 5234 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5235 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5236 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5237 | goto http_msg_closed; |
| 5238 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5239 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5240 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5241 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5242 | if (objt_server(s->target)) |
| 5243 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5244 | goto wait_other_side; |
| 5245 | } |
| 5246 | } |
| 5247 | |
| 5248 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5249 | http_msg_closed: |
| 5250 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5251 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5252 | channel_auto_close(chn); |
| 5253 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5254 | goto wait_other_side; |
| 5255 | } |
| 5256 | |
| 5257 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5258 | /* We force the response to leave immediately if we're waiting for the |
| 5259 | * other side, since there is no pending shutdown to push it out. |
| 5260 | */ |
| 5261 | if (!channel_is_empty(chn)) |
| 5262 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5263 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5264 | } |
| 5265 | |
| 5266 | |
| 5267 | /* Resync the request and response state machines. Return 1 if either state |
| 5268 | * changes. |
| 5269 | */ |
| 5270 | int http_resync_states(struct session *s) |
| 5271 | { |
| 5272 | struct http_txn *txn = &s->txn; |
| 5273 | int old_req_state = txn->req.msg_state; |
| 5274 | int old_res_state = txn->rsp.msg_state; |
| 5275 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5276 | http_sync_req_state(s); |
| 5277 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5278 | if (!http_sync_res_state(s)) |
| 5279 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5280 | if (!http_sync_req_state(s)) |
| 5281 | break; |
| 5282 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5283 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5284 | /* OK, both state machines agree on a compatible state. |
| 5285 | * There are a few cases we're interested in : |
| 5286 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5287 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5288 | * directions, so let's simply disable both analysers. |
| 5289 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5290 | * request. |
| 5291 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5292 | * with server-close mode means we've completed one request and we |
| 5293 | * must re-initialize the server connection. |
| 5294 | */ |
| 5295 | |
| 5296 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5297 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5298 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5299 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 5300 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5301 | channel_auto_close(s->req); |
| 5302 | channel_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5303 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5304 | channel_auto_close(s->rep); |
| 5305 | channel_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5306 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5307 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
| 5308 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5309 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5310 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5311 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5312 | channel_auto_close(s->rep); |
| 5313 | channel_auto_read(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5314 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5315 | channel_abort(s->req); |
| 5316 | channel_auto_close(s->req); |
| 5317 | channel_auto_read(s->req); |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5318 | channel_truncate(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5319 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5320 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5321 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5322 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5323 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5324 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5325 | /* server-close/keep-alive: terminate this transaction, |
| 5326 | * possibly killing the server connection and reinitialize |
| 5327 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5328 | */ |
| 5329 | http_end_txn_clean_session(s); |
| 5330 | } |
| 5331 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5332 | return txn->req.msg_state != old_req_state || |
| 5333 | txn->rsp.msg_state != old_res_state; |
| 5334 | } |
| 5335 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5336 | /* This function is an analyser which forwards request body (including chunk |
| 5337 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5338 | * zero byte. The only situation where it must not be called is when we're in |
| 5339 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5340 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5341 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5342 | * 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] | 5343 | * 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] | 5344 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5345 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5346 | 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] | 5347 | { |
| 5348 | struct http_txn *txn = &s->txn; |
| 5349 | struct http_msg *msg = &s->txn.req; |
| 5350 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5351 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5352 | return 0; |
| 5353 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5354 | 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] | 5355 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5356 | /* Output closed while we were sending data. We must abort and |
| 5357 | * wake the other side up. |
| 5358 | */ |
| 5359 | msg->msg_state = HTTP_MSG_ERROR; |
| 5360 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5361 | return 1; |
| 5362 | } |
| 5363 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5364 | /* Note that we don't have to send 100-continue back because we don't |
| 5365 | * need the data to complete our job, and it's up to the server to |
| 5366 | * decide whether to return 100, 417 or anything else in return of |
| 5367 | * an "Expect: 100-continue" header. |
| 5368 | */ |
| 5369 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5370 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5371 | /* we have msg->sov which points to the first byte of message |
| 5372 | * body, and req->buf.p still points to the beginning of the |
| 5373 | * message. We forward the headers now, as we don't need them |
| 5374 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5375 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5376 | b_adv(req->buf, msg->sov); |
| 5377 | msg->next -= msg->sov; |
| 5378 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5379 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5380 | /* The previous analysers guarantee that the state is somewhere |
| 5381 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 5382 | * msg->next are always correct. |
| 5383 | */ |
| 5384 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5385 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 5386 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5387 | else |
| 5388 | msg->msg_state = HTTP_MSG_DATA; |
| 5389 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5390 | } |
| 5391 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5392 | /* Some post-connect processing might want us to refrain from starting to |
| 5393 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5394 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5395 | */ |
| 5396 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
| 5397 | if (!(s->rep->flags & CF_READ_ATTACHED)) { |
| 5398 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5399 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5400 | goto missing_data; |
| 5401 | } |
| 5402 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5403 | } |
| 5404 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5405 | /* in most states, we should abort in case of early close */ |
| 5406 | channel_auto_close(req); |
| 5407 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5408 | if (req->to_forward) { |
| 5409 | /* We can't process the buffer's contents yet */ |
| 5410 | req->flags |= CF_WAKE_WRITE; |
| 5411 | goto missing_data; |
| 5412 | } |
| 5413 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5414 | while (1) { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5415 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5416 | /* must still forward */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5417 | /* we may have some pending data starting at req->buf->p */ |
| 5418 | if (msg->chunk_len > req->buf->i - msg->next) { |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5419 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5420 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5421 | } |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5422 | msg->next += msg->chunk_len; |
| 5423 | msg->chunk_len = 0; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5424 | |
| 5425 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5426 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5427 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5428 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5429 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5430 | } |
| 5431 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5432 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 5433 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5434 | * TRAILERS state. |
| 5435 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5436 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5437 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5438 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5439 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5440 | else if (ret < 0) { |
| 5441 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5442 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5443 | 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] | 5444 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5445 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5446 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5447 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5448 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5449 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5450 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5451 | |
| 5452 | if (ret == 0) |
| 5453 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5454 | else if (ret < 0) { |
| 5455 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5456 | if (msg->err_pos >= 0) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5457 | 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] | 5458 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5459 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5460 | /* we're in MSG_CHUNK_SIZE now */ |
| 5461 | } |
| 5462 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5463 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5464 | |
| 5465 | if (ret == 0) |
| 5466 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5467 | else if (ret < 0) { |
| 5468 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5469 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5470 | 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] | 5471 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5472 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5473 | /* we're in HTTP_MSG_DONE now */ |
| 5474 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5475 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5476 | int old_state = msg->msg_state; |
| 5477 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5478 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5479 | |
| 5480 | /* we may have some pending data starting at req->buf->p |
| 5481 | * such as last chunk of data or trailers. |
| 5482 | */ |
| 5483 | b_adv(req->buf, msg->next); |
Willy Tarreau | 9dc1c61 | 2014-09-01 20:35:55 +0200 | [diff] [blame] | 5484 | if (unlikely(!(s->req->flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5485 | msg->sov -= msg->next; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5486 | msg->next = 0; |
| 5487 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5488 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5489 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5490 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5491 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5492 | if (http_resync_states(s)) { |
| 5493 | /* some state changes occurred, maybe the analyser |
| 5494 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 5495 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5496 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5497 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5498 | /* request errors are most likely due to |
| 5499 | * the server aborting the transfer. |
| 5500 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5501 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5502 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5503 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5504 | 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] | 5505 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5506 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5507 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5508 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5509 | |
| 5510 | /* If "option abortonclose" is set on the backend, we |
| 5511 | * want to monitor the client's connection and forward |
| 5512 | * any shutdown notification to the server, which will |
| 5513 | * decide whether to close or to go on processing the |
| 5514 | * request. |
| 5515 | */ |
| 5516 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5517 | channel_auto_read(req); |
| 5518 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5519 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5520 | else if (s->txn.meth == HTTP_METH_POST) { |
| 5521 | /* POST requests may require to read extra CRLF |
| 5522 | * sent by broken browsers and which could cause |
| 5523 | * an RST to be sent upon close on some systems |
| 5524 | * (eg: Linux). |
| 5525 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5526 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5527 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5528 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5529 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5530 | } |
| 5531 | } |
| 5532 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5533 | missing_data: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5534 | /* we may have some pending data starting at req->buf->p */ |
| 5535 | b_adv(req->buf, msg->next); |
Willy Tarreau | 9dc1c61 | 2014-09-01 20:35:55 +0200 | [diff] [blame] | 5536 | if (unlikely(!(s->req->flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5537 | msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i); |
| 5538 | |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5539 | msg->next = 0; |
| 5540 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
| 5541 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5542 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5543 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5544 | if (!(s->flags & SN_ERR_MASK)) |
| 5545 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5546 | if (!(s->flags & SN_FINST_MASK)) { |
| 5547 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5548 | s->flags |= SN_FINST_H; |
| 5549 | else |
| 5550 | s->flags |= SN_FINST_D; |
| 5551 | } |
| 5552 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5553 | s->fe->fe_counters.cli_aborts++; |
| 5554 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5555 | if (objt_server(s->target)) |
| 5556 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5557 | |
| 5558 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5559 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5560 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5561 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5562 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5563 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5564 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5565 | /* When TE: chunked is used, we need to get there again to parse remaining |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5566 | * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE. |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5567 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5568 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5569 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5570 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5571 | /* We know that more data are expected, but we couldn't send more that |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5572 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 5573 | * system knows it must not set a PUSH on this first part. Interactive |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 5574 | * modes are already handled by the stream sock layer. We must not do |
| 5575 | * this in content-length mode because it could present the MSG_MORE |
| 5576 | * flag with the last block of forwarded data, which would cause an |
| 5577 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5578 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5579 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5580 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5581 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5582 | return 0; |
| 5583 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5584 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5585 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5586 | if (s->listener->counters) |
| 5587 | s->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5588 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5589 | return_bad_req_stats_ok: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5590 | /* we may have some pending data starting at req->buf->p */ |
| 5591 | b_adv(req->buf, msg->next); |
| 5592 | msg->next = 0; |
| 5593 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5594 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5595 | if (txn->status) { |
| 5596 | /* Note: we don't send any error if some data were already sent */ |
| 5597 | stream_int_retnclose(req->prod, NULL); |
| 5598 | } else { |
| 5599 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5600 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5601 | } |
| 5602 | req->analysers = 0; |
| 5603 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5604 | |
| 5605 | if (!(s->flags & SN_ERR_MASK)) |
| 5606 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5607 | if (!(s->flags & SN_FINST_MASK)) { |
| 5608 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5609 | s->flags |= SN_FINST_H; |
| 5610 | else |
| 5611 | s->flags |= SN_FINST_D; |
| 5612 | } |
| 5613 | return 0; |
| 5614 | |
| 5615 | aborted_xfer: |
| 5616 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5617 | if (txn->status) { |
| 5618 | /* Note: we don't send any error if some data were already sent */ |
| 5619 | stream_int_retnclose(req->prod, NULL); |
| 5620 | } else { |
| 5621 | txn->status = 502; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5622 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5623 | } |
| 5624 | req->analysers = 0; |
| 5625 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5626 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5627 | s->fe->fe_counters.srv_aborts++; |
| 5628 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5629 | if (objt_server(s->target)) |
| 5630 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5631 | |
| 5632 | if (!(s->flags & SN_ERR_MASK)) |
| 5633 | s->flags |= SN_ERR_SRVCL; |
| 5634 | if (!(s->flags & SN_FINST_MASK)) { |
| 5635 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5636 | s->flags |= SN_FINST_H; |
| 5637 | else |
| 5638 | s->flags |= SN_FINST_D; |
| 5639 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5640 | return 0; |
| 5641 | } |
| 5642 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5643 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5644 | * processing can continue on next analysers, or zero if it either needs more |
| 5645 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 5646 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 5647 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5648 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5649 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5650 | int http_wait_for_response(struct session *s, struct channel *rep, int an_bit) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5651 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5652 | struct http_txn *txn = &s->txn; |
| 5653 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5654 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5655 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5656 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5657 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5658 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5659 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 5660 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5661 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5662 | rep, |
| 5663 | rep->rex, rep->wex, |
| 5664 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5665 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5666 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5667 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5668 | /* |
| 5669 | * Now parse the partial (or complete) lines. |
| 5670 | * We will check the response syntax, and also join multi-line |
| 5671 | * headers. An index of all the lines will be elaborated while |
| 5672 | * parsing. |
| 5673 | * |
| 5674 | * For the parsing, we use a 28 states FSM. |
| 5675 | * |
| 5676 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5677 | * rep->buf->p = beginning of response |
| 5678 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5679 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5680 | * msg->eol = end of current header or line (LF or CRLF) |
| 5681 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5682 | */ |
| 5683 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5684 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5685 | /* There's a protected area at the end of the buffer for rewriting |
| 5686 | * purposes. We don't want to start to parse the request if the |
| 5687 | * protected area is affected, because we may have to move processed |
| 5688 | * data later, which is much more complicated. |
| 5689 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5690 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5691 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5692 | /* some data has still not left the buffer, wake us once that's done */ |
| 5693 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5694 | goto abort_response; |
| 5695 | channel_dont_close(rep); |
| 5696 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5697 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5698 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5699 | } |
| 5700 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5701 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5702 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5703 | buffer_slow_realign(rep->buf); |
| 5704 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5705 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5706 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5707 | } |
| 5708 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5709 | /* 1: we might have to print this header in debug mode */ |
| 5710 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5711 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5712 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5713 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5714 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5715 | sol = rep->buf->p; |
| 5716 | eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5717 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5718 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5719 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5720 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5721 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5722 | while (cur_idx) { |
| 5723 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5724 | debug_hdr("srvhdr", s, sol, eol); |
| 5725 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5726 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5727 | } |
| 5728 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5729 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5730 | /* |
| 5731 | * Now we quickly check if we have found a full valid response. |
| 5732 | * If not so, we check the FD and buffer states before leaving. |
| 5733 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5734 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5735 | * responses are checked first. |
| 5736 | * |
| 5737 | * Depending on whether the client is still there or not, we |
| 5738 | * may send an error response back or not. Note that normally |
| 5739 | * we should only check for HTTP status there, and check I/O |
| 5740 | * errors somewhere else. |
| 5741 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5742 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5743 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5744 | /* Invalid response */ |
| 5745 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5746 | /* we detected a parsing error. We want to archive this response |
| 5747 | * in the dedicated proxy area for later troubleshooting. |
| 5748 | */ |
| 5749 | hdr_response_bad: |
| 5750 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5751 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5752 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5753 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5754 | if (objt_server(s->target)) { |
| 5755 | objt_server(s->target)->counters.failed_resp++; |
| 5756 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5757 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5758 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5759 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5760 | rep->analysers = 0; |
| 5761 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5762 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5763 | channel_truncate(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5764 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5765 | |
| 5766 | if (!(s->flags & SN_ERR_MASK)) |
| 5767 | s->flags |= SN_ERR_PRXCOND; |
| 5768 | if (!(s->flags & SN_FINST_MASK)) |
| 5769 | s->flags |= SN_FINST_H; |
| 5770 | |
| 5771 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5772 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5773 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5774 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5775 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5776 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5777 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5778 | goto hdr_response_bad; |
| 5779 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5780 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5781 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5782 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5783 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5784 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5785 | else if (txn->flags & TX_NOT_FIRST) |
| 5786 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5787 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5788 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5789 | if (objt_server(s->target)) { |
| 5790 | objt_server(s->target)->counters.failed_resp++; |
| 5791 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5792 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5793 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5794 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5795 | rep->analysers = 0; |
| 5796 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5797 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5798 | channel_truncate(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5799 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5800 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5801 | if (!(s->flags & SN_ERR_MASK)) |
| 5802 | s->flags |= SN_ERR_SRVCL; |
| 5803 | if (!(s->flags & SN_FINST_MASK)) |
| 5804 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5805 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5806 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5807 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 5808 | /* read timeout : return a 504 to the client. */ |
| 5809 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5810 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5811 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5812 | else if (txn->flags & TX_NOT_FIRST) |
| 5813 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5814 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5815 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5816 | if (objt_server(s->target)) { |
| 5817 | objt_server(s->target)->counters.failed_resp++; |
| 5818 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5819 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5820 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5821 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5822 | rep->analysers = 0; |
| 5823 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5824 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5825 | channel_truncate(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5826 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5827 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5828 | if (!(s->flags & SN_ERR_MASK)) |
| 5829 | s->flags |= SN_ERR_SRVTO; |
| 5830 | if (!(s->flags & SN_FINST_MASK)) |
| 5831 | s->flags |= SN_FINST_H; |
| 5832 | return 0; |
| 5833 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5834 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5835 | /* client abort with an abortonclose */ |
| 5836 | else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
| 5837 | s->fe->fe_counters.cli_aborts++; |
| 5838 | s->be->be_counters.cli_aborts++; |
| 5839 | if (objt_server(s->target)) |
| 5840 | objt_server(s->target)->counters.cli_aborts++; |
| 5841 | |
| 5842 | rep->analysers = 0; |
| 5843 | channel_auto_close(rep); |
| 5844 | |
| 5845 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5846 | channel_truncate(rep); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5847 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400)); |
| 5848 | |
| 5849 | if (!(s->flags & SN_ERR_MASK)) |
| 5850 | s->flags |= SN_ERR_CLICL; |
| 5851 | if (!(s->flags & SN_FINST_MASK)) |
| 5852 | s->flags |= SN_FINST_H; |
| 5853 | |
| 5854 | /* process_session() will take care of the error */ |
| 5855 | return 0; |
| 5856 | } |
| 5857 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5858 | /* 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] | 5859 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5860 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5861 | 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] | 5862 | else if (txn->flags & TX_NOT_FIRST) |
| 5863 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5864 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5865 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5866 | if (objt_server(s->target)) { |
| 5867 | objt_server(s->target)->counters.failed_resp++; |
| 5868 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5869 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5870 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5871 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5872 | rep->analysers = 0; |
| 5873 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5874 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5875 | channel_truncate(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5876 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5877 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5878 | if (!(s->flags & SN_ERR_MASK)) |
| 5879 | s->flags |= SN_ERR_SRVCL; |
| 5880 | if (!(s->flags & SN_FINST_MASK)) |
| 5881 | s->flags |= SN_FINST_H; |
| 5882 | return 0; |
| 5883 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5884 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5885 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5886 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5887 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5888 | 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] | 5889 | else if (txn->flags & TX_NOT_FIRST) |
| 5890 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5891 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5892 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5893 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5894 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5895 | |
| 5896 | if (!(s->flags & SN_ERR_MASK)) |
| 5897 | s->flags |= SN_ERR_CLICL; |
| 5898 | if (!(s->flags & SN_FINST_MASK)) |
| 5899 | s->flags |= SN_FINST_H; |
| 5900 | |
| 5901 | /* process_session() will take care of the error */ |
| 5902 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5903 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5904 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5905 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5906 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5907 | return 0; |
| 5908 | } |
| 5909 | |
| 5910 | /* More interesting part now : we know that we have a complete |
| 5911 | * response which at least looks like HTTP. We have an indicator |
| 5912 | * of each header's length, so we can parse them quickly. |
| 5913 | */ |
| 5914 | |
| 5915 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5916 | 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] | 5917 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5918 | /* |
| 5919 | * 1: get the status code |
| 5920 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5921 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5922 | if (n < 1 || n > 5) |
| 5923 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5924 | /* when the client triggers a 4xx from the server, it's most often due |
| 5925 | * to a missing object or permission. These events should be tracked |
| 5926 | * because if they happen often, it may indicate a brute force or a |
| 5927 | * vulnerability scan. |
| 5928 | */ |
| 5929 | if (n == 4) |
| 5930 | session_inc_http_err_ctr(s); |
| 5931 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5932 | if (objt_server(s->target)) |
| 5933 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5934 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5935 | /* check if the response is HTTP/1.1 or above */ |
| 5936 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5937 | ((rep->buf->p[5] > '1') || |
| 5938 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5939 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5940 | |
| 5941 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5942 | 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] | 5943 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5944 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5945 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5946 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5947 | 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] | 5948 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5949 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5950 | * and 505 are triggered on demand by client request, so we must not |
| 5951 | * count them as server failures. |
| 5952 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5953 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5954 | 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] | 5955 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5956 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5957 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5958 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5959 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5960 | /* |
| 5961 | * 2: check for cacheability. |
| 5962 | */ |
| 5963 | |
| 5964 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5965 | case 100: |
| 5966 | /* |
| 5967 | * We may be facing a 100-continue response, in which case this |
| 5968 | * is not the right response, and we're waiting for the next one. |
| 5969 | * Let's allow this response to go to the client and wait for the |
| 5970 | * next one. |
| 5971 | */ |
| 5972 | hdr_idx_init(&txn->hdr_idx); |
| 5973 | msg->next -= channel_forward(rep, msg->next); |
| 5974 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5975 | txn->status = 0; |
| 5976 | s->logs.t_data = -1; /* was not a response yet */ |
| 5977 | goto next_one; |
| 5978 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5979 | case 200: |
| 5980 | case 203: |
| 5981 | case 206: |
| 5982 | case 300: |
| 5983 | case 301: |
| 5984 | case 410: |
| 5985 | /* RFC2616 @13.4: |
| 5986 | * "A response received with a status code of |
| 5987 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 5988 | * by a cache (...) unless a cache-control |
| 5989 | * directive prohibits caching." |
| 5990 | * |
| 5991 | * RFC2616 @9.5: POST method : |
| 5992 | * "Responses to this method are not cacheable, |
| 5993 | * unless the response includes appropriate |
| 5994 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5995 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5996 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5997 | ((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] | 5998 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 5999 | break; |
| 6000 | default: |
| 6001 | break; |
| 6002 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6003 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6004 | /* |
| 6005 | * 3: we may need to capture headers |
| 6006 | */ |
| 6007 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 6008 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6009 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6010 | txn->rsp.cap, s->fe->rsp_cap); |
| 6011 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6012 | /* 4: determine the transfer-length. |
| 6013 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 6014 | * the presence of a message-body in a RESPONSE and its transfer length |
| 6015 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6016 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6017 | * All responses to the HEAD request method MUST NOT include a |
| 6018 | * message-body, even though the presence of entity-header fields |
| 6019 | * might lead one to believe they do. All 1xx (informational), 204 |
| 6020 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 6021 | * message-body. All other responses do include a message-body, |
| 6022 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6023 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6024 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 6025 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 6026 | * always terminated by the first empty line after the header fields, |
| 6027 | * regardless of the entity-header fields present in the message. |
| 6028 | * |
| 6029 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 6030 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 6031 | * transfer-length is defined by the use of this transfer-coding. |
| 6032 | * If a Transfer-Encoding header field is present and the "chunked" |
| 6033 | * transfer-coding is not present, the transfer-length is defined by |
| 6034 | * the sender closing the connection. |
| 6035 | * |
| 6036 | * 3. If a Content-Length header field is present, its decimal value in |
| 6037 | * OCTETs represents both the entity-length and the transfer-length. |
| 6038 | * If a message is received with both a Transfer-Encoding header |
| 6039 | * field and a Content-Length header field, the latter MUST be ignored. |
| 6040 | * |
| 6041 | * 4. If the message uses the media type "multipart/byteranges", and |
| 6042 | * the transfer-length is not otherwise specified, then this self- |
| 6043 | * delimiting media type defines the transfer-length. This media |
| 6044 | * type MUST NOT be used unless the sender knows that the recipient |
| 6045 | * can parse it; the presence in a request of a Range header with |
| 6046 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 6047 | * client can parse multipart/byteranges responses. |
| 6048 | * |
| 6049 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6050 | */ |
| 6051 | |
| 6052 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6053 | * 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] | 6054 | * 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] | 6055 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6056 | */ |
| 6057 | if (txn->meth == HTTP_METH_HEAD || |
| 6058 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6059 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6060 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 6061 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6062 | goto skip_content_length; |
| 6063 | } |
| 6064 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6065 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6066 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6067 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6068 | 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] | 6069 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6070 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6071 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6072 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6073 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6074 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6075 | break; |
| 6076 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6077 | } |
| 6078 | |
| 6079 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 6080 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6081 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6082 | 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] | 6083 | signed long long cl; |
| 6084 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6085 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6086 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6087 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6088 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6089 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6090 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6091 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6092 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6093 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6094 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6095 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6096 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6097 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6098 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6099 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6100 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6101 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6102 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6103 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6104 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6105 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6106 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6107 | } |
| 6108 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6109 | if (s->fe->comp || s->be->comp) |
| 6110 | select_compression_response_header(s, rep->buf); |
| 6111 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6112 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6113 | /* Now we have to check if we need to modify the Connection header. |
| 6114 | * This is more difficult on the response than it is on the request, |
| 6115 | * because we can have two different HTTP versions and we don't know |
| 6116 | * how the client will interprete a response. For instance, let's say |
| 6117 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6118 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6119 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6120 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6121 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6122 | * indicating how a request MAY be understood by the client. In case |
| 6123 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6124 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6125 | * will fall back to explicit close. Note that we won't take risks with |
| 6126 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6127 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6128 | */ |
| 6129 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6130 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6131 | txn->status == 101)) { |
| 6132 | /* Either we've established an explicit tunnel, or we're |
| 6133 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6134 | * to understand the next protocols. We have to switch to tunnel |
| 6135 | * mode, so that we transfer the request and responses then let |
| 6136 | * this protocol pass unmodified. When we later implement specific |
| 6137 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6138 | * header which contains information about that protocol for |
| 6139 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6140 | */ |
| 6141 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6142 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6143 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6144 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6145 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6146 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6147 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6148 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6149 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6150 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6151 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6152 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6153 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6154 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6155 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6156 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6157 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6158 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6159 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6160 | /* now adjust header transformations depending on current state */ |
| 6161 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6162 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6163 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6164 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6165 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6166 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6167 | else { /* SCL / KAL */ |
| 6168 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6169 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6170 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6171 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6172 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6173 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6174 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6175 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6176 | /* Some keep-alive responses are converted to Server-close if |
| 6177 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6178 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6179 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6180 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6181 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6182 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6183 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6184 | } |
| 6185 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6186 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6187 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6188 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6189 | /* end of job, return OK */ |
| 6190 | rep->analysers &= ~an_bit; |
| 6191 | rep->analyse_exp = TICK_ETERNITY; |
| 6192 | channel_auto_close(rep); |
| 6193 | return 1; |
| 6194 | |
| 6195 | abort_keep_alive: |
| 6196 | /* A keep-alive request to the server failed on a network error. |
| 6197 | * The client is required to retry. We need to close without returning |
| 6198 | * any other information so that the client retries. |
| 6199 | */ |
| 6200 | txn->status = 0; |
| 6201 | rep->analysers = 0; |
| 6202 | s->req->analysers = 0; |
| 6203 | channel_auto_close(rep); |
| 6204 | s->logs.logwait = 0; |
| 6205 | s->logs.level = 0; |
| 6206 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6207 | channel_truncate(rep); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6208 | stream_int_retnclose(rep->cons, NULL); |
| 6209 | return 0; |
| 6210 | } |
| 6211 | |
| 6212 | /* This function performs all the processing enabled for the current response. |
| 6213 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 6214 | * and updates s->rep->analysers. It might make sense to explode it into several |
| 6215 | * other functions. It works like process_request (see indications above). |
| 6216 | */ |
| 6217 | int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px) |
| 6218 | { |
| 6219 | struct http_txn *txn = &s->txn; |
| 6220 | struct http_msg *msg = &txn->rsp; |
| 6221 | struct proxy *cur_proxy; |
| 6222 | struct cond_wordlist *wl; |
| 6223 | struct http_res_rule *http_res_last_rule = NULL; |
| 6224 | |
| 6225 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 6226 | now_ms, __FUNCTION__, |
| 6227 | s, |
| 6228 | rep, |
| 6229 | rep->rex, rep->wex, |
| 6230 | rep->flags, |
| 6231 | rep->buf->i, |
| 6232 | rep->analysers); |
| 6233 | |
| 6234 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6235 | return 0; |
| 6236 | |
| 6237 | rep->analysers &= ~an_bit; |
| 6238 | rep->analyse_exp = TICK_ETERNITY; |
| 6239 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6240 | /* The stats applet needs to adjust the Connection header but we don't |
| 6241 | * apply any filter there. |
| 6242 | */ |
| 6243 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) |
| 6244 | goto skip_filters; |
| 6245 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6246 | /* |
| 6247 | * We will have to evaluate the filters. |
| 6248 | * As opposed to version 1.2, now they will be evaluated in the |
| 6249 | * filters order and not in the header order. This means that |
| 6250 | * each filter has to be validated among all headers. |
| 6251 | * |
| 6252 | * Filters are tried with ->be first, then with ->fe if it is |
| 6253 | * different from ->be. |
| 6254 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6255 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6256 | cur_proxy = s->be; |
| 6257 | while (1) { |
| 6258 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6259 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6260 | /* evaluate http-response rules */ |
| 6261 | if (!http_res_last_rule) |
| 6262 | 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] | 6263 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6264 | /* try headers filters */ |
| 6265 | if (rule_set->rsp_exp != NULL) { |
| 6266 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6267 | return_bad_resp: |
| 6268 | if (objt_server(s->target)) { |
| 6269 | objt_server(s->target)->counters.failed_resp++; |
| 6270 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6271 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6272 | s->be->be_counters.failed_resp++; |
| 6273 | return_srv_prx_502: |
| 6274 | rep->analysers = 0; |
| 6275 | txn->status = 502; |
| 6276 | s->logs.t_data = -1; /* was not a valid response */ |
| 6277 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6278 | channel_truncate(rep); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6279 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
| 6280 | if (!(s->flags & SN_ERR_MASK)) |
| 6281 | s->flags |= SN_ERR_PRXCOND; |
| 6282 | if (!(s->flags & SN_FINST_MASK)) |
| 6283 | s->flags |= SN_FINST_H; |
| 6284 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6285 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6286 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6287 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6288 | /* has the response been denied ? */ |
| 6289 | if (txn->flags & TX_SVDENY) { |
| 6290 | if (objt_server(s->target)) |
| 6291 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6292 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6293 | s->be->be_counters.denied_resp++; |
| 6294 | s->fe->fe_counters.denied_resp++; |
| 6295 | if (s->listener->counters) |
| 6296 | s->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6297 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6298 | goto return_srv_prx_502; |
| 6299 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6300 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6301 | /* add response headers from the rule sets in the same order */ |
| 6302 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6303 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6304 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6305 | if (wl->cond) { |
| 6306 | int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 6307 | ret = acl_pass(ret); |
| 6308 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6309 | ret = !ret; |
| 6310 | if (!ret) |
| 6311 | continue; |
| 6312 | } |
| 6313 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6314 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6315 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6316 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6317 | /* check whether we're already working on the frontend */ |
| 6318 | if (cur_proxy == s->fe) |
| 6319 | break; |
| 6320 | cur_proxy = s->fe; |
| 6321 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6322 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6323 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6324 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6325 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6326 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6327 | /* |
| 6328 | * Now check for a server cookie. |
| 6329 | */ |
| 6330 | if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name || |
| 6331 | (s->be->options & PR_O_CHK_CACHE)) |
| 6332 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6333 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6334 | /* |
| 6335 | * Check for cache-control or pragma headers if required. |
| 6336 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6337 | 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] | 6338 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6339 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6340 | /* |
| 6341 | * Add server cookie in the response if needed |
| 6342 | */ |
| 6343 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6344 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 6345 | (!(s->flags & SN_DIRECT) || |
| 6346 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6347 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6348 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6349 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6350 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 6351 | !(s->flags & SN_IGNORE_PRST)) { |
| 6352 | /* the server is known, it's not the one the client requested, or the |
| 6353 | * cookie's last seen date needs to be refreshed. We have to |
| 6354 | * insert a set-cookie here, except if we want to insert only on POST |
| 6355 | * requests and this one isn't. Note that servers which don't have cookies |
| 6356 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6357 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6358 | if (!objt_server(s->target)->cookie) { |
| 6359 | chunk_printf(&trash, |
| 6360 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6361 | s->be->cookie_name); |
| 6362 | } |
| 6363 | else { |
| 6364 | 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] | 6365 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6366 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6367 | /* emit last_date, which is mandatory */ |
| 6368 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6369 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6370 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6371 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6372 | if (s->be->cookie_maxlife) { |
| 6373 | /* emit first_date, which is either the original one or |
| 6374 | * the current date. |
| 6375 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6376 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6377 | s30tob64(txn->cookie_first_date ? |
| 6378 | txn->cookie_first_date >> 2 : |
| 6379 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6380 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6381 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6382 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6383 | chunk_appendf(&trash, "; path=/"); |
| 6384 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6385 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6386 | if (s->be->cookie_domain) |
| 6387 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6388 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6389 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6390 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6391 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6392 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6393 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6394 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6395 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6396 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6397 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6398 | txn->flags &= ~TX_SCK_MASK; |
| 6399 | if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT)) |
| 6400 | /* the server did not change, only the date was updated */ |
| 6401 | txn->flags |= TX_SCK_UPDATED; |
| 6402 | else |
| 6403 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6404 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6405 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6406 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6407 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6408 | * 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] | 6409 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6410 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6411 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6412 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6413 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6414 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6415 | "Cache-control: private", 22) < 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 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6419 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6420 | /* |
| 6421 | * Check if result will be cacheable with a cookie. |
| 6422 | * We'll block the response if security checks have caught |
| 6423 | * nasty things such as a cacheable cookie. |
| 6424 | */ |
| 6425 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6426 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6427 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6428 | /* we're in presence of a cacheable response containing |
| 6429 | * a set-cookie header. We'll block it as requested by |
| 6430 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6431 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6432 | if (objt_server(s->target)) |
| 6433 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6434 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6435 | s->be->be_counters.denied_resp++; |
| 6436 | s->fe->fe_counters.denied_resp++; |
| 6437 | if (s->listener->counters) |
| 6438 | s->listener->counters->denied_resp++; |
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 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6441 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6442 | send_log(s->be, LOG_ALERT, |
| 6443 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6444 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6445 | goto return_srv_prx_502; |
| 6446 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6447 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6448 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6449 | /* |
| 6450 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6451 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6452 | * not to have to deal with some client bugs : some of them fail an upgrade |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6453 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6454 | * want to touch any 101 response either since it's switching to another |
| 6455 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6456 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6457 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6458 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 6459 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6460 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6461 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6462 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6463 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6464 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6465 | /* we want a keep-alive response here. Keep-alive header |
| 6466 | * required if either side is not 1.1. |
| 6467 | */ |
| 6468 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6469 | want_flags |= TX_CON_KAL_SET; |
| 6470 | } |
| 6471 | else { |
| 6472 | /* we want a close response here. Close header required if |
| 6473 | * the server is 1.1, regardless of the client. |
| 6474 | */ |
| 6475 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6476 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6477 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6478 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6479 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6480 | http_change_connection_header(txn, msg, want_flags); |
| 6481 | } |
| 6482 | |
| 6483 | skip_header_mangling: |
| 6484 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
| 6485 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 6486 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6487 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6488 | /* if the user wants to log as soon as possible, without counting |
| 6489 | * bytes from the server, then this is the right moment. We have |
| 6490 | * to temporarily assign bytes_out to log what we currently have. |
| 6491 | */ |
| 6492 | if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 6493 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6494 | s->logs.bytes_out = txn->rsp.eoh; |
| 6495 | s->do_log(s); |
| 6496 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6497 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6498 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6499 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6500 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6501 | /* This function is an analyser which forwards response body (including chunk |
| 6502 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6503 | * zero byte. The only situation where it must not be called is when we're in |
| 6504 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6505 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6506 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 6507 | * 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] | 6508 | * |
| 6509 | * It is capable of compressing response data both in content-length mode and |
| 6510 | * in chunked mode. The state machines follows different flows depending on |
| 6511 | * whether content-length and chunked modes are used, since there are no |
| 6512 | * trailers in content-length : |
| 6513 | * |
| 6514 | * chk-mode cl-mode |
| 6515 | * ,----- BODY -----. |
| 6516 | * / \ |
| 6517 | * V size > 0 V chk-mode |
| 6518 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6519 | * | | size == 0 | last byte | |
| 6520 | * | v final crlf v inspected | |
| 6521 | * | TRAILERS -----------> DONE | |
| 6522 | * | | |
| 6523 | * `----------------------------------------------' |
| 6524 | * |
| 6525 | * Compression only happens in the DATA state, and must be flushed in final |
| 6526 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6527 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6528 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6529 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6530 | 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] | 6531 | { |
| 6532 | struct http_txn *txn = &s->txn; |
| 6533 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6534 | static struct buffer *tmpbuf = &buf_empty; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6535 | int compressing = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6536 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6537 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6538 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6539 | return 0; |
| 6540 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6541 | 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] | 6542 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 6543 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6544 | /* Output closed while we were sending data. We must abort and |
| 6545 | * wake the other side up. |
| 6546 | */ |
| 6547 | msg->msg_state = HTTP_MSG_ERROR; |
| 6548 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6549 | return 1; |
| 6550 | } |
| 6551 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6552 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6553 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6554 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 6555 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6556 | /* we have msg->sov which points to the first byte of message |
| 6557 | * body, and res->buf.p still points to the beginning of the |
| 6558 | * message. We forward the headers now, as we don't need them |
| 6559 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6560 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6561 | b_adv(res->buf, msg->sov); |
| 6562 | msg->next -= msg->sov; |
| 6563 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6564 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6565 | /* The previous analysers guarantee that the state is somewhere |
| 6566 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 6567 | * msg->next are always correct. |
| 6568 | */ |
| 6569 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 6570 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 6571 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6572 | else |
| 6573 | msg->msg_state = HTTP_MSG_DATA; |
| 6574 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6575 | } |
| 6576 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6577 | if (res->to_forward) { |
| 6578 | /* We can't process the buffer's contents yet */ |
| 6579 | res->flags |= CF_WAKE_WRITE; |
| 6580 | goto missing_data; |
| 6581 | } |
| 6582 | |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6583 | if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) { |
| 6584 | /* We need a compression buffer in the DATA state to put the |
| 6585 | * output of compressed data, and in CRLF state to let the |
| 6586 | * TRAILERS state finish the job of removing the trailing CRLF. |
| 6587 | */ |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6588 | if (unlikely(!tmpbuf->size)) { |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6589 | /* this is the first time we need the compression buffer */ |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 6590 | if (b_alloc(&tmpbuf) == NULL) |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6591 | goto aborted_xfer; /* no memory */ |
| 6592 | } |
| 6593 | |
| 6594 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6595 | if (ret < 0) { |
| 6596 | res->flags |= CF_WAKE_WRITE; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6597 | goto missing_data; /* not enough spaces in buffers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6598 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6599 | compressing = 1; |
| 6600 | } |
| 6601 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6602 | while (1) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6603 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6604 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6605 | /* we may have some pending data starting at res->buf->p */ |
| 6606 | if (unlikely(s->comp_algo)) { |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 6607 | ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6608 | if (ret < 0) |
| 6609 | goto aborted_xfer; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6610 | |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6611 | if (msg->chunk_len) { |
| 6612 | /* input empty or output full */ |
| 6613 | if (res->buf->i > msg->next) |
| 6614 | res->flags |= CF_WAKE_WRITE; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6615 | goto missing_data; |
| 6616 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6617 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6618 | else { |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6619 | if (msg->chunk_len > res->buf->i - msg->next) { |
| 6620 | /* output full */ |
| 6621 | res->flags |= CF_WAKE_WRITE; |
| 6622 | goto missing_data; |
| 6623 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6624 | msg->next += msg->chunk_len; |
| 6625 | msg->chunk_len = 0; |
| 6626 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6627 | |
| 6628 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6629 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6630 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6631 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6632 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6633 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6634 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6635 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6636 | |
| 6637 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 6638 | /* we want the CRLF after the data */ |
| 6639 | |
| 6640 | ret = http_skip_chunk_crlf(msg); |
| 6641 | if (ret == 0) |
| 6642 | goto missing_data; |
| 6643 | else if (ret < 0) { |
| 6644 | if (msg->err_pos >= 0) |
| 6645 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe); |
| 6646 | goto return_bad_res; |
| 6647 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6648 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 6649 | |
| 6650 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6651 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 6652 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6653 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6654 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6655 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6656 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6657 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6658 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6659 | else if (ret < 0) { |
| 6660 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6661 | 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] | 6662 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6663 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 6664 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6665 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6666 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6667 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6668 | if (unlikely(compressing)) { |
| 6669 | /* we need to flush output contents before syncing FSMs */ |
| 6670 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6671 | compressing = 0; |
| 6672 | } |
| 6673 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6674 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6675 | if (ret == 0) |
| 6676 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6677 | else if (ret < 0) { |
| 6678 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6679 | 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] | 6680 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6681 | } |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6682 | /* we're in HTTP_MSG_DONE now, fall through */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6683 | |
| 6684 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6685 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6686 | if (unlikely(compressing)) { |
| 6687 | /* we need to flush output contents before syncing FSMs */ |
| 6688 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6689 | compressing = 0; |
| 6690 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6691 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6692 | /* we may have some pending data starting at res->buf->p |
| 6693 | * such as a last chunk of data or trailers. |
| 6694 | */ |
| 6695 | b_adv(res->buf, msg->next); |
| 6696 | msg->next = 0; |
| 6697 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6698 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6699 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6700 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6701 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6702 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6703 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6704 | if (http_resync_states(s)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6705 | /* some state changes occurred, maybe the analyser |
| 6706 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6707 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6708 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6709 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6710 | /* response errors are most likely due to |
| 6711 | * the client aborting the transfer. |
| 6712 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6713 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6714 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6715 | if (msg->err_pos >= 0) |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6716 | 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] | 6717 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6718 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6719 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6720 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6721 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6722 | } |
| 6723 | } |
| 6724 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6725 | missing_data: |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6726 | /* we may have some pending data starting at res->buf->p */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6727 | if (unlikely(compressing)) { |
| 6728 | 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] | 6729 | compressing = 0; |
| 6730 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6731 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6732 | if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) { |
| 6733 | b_adv(res->buf, msg->next); |
| 6734 | msg->next = 0; |
| 6735 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6736 | } |
| 6737 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6738 | if (res->flags & CF_SHUTW) |
| 6739 | goto aborted_xfer; |
| 6740 | |
| 6741 | /* stop waiting for data if the input is closed before the end. If the |
| 6742 | * client side was already closed, it means that the client has aborted, |
| 6743 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6744 | * server abort. |
| 6745 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6746 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | b2c6a78 | 2014-04-23 20:29:01 +0200 | [diff] [blame] | 6747 | if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6748 | goto aborted_xfer; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6749 | if (!(s->flags & SN_ERR_MASK)) |
| 6750 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6751 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6752 | if (objt_server(s->target)) |
| 6753 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6754 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6755 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6756 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6757 | /* 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] | 6758 | if (!s->req->analysers) |
| 6759 | goto return_bad_res; |
| 6760 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6761 | /* 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] | 6762 | * 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] | 6763 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 6764 | * close. |
| 6765 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6766 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6767 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6768 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6769 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6770 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6771 | /* 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] | 6772 | * 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] | 6773 | * 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] | 6774 | * modes are already handled by the stream sock layer. We must not do |
| 6775 | * this in content-length mode because it could present the MSG_MORE |
| 6776 | * flag with the last block of forwarded data, which would cause an |
| 6777 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6778 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6779 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6780 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6781 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6782 | /* the session handler will take care of timeouts and errors */ |
| 6783 | return 0; |
| 6784 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6785 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6786 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6787 | if (objt_server(s->target)) |
| 6788 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6789 | |
| 6790 | return_bad_res_stats_ok: |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 6791 | if (unlikely(compressing)) { |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6792 | 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] | 6793 | compressing = 0; |
| 6794 | } |
| 6795 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6796 | /* we may have some pending data starting at res->buf->p */ |
| 6797 | if (s->comp_algo == NULL) { |
| 6798 | b_adv(res->buf, msg->next); |
| 6799 | msg->next = 0; |
| 6800 | } |
| 6801 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6802 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6803 | /* don't send any error message as we're in the body */ |
| 6804 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6805 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6806 | 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] | 6807 | if (objt_server(s->target)) |
| 6808 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6809 | |
| 6810 | if (!(s->flags & SN_ERR_MASK)) |
| 6811 | s->flags |= SN_ERR_PRXCOND; |
| 6812 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6813 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6814 | return 0; |
| 6815 | |
| 6816 | aborted_xfer: |
Willy Tarreau | 6fef8ae | 2014-04-22 21:22:06 +0200 | [diff] [blame] | 6817 | if (unlikely(compressing)) { |
| 6818 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
| 6819 | compressing = 0; |
| 6820 | } |
| 6821 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6822 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6823 | /* don't send any error message as we're in the body */ |
| 6824 | stream_int_retnclose(res->cons, NULL); |
| 6825 | res->analysers = 0; |
| 6826 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 6827 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6828 | s->fe->fe_counters.cli_aborts++; |
| 6829 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6830 | if (objt_server(s->target)) |
| 6831 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6832 | |
| 6833 | if (!(s->flags & SN_ERR_MASK)) |
| 6834 | s->flags |= SN_ERR_CLICL; |
| 6835 | if (!(s->flags & SN_FINST_MASK)) |
| 6836 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6837 | return 0; |
| 6838 | } |
| 6839 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6840 | /* Iterate the same filter through all request headers. |
| 6841 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6842 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6843 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6844 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6845 | 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] | 6846 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6847 | char *cur_ptr, *cur_end, *cur_next; |
| 6848 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6849 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6850 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6851 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6852 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6853 | last_hdr = 0; |
| 6854 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6855 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6856 | old_idx = 0; |
| 6857 | |
| 6858 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6859 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6860 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6861 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6862 | (exp->action == ACT_ALLOW || |
| 6863 | exp->action == ACT_DENY || |
| 6864 | exp->action == ACT_TARPIT)) |
| 6865 | return 0; |
| 6866 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6867 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6868 | if (!cur_idx) |
| 6869 | break; |
| 6870 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6871 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6872 | cur_ptr = cur_next; |
| 6873 | cur_end = cur_ptr + cur_hdr->len; |
| 6874 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6875 | |
| 6876 | /* Now we have one header between cur_ptr and cur_end, |
| 6877 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6878 | */ |
| 6879 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 6880 | 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] | 6881 | switch (exp->action) { |
| 6882 | case ACT_SETBE: |
| 6883 | /* It is not possible to jump a second time. |
| 6884 | * FIXME: should we return an HTTP/500 here so that |
| 6885 | * the admin knows there's a problem ? |
| 6886 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6887 | if (s->be != s->fe) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6888 | break; |
| 6889 | |
| 6890 | /* Swithing Proxy */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6891 | session_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6892 | last_hdr = 1; |
| 6893 | break; |
| 6894 | |
| 6895 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6896 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6897 | last_hdr = 1; |
| 6898 | break; |
| 6899 | |
| 6900 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6901 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6902 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6903 | break; |
| 6904 | |
| 6905 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6906 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6907 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6908 | break; |
| 6909 | |
| 6910 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6911 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6912 | if (trash.len < 0) |
| 6913 | return -1; |
| 6914 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6915 | 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] | 6916 | /* FIXME: if the user adds a newline in the replacement, the |
| 6917 | * index will not be recalculated for now, and the new line |
| 6918 | * will not be counted as a new header. |
| 6919 | */ |
| 6920 | |
| 6921 | cur_end += delta; |
| 6922 | cur_next += delta; |
| 6923 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6924 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6925 | break; |
| 6926 | |
| 6927 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6928 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6929 | cur_next += delta; |
| 6930 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6931 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6932 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6933 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6934 | cur_hdr->len = 0; |
| 6935 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6936 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6937 | break; |
| 6938 | |
| 6939 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6940 | } |
| 6941 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6942 | /* keep the link from this header to next one in case of later |
| 6943 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6944 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6945 | old_idx = cur_idx; |
| 6946 | } |
| 6947 | return 0; |
| 6948 | } |
| 6949 | |
| 6950 | |
| 6951 | /* Apply the filter to the request line. |
| 6952 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6953 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6954 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6955 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6956 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6957 | 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] | 6958 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6959 | char *cur_ptr, *cur_end; |
| 6960 | int done; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6961 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6962 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6963 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6964 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6965 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6966 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6967 | (exp->action == ACT_ALLOW || |
| 6968 | exp->action == ACT_DENY || |
| 6969 | exp->action == ACT_TARPIT)) |
| 6970 | return 0; |
| 6971 | else if (exp->action == ACT_REMOVE) |
| 6972 | return 0; |
| 6973 | |
| 6974 | done = 0; |
| 6975 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6976 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6977 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6978 | |
| 6979 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6980 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 6981 | 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] | 6982 | switch (exp->action) { |
| 6983 | case ACT_SETBE: |
| 6984 | /* It is not possible to jump a second time. |
| 6985 | * FIXME: should we return an HTTP/500 here so that |
| 6986 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6987 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6988 | if (s->be != s->fe) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6989 | break; |
| 6990 | |
| 6991 | /* Swithing Proxy */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6992 | session_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6993 | done = 1; |
| 6994 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6995 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6996 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6997 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6998 | done = 1; |
| 6999 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7000 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7001 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7002 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7003 | done = 1; |
| 7004 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7005 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7006 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7007 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7008 | done = 1; |
| 7009 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7010 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7011 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7012 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7013 | if (trash.len < 0) |
| 7014 | return -1; |
| 7015 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7016 | 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] | 7017 | /* FIXME: if the user adds a newline in the replacement, the |
| 7018 | * index will not be recalculated for now, and the new line |
| 7019 | * will not be counted as a new header. |
| 7020 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7021 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7022 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7023 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7024 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7025 | HTTP_MSG_RQMETH, |
| 7026 | cur_ptr, cur_end + 1, |
| 7027 | NULL, NULL); |
| 7028 | if (unlikely(!cur_end)) |
| 7029 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7030 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7031 | /* we have a full request and we know that we have either a CR |
| 7032 | * or an LF at <ptr>. |
| 7033 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7034 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7035 | 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] | 7036 | /* there is no point trying this regex on headers */ |
| 7037 | return 1; |
| 7038 | } |
| 7039 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7040 | return done; |
| 7041 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7042 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7043 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7044 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7045 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7046 | * 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] | 7047 | * 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] | 7048 | * unparsable request. Since it can manage the switch to another backend, it |
| 7049 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7050 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7051 | 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] | 7052 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7053 | struct http_txn *txn = &s->txn; |
| 7054 | struct hdr_exp *exp; |
| 7055 | |
| 7056 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7057 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7058 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7059 | /* |
| 7060 | * The interleaving of transformations and verdicts |
| 7061 | * makes it difficult to decide to continue or stop |
| 7062 | * the evaluation. |
| 7063 | */ |
| 7064 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7065 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7066 | break; |
| 7067 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7068 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7069 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7070 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7071 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7072 | |
| 7073 | /* if this filter had a condition, evaluate it now and skip to |
| 7074 | * next filter if the condition does not match. |
| 7075 | */ |
| 7076 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7077 | 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] | 7078 | ret = acl_pass(ret); |
| 7079 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7080 | ret = !ret; |
| 7081 | |
| 7082 | if (!ret) |
| 7083 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7084 | } |
| 7085 | |
| 7086 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7087 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7088 | if (unlikely(ret < 0)) |
| 7089 | return -1; |
| 7090 | |
| 7091 | if (likely(ret == 0)) { |
| 7092 | /* The filter did not match the request, it can be |
| 7093 | * iterated through all headers. |
| 7094 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7095 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7096 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7097 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7098 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7099 | } |
| 7100 | |
| 7101 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7102 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7103 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7104 | * Try to retrieve the server associated to the appsession. |
| 7105 | * If the server is found, it's assigned to the session. |
| 7106 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7107 | void manage_client_side_appsession(struct session *s, const char *buf, int len) { |
| 7108 | struct http_txn *txn = &s->txn; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7109 | appsess *asession = NULL; |
| 7110 | char *sessid_temp = NULL; |
| 7111 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7112 | if (len > s->be->appsession_len) { |
| 7113 | len = s->be->appsession_len; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7114 | } |
| 7115 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7116 | if (s->be->options2 & PR_O2_AS_REQL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7117 | /* 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] | 7118 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7119 | /* 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] | 7120 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7121 | } |
| 7122 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7123 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7124 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7125 | 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] | 7126 | return; |
| 7127 | } |
| 7128 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7129 | memcpy(txn->sessid, buf, len); |
| 7130 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7131 | } |
| 7132 | |
| 7133 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 7134 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7135 | 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] | 7136 | return; |
| 7137 | } |
| 7138 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7139 | memcpy(sessid_temp, buf, len); |
| 7140 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7141 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7142 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7143 | /* free previously allocated memory */ |
| 7144 | pool_free2(apools.sessid, sessid_temp); |
| 7145 | |
| 7146 | if (asession != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7147 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
| 7148 | if (!(s->be->options2 & PR_O2_AS_REQL)) |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7149 | asession->request_count++; |
| 7150 | |
| 7151 | if (asession->serverid != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7152 | struct server *srv = s->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7153 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7154 | while (srv) { |
| 7155 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7156 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7157 | (s->be->options & PR_O_PERSIST) || |
| 7158 | (s->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7159 | /* we found the server and it's usable */ |
| 7160 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7161 | 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] | 7162 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 7163 | s->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 7164 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7165 | break; |
| 7166 | } else { |
| 7167 | txn->flags &= ~TX_CK_MASK; |
| 7168 | txn->flags |= TX_CK_DOWN; |
| 7169 | } |
| 7170 | } |
| 7171 | srv = srv->next; |
| 7172 | } |
| 7173 | } |
| 7174 | } |
| 7175 | } |
| 7176 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7177 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7178 | * same way as with headers above except that the semi-colon also ends a token. |
| 7179 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7180 | * return a valid result. |
| 7181 | */ |
| 7182 | char *find_cookie_value_end(char *s, const char *e) |
| 7183 | { |
| 7184 | int quoted, qdpair; |
| 7185 | |
| 7186 | quoted = qdpair = 0; |
| 7187 | for (; s < e; s++) { |
| 7188 | if (qdpair) qdpair = 0; |
| 7189 | else if (quoted) { |
| 7190 | if (*s == '\\') qdpair = 1; |
| 7191 | else if (*s == '"') quoted = 0; |
| 7192 | } |
| 7193 | else if (*s == '"') quoted = 1; |
| 7194 | else if (*s == ',' || *s == ';') return s; |
| 7195 | } |
| 7196 | return s; |
| 7197 | } |
| 7198 | |
| 7199 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7200 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7201 | * the first delimiter is updated if required. The function tries as much as |
| 7202 | * possible to respect the following principles : |
| 7203 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7204 | * colon, in which case <next> is simply removed |
| 7205 | * - set exactly one space character after the new first delimiter, unless |
| 7206 | * there are not enough characters in the block being moved to do so. |
| 7207 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7208 | * one. |
| 7209 | * |
| 7210 | * It is the caller's responsibility to ensure that : |
| 7211 | * - <from> points to a valid delimiter or the colon ; |
| 7212 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7213 | * - there are non-space chars before <from> ; |
| 7214 | * - there is a CR/LF at or after <next>. |
| 7215 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7216 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7217 | { |
| 7218 | char *prev = *from; |
| 7219 | |
| 7220 | if (*prev == ':') { |
| 7221 | /* We're removing the first value, preserve the colon and add a |
| 7222 | * space if possible. |
| 7223 | */ |
| 7224 | if (!http_is_crlf[(unsigned char)*next]) |
| 7225 | next++; |
| 7226 | prev++; |
| 7227 | if (prev < next) |
| 7228 | *prev++ = ' '; |
| 7229 | |
| 7230 | while (http_is_spht[(unsigned char)*next]) |
| 7231 | next++; |
| 7232 | } else { |
| 7233 | /* Remove useless spaces before the old delimiter. */ |
| 7234 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 7235 | prev--; |
| 7236 | *from = prev; |
| 7237 | |
| 7238 | /* copy the delimiter and if possible a space if we're |
| 7239 | * not at the end of the line. |
| 7240 | */ |
| 7241 | if (!http_is_crlf[(unsigned char)*next]) { |
| 7242 | *prev++ = *next++; |
| 7243 | if (prev + 1 < next) |
| 7244 | *prev++ = ' '; |
| 7245 | while (http_is_spht[(unsigned char)*next]) |
| 7246 | next++; |
| 7247 | } |
| 7248 | } |
| 7249 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7250 | } |
| 7251 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7252 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7253 | * 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] | 7254 | * desirable to call it only when needed. This code is quite complex because |
| 7255 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7256 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7257 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7258 | void manage_client_side_cookies(struct session *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7259 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7260 | struct http_txn *txn = &s->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7261 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7262 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7263 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7264 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7265 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7266 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7267 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7268 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7269 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7270 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7271 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7272 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7273 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7274 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7275 | hdr_beg = hdr_next; |
| 7276 | hdr_end = hdr_beg + cur_hdr->len; |
| 7277 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7278 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7279 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7280 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7281 | * "Cookie:" headers. |
| 7282 | */ |
| 7283 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7284 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7285 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7286 | old_idx = cur_idx; |
| 7287 | continue; |
| 7288 | } |
| 7289 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7290 | del_from = NULL; /* nothing to be deleted */ |
| 7291 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7292 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7293 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7294 | * attributes whose name begin with a '$', and associate them with |
| 7295 | * the right cookie, if we want to delete this cookie. |
| 7296 | * So there are 3 cases for each cookie read : |
| 7297 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7298 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7299 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7300 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7301 | * "special" cookie. |
| 7302 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7303 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7304 | * *MUST* delete it. |
| 7305 | * |
| 7306 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7307 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7308 | * the RFC explicitly allows spaces before it, and not within the |
| 7309 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7310 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7311 | * after the equal sign too, resulting in some (rare) buggy |
| 7312 | * implementations trying to do that. So let's do what servers do. |
| 7313 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7314 | * allowed quoted strings in values, with any possible character |
| 7315 | * after a backslash, including control chars and delimitors, which |
| 7316 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7317 | * within values even without quotes. |
| 7318 | * |
| 7319 | * We have to keep multiple pointers in order to support cookie |
| 7320 | * removal at the beginning, middle or end of header without |
| 7321 | * corrupting the header. All of these headers are valid : |
| 7322 | * |
| 7323 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7324 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7325 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7326 | * | | | | | | | | | |
| 7327 | * | | | | | | | | hdr_end <--+ |
| 7328 | * | | | | | | | +--> next |
| 7329 | * | | | | | | +----> val_end |
| 7330 | * | | | | | +-----------> val_beg |
| 7331 | * | | | | +--------------> equal |
| 7332 | * | | | +----------------> att_end |
| 7333 | * | | +---------------------> att_beg |
| 7334 | * | +--------------------------> prev |
| 7335 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7336 | */ |
| 7337 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7338 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7339 | /* Iterate through all cookies on this line */ |
| 7340 | |
| 7341 | /* find att_beg */ |
| 7342 | att_beg = prev + 1; |
| 7343 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7344 | att_beg++; |
| 7345 | |
| 7346 | /* find att_end : this is the first character after the last non |
| 7347 | * space before the equal. It may be equal to hdr_end. |
| 7348 | */ |
| 7349 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7350 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7351 | while (equal < hdr_end) { |
| 7352 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7353 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7354 | if (http_is_spht[(unsigned char)*equal++]) |
| 7355 | continue; |
| 7356 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7357 | } |
| 7358 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7359 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7360 | * is between <att_beg> and <equal>, both may be identical. |
| 7361 | */ |
| 7362 | |
| 7363 | /* look for end of cookie if there is an equal sign */ |
| 7364 | if (equal < hdr_end && *equal == '=') { |
| 7365 | /* look for the beginning of the value */ |
| 7366 | val_beg = equal + 1; |
| 7367 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7368 | val_beg++; |
| 7369 | |
| 7370 | /* find the end of the value, respecting quotes */ |
| 7371 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7372 | |
| 7373 | /* make val_end point to the first white space or delimitor after the value */ |
| 7374 | val_end = next; |
| 7375 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7376 | val_end--; |
| 7377 | } else { |
| 7378 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7379 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7380 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7381 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7382 | * they will automatically be removed if a header before them is removed, |
| 7383 | * since they're supposed to be linked together. |
| 7384 | */ |
| 7385 | if (*att_beg == '$') |
| 7386 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7387 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7388 | /* Ignore cookies with no equal sign */ |
| 7389 | if (equal == next) { |
| 7390 | /* This is not our cookie, so we must preserve it. But if we already |
| 7391 | * scheduled another cookie for removal, we cannot remove the |
| 7392 | * complete header, but we can remove the previous block itself. |
| 7393 | */ |
| 7394 | preserve_hdr = 1; |
| 7395 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7396 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7397 | val_end += delta; |
| 7398 | next += delta; |
| 7399 | hdr_end += delta; |
| 7400 | hdr_next += delta; |
| 7401 | cur_hdr->len += delta; |
| 7402 | http_msg_move_end(&txn->req, delta); |
| 7403 | prev = del_from; |
| 7404 | del_from = NULL; |
| 7405 | } |
| 7406 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7407 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7408 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7409 | /* if there are spaces around the equal sign, we need to |
| 7410 | * strip them otherwise we'll get trouble for cookie captures, |
| 7411 | * or even for rewrites. Since this happens extremely rarely, |
| 7412 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7413 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7414 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7415 | int stripped_before = 0; |
| 7416 | int stripped_after = 0; |
| 7417 | |
| 7418 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7419 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7420 | equal += stripped_before; |
| 7421 | val_beg += stripped_before; |
| 7422 | } |
| 7423 | |
| 7424 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7425 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7426 | val_beg += stripped_after; |
| 7427 | stripped_before += stripped_after; |
| 7428 | } |
| 7429 | |
| 7430 | val_end += stripped_before; |
| 7431 | next += stripped_before; |
| 7432 | hdr_end += stripped_before; |
| 7433 | hdr_next += stripped_before; |
| 7434 | cur_hdr->len += stripped_before; |
| 7435 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7436 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7437 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7438 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7439 | /* First, let's see if we want to capture this cookie. We check |
| 7440 | * that we don't already have a client side cookie, because we |
| 7441 | * can only capture one. Also as an optimisation, we ignore |
| 7442 | * cookies shorter than the declared name. |
| 7443 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7444 | if (s->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7445 | (val_end - att_beg >= s->fe->capture_namelen) && |
| 7446 | memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7447 | int log_len = val_end - att_beg; |
| 7448 | |
| 7449 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7450 | Alert("HTTP logging : out of memory.\n"); |
| 7451 | } else { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7452 | if (log_len > s->fe->capture_len) |
| 7453 | log_len = s->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7454 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7455 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7456 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7457 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7458 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7459 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7460 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7461 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7462 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7463 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7464 | * For cookies in prefix mode, the form is : |
| 7465 | * |
| 7466 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7467 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7468 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7469 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7470 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7471 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7472 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7473 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7474 | * have the server ID between val_beg and delim, and the original cookie between |
| 7475 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7476 | * |
| 7477 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7478 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7479 | * | || || | |+-> next |
| 7480 | * | || || | +--> val_end |
| 7481 | * | || || +---------> delim |
| 7482 | * | || |+------------> val_beg |
| 7483 | * | || +-------------> att_end = equal |
| 7484 | * | |+-----------------> att_beg |
| 7485 | * | +------------------> prev |
| 7486 | * +-------------------------> hdr_beg |
| 7487 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7488 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7489 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7490 | for (delim = val_beg; delim < val_end; delim++) |
| 7491 | if (*delim == COOKIE_DELIM) |
| 7492 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7493 | } else { |
| 7494 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7495 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7496 | /* Now check if the cookie contains a date field, which would |
| 7497 | * appear after a vertical bar ('|') just after the server name |
| 7498 | * and before the delimiter. |
| 7499 | */ |
| 7500 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7501 | if (vbar1) { |
| 7502 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7503 | * right is the last seen date. It is a base64 encoded |
| 7504 | * 30-bit value representing the UNIX date since the |
| 7505 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7506 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7507 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7508 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7509 | if (val_end - vbar1 >= 5) { |
| 7510 | val = b64tos30(vbar1); |
| 7511 | if (val > 0) |
| 7512 | txn->cookie_last_date = val << 2; |
| 7513 | } |
| 7514 | /* look for a second vertical bar */ |
| 7515 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7516 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7517 | val = b64tos30(vbar1 + 1); |
| 7518 | if (val > 0) |
| 7519 | txn->cookie_first_date = val << 2; |
| 7520 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7521 | } |
| 7522 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7523 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7524 | /* if the cookie has an expiration date and the proxy wants to check |
| 7525 | * it, then we do that now. We first check if the cookie is too old, |
| 7526 | * then only if it has expired. We detect strict overflow because the |
| 7527 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7528 | * in the future are ignored if their offset is beyond one day. This |
| 7529 | * allows an admin to fix timezone issues without expiring everyone |
| 7530 | * and at the same time avoids keeping unwanted side effects for too |
| 7531 | * long. |
| 7532 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7533 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7534 | (((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] | 7535 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7536 | txn->flags &= ~TX_CK_MASK; |
| 7537 | txn->flags |= TX_CK_OLD; |
| 7538 | delim = val_beg; // let's pretend we have not found the cookie |
| 7539 | txn->cookie_first_date = 0; |
| 7540 | txn->cookie_last_date = 0; |
| 7541 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7542 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7543 | (((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] | 7544 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7545 | txn->flags &= ~TX_CK_MASK; |
| 7546 | txn->flags |= TX_CK_EXPIRED; |
| 7547 | delim = val_beg; // let's pretend we have not found the cookie |
| 7548 | txn->cookie_first_date = 0; |
| 7549 | txn->cookie_last_date = 0; |
| 7550 | } |
| 7551 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7552 | /* Here, we'll look for the first running server which supports the cookie. |
| 7553 | * This allows to share a same cookie between several servers, for example |
| 7554 | * to dedicate backup servers to specific servers only. |
| 7555 | * However, to prevent clients from sticking to cookie-less backup server |
| 7556 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7557 | * empty cookies and mark them as invalid. |
| 7558 | * The same behaviour is applied when persistence must be ignored. |
| 7559 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7560 | if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7561 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7562 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7563 | while (srv) { |
| 7564 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7565 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7566 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7567 | (s->be->options & PR_O_PERSIST) || |
| 7568 | (s->flags & SN_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7569 | /* we found the server and we can use it */ |
| 7570 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7571 | 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] | 7572 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 7573 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7574 | break; |
| 7575 | } else { |
| 7576 | /* we found a server, but it's down, |
| 7577 | * mark it as such and go on in case |
| 7578 | * another one is available. |
| 7579 | */ |
| 7580 | txn->flags &= ~TX_CK_MASK; |
| 7581 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7582 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7583 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7584 | srv = srv->next; |
| 7585 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7586 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7587 | 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] | 7588 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7589 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7590 | if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7591 | txn->flags |= TX_CK_UNUSED; |
| 7592 | else |
| 7593 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7594 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7595 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7596 | /* depending on the cookie mode, we may have to either : |
| 7597 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7598 | * the server never sees it ; |
| 7599 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7600 | * application cookie so that it does not get accidentely removed later, |
| 7601 | * if we're in cookie prefix mode |
| 7602 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7603 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7604 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7605 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7606 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7607 | val_end += delta; |
| 7608 | next += delta; |
| 7609 | hdr_end += delta; |
| 7610 | hdr_next += delta; |
| 7611 | cur_hdr->len += delta; |
| 7612 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7613 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7614 | del_from = NULL; |
| 7615 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7616 | } |
| 7617 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7618 | (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] | 7619 | del_from = prev; |
| 7620 | } |
| 7621 | } else { |
| 7622 | /* This is not our cookie, so we must preserve it. But if we already |
| 7623 | * scheduled another cookie for removal, we cannot remove the |
| 7624 | * complete header, but we can remove the previous block itself. |
| 7625 | */ |
| 7626 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7627 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7628 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7629 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7630 | if (att_beg >= del_from) |
| 7631 | att_beg += delta; |
| 7632 | if (att_end >= del_from) |
| 7633 | att_end += delta; |
| 7634 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7635 | val_end += delta; |
| 7636 | next += delta; |
| 7637 | hdr_end += delta; |
| 7638 | hdr_next += delta; |
| 7639 | cur_hdr->len += delta; |
| 7640 | http_msg_move_end(&txn->req, delta); |
| 7641 | prev = del_from; |
| 7642 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7643 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7644 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7645 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7646 | /* Look for the appsession cookie unless persistence must be ignored */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7647 | if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7648 | int cmp_len, value_len; |
| 7649 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7650 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7651 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 7652 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 7653 | value_begin = att_beg + s->be->appsession_name_len; |
| 7654 | value_len = val_end - att_beg - s->be->appsession_name_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7655 | } else { |
| 7656 | cmp_len = att_end - att_beg; |
| 7657 | value_begin = val_beg; |
| 7658 | value_len = val_end - val_beg; |
| 7659 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7660 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7661 | /* let's see if the cookie is our appcookie */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7662 | if (cmp_len == s->be->appsession_name_len && |
| 7663 | memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) { |
| 7664 | manage_client_side_appsession(s, value_begin, value_len); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7665 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7666 | } |
| 7667 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7668 | /* continue with next cookie on this header line */ |
| 7669 | att_beg = next; |
| 7670 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7671 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7672 | /* There are no more cookies on this line. |
| 7673 | * We may still have one (or several) marked for deletion at the |
| 7674 | * end of the line. We must do this now in two ways : |
| 7675 | * - if some cookies must be preserved, we only delete from the |
| 7676 | * mark to the end of line ; |
| 7677 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7678 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7679 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7680 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7681 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7682 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7683 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7684 | cur_hdr->len += delta; |
| 7685 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7686 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7687 | |
| 7688 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7689 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7690 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7691 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7692 | cur_idx = old_idx; |
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 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7695 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7696 | } |
| 7697 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7698 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7699 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7700 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7701 | } |
| 7702 | |
| 7703 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7704 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7705 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7706 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7707 | 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] | 7708 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7709 | char *cur_ptr, *cur_end, *cur_next; |
| 7710 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7711 | struct http_txn *txn = &s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7712 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7713 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7714 | |
| 7715 | last_hdr = 0; |
| 7716 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7717 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7718 | old_idx = 0; |
| 7719 | |
| 7720 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7721 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7722 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7723 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7724 | (exp->action == ACT_ALLOW || |
| 7725 | exp->action == ACT_DENY)) |
| 7726 | return 0; |
| 7727 | |
| 7728 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7729 | if (!cur_idx) |
| 7730 | break; |
| 7731 | |
| 7732 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7733 | cur_ptr = cur_next; |
| 7734 | cur_end = cur_ptr + cur_hdr->len; |
| 7735 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7736 | |
| 7737 | /* Now we have one header between cur_ptr and cur_end, |
| 7738 | * and the next header starts at cur_next. |
| 7739 | */ |
| 7740 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 7741 | 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] | 7742 | switch (exp->action) { |
| 7743 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7744 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7745 | last_hdr = 1; |
| 7746 | break; |
| 7747 | |
| 7748 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7749 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7750 | last_hdr = 1; |
| 7751 | break; |
| 7752 | |
| 7753 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7754 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7755 | if (trash.len < 0) |
| 7756 | return -1; |
| 7757 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7758 | 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] | 7759 | /* FIXME: if the user adds a newline in the replacement, the |
| 7760 | * index will not be recalculated for now, and the new line |
| 7761 | * will not be counted as a new header. |
| 7762 | */ |
| 7763 | |
| 7764 | cur_end += delta; |
| 7765 | cur_next += delta; |
| 7766 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7767 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7768 | break; |
| 7769 | |
| 7770 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7771 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7772 | cur_next += delta; |
| 7773 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7774 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7775 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7776 | txn->hdr_idx.used--; |
| 7777 | cur_hdr->len = 0; |
| 7778 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7779 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7780 | break; |
| 7781 | |
| 7782 | } |
| 7783 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7784 | |
| 7785 | /* keep the link from this header to next one in case of later |
| 7786 | * removal of next header. |
| 7787 | */ |
| 7788 | old_idx = cur_idx; |
| 7789 | } |
| 7790 | return 0; |
| 7791 | } |
| 7792 | |
| 7793 | |
| 7794 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7795 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7796 | * or -1 if a replacement resulted in an invalid status line. |
| 7797 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7798 | 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] | 7799 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7800 | char *cur_ptr, *cur_end; |
| 7801 | int done; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7802 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7803 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7804 | |
| 7805 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7806 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7807 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7808 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7809 | (exp->action == ACT_ALLOW || |
| 7810 | exp->action == ACT_DENY)) |
| 7811 | return 0; |
| 7812 | else if (exp->action == ACT_REMOVE) |
| 7813 | return 0; |
| 7814 | |
| 7815 | done = 0; |
| 7816 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7817 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7818 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7819 | |
| 7820 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7821 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 7822 | 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] | 7823 | switch (exp->action) { |
| 7824 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7825 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7826 | done = 1; |
| 7827 | break; |
| 7828 | |
| 7829 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7830 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7831 | done = 1; |
| 7832 | break; |
| 7833 | |
| 7834 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7835 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7836 | if (trash.len < 0) |
| 7837 | return -1; |
| 7838 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7839 | 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] | 7840 | /* FIXME: if the user adds a newline in the replacement, the |
| 7841 | * index will not be recalculated for now, and the new line |
| 7842 | * will not be counted as a new header. |
| 7843 | */ |
| 7844 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7845 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7846 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7847 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7848 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7849 | cur_ptr, cur_end + 1, |
| 7850 | NULL, NULL); |
| 7851 | if (unlikely(!cur_end)) |
| 7852 | return -1; |
| 7853 | |
| 7854 | /* we have a full respnse and we know that we have either a CR |
| 7855 | * or an LF at <ptr>. |
| 7856 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7857 | 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] | 7858 | 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] | 7859 | /* there is no point trying this regex on headers */ |
| 7860 | return 1; |
| 7861 | } |
| 7862 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7863 | return done; |
| 7864 | } |
| 7865 | |
| 7866 | |
| 7867 | |
| 7868 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7869 | * 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] | 7870 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7871 | * unparsable response. |
| 7872 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7873 | 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] | 7874 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7875 | struct http_txn *txn = &s->txn; |
| 7876 | struct hdr_exp *exp; |
| 7877 | |
| 7878 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7879 | int ret; |
| 7880 | |
| 7881 | /* |
| 7882 | * The interleaving of transformations and verdicts |
| 7883 | * makes it difficult to decide to continue or stop |
| 7884 | * the evaluation. |
| 7885 | */ |
| 7886 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7887 | if (txn->flags & TX_SVDENY) |
| 7888 | break; |
| 7889 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7890 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7891 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7892 | exp->action == ACT_PASS)) { |
| 7893 | exp = exp->next; |
| 7894 | continue; |
| 7895 | } |
| 7896 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7897 | /* if this filter had a condition, evaluate it now and skip to |
| 7898 | * next filter if the condition does not match. |
| 7899 | */ |
| 7900 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7901 | 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] | 7902 | ret = acl_pass(ret); |
| 7903 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7904 | ret = !ret; |
| 7905 | if (!ret) |
| 7906 | continue; |
| 7907 | } |
| 7908 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7909 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7910 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7911 | if (unlikely(ret < 0)) |
| 7912 | return -1; |
| 7913 | |
| 7914 | if (likely(ret == 0)) { |
| 7915 | /* The filter did not match the response, it can be |
| 7916 | * iterated through all headers. |
| 7917 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7918 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 7919 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7920 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7921 | } |
| 7922 | return 0; |
| 7923 | } |
| 7924 | |
| 7925 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7926 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7927 | * 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] | 7928 | * desirable to call it only when needed. This function is also used when we |
| 7929 | * 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] | 7930 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7931 | void manage_server_side_cookies(struct session *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7932 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7933 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7934 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7935 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7936 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7937 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7938 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7939 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7940 | /* Iterate through the headers. |
| 7941 | * we start with the start line. |
| 7942 | */ |
| 7943 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7944 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7945 | |
| 7946 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7947 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7948 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7949 | |
| 7950 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7951 | hdr_beg = hdr_next; |
| 7952 | hdr_end = hdr_beg + cur_hdr->len; |
| 7953 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7954 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7955 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7956 | * next header starts at hdr_next. We're only interested in |
| 7957 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7958 | */ |
| 7959 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7960 | is_cookie2 = 0; |
| 7961 | prev = hdr_beg + 10; |
| 7962 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7963 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7964 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7965 | if (!val) { |
| 7966 | old_idx = cur_idx; |
| 7967 | continue; |
| 7968 | } |
| 7969 | is_cookie2 = 1; |
| 7970 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7971 | } |
| 7972 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7973 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7974 | * <prev> points to the colon. |
| 7975 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7976 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7977 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7978 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 7979 | * check-cache is enabled) and we are not interested in checking |
| 7980 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7981 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7982 | if (s->be->cookie_name == NULL && |
| 7983 | s->be->appsession_name == NULL && |
| 7984 | s->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7985 | return; |
| 7986 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7987 | /* OK so now we know we have to process this response cookie. |
| 7988 | * The format of the Set-Cookie header is slightly different |
| 7989 | * from the format of the Cookie header in that it does not |
| 7990 | * support the comma as a cookie delimiter (thus the header |
| 7991 | * cannot be folded) because the Expires attribute described in |
| 7992 | * the original Netscape's spec may contain an unquoted date |
| 7993 | * with a comma inside. We have to live with this because |
| 7994 | * many browsers don't support Max-Age and some browsers don't |
| 7995 | * support quoted strings. However the Set-Cookie2 header is |
| 7996 | * clean. |
| 7997 | * |
| 7998 | * We have to keep multiple pointers in order to support cookie |
| 7999 | * removal at the beginning, middle or end of header without |
| 8000 | * corrupting the header (in case of set-cookie2). A special |
| 8001 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8002 | * fields after the first semi-colon. The <next> pointer points |
| 8003 | * either to the end of line (set-cookie) or next unquoted comma |
| 8004 | * (set-cookie2). All of these headers are valid : |
| 8005 | * |
| 8006 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8007 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8008 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8009 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8010 | * | | | | | | | | | | |
| 8011 | * | | | | | | | | +-> next hdr_end <--+ |
| 8012 | * | | | | | | | +------------> scav |
| 8013 | * | | | | | | +--------------> val_end |
| 8014 | * | | | | | +--------------------> val_beg |
| 8015 | * | | | | +----------------------> equal |
| 8016 | * | | | +------------------------> att_end |
| 8017 | * | | +----------------------------> att_beg |
| 8018 | * | +------------------------------> prev |
| 8019 | * +-----------------------------------------> hdr_beg |
| 8020 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8021 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8022 | for (; prev < hdr_end; prev = next) { |
| 8023 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8024 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8025 | /* find att_beg */ |
| 8026 | att_beg = prev + 1; |
| 8027 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8028 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8029 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8030 | /* find att_end : this is the first character after the last non |
| 8031 | * space before the equal. It may be equal to hdr_end. |
| 8032 | */ |
| 8033 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8034 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8035 | while (equal < hdr_end) { |
| 8036 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8037 | break; |
| 8038 | if (http_is_spht[(unsigned char)*equal++]) |
| 8039 | continue; |
| 8040 | att_end = equal; |
| 8041 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8042 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8043 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8044 | * is between <att_beg> and <equal>, both may be identical. |
| 8045 | */ |
| 8046 | |
| 8047 | /* look for end of cookie if there is an equal sign */ |
| 8048 | if (equal < hdr_end && *equal == '=') { |
| 8049 | /* look for the beginning of the value */ |
| 8050 | val_beg = equal + 1; |
| 8051 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8052 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8053 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8054 | /* find the end of the value, respecting quotes */ |
| 8055 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8056 | |
| 8057 | /* make val_end point to the first white space or delimitor after the value */ |
| 8058 | val_end = next; |
| 8059 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8060 | val_end--; |
| 8061 | } else { |
| 8062 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8063 | val_beg = val_end = next = equal; |
| 8064 | } |
| 8065 | |
| 8066 | if (next < hdr_end) { |
| 8067 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8068 | * a colon or semi-colon before the end. So skip all attr-value |
| 8069 | * pairs and look for the next comma. For Set-Cookie, since |
| 8070 | * commas are permitted in values, skip to the end. |
| 8071 | */ |
| 8072 | if (is_cookie2) |
| 8073 | next = find_hdr_value_end(next, hdr_end); |
| 8074 | else |
| 8075 | next = hdr_end; |
| 8076 | } |
| 8077 | |
| 8078 | /* Now everything is as on the diagram above */ |
| 8079 | |
| 8080 | /* Ignore cookies with no equal sign */ |
| 8081 | if (equal == val_end) |
| 8082 | continue; |
| 8083 | |
| 8084 | /* If there are spaces around the equal sign, we need to |
| 8085 | * strip them otherwise we'll get trouble for cookie captures, |
| 8086 | * or even for rewrites. Since this happens extremely rarely, |
| 8087 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8088 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8089 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8090 | int stripped_before = 0; |
| 8091 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8092 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8093 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8094 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8095 | equal += stripped_before; |
| 8096 | val_beg += stripped_before; |
| 8097 | } |
| 8098 | |
| 8099 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8100 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8101 | val_beg += stripped_after; |
| 8102 | stripped_before += stripped_after; |
| 8103 | } |
| 8104 | |
| 8105 | val_end += stripped_before; |
| 8106 | next += stripped_before; |
| 8107 | hdr_end += stripped_before; |
| 8108 | hdr_next += stripped_before; |
| 8109 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8110 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8111 | } |
| 8112 | |
| 8113 | /* First, let's see if we want to capture this cookie. We check |
| 8114 | * that we don't already have a server side cookie, because we |
| 8115 | * can only capture one. Also as an optimisation, we ignore |
| 8116 | * cookies shorter than the declared name. |
| 8117 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8118 | if (s->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8119 | txn->srv_cookie == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8120 | (val_end - att_beg >= s->fe->capture_namelen) && |
| 8121 | memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8122 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8123 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8124 | Alert("HTTP logging : out of memory.\n"); |
| 8125 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8126 | else { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8127 | if (log_len > s->fe->capture_len) |
| 8128 | log_len = s->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8129 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8130 | txn->srv_cookie[log_len] = 0; |
| 8131 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8132 | } |
| 8133 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8134 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8135 | /* now check if we need to process it for persistence */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8136 | if (!(s->flags & SN_IGNORE_PRST) && |
| 8137 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8138 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8139 | /* assume passive cookie by default */ |
| 8140 | txn->flags &= ~TX_SCK_MASK; |
| 8141 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8142 | |
| 8143 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8144 | * this occurrence because we'll insert another one later. |
| 8145 | * 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] | 8146 | * a direct access. |
| 8147 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8148 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8149 | /* The "preserve" flag was set, we don't want to touch the |
| 8150 | * server's cookie. |
| 8151 | */ |
| 8152 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8153 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 8154 | ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8155 | /* this cookie must be deleted */ |
| 8156 | if (*prev == ':' && next == hdr_end) { |
| 8157 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8158 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8159 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8160 | txn->hdr_idx.used--; |
| 8161 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8162 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8163 | hdr_next += delta; |
| 8164 | http_msg_move_end(&txn->rsp, delta); |
| 8165 | /* note: while both invalid now, <next> and <hdr_end> |
| 8166 | * are still equal, so the for() will stop as expected. |
| 8167 | */ |
| 8168 | } else { |
| 8169 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8170 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8171 | next = prev; |
| 8172 | hdr_end += delta; |
| 8173 | hdr_next += delta; |
| 8174 | cur_hdr->len += delta; |
| 8175 | http_msg_move_end(&txn->rsp, delta); |
| 8176 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8177 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8178 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8179 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8180 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8181 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8182 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8183 | * with this server since we know it. |
| 8184 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8185 | 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] | 8186 | next += delta; |
| 8187 | hdr_end += delta; |
| 8188 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8189 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8190 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8191 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8192 | txn->flags &= ~TX_SCK_MASK; |
| 8193 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8194 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8195 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8196 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8197 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8198 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8199 | 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] | 8200 | next += delta; |
| 8201 | hdr_end += delta; |
| 8202 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8203 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8204 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8205 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8206 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8207 | txn->flags &= ~TX_SCK_MASK; |
| 8208 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8209 | } |
| 8210 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 8211 | /* 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] | 8212 | else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8213 | int cmp_len, value_len; |
| 8214 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8215 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8216 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 8217 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 8218 | value_begin = att_beg + s->be->appsession_name_len; |
| 8219 | 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] | 8220 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8221 | cmp_len = att_end - att_beg; |
| 8222 | value_begin = val_beg; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8223 | value_len = MIN(s->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8224 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8225 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8226 | if ((cmp_len == s->be->appsession_name_len) && |
| 8227 | (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] | 8228 | /* free a possibly previously allocated memory */ |
| 8229 | pool_free2(apools.sessid, txn->sessid); |
| 8230 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8231 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8232 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8233 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8234 | 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] | 8235 | return; |
| 8236 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8237 | memcpy(txn->sessid, value_begin, value_len); |
| 8238 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8239 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8240 | } |
| 8241 | /* that's done for this cookie, check the next one on the same |
| 8242 | * line when next != hdr_end (only if is_cookie2). |
| 8243 | */ |
| 8244 | } |
| 8245 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8246 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8247 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8248 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8249 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8250 | appsess *asession = NULL; |
| 8251 | /* only do insert, if lookup fails */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8252 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8253 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 8254 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8255 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 8256 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8257 | 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] | 8258 | return; |
| 8259 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8260 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 8261 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8262 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 8263 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8264 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8265 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8266 | return; |
| 8267 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8268 | memcpy(asession->sessid, txn->sessid, s->be->appsession_len); |
| 8269 | asession->sessid[s->be->appsession_len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8270 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8271 | server_id_len = strlen(objt_server(s->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8272 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8273 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8274 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8275 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8276 | return; |
| 8277 | } |
| 8278 | asession->serverid[0] = '\0'; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8279 | memcpy(asession->serverid, objt_server(s->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8280 | |
| 8281 | asession->request_count = 0; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8282 | appsession_hash_insert(&(s->be->htbl_proxy), asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8283 | } |
| 8284 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8285 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8286 | asession->request_count++; |
| 8287 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8288 | } |
| 8289 | |
| 8290 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8291 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8292 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8293 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8294 | void check_response_for_cacheability(struct session *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8295 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8296 | struct http_txn *txn = &s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8297 | char *p1, *p2; |
| 8298 | |
| 8299 | char *cur_ptr, *cur_end, *cur_next; |
| 8300 | int cur_idx; |
| 8301 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8302 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8303 | return; |
| 8304 | |
| 8305 | /* Iterate through the headers. |
| 8306 | * we start with the start line. |
| 8307 | */ |
| 8308 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8309 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8310 | |
| 8311 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8312 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8313 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8314 | |
| 8315 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8316 | cur_ptr = cur_next; |
| 8317 | cur_end = cur_ptr + cur_hdr->len; |
| 8318 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8319 | |
| 8320 | /* We have one full header between cur_ptr and cur_end, and the |
| 8321 | * next header starts at cur_next. We're only interested in |
| 8322 | * "Cookie:" headers. |
| 8323 | */ |
| 8324 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8325 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8326 | if (val) { |
| 8327 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8328 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8329 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8330 | return; |
| 8331 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8332 | } |
| 8333 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8334 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8335 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8336 | continue; |
| 8337 | |
| 8338 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8339 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8340 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8341 | |
| 8342 | if (p1 >= cur_end) /* no more info */ |
| 8343 | continue; |
| 8344 | |
| 8345 | /* p1 is at the beginning of the value */ |
| 8346 | p2 = p1; |
| 8347 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8348 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8349 | p2++; |
| 8350 | |
| 8351 | /* we have a complete value between p1 and p2 */ |
| 8352 | if (p2 < cur_end && *p2 == '=') { |
| 8353 | /* we have something of the form no-cache="set-cookie" */ |
| 8354 | if ((cur_end - p1 >= 21) && |
| 8355 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8356 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8357 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8358 | continue; |
| 8359 | } |
| 8360 | |
| 8361 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8362 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8363 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8364 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8365 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8366 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8367 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8368 | return; |
| 8369 | } |
| 8370 | |
| 8371 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8372 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8373 | continue; |
| 8374 | } |
| 8375 | } |
| 8376 | } |
| 8377 | |
| 8378 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8379 | /* |
| 8380 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 8381 | * If the server is found, it's assigned to the session. |
| 8382 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8383 | 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] | 8384 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8385 | char *end_params, *first_param, *cur_param, *next_param; |
| 8386 | char separator; |
| 8387 | int value_len; |
| 8388 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8389 | int mode = s->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8390 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8391 | if (s->be->appsession_name == NULL || |
| 8392 | (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] | 8393 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8394 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8395 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8396 | first_param = NULL; |
| 8397 | switch (mode) { |
| 8398 | case PR_O2_AS_M_PP: |
| 8399 | first_param = memchr(begin, ';', len); |
| 8400 | break; |
| 8401 | case PR_O2_AS_M_QS: |
| 8402 | first_param = memchr(begin, '?', len); |
| 8403 | break; |
| 8404 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8405 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8406 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8407 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8408 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8409 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8410 | switch (mode) { |
| 8411 | case PR_O2_AS_M_PP: |
| 8412 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 8413 | end_params = (char *) begin + len; |
| 8414 | } |
| 8415 | separator = ';'; |
| 8416 | break; |
| 8417 | case PR_O2_AS_M_QS: |
| 8418 | end_params = (char *) begin + len; |
| 8419 | separator = '&'; |
| 8420 | break; |
| 8421 | default: |
| 8422 | /* unknown mode, shouldn't happen */ |
| 8423 | return; |
| 8424 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8425 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8426 | cur_param = next_param = end_params; |
| 8427 | while (cur_param > first_param) { |
| 8428 | cur_param--; |
| 8429 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 8430 | /* let's see if this is the appsession parameter */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8431 | if ((cur_param + s->be->appsession_name_len + 1 < next_param) && |
| 8432 | ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') && |
| 8433 | (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] | 8434 | /* Cool... it's the right one */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8435 | cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 8436 | value_len = MIN(s->be->appsession_len, next_param - cur_param); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8437 | if (value_len > 0) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8438 | manage_client_side_appsession(s, cur_param, value_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8439 | } |
| 8440 | break; |
| 8441 | } |
| 8442 | next_param = cur_param; |
| 8443 | } |
| 8444 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8445 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 8446 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8447 | appsession_hash_dump(&(s->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8448 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8449 | } |
| 8450 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8451 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8452 | * 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] | 8453 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8454 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8455 | * 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] | 8456 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8457 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8458 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8459 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8460 | 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] | 8461 | { |
| 8462 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8463 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8464 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8465 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8466 | if (!uri_auth) |
| 8467 | return 0; |
| 8468 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8469 | 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] | 8470 | return 0; |
| 8471 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8472 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8473 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8474 | return 0; |
| 8475 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8476 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8477 | return 0; |
| 8478 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8479 | return 1; |
| 8480 | } |
| 8481 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8482 | /* |
| 8483 | * 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] | 8484 | * By default it tries to report the error position as msg->err_pos. However if |
| 8485 | * this one is not set, it will then report msg->next, which is the last known |
| 8486 | * 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] | 8487 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8488 | */ |
| 8489 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8490 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8491 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8492 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8493 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8494 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8495 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8496 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8497 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8498 | len1 = MIN(len1, es->len); |
| 8499 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8500 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8501 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8502 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8503 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8504 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8505 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8506 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8507 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8508 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8509 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8510 | es->when = date; // user-visible date |
| 8511 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8512 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8513 | es->oe = other_end; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8514 | if (objt_conn(s->req->prod->end)) |
| 8515 | es->src = __objt_conn(s->req->prod->end)->addr.from; |
| 8516 | else |
| 8517 | memset(&es->src, 0, sizeof(es->src)); |
| 8518 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8519 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8520 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8521 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8522 | es->s_flags = s->flags; |
| 8523 | es->t_flags = s->txn.flags; |
| 8524 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8525 | es->b_out = chn->buf->o; |
| 8526 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8527 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8528 | es->m_clen = msg->chunk_len; |
| 8529 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8530 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8531 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8532 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8533 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8534 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8535 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8536 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8537 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8538 | * 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] | 8539 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8540 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8541 | * 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] | 8542 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8543 | 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] | 8544 | struct hdr_idx *idx, int occ, |
| 8545 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8546 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8547 | struct hdr_ctx local_ctx; |
| 8548 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8549 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8550 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8551 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8552 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8553 | if (!ctx) { |
| 8554 | local_ctx.idx = 0; |
| 8555 | ctx = &local_ctx; |
| 8556 | } |
| 8557 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8558 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8559 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8560 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8561 | occ--; |
| 8562 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8563 | *vptr = ctx->line + ctx->val; |
| 8564 | *vlen = ctx->vlen; |
| 8565 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8566 | } |
| 8567 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8568 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8569 | } |
| 8570 | |
| 8571 | /* negative occurrence, we scan all the list then walk back */ |
| 8572 | if (-occ > MAX_HDR_HISTORY) |
| 8573 | return 0; |
| 8574 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8575 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8576 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8577 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8578 | len_hist[hist_ptr] = ctx->vlen; |
| 8579 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8580 | hist_ptr = 0; |
| 8581 | found++; |
| 8582 | } |
| 8583 | if (-occ > found) |
| 8584 | return 0; |
| 8585 | /* 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] | 8586 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8587 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8588 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8589 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8590 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8591 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8592 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8593 | *vptr = ptr_hist[hist_ptr]; |
| 8594 | *vlen = len_hist[hist_ptr]; |
| 8595 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8596 | } |
| 8597 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8598 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8599 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8600 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8601 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8602 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8603 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8604 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8605 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8606 | * line header values and does not stop at commas. |
| 8607 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8608 | */ |
| 8609 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8610 | struct hdr_idx *idx, int occ, |
| 8611 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8612 | { |
| 8613 | struct hdr_ctx local_ctx; |
| 8614 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8615 | int len_hist[MAX_HDR_HISTORY]; |
| 8616 | unsigned int hist_ptr; |
| 8617 | int found; |
| 8618 | |
| 8619 | if (!ctx) { |
| 8620 | local_ctx.idx = 0; |
| 8621 | ctx = &local_ctx; |
| 8622 | } |
| 8623 | |
| 8624 | if (occ >= 0) { |
| 8625 | /* search from the beginning */ |
| 8626 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8627 | occ--; |
| 8628 | if (occ <= 0) { |
| 8629 | *vptr = ctx->line + ctx->val; |
| 8630 | *vlen = ctx->vlen; |
| 8631 | return 1; |
| 8632 | } |
| 8633 | } |
| 8634 | return 0; |
| 8635 | } |
| 8636 | |
| 8637 | /* negative occurrence, we scan all the list then walk back */ |
| 8638 | if (-occ > MAX_HDR_HISTORY) |
| 8639 | return 0; |
| 8640 | |
| 8641 | found = hist_ptr = 0; |
| 8642 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8643 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8644 | len_hist[hist_ptr] = ctx->vlen; |
| 8645 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8646 | hist_ptr = 0; |
| 8647 | found++; |
| 8648 | } |
| 8649 | if (-occ > found) |
| 8650 | return 0; |
| 8651 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8652 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8653 | */ |
| 8654 | hist_ptr += occ; |
| 8655 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8656 | hist_ptr -= MAX_HDR_HISTORY; |
| 8657 | *vptr = ptr_hist[hist_ptr]; |
| 8658 | *vlen = len_hist[hist_ptr]; |
| 8659 | return 1; |
| 8660 | } |
| 8661 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8662 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8663 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8664 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8665 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8666 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8667 | 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] | 8668 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8669 | int max; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8670 | 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] | 8671 | dir, |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8672 | objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1, |
| 8673 | 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] | 8674 | |
| 8675 | for (max = 0; start + max < end; max++) |
| 8676 | if (start[max] == '\r' || start[max] == '\n') |
| 8677 | break; |
| 8678 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8679 | UBOUND(max, trash.size - trash.len - 3); |
| 8680 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8681 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8682 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8683 | } |
| 8684 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8685 | /* |
| 8686 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 8687 | * the required fields are properly allocated and that we only need to (re)init |
| 8688 | * them. This should be used before processing any new request. |
| 8689 | */ |
| 8690 | void http_init_txn(struct session *s) |
| 8691 | { |
| 8692 | struct http_txn *txn = &s->txn; |
| 8693 | struct proxy *fe = s->fe; |
| 8694 | |
| 8695 | txn->flags = 0; |
| 8696 | txn->status = -1; |
| 8697 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8698 | txn->cookie_first_date = 0; |
| 8699 | txn->cookie_last_date = 0; |
| 8700 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8701 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8702 | 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] | 8703 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8704 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8705 | 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] | 8706 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 8707 | txn->req.chunk_len = 0LL; |
| 8708 | txn->req.body_len = 0LL; |
| 8709 | txn->rsp.chunk_len = 0LL; |
| 8710 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8711 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8712 | 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] | 8713 | txn->req.chn = s->req; |
| 8714 | txn->rsp.chn = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8715 | |
| 8716 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8717 | |
| 8718 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8719 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8720 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8721 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8722 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8723 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 8724 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8725 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8726 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 8727 | |
| 8728 | if (txn->hdr_idx.v) |
| 8729 | hdr_idx_init(&txn->hdr_idx); |
| 8730 | } |
| 8731 | |
| 8732 | /* to be used at the end of a transaction */ |
| 8733 | void http_end_txn(struct session *s) |
| 8734 | { |
| 8735 | struct http_txn *txn = &s->txn; |
| 8736 | |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8737 | /* release any possible compression context */ |
| 8738 | if (s->flags & SN_COMP_READY) |
| 8739 | s->comp_algo->end(&s->comp_ctx); |
| 8740 | s->comp_algo = NULL; |
| 8741 | s->flags &= ~SN_COMP_READY; |
| 8742 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8743 | /* these ones will have been dynamically allocated */ |
| 8744 | pool_free2(pool2_requri, txn->uri); |
| 8745 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8746 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8747 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8748 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8749 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8750 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8751 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8752 | txn->uri = NULL; |
| 8753 | txn->srv_cookie = NULL; |
| 8754 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8755 | |
| 8756 | if (txn->req.cap) { |
| 8757 | struct cap_hdr *h; |
| 8758 | for (h = s->fe->req_cap; h; h = h->next) |
| 8759 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 8760 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 8761 | } |
| 8762 | |
| 8763 | if (txn->rsp.cap) { |
| 8764 | struct cap_hdr *h; |
| 8765 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 8766 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 8767 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 8768 | } |
| 8769 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8770 | } |
| 8771 | |
| 8772 | /* to be used at the end of a transaction to prepare a new one */ |
| 8773 | void http_reset_txn(struct session *s) |
| 8774 | { |
| 8775 | http_end_txn(s); |
| 8776 | http_init_txn(s); |
| 8777 | |
| 8778 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8779 | s->logs.logwait = s->fe->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8780 | s->logs.level = 0; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 8781 | session_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8782 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8783 | /* re-init store persistence */ |
| 8784 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8785 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8786 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8787 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8788 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 8789 | s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8790 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8791 | /* We must trim any excess data from the response buffer, because we |
| 8792 | * may have blocked an invalid response from a server that we don't |
| 8793 | * want to accidentely forward once we disable the analysers, nor do |
| 8794 | * we want those data to come along with next response. A typical |
| 8795 | * example of such data would be from a buggy server responding to |
| 8796 | * a HEAD with some data, or sending more than the advertised |
| 8797 | * content-length. |
| 8798 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8799 | if (unlikely(s->rep->buf->i)) |
| 8800 | s->rep->buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8801 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8802 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8803 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8804 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8805 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8806 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8807 | |
| 8808 | s->req->rex = TICK_ETERNITY; |
| 8809 | s->req->wex = TICK_ETERNITY; |
| 8810 | s->req->analyse_exp = TICK_ETERNITY; |
| 8811 | s->rep->rex = TICK_ETERNITY; |
| 8812 | s->rep->wex = TICK_ETERNITY; |
| 8813 | s->rep->analyse_exp = TICK_ETERNITY; |
| 8814 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8815 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8816 | void free_http_res_rules(struct list *r) |
| 8817 | { |
| 8818 | struct http_res_rule *tr, *pr; |
| 8819 | |
| 8820 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8821 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8822 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8823 | free(pr); |
| 8824 | } |
| 8825 | } |
| 8826 | |
| 8827 | void free_http_req_rules(struct list *r) |
| 8828 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8829 | struct http_req_rule *tr, *pr; |
| 8830 | |
| 8831 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8832 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8833 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8834 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8835 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8836 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8837 | free(pr); |
| 8838 | } |
| 8839 | } |
| 8840 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8841 | /* parse an "http-request" rule */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8842 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8843 | { |
| 8844 | struct http_req_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8845 | struct http_req_action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8846 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8847 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8848 | |
| 8849 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 8850 | if (!rule) { |
| 8851 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8852 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8853 | } |
| 8854 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8855 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8856 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 8857 | cur_arg = 1; |
Willy Tarreau | 5bd6759 | 2014-04-28 22:00:46 +0200 | [diff] [blame] | 8858 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8859 | rule->action = HTTP_REQ_ACT_DENY; |
| 8860 | cur_arg = 1; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8861 | } else if (!strcmp(args[0], "tarpit")) { |
| 8862 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 8863 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8864 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8865 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8866 | cur_arg = 1; |
| 8867 | |
| 8868 | while(*args[cur_arg]) { |
| 8869 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8870 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8871 | cur_arg+=2; |
| 8872 | continue; |
| 8873 | } else |
| 8874 | break; |
| 8875 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8876 | } else if (!strcmp(args[0], "set-nice")) { |
| 8877 | rule->action = HTTP_REQ_ACT_SET_NICE; |
| 8878 | cur_arg = 1; |
| 8879 | |
| 8880 | if (!*args[cur_arg] || |
| 8881 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8882 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8883 | file, linenum, args[0]); |
| 8884 | goto out_err; |
| 8885 | } |
| 8886 | rule->arg.nice = atoi(args[cur_arg]); |
| 8887 | if (rule->arg.nice < -1024) |
| 8888 | rule->arg.nice = -1024; |
| 8889 | else if (rule->arg.nice > 1024) |
| 8890 | rule->arg.nice = 1024; |
| 8891 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8892 | } else if (!strcmp(args[0], "set-tos")) { |
| 8893 | #ifdef IP_TOS |
| 8894 | char *err; |
| 8895 | rule->action = HTTP_REQ_ACT_SET_TOS; |
| 8896 | cur_arg = 1; |
| 8897 | |
| 8898 | if (!*args[cur_arg] || |
| 8899 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8900 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8901 | file, linenum, args[0]); |
| 8902 | goto out_err; |
| 8903 | } |
| 8904 | |
| 8905 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8906 | if (err && *err != '\0') { |
| 8907 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8908 | file, linenum, err, args[0]); |
| 8909 | goto out_err; |
| 8910 | } |
| 8911 | cur_arg++; |
| 8912 | #else |
| 8913 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8914 | goto out_err; |
| 8915 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8916 | } else if (!strcmp(args[0], "set-mark")) { |
| 8917 | #ifdef SO_MARK |
| 8918 | char *err; |
| 8919 | rule->action = HTTP_REQ_ACT_SET_MARK; |
| 8920 | cur_arg = 1; |
| 8921 | |
| 8922 | if (!*args[cur_arg] || |
| 8923 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8924 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8925 | file, linenum, args[0]); |
| 8926 | goto out_err; |
| 8927 | } |
| 8928 | |
| 8929 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8930 | if (err && *err != '\0') { |
| 8931 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8932 | file, linenum, err, args[0]); |
| 8933 | goto out_err; |
| 8934 | } |
| 8935 | cur_arg++; |
| 8936 | global.last_checks |= LSTCHK_NETADM; |
| 8937 | #else |
| 8938 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8939 | goto out_err; |
| 8940 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8941 | } else if (!strcmp(args[0], "set-log-level")) { |
| 8942 | rule->action = HTTP_REQ_ACT_SET_LOGL; |
| 8943 | cur_arg = 1; |
| 8944 | |
| 8945 | if (!*args[cur_arg] || |
| 8946 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8947 | bad_log_level: |
| 8948 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8949 | file, linenum, args[0]); |
| 8950 | goto out_err; |
| 8951 | } |
| 8952 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8953 | rule->arg.loglevel = -1; |
| 8954 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8955 | goto bad_log_level; |
| 8956 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8957 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 8958 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 8959 | cur_arg = 1; |
| 8960 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 8961 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8962 | (*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] | 8963 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8964 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8965 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8966 | } |
| 8967 | |
| 8968 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8969 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8970 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 8971 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8972 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 8973 | 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] | 8974 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 8975 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8976 | free(proxy->conf.lfs_file); |
| 8977 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8978 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8979 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 8980 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
| 8981 | 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] | 8982 | cur_arg = 1; |
| 8983 | |
| 8984 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 8985 | (*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] | 8986 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 8987 | file, linenum, args[0]); |
| 8988 | goto out_err; |
| 8989 | } |
| 8990 | |
| 8991 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8992 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8993 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8994 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8995 | error = NULL; |
| 8996 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 8997 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 8998 | args[cur_arg + 1], error); |
| 8999 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9000 | goto out_err; |
| 9001 | } |
| 9002 | |
| 9003 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9004 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9005 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9006 | file, linenum); |
| 9007 | |
| 9008 | free(proxy->conf.lfs_file); |
| 9009 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9010 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9011 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9012 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9013 | rule->action = HTTP_REQ_ACT_DEL_HDR; |
| 9014 | cur_arg = 1; |
| 9015 | |
| 9016 | if (!*args[cur_arg] || |
| 9017 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9018 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9019 | file, linenum, args[0]); |
| 9020 | goto out_err; |
| 9021 | } |
| 9022 | |
| 9023 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9024 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9025 | |
| 9026 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9027 | free(proxy->conf.lfs_file); |
| 9028 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9029 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9030 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9031 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9032 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 9033 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9034 | struct sample_expr *expr; |
| 9035 | unsigned int where; |
| 9036 | char *err = NULL; |
| 9037 | |
| 9038 | cur_arg = 1; |
| 9039 | proxy->conf.args.ctx = ARGC_TRK; |
| 9040 | |
| 9041 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9042 | if (!expr) { |
| 9043 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9044 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9045 | free(err); |
| 9046 | goto out_err; |
| 9047 | } |
| 9048 | |
| 9049 | where = 0; |
| 9050 | if (proxy->cap & PR_CAP_FE) |
| 9051 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9052 | if (proxy->cap & PR_CAP_BE) |
| 9053 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9054 | |
| 9055 | if (!(expr->fetch->val & where)) { |
| 9056 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9057 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9058 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9059 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9060 | free(expr); |
| 9061 | goto out_err; |
| 9062 | } |
| 9063 | |
| 9064 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9065 | cur_arg++; |
| 9066 | if (!args[cur_arg]) { |
| 9067 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 9068 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9069 | free(expr); |
| 9070 | goto out_err; |
| 9071 | } |
| 9072 | /* we copy the table name for now, it will be resolved later */ |
| 9073 | rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]); |
| 9074 | cur_arg++; |
| 9075 | } |
| 9076 | rule->act_prm.trk_ctr.expr = expr; |
| 9077 | rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0'; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9078 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9079 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9080 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9081 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9082 | 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] | 9083 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9084 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9085 | goto out_err; |
| 9086 | } |
| 9087 | |
| 9088 | /* this redirect rule might already contain a parsed condition which |
| 9089 | * we'll pass to the http-request rule. |
| 9090 | */ |
| 9091 | rule->action = HTTP_REQ_ACT_REDIR; |
| 9092 | rule->arg.redir = redir; |
| 9093 | rule->cond = redir->cond; |
| 9094 | redir->cond = NULL; |
| 9095 | cur_arg = 2; |
| 9096 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9097 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9098 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9099 | rule->action = HTTP_REQ_ACT_ADD_ACL; |
| 9100 | /* |
| 9101 | * '+ 8' for 'add-acl(' |
| 9102 | * '- 9' for 'add-acl(' + trailing ')' |
| 9103 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9104 | 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] | 9105 | |
| 9106 | cur_arg = 1; |
| 9107 | |
| 9108 | if (!*args[cur_arg] || |
| 9109 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9110 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9111 | file, linenum, args[0]); |
| 9112 | goto out_err; |
| 9113 | } |
| 9114 | |
| 9115 | LIST_INIT(&rule->arg.map.key); |
| 9116 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9117 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9118 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9119 | file, linenum); |
| 9120 | free(proxy->conf.lfs_file); |
| 9121 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9122 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9123 | cur_arg += 1; |
| 9124 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9125 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
| 9126 | rule->action = HTTP_REQ_ACT_DEL_ACL; |
| 9127 | /* |
| 9128 | * '+ 8' for 'del-acl(' |
| 9129 | * '- 9' for 'del-acl(' + trailing ')' |
| 9130 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9131 | 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] | 9132 | |
| 9133 | cur_arg = 1; |
| 9134 | |
| 9135 | if (!*args[cur_arg] || |
| 9136 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9137 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9138 | file, linenum, args[0]); |
| 9139 | goto out_err; |
| 9140 | } |
| 9141 | |
| 9142 | LIST_INIT(&rule->arg.map.key); |
| 9143 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9144 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9145 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9146 | file, linenum); |
| 9147 | free(proxy->conf.lfs_file); |
| 9148 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9149 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9150 | cur_arg += 1; |
| 9151 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9152 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
| 9153 | rule->action = HTTP_REQ_ACT_DEL_MAP; |
| 9154 | /* |
| 9155 | * '+ 8' for 'del-map(' |
| 9156 | * '- 9' for 'del-map(' + trailing ')' |
| 9157 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9158 | 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] | 9159 | |
| 9160 | cur_arg = 1; |
| 9161 | |
| 9162 | if (!*args[cur_arg] || |
| 9163 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9164 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9165 | file, linenum, args[0]); |
| 9166 | goto out_err; |
| 9167 | } |
| 9168 | |
| 9169 | LIST_INIT(&rule->arg.map.key); |
| 9170 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9171 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9172 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9173 | file, linenum); |
| 9174 | free(proxy->conf.lfs_file); |
| 9175 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9176 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9177 | cur_arg += 1; |
| 9178 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9179 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9180 | rule->action = HTTP_REQ_ACT_SET_MAP; |
| 9181 | /* |
| 9182 | * '+ 8' for 'set-map(' |
| 9183 | * '- 9' for 'set-map(' + trailing ')' |
| 9184 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9185 | 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] | 9186 | |
| 9187 | cur_arg = 1; |
| 9188 | |
| 9189 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9190 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9191 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9192 | file, linenum, args[0]); |
| 9193 | goto out_err; |
| 9194 | } |
| 9195 | |
| 9196 | LIST_INIT(&rule->arg.map.key); |
| 9197 | LIST_INIT(&rule->arg.map.value); |
| 9198 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9199 | |
| 9200 | /* key pattern */ |
| 9201 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9202 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9203 | file, linenum); |
| 9204 | |
| 9205 | /* value pattern */ |
| 9206 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9207 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9208 | file, linenum); |
| 9209 | free(proxy->conf.lfs_file); |
| 9210 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9211 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9212 | |
| 9213 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9214 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9215 | char *errmsg = NULL; |
| 9216 | cur_arg = 1; |
| 9217 | /* try in the module list */ |
| 9218 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9219 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9220 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9221 | free(errmsg); |
| 9222 | goto out_err; |
| 9223 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9224 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9225 | 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] | 9226 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9227 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9228 | } |
| 9229 | |
| 9230 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9231 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9232 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9233 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9234 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9235 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9236 | file, linenum, args[0], errmsg); |
| 9237 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9238 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9239 | } |
| 9240 | rule->cond = cond; |
| 9241 | } |
| 9242 | else if (*args[cur_arg]) { |
| 9243 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9244 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9245 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9246 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9247 | } |
| 9248 | |
| 9249 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9250 | out_err: |
| 9251 | free(rule); |
| 9252 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9253 | } |
| 9254 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9255 | /* parse an "http-respose" rule */ |
| 9256 | struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 9257 | { |
| 9258 | struct http_res_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9259 | struct http_res_action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9260 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9261 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9262 | |
| 9263 | rule = calloc(1, sizeof(*rule)); |
| 9264 | if (!rule) { |
| 9265 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9266 | goto out_err; |
| 9267 | } |
| 9268 | |
| 9269 | if (!strcmp(args[0], "allow")) { |
| 9270 | rule->action = HTTP_RES_ACT_ALLOW; |
| 9271 | cur_arg = 1; |
| 9272 | } else if (!strcmp(args[0], "deny")) { |
| 9273 | rule->action = HTTP_RES_ACT_DENY; |
| 9274 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9275 | } else if (!strcmp(args[0], "set-nice")) { |
| 9276 | rule->action = HTTP_RES_ACT_SET_NICE; |
| 9277 | cur_arg = 1; |
| 9278 | |
| 9279 | if (!*args[cur_arg] || |
| 9280 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9281 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9282 | file, linenum, args[0]); |
| 9283 | goto out_err; |
| 9284 | } |
| 9285 | rule->arg.nice = atoi(args[cur_arg]); |
| 9286 | if (rule->arg.nice < -1024) |
| 9287 | rule->arg.nice = -1024; |
| 9288 | else if (rule->arg.nice > 1024) |
| 9289 | rule->arg.nice = 1024; |
| 9290 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9291 | } else if (!strcmp(args[0], "set-tos")) { |
| 9292 | #ifdef IP_TOS |
| 9293 | char *err; |
| 9294 | rule->action = HTTP_RES_ACT_SET_TOS; |
| 9295 | cur_arg = 1; |
| 9296 | |
| 9297 | if (!*args[cur_arg] || |
| 9298 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9299 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9300 | file, linenum, args[0]); |
| 9301 | goto out_err; |
| 9302 | } |
| 9303 | |
| 9304 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9305 | if (err && *err != '\0') { |
| 9306 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9307 | file, linenum, err, args[0]); |
| 9308 | goto out_err; |
| 9309 | } |
| 9310 | cur_arg++; |
| 9311 | #else |
| 9312 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9313 | goto out_err; |
| 9314 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9315 | } else if (!strcmp(args[0], "set-mark")) { |
| 9316 | #ifdef SO_MARK |
| 9317 | char *err; |
| 9318 | rule->action = HTTP_RES_ACT_SET_MARK; |
| 9319 | cur_arg = 1; |
| 9320 | |
| 9321 | if (!*args[cur_arg] || |
| 9322 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9323 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9324 | file, linenum, args[0]); |
| 9325 | goto out_err; |
| 9326 | } |
| 9327 | |
| 9328 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9329 | if (err && *err != '\0') { |
| 9330 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9331 | file, linenum, err, args[0]); |
| 9332 | goto out_err; |
| 9333 | } |
| 9334 | cur_arg++; |
| 9335 | global.last_checks |= LSTCHK_NETADM; |
| 9336 | #else |
| 9337 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9338 | goto out_err; |
| 9339 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9340 | } else if (!strcmp(args[0], "set-log-level")) { |
| 9341 | rule->action = HTTP_RES_ACT_SET_LOGL; |
| 9342 | cur_arg = 1; |
| 9343 | |
| 9344 | if (!*args[cur_arg] || |
| 9345 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9346 | bad_log_level: |
| 9347 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9348 | file, linenum, args[0]); |
| 9349 | goto out_err; |
| 9350 | } |
| 9351 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9352 | rule->arg.loglevel = -1; |
| 9353 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 9354 | goto bad_log_level; |
| 9355 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9356 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9357 | rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR; |
| 9358 | cur_arg = 1; |
| 9359 | |
| 9360 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9361 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9362 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9363 | file, linenum, args[0]); |
| 9364 | goto out_err; |
| 9365 | } |
| 9366 | |
| 9367 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9368 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9369 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9370 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9371 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9372 | 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] | 9373 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9374 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9375 | free(proxy->conf.lfs_file); |
| 9376 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9377 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9378 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9379 | } 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] | 9380 | 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] | 9381 | cur_arg = 1; |
| 9382 | |
| 9383 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9384 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9385 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9386 | file, linenum, args[0]); |
| 9387 | goto out_err; |
| 9388 | } |
| 9389 | |
| 9390 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9391 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9392 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9393 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9394 | error = NULL; |
| 9395 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9396 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9397 | args[cur_arg + 1], error); |
| 9398 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9399 | goto out_err; |
| 9400 | } |
| 9401 | |
| 9402 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9403 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9404 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9405 | file, linenum); |
| 9406 | |
| 9407 | free(proxy->conf.lfs_file); |
| 9408 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9409 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9410 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9411 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9412 | rule->action = HTTP_RES_ACT_DEL_HDR; |
| 9413 | cur_arg = 1; |
| 9414 | |
| 9415 | if (!*args[cur_arg] || |
| 9416 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9417 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9418 | file, linenum, args[0]); |
| 9419 | goto out_err; |
| 9420 | } |
| 9421 | |
| 9422 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9423 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9424 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9425 | proxy->conf.args.ctx = ARGC_HRS; |
| 9426 | free(proxy->conf.lfs_file); |
| 9427 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9428 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9429 | cur_arg += 1; |
| 9430 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9431 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9432 | rule->action = HTTP_RES_ACT_ADD_ACL; |
| 9433 | /* |
| 9434 | * '+ 8' for 'add-acl(' |
| 9435 | * '- 9' for 'add-acl(' + trailing ')' |
| 9436 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9437 | 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] | 9438 | |
| 9439 | cur_arg = 1; |
| 9440 | |
| 9441 | if (!*args[cur_arg] || |
| 9442 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9443 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9444 | file, linenum, args[0]); |
| 9445 | goto out_err; |
| 9446 | } |
| 9447 | |
| 9448 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9449 | proxy->conf.args.ctx = ARGC_HRS; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9450 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9451 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9452 | file, linenum); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9453 | free(proxy->conf.lfs_file); |
| 9454 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9455 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9456 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9457 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9458 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9459 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
| 9460 | rule->action = HTTP_RES_ACT_DEL_ACL; |
| 9461 | /* |
| 9462 | * '+ 8' for 'del-acl(' |
| 9463 | * '- 9' for 'del-acl(' + trailing ')' |
| 9464 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9465 | 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] | 9466 | |
| 9467 | cur_arg = 1; |
| 9468 | |
| 9469 | if (!*args[cur_arg] || |
| 9470 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9471 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9472 | file, linenum, args[0]); |
| 9473 | goto out_err; |
| 9474 | } |
| 9475 | |
| 9476 | LIST_INIT(&rule->arg.map.key); |
| 9477 | proxy->conf.args.ctx = ARGC_HRS; |
| 9478 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9479 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9480 | file, linenum); |
| 9481 | free(proxy->conf.lfs_file); |
| 9482 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9483 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9484 | cur_arg += 1; |
| 9485 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9486 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
| 9487 | rule->action = HTTP_RES_ACT_DEL_MAP; |
| 9488 | /* |
| 9489 | * '+ 8' for 'del-map(' |
| 9490 | * '- 9' for 'del-map(' + trailing ')' |
| 9491 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9492 | 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] | 9493 | |
| 9494 | cur_arg = 1; |
| 9495 | |
| 9496 | if (!*args[cur_arg] || |
| 9497 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9498 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9499 | file, linenum, args[0]); |
| 9500 | goto out_err; |
| 9501 | } |
| 9502 | |
| 9503 | LIST_INIT(&rule->arg.map.key); |
| 9504 | proxy->conf.args.ctx = ARGC_HRS; |
| 9505 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9506 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9507 | file, linenum); |
| 9508 | free(proxy->conf.lfs_file); |
| 9509 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9510 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9511 | cur_arg += 1; |
| 9512 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9513 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9514 | rule->action = HTTP_RES_ACT_SET_MAP; |
| 9515 | /* |
| 9516 | * '+ 8' for 'set-map(' |
| 9517 | * '- 9' for 'set-map(' + trailing ')' |
| 9518 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9519 | 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] | 9520 | |
| 9521 | cur_arg = 1; |
| 9522 | |
| 9523 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9524 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9525 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9526 | file, linenum, args[0]); |
| 9527 | goto out_err; |
| 9528 | } |
| 9529 | |
| 9530 | LIST_INIT(&rule->arg.map.key); |
| 9531 | LIST_INIT(&rule->arg.map.value); |
| 9532 | |
| 9533 | proxy->conf.args.ctx = ARGC_HRS; |
| 9534 | |
| 9535 | /* key pattern */ |
| 9536 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9537 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9538 | file, linenum); |
| 9539 | |
| 9540 | /* value pattern */ |
| 9541 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9542 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9543 | file, linenum); |
| 9544 | |
| 9545 | free(proxy->conf.lfs_file); |
| 9546 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9547 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9548 | |
| 9549 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9550 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9551 | char *errmsg = NULL; |
| 9552 | cur_arg = 1; |
| 9553 | /* try in the module list */ |
| 9554 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9555 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9556 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9557 | free(errmsg); |
| 9558 | goto out_err; |
| 9559 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9560 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9561 | 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] | 9562 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 9563 | goto out_err; |
| 9564 | } |
| 9565 | |
| 9566 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9567 | struct acl_cond *cond; |
| 9568 | char *errmsg = NULL; |
| 9569 | |
| 9570 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9571 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9572 | file, linenum, args[0], errmsg); |
| 9573 | free(errmsg); |
| 9574 | goto out_err; |
| 9575 | } |
| 9576 | rule->cond = cond; |
| 9577 | } |
| 9578 | else if (*args[cur_arg]) { |
| 9579 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9580 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9581 | file, linenum, args[0], args[cur_arg]); |
| 9582 | goto out_err; |
| 9583 | } |
| 9584 | |
| 9585 | return rule; |
| 9586 | out_err: |
| 9587 | free(rule); |
| 9588 | return NULL; |
| 9589 | } |
| 9590 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9591 | /* 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] | 9592 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
| 9593 | * dynamic log-format rule instead of a static string. |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9594 | */ |
| 9595 | 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] | 9596 | const char **args, char **errmsg, int use_fmt) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9597 | { |
| 9598 | struct redirect_rule *rule; |
| 9599 | int cur_arg; |
| 9600 | int type = REDIRECT_TYPE_NONE; |
| 9601 | int code = 302; |
| 9602 | const char *destination = NULL; |
| 9603 | const char *cookie = NULL; |
| 9604 | int cookie_set = 0; |
| 9605 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9606 | struct acl_cond *cond = NULL; |
| 9607 | |
| 9608 | cur_arg = 0; |
| 9609 | while (*(args[cur_arg])) { |
| 9610 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9611 | if (!*args[cur_arg + 1]) |
| 9612 | goto missing_arg; |
| 9613 | |
| 9614 | type = REDIRECT_TYPE_LOCATION; |
| 9615 | cur_arg++; |
| 9616 | destination = args[cur_arg]; |
| 9617 | } |
| 9618 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9619 | if (!*args[cur_arg + 1]) |
| 9620 | goto missing_arg; |
| 9621 | |
| 9622 | type = REDIRECT_TYPE_PREFIX; |
| 9623 | cur_arg++; |
| 9624 | destination = args[cur_arg]; |
| 9625 | } |
| 9626 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9627 | if (!*args[cur_arg + 1]) |
| 9628 | goto missing_arg; |
| 9629 | |
| 9630 | type = REDIRECT_TYPE_SCHEME; |
| 9631 | cur_arg++; |
| 9632 | destination = args[cur_arg]; |
| 9633 | } |
| 9634 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9635 | if (!*args[cur_arg + 1]) |
| 9636 | goto missing_arg; |
| 9637 | |
| 9638 | cur_arg++; |
| 9639 | cookie = args[cur_arg]; |
| 9640 | cookie_set = 1; |
| 9641 | } |
| 9642 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9643 | if (!*args[cur_arg + 1]) |
| 9644 | goto missing_arg; |
| 9645 | |
| 9646 | cur_arg++; |
| 9647 | cookie = args[cur_arg]; |
| 9648 | cookie_set = 0; |
| 9649 | } |
| 9650 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9651 | if (!*args[cur_arg + 1]) |
| 9652 | goto missing_arg; |
| 9653 | |
| 9654 | cur_arg++; |
| 9655 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9656 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9657 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9658 | "'%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] | 9659 | args[cur_arg - 1], args[cur_arg]); |
| 9660 | return NULL; |
| 9661 | } |
| 9662 | } |
| 9663 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9664 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9665 | } |
| 9666 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9667 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9668 | } |
| 9669 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9670 | strcmp(args[cur_arg], "unless") == 0) { |
| 9671 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 9672 | if (!cond) { |
| 9673 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9674 | return NULL; |
| 9675 | } |
| 9676 | break; |
| 9677 | } |
| 9678 | else { |
| 9679 | memprintf(errmsg, |
| 9680 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9681 | args[cur_arg]); |
| 9682 | return NULL; |
| 9683 | } |
| 9684 | cur_arg++; |
| 9685 | } |
| 9686 | |
| 9687 | if (type == REDIRECT_TYPE_NONE) { |
| 9688 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9689 | return NULL; |
| 9690 | } |
| 9691 | |
| 9692 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 9693 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9694 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9695 | |
| 9696 | if (!use_fmt) { |
| 9697 | /* old-style static redirect rule */ |
| 9698 | rule->rdr_str = strdup(destination); |
| 9699 | rule->rdr_len = strlen(destination); |
| 9700 | } |
| 9701 | else { |
| 9702 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9703 | |
| 9704 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9705 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9706 | * makes it hard for the user to configure a self-redirection. |
| 9707 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9708 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9709 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9710 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9711 | (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9712 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9713 | free(curproxy->conf.lfs_file); |
| 9714 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9715 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9716 | } |
| 9717 | } |
| 9718 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9719 | if (cookie) { |
| 9720 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9721 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9722 | */ |
| 9723 | rule->cookie_len = strlen(cookie); |
| 9724 | if (cookie_set) { |
| 9725 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9726 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9727 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9728 | rule->cookie_len += 9; |
| 9729 | } else { |
| 9730 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9731 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9732 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9733 | rule->cookie_len += 20; |
| 9734 | } |
| 9735 | } |
| 9736 | rule->type = type; |
| 9737 | rule->code = code; |
| 9738 | rule->flags = flags; |
| 9739 | LIST_INIT(&rule->list); |
| 9740 | return rule; |
| 9741 | |
| 9742 | missing_arg: |
| 9743 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9744 | return NULL; |
| 9745 | } |
| 9746 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9747 | /************************************************************************/ |
| 9748 | /* The code below is dedicated to ACL parsing and matching */ |
| 9749 | /************************************************************************/ |
| 9750 | |
| 9751 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9752 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9753 | * which means that a request or response is ready. If some data is missing, |
| 9754 | * 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] | 9755 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9756 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9757 | * |
| 9758 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9759 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9760 | * decide whether or not an HTTP message is present ; |
| 9761 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9762 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9763 | * 1 if an HTTP message is ready |
| 9764 | */ |
| 9765 | static int |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9766 | 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] | 9767 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9768 | { |
| 9769 | struct http_txn *txn = l7; |
| 9770 | struct http_msg *msg = &txn->req; |
| 9771 | |
| 9772 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 9773 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 9774 | */ |
| 9775 | |
| 9776 | if (unlikely(!s || !txn)) |
| 9777 | return 0; |
| 9778 | |
| 9779 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9780 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9781 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9782 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9783 | if (unlikely(!s->req)) |
| 9784 | return 0; |
| 9785 | |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9786 | /* If the buffer does not leave enough free space at the end, |
| 9787 | * we must first realign it. |
| 9788 | */ |
| 9789 | if (s->req->buf->p > s->req->buf->data && |
| 9790 | s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite) |
| 9791 | buffer_slow_realign(s->req->buf); |
| 9792 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9793 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9794 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9795 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9796 | |
| 9797 | /* Try to decode HTTP request */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9798 | if (likely(msg->next < s->req->buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9799 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9800 | |
| 9801 | /* Still no valid request ? */ |
| 9802 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9803 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9804 | buffer_full(s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9805 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9806 | } |
| 9807 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9808 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9809 | return 0; |
| 9810 | } |
| 9811 | |
| 9812 | /* OK we just got a valid HTTP request. We have some minor |
| 9813 | * preparation to perform so that further checks can rely |
| 9814 | * on HTTP tests. |
| 9815 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9816 | |
| 9817 | /* If the request was parsed but was too large, we must absolutely |
| 9818 | * return an error so that it is not processed. At the moment this |
| 9819 | * cannot happen, but if the parsers are to change in the future, |
| 9820 | * we want this check to be maintained. |
| 9821 | */ |
| 9822 | if (unlikely(s->req->buf->i + s->req->buf->p > |
| 9823 | s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) { |
| 9824 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9825 | smp->data.uint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9826 | return 1; |
| 9827 | } |
| 9828 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9829 | 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] | 9830 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 9831 | s->flags |= SN_REDIRECTABLE; |
| 9832 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9833 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9834 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9835 | } |
| 9836 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9837 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9838 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9839 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9840 | |
| 9841 | /* otherwise everything's ready for the request */ |
| 9842 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9843 | else { |
| 9844 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9845 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9846 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9847 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9848 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9849 | } |
| 9850 | |
| 9851 | /* everything's OK */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9852 | smp->data.uint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9853 | return 1; |
| 9854 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9855 | |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 9856 | /* Note: these functinos *do* modify the sample. Even in case of success, at |
| 9857 | * least the type and uint value are modified. |
| 9858 | */ |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9859 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9860 | 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] | 9861 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9862 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9863 | 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] | 9864 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9865 | |
| 9866 | /* 1. Check on METHOD |
| 9867 | * We use the pre-parsed method if it is known, and store its number as an |
| 9868 | * integer. If it is unknown, we use the pointer and the length. |
| 9869 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9870 | 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] | 9871 | { |
| 9872 | int len, meth; |
| 9873 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9874 | len = strlen(text); |
| 9875 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9876 | |
| 9877 | pattern->val.i = meth; |
| 9878 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 9879 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9880 | pattern->len = len; |
| 9881 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9882 | else { |
| 9883 | pattern->ptr.str = NULL; |
| 9884 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9885 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9886 | return 1; |
| 9887 | } |
| 9888 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9889 | /* This function fetches the method of current HTTP request and stores |
| 9890 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 9891 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 9892 | * in <len> and <ptr> is NULL ; |
| 9893 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 9894 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9895 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9896 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9897 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9898 | 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] | 9899 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9900 | { |
| 9901 | int meth; |
| 9902 | struct http_txn *txn = l7; |
| 9903 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9904 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9905 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9906 | meth = txn->meth; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9907 | smp->type = SMP_T_METH; |
| 9908 | smp->data.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9909 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9910 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9911 | /* ensure the indexes are not affected */ |
| 9912 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9913 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9914 | smp->data.meth.str.len = txn->req.sl.rq.m_l; |
| 9915 | smp->data.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9916 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9917 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9918 | return 1; |
| 9919 | } |
| 9920 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9921 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9922 | 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] | 9923 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9924 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9925 | struct pattern_list *lst; |
| 9926 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9927 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9928 | list_for_each_entry(lst, &expr->patterns, list) { |
| 9929 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9930 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9931 | /* well-known method */ |
| 9932 | if (pattern->val.i != HTTP_METH_OTHER) { |
| 9933 | if (smp->data.meth.meth == pattern->val.i) |
| 9934 | return pattern; |
| 9935 | else |
| 9936 | continue; |
| 9937 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9938 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9939 | /* Other method, we must compare the strings */ |
| 9940 | if (pattern->len != smp->data.meth.str.len) |
| 9941 | continue; |
| 9942 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9943 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | 4de2a94 | 2014-08-28 20:42:57 +0200 | [diff] [blame] | 9944 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) || |
| 9945 | (!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] | 9946 | return pattern; |
| 9947 | } |
| 9948 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 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_rqver(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 | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9961 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9962 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9963 | |
| 9964 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9965 | if (len <= 0) |
| 9966 | return 0; |
| 9967 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9968 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9969 | smp->data.str.str = ptr; |
| 9970 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9971 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9972 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9973 | return 1; |
| 9974 | } |
| 9975 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9976 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9977 | 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] | 9978 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9979 | { |
| 9980 | struct http_txn *txn = l7; |
| 9981 | char *ptr; |
| 9982 | int len; |
| 9983 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9984 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9985 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9986 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9987 | return 0; |
| 9988 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9989 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9990 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9991 | |
| 9992 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9993 | if (len <= 0) |
| 9994 | return 0; |
| 9995 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9996 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9997 | smp->data.str.str = ptr; |
| 9998 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9999 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10000 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10001 | return 1; |
| 10002 | } |
| 10003 | |
| 10004 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10005 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10006 | 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] | 10007 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10008 | { |
| 10009 | struct http_txn *txn = l7; |
| 10010 | char *ptr; |
| 10011 | int len; |
| 10012 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10013 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10014 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10015 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10016 | return 0; |
| 10017 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10018 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10019 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10020 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10021 | smp->type = SMP_T_UINT; |
| 10022 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10023 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10024 | return 1; |
| 10025 | } |
| 10026 | |
| 10027 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10028 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10029 | 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] | 10030 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10031 | { |
| 10032 | struct http_txn *txn = l7; |
| 10033 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10034 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10035 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10036 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10037 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10038 | 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] | 10039 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10040 | return 1; |
| 10041 | } |
| 10042 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10043 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10044 | 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] | 10045 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10046 | { |
| 10047 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10048 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10049 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10050 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10051 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10052 | 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] | 10053 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10054 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10055 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10056 | smp->type = SMP_T_IPV4; |
| 10057 | smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10058 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10059 | return 1; |
| 10060 | } |
| 10061 | |
| 10062 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10063 | 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] | 10064 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10065 | { |
| 10066 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10067 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10068 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10069 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10070 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10071 | 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] | 10072 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10073 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10074 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10075 | smp->type = SMP_T_UINT; |
| 10076 | smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10077 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10078 | return 1; |
| 10079 | } |
| 10080 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10081 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10082 | * Accepts an optional argument of type string containing the header field name, |
| 10083 | * and an optional argument of type signed or unsigned integer to request an |
| 10084 | * 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] | 10085 | * headers are considered from the first one. It does not stop on commas and |
| 10086 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10087 | */ |
| 10088 | static int |
| 10089 | 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] | 10090 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10091 | { |
| 10092 | struct http_txn *txn = l7; |
| 10093 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10094 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
| 10095 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 10096 | int occ = 0; |
| 10097 | const char *name_str = NULL; |
| 10098 | int name_len = 0; |
| 10099 | |
| 10100 | if (!ctx) { |
| 10101 | /* first call */ |
| 10102 | ctx = &static_hdr_ctx; |
| 10103 | ctx->idx = 0; |
| 10104 | smp->ctx.a[0] = ctx; |
| 10105 | } |
| 10106 | |
| 10107 | if (args) { |
| 10108 | if (args[0].type != ARGT_STR) |
| 10109 | return 0; |
| 10110 | name_str = args[0].data.str.str; |
| 10111 | name_len = args[0].data.str.len; |
| 10112 | |
| 10113 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10114 | occ = args[1].data.uint; |
| 10115 | } |
| 10116 | |
| 10117 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10118 | |
| 10119 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10120 | /* search for header from the beginning */ |
| 10121 | ctx->idx = 0; |
| 10122 | |
| 10123 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10124 | /* no explicit occurrence and single fetch => last header by default */ |
| 10125 | occ = -1; |
| 10126 | |
| 10127 | if (!occ) |
| 10128 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10129 | smp->flags |= SMP_F_NOT_LAST; |
| 10130 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10131 | smp->type = SMP_T_STR; |
| 10132 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10133 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
| 10134 | return 1; |
| 10135 | |
| 10136 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10137 | return 0; |
| 10138 | } |
| 10139 | |
| 10140 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10141 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10142 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10143 | */ |
| 10144 | static int |
| 10145 | 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] | 10146 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10147 | { |
| 10148 | struct http_txn *txn = l7; |
| 10149 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10150 | struct hdr_ctx ctx; |
| 10151 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 10152 | int cnt; |
| 10153 | |
| 10154 | if (!args || args->type != ARGT_STR) |
| 10155 | return 0; |
| 10156 | |
| 10157 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10158 | |
| 10159 | ctx.idx = 0; |
| 10160 | cnt = 0; |
| 10161 | while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx)) |
| 10162 | cnt++; |
| 10163 | |
| 10164 | smp->type = SMP_T_UINT; |
| 10165 | smp->data.uint = cnt; |
| 10166 | smp->flags = SMP_F_VOL_HDR; |
| 10167 | return 1; |
| 10168 | } |
| 10169 | |
| 10170 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10171 | * Accepts an optional argument of type string containing the header field name, |
| 10172 | * and an optional argument of type signed or unsigned integer to request an |
| 10173 | * 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] | 10174 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10175 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10176 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10177 | 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] | 10178 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10179 | { |
| 10180 | struct http_txn *txn = l7; |
| 10181 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10182 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10183 | 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] | 10184 | int occ = 0; |
| 10185 | const char *name_str = NULL; |
| 10186 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10187 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10188 | if (!ctx) { |
| 10189 | /* first call */ |
| 10190 | ctx = &static_hdr_ctx; |
| 10191 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10192 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10193 | } |
| 10194 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10195 | if (args) { |
| 10196 | if (args[0].type != ARGT_STR) |
| 10197 | return 0; |
| 10198 | name_str = args[0].data.str.str; |
| 10199 | name_len = args[0].data.str.len; |
| 10200 | |
| 10201 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10202 | occ = args[1].data.uint; |
| 10203 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10204 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10205 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10206 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10207 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10208 | /* search for header from the beginning */ |
| 10209 | ctx->idx = 0; |
| 10210 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10211 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10212 | /* no explicit occurrence and single fetch => last header by default */ |
| 10213 | occ = -1; |
| 10214 | |
| 10215 | if (!occ) |
| 10216 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10217 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10218 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10219 | smp->type = SMP_T_STR; |
| 10220 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10221 | 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] | 10222 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10223 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10224 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10225 | return 0; |
| 10226 | } |
| 10227 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10228 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10229 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10230 | */ |
| 10231 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10232 | 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] | 10233 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10234 | { |
| 10235 | struct http_txn *txn = l7; |
| 10236 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10237 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10238 | 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] | 10239 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10240 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10241 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10242 | return 0; |
| 10243 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10244 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10245 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10246 | ctx.idx = 0; |
| 10247 | cnt = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10248 | while (http_find_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10249 | cnt++; |
| 10250 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10251 | smp->type = SMP_T_UINT; |
| 10252 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10253 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10254 | return 1; |
| 10255 | } |
| 10256 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10257 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10258 | * takes a mandatory argument of type string and an optional one of type int |
| 10259 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10260 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10261 | */ |
| 10262 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10263 | 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] | 10264 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10265 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10266 | int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10267 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10268 | if (ret > 0) { |
| 10269 | smp->type = SMP_T_UINT; |
| 10270 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10271 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10272 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10273 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10274 | } |
| 10275 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10276 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10277 | * and an optional one of type int to designate a specific occurrence. |
| 10278 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10279 | */ |
| 10280 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10281 | 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] | 10282 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10283 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10284 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10285 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10286 | 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] | 10287 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 10288 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10289 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10290 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10291 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10292 | if (smp->data.str.len < temp->size - 1) { |
| 10293 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 10294 | temp->str[smp->data.str.len] = '\0'; |
| 10295 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 10296 | smp->type = SMP_T_IPV6; |
| 10297 | break; |
| 10298 | } |
| 10299 | } |
| 10300 | } |
| 10301 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10302 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10303 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10304 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10305 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10306 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10307 | } |
| 10308 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10309 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10310 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10311 | */ |
| 10312 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10313 | 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] | 10314 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10315 | { |
| 10316 | struct http_txn *txn = l7; |
| 10317 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10318 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10319 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10320 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10321 | 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] | 10322 | ptr = http_get_path(txn); |
| 10323 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10324 | return 0; |
| 10325 | |
| 10326 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10327 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10328 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10329 | |
| 10330 | while (ptr < end && *ptr != '?') |
| 10331 | ptr++; |
| 10332 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10333 | smp->data.str.len = ptr - smp->data.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10334 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10335 | return 1; |
| 10336 | } |
| 10337 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10338 | /* This produces a concatenation of the first occurrence of the Host header |
| 10339 | * followed by the path component if it begins with a slash ('/'). This means |
| 10340 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10341 | * If no Host header is found, then the path is returned as-is. The returned |
| 10342 | * 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] | 10343 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10344 | */ |
| 10345 | static int |
| 10346 | 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] | 10347 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10348 | { |
| 10349 | struct http_txn *txn = l7; |
| 10350 | char *ptr, *end, *beg; |
| 10351 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10352 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10353 | |
| 10354 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10355 | |
| 10356 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10357 | 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] | 10358 | return smp_fetch_path(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10359 | |
| 10360 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10361 | temp = get_trash_chunk(); |
| 10362 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10363 | smp->type = SMP_T_STR; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10364 | smp->data.str.str = temp->str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10365 | smp->data.str.len = ctx.vlen; |
| 10366 | |
| 10367 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10368 | 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] | 10369 | beg = http_get_path(txn); |
| 10370 | if (!beg) |
| 10371 | beg = end; |
| 10372 | |
| 10373 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10374 | |
| 10375 | if (beg < ptr && *beg == '/') { |
| 10376 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 10377 | smp->data.str.len += ptr - beg; |
| 10378 | } |
| 10379 | |
| 10380 | smp->flags = SMP_F_VOL_1ST; |
| 10381 | return 1; |
| 10382 | } |
| 10383 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10384 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10385 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10386 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10387 | * 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] | 10388 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10389 | * 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] | 10390 | * high-traffic sites without having to store whole paths. |
| 10391 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10392 | int |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10393 | 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] | 10394 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10395 | { |
| 10396 | struct http_txn *txn = l7; |
| 10397 | struct hdr_ctx ctx; |
| 10398 | unsigned int hash = 0; |
| 10399 | char *ptr, *beg, *end; |
| 10400 | int len; |
| 10401 | |
| 10402 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10403 | |
| 10404 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10405 | 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] | 10406 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10407 | ptr = ctx.line + ctx.val; |
| 10408 | len = ctx.vlen; |
| 10409 | while (len--) |
| 10410 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10411 | } |
| 10412 | |
| 10413 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10414 | 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] | 10415 | beg = http_get_path(txn); |
| 10416 | if (!beg) |
| 10417 | beg = end; |
| 10418 | |
| 10419 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10420 | |
| 10421 | if (beg < ptr && *beg == '/') { |
| 10422 | while (beg < ptr) |
| 10423 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10424 | } |
| 10425 | hash = full_hash(hash); |
| 10426 | |
| 10427 | smp->type = SMP_T_UINT; |
| 10428 | smp->data.uint = hash; |
| 10429 | smp->flags = SMP_F_VOL_1ST; |
| 10430 | return 1; |
| 10431 | } |
| 10432 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10433 | /* 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] | 10434 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10435 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10436 | * 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] | 10437 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10438 | * 8 bytes would still work. |
| 10439 | */ |
| 10440 | static int |
| 10441 | 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] | 10442 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10443 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10444 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10445 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
| 10446 | |
| 10447 | if (!cli_conn) |
| 10448 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10449 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10450 | if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10451 | return 0; |
| 10452 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10453 | temp = get_trash_chunk(); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10454 | *(unsigned int *)temp->str = htonl(smp->data.uint); |
| 10455 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10456 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10457 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10458 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10459 | 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] | 10460 | temp->len += 4; |
| 10461 | break; |
| 10462 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10463 | 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] | 10464 | temp->len += 16; |
| 10465 | break; |
| 10466 | default: |
| 10467 | return 0; |
| 10468 | } |
| 10469 | |
| 10470 | smp->data.str = *temp; |
| 10471 | smp->type = SMP_T_BIN; |
| 10472 | return 1; |
| 10473 | } |
| 10474 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10475 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10476 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10477 | * of type string carrying the whole query string. |
| 10478 | */ |
| 10479 | static int |
| 10480 | smp_fetch_query(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10481 | const struct arg *args, struct sample *smp, const char *kw) |
| 10482 | { |
| 10483 | struct http_txn *txn = l7; |
| 10484 | char *ptr, *end; |
| 10485 | |
| 10486 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10487 | |
| 10488 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 10489 | end = ptr + txn->req.sl.rq.u_l; |
| 10490 | |
| 10491 | /* look up the '?' */ |
| 10492 | do { |
| 10493 | if (ptr == end) |
| 10494 | return 0; |
| 10495 | } while (*ptr++ != '?'); |
| 10496 | |
| 10497 | smp->type = SMP_T_STR; |
| 10498 | smp->data.str.str = ptr; |
| 10499 | smp->data.str.len = end - ptr; |
| 10500 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10501 | return 1; |
| 10502 | } |
| 10503 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10504 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10505 | 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] | 10506 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10507 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10508 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10509 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10510 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10511 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10512 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10513 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10514 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 10515 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10516 | return 1; |
| 10517 | } |
| 10518 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10519 | /* return a valid test if the current request is the first one on the connection */ |
| 10520 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10521 | 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] | 10522 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10523 | { |
| 10524 | if (!s) |
| 10525 | return 0; |
| 10526 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10527 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 10528 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10529 | return 1; |
| 10530 | } |
| 10531 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10532 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10533 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10534 | 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] | 10535 | const struct arg *args, struct sample *smp, const char *kw) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10536 | { |
| 10537 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10538 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10539 | return 0; |
| 10540 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10541 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10542 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10543 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10544 | return 0; |
| 10545 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10546 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10547 | 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] | 10548 | return 1; |
| 10549 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10550 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10551 | /* Accepts exactly 1 argument of type userlist */ |
| 10552 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10553 | 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] | 10554 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10555 | { |
| 10556 | |
| 10557 | if (!args || args->type != ARGT_USR) |
| 10558 | return 0; |
| 10559 | |
| 10560 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10561 | |
| 10562 | if (!get_http_auth(l4)) |
| 10563 | return 0; |
| 10564 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10565 | /* if the user does not belong to the userlist or has a wrong password, |
| 10566 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10567 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10568 | */ |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10569 | if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass)) |
| 10570 | return 0; |
| 10571 | |
| 10572 | /* pat_match_auth() will need the user list */ |
| 10573 | smp->ctx.a[0] = args->data.usr; |
| 10574 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10575 | smp->type = SMP_T_STR; |
| 10576 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10577 | smp->data.str.str = l4->txn.auth.user; |
| 10578 | smp->data.str.len = strlen(l4->txn.auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10579 | |
| 10580 | return 1; |
| 10581 | } |
| 10582 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10583 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10584 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10585 | * the cookie value is returned into *value and *value_l, and the function |
| 10586 | * returns a pointer to the next pointer to search from if the value was found. |
| 10587 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10588 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10589 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10590 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10591 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10592 | * list is expected. |
| 10593 | */ |
| 10594 | static char * |
| 10595 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10596 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10597 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10598 | { |
| 10599 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10600 | char *next; |
| 10601 | |
| 10602 | /* we search at least a cookie name followed by an equal, and more |
| 10603 | * generally something like this : |
| 10604 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10605 | */ |
| 10606 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10607 | /* Iterate through all cookies on this line */ |
| 10608 | |
| 10609 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 10610 | att_beg++; |
| 10611 | |
| 10612 | /* find att_end : this is the first character after the last non |
| 10613 | * space before the equal. It may be equal to hdr_end. |
| 10614 | */ |
| 10615 | equal = att_end = att_beg; |
| 10616 | |
| 10617 | while (equal < hdr_end) { |
| 10618 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10619 | break; |
| 10620 | if (http_is_spht[(unsigned char)*equal++]) |
| 10621 | continue; |
| 10622 | att_end = equal; |
| 10623 | } |
| 10624 | |
| 10625 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10626 | * is between <att_beg> and <equal>, both may be identical. |
| 10627 | */ |
| 10628 | |
| 10629 | /* look for end of cookie if there is an equal sign */ |
| 10630 | if (equal < hdr_end && *equal == '=') { |
| 10631 | /* look for the beginning of the value */ |
| 10632 | val_beg = equal + 1; |
| 10633 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 10634 | val_beg++; |
| 10635 | |
| 10636 | /* find the end of the value, respecting quotes */ |
| 10637 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10638 | |
| 10639 | /* make val_end point to the first white space or delimitor after the value */ |
| 10640 | val_end = next; |
| 10641 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 10642 | val_end--; |
| 10643 | } else { |
| 10644 | val_beg = val_end = next = equal; |
| 10645 | } |
| 10646 | |
| 10647 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10648 | * they will automatically be removed if a header before them is removed, |
| 10649 | * since they're supposed to be linked together. |
| 10650 | */ |
| 10651 | if (*att_beg == '$') |
| 10652 | continue; |
| 10653 | |
| 10654 | /* Ignore cookies with no equal sign */ |
| 10655 | if (equal == next) |
| 10656 | continue; |
| 10657 | |
| 10658 | /* Now we have the cookie name between att_beg and att_end, and |
| 10659 | * its value between val_beg and val_end. |
| 10660 | */ |
| 10661 | |
| 10662 | if (att_end - att_beg == cookie_name_l && |
| 10663 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10664 | /* let's return this value and indicate where to go on from */ |
| 10665 | *value = val_beg; |
| 10666 | *value_l = val_end - val_beg; |
| 10667 | return next + 1; |
| 10668 | } |
| 10669 | |
| 10670 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10671 | if (!list) |
| 10672 | break; |
| 10673 | } |
| 10674 | |
| 10675 | return NULL; |
| 10676 | } |
| 10677 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10678 | /* Fetch a captured HTTP request header. The index is the position of |
| 10679 | * the "capture" option in the configuration file |
| 10680 | */ |
| 10681 | static int |
| 10682 | smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10683 | const struct arg *args, struct sample *smp, const char *kw) |
| 10684 | { |
| 10685 | struct proxy *fe = l4->fe; |
| 10686 | struct http_txn *txn = l7; |
| 10687 | int idx; |
| 10688 | |
| 10689 | if (!args || args->type != ARGT_UINT) |
| 10690 | return 0; |
| 10691 | |
| 10692 | idx = args->data.uint; |
| 10693 | |
| 10694 | if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL) |
| 10695 | return 0; |
| 10696 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10697 | smp->type = SMP_T_STR; |
| 10698 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10699 | smp->data.str.str = txn->req.cap[idx]; |
| 10700 | smp->data.str.len = strlen(txn->req.cap[idx]); |
| 10701 | |
| 10702 | return 1; |
| 10703 | } |
| 10704 | |
| 10705 | /* Fetch a captured HTTP response header. The index is the position of |
| 10706 | * the "capture" option in the configuration file |
| 10707 | */ |
| 10708 | static int |
| 10709 | smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10710 | const struct arg *args, struct sample *smp, const char *kw) |
| 10711 | { |
| 10712 | struct proxy *fe = l4->fe; |
| 10713 | struct http_txn *txn = l7; |
| 10714 | int idx; |
| 10715 | |
| 10716 | if (!args || args->type != ARGT_UINT) |
| 10717 | return 0; |
| 10718 | |
| 10719 | idx = args->data.uint; |
| 10720 | |
| 10721 | if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL) |
| 10722 | return 0; |
| 10723 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10724 | smp->type = SMP_T_STR; |
| 10725 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10726 | smp->data.str.str = txn->rsp.cap[idx]; |
| 10727 | smp->data.str.len = strlen(txn->rsp.cap[idx]); |
| 10728 | |
| 10729 | return 1; |
| 10730 | } |
| 10731 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10732 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 10733 | static int |
| 10734 | smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10735 | const struct arg *args, struct sample *smp, const char *kw) |
| 10736 | { |
| 10737 | struct chunk *temp; |
| 10738 | struct http_txn *txn = l7; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10739 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10740 | |
| 10741 | if (!txn->uri) |
| 10742 | return 0; |
| 10743 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10744 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10745 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10746 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10747 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10748 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10749 | temp = get_trash_chunk(); |
| 10750 | temp->str = txn->uri; |
| 10751 | temp->len = ptr - txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10752 | smp->data.str = *temp; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10753 | smp->type = SMP_T_STR; |
| 10754 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10755 | |
| 10756 | return 1; |
| 10757 | |
| 10758 | } |
| 10759 | |
| 10760 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 10761 | static int |
| 10762 | smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10763 | const struct arg *args, struct sample *smp, const char *kw) |
| 10764 | { |
| 10765 | struct chunk *temp; |
| 10766 | struct http_txn *txn = l7; |
| 10767 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10768 | |
| 10769 | if (!txn->uri) |
| 10770 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10771 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10772 | ptr = txn->uri; |
| 10773 | |
| 10774 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10775 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10776 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10777 | if (!*ptr) |
| 10778 | return 0; |
| 10779 | |
| 10780 | ptr++; /* skip the space */ |
| 10781 | |
| 10782 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10783 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10784 | if (!ptr) |
| 10785 | return 0; |
| 10786 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 10787 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10788 | |
| 10789 | smp->data.str = *temp; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10790 | smp->data.str.len = ptr - temp->str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10791 | smp->type = SMP_T_STR; |
| 10792 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10793 | |
| 10794 | return 1; |
| 10795 | } |
| 10796 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10797 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 10798 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10799 | */ |
| 10800 | static int |
| 10801 | smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10802 | const struct arg *args, struct sample *smp, const char *kw) |
| 10803 | { |
| 10804 | struct http_txn *txn = l7; |
| 10805 | |
| 10806 | if (txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
| 10807 | return 0; |
| 10808 | |
| 10809 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 10810 | smp->data.str.str = "HTTP/1.1"; |
| 10811 | else |
| 10812 | smp->data.str.str = "HTTP/1.0"; |
| 10813 | |
| 10814 | smp->data.str.len = 8; |
| 10815 | smp->type = SMP_T_STR; |
| 10816 | smp->flags = SMP_F_CONST; |
| 10817 | return 1; |
| 10818 | |
| 10819 | } |
| 10820 | |
| 10821 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 10822 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10823 | */ |
| 10824 | static int |
| 10825 | smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10826 | const struct arg *args, struct sample *smp, const char *kw) |
| 10827 | { |
| 10828 | struct http_txn *txn = l7; |
| 10829 | |
| 10830 | if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
| 10831 | return 0; |
| 10832 | |
| 10833 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
| 10834 | smp->data.str.str = "HTTP/1.1"; |
| 10835 | else |
| 10836 | smp->data.str.str = "HTTP/1.0"; |
| 10837 | |
| 10838 | smp->data.str.len = 8; |
| 10839 | smp->type = SMP_T_STR; |
| 10840 | smp->flags = SMP_F_CONST; |
| 10841 | return 1; |
| 10842 | |
| 10843 | } |
| 10844 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10845 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10846 | /* 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] | 10847 | * 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] | 10848 | * 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] | 10849 | * 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] | 10850 | * 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] | 10851 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 10852 | * 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] | 10853 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 10854 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10855 | */ |
William Lallemand | 07c8b24 | 2014-05-02 17:11:07 +0200 | [diff] [blame] | 10856 | 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] | 10857 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10858 | { |
| 10859 | struct http_txn *txn = l7; |
| 10860 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10861 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10862 | const struct http_msg *msg; |
| 10863 | const char *hdr_name; |
| 10864 | int hdr_name_len; |
| 10865 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10866 | int occ = 0; |
| 10867 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10868 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10869 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10870 | return 0; |
| 10871 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10872 | if (!ctx) { |
| 10873 | /* first call */ |
| 10874 | ctx = &static_hdr_ctx; |
| 10875 | ctx->idx = 0; |
| 10876 | smp->ctx.a[2] = ctx; |
| 10877 | } |
| 10878 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10879 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10880 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10881 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10882 | msg = &txn->req; |
| 10883 | hdr_name = "Cookie"; |
| 10884 | hdr_name_len = 6; |
| 10885 | } else { |
| 10886 | msg = &txn->rsp; |
| 10887 | hdr_name = "Set-Cookie"; |
| 10888 | hdr_name_len = 10; |
| 10889 | } |
| 10890 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10891 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10892 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 10893 | occ = -1; |
| 10894 | |
| 10895 | /* OK so basically here, either we want only one value and it's the |
| 10896 | * last one, or we want to iterate over all of them and we fetch the |
| 10897 | * next one. |
| 10898 | */ |
| 10899 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10900 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10901 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10902 | /* search for the header from the beginning, we must first initialize |
| 10903 | * the search parameters. |
| 10904 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10905 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10906 | ctx->idx = 0; |
| 10907 | } |
| 10908 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10909 | smp->flags |= SMP_F_VOL_HDR; |
| 10910 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10911 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10912 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 10913 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10914 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 10915 | goto out; |
| 10916 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10917 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10918 | continue; |
| 10919 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10920 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 10921 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10922 | } |
| 10923 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10924 | smp->type = SMP_T_STR; |
| 10925 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10926 | 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] | 10927 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10928 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10929 | &smp->data.str.str, |
| 10930 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10931 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10932 | found = 1; |
| 10933 | if (occ >= 0) { |
| 10934 | /* one value was returned into smp->data.str.{str,len} */ |
| 10935 | smp->flags |= SMP_F_NOT_LAST; |
| 10936 | return 1; |
| 10937 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10938 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10939 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10940 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10941 | /* all cookie headers and values were scanned. If we're looking for the |
| 10942 | * last occurrence, we may return it now. |
| 10943 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10944 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10945 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10946 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10947 | } |
| 10948 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10949 | /* 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] | 10950 | * 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] | 10951 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 10952 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10953 | */ |
| 10954 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10955 | 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] | 10956 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10957 | { |
| 10958 | struct http_txn *txn = l7; |
| 10959 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10960 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10961 | const struct http_msg *msg; |
| 10962 | const char *hdr_name; |
| 10963 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10964 | int cnt; |
| 10965 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10966 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10967 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10968 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10969 | return 0; |
| 10970 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10971 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10972 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10973 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10974 | msg = &txn->req; |
| 10975 | hdr_name = "Cookie"; |
| 10976 | hdr_name_len = 6; |
| 10977 | } else { |
| 10978 | msg = &txn->rsp; |
| 10979 | hdr_name = "Set-Cookie"; |
| 10980 | hdr_name_len = 10; |
| 10981 | } |
| 10982 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10983 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 10984 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10985 | ctx.idx = 0; |
| 10986 | cnt = 0; |
| 10987 | |
| 10988 | while (1) { |
| 10989 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 10990 | if (!val_beg) { |
| 10991 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 10992 | break; |
| 10993 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10994 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10995 | continue; |
| 10996 | |
| 10997 | val_beg = ctx.line + ctx.val; |
| 10998 | val_end = val_beg + ctx.vlen; |
| 10999 | } |
| 11000 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11001 | smp->type = SMP_T_STR; |
| 11002 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11003 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11004 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 11005 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11006 | &smp->data.str.str, |
| 11007 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11008 | cnt++; |
| 11009 | } |
| 11010 | } |
| 11011 | |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 11012 | smp->type = SMP_T_UINT; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11013 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11014 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11015 | return 1; |
| 11016 | } |
| 11017 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11018 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 11019 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 11020 | */ |
| 11021 | static int |
| 11022 | 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] | 11023 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11024 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 11025 | int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11026 | |
| 11027 | if (ret > 0) { |
| 11028 | smp->type = SMP_T_UINT; |
| 11029 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 11030 | } |
| 11031 | |
| 11032 | return ret; |
| 11033 | } |
| 11034 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11035 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11036 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11037 | /************************************************************************/ |
| 11038 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11039 | /* |
| 11040 | * Given a path string and its length, find the position of beginning of the |
| 11041 | * query string. Returns NULL if no query string is found in the path. |
| 11042 | * |
| 11043 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11044 | * |
| 11045 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 11046 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11047 | 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] | 11048 | { |
| 11049 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11050 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11051 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11052 | return p ? p + 1 : NULL; |
| 11053 | } |
| 11054 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11055 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11056 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11057 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11058 | } |
| 11059 | |
| 11060 | /* |
| 11061 | * Given a url parameter, find the starting position of the first occurence, |
| 11062 | * or NULL if the parameter is not found. |
| 11063 | * |
| 11064 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11065 | * the function will return query_string+8. |
| 11066 | */ |
| 11067 | static char* |
| 11068 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11069 | char* url_param_name, size_t url_param_name_l, |
| 11070 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11071 | { |
| 11072 | char *pos, *last; |
| 11073 | |
| 11074 | pos = query_string; |
| 11075 | last = query_string + query_string_l - url_param_name_l - 1; |
| 11076 | |
| 11077 | while (pos <= last) { |
| 11078 | if (pos[url_param_name_l] == '=') { |
| 11079 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 11080 | return pos; |
| 11081 | pos += url_param_name_l + 1; |
| 11082 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11083 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11084 | pos++; |
| 11085 | pos++; |
| 11086 | } |
| 11087 | return NULL; |
| 11088 | } |
| 11089 | |
| 11090 | /* |
| 11091 | * Given a url parameter name, returns its value and size into *value and |
| 11092 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 11093 | * zero is returned and value/value_l are not touched. |
| 11094 | */ |
| 11095 | static int |
| 11096 | find_url_param_value(char* path, size_t path_l, |
| 11097 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11098 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11099 | { |
| 11100 | char *query_string, *qs_end; |
| 11101 | char *arg_start; |
| 11102 | char *value_start, *value_end; |
| 11103 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11104 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11105 | if (!query_string) |
| 11106 | return 0; |
| 11107 | |
| 11108 | qs_end = path + path_l; |
| 11109 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11110 | url_param_name, url_param_name_l, |
| 11111 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11112 | if (!arg_start) |
| 11113 | return 0; |
| 11114 | |
| 11115 | value_start = arg_start + url_param_name_l + 1; |
| 11116 | value_end = value_start; |
| 11117 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11118 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11119 | value_end++; |
| 11120 | |
| 11121 | *value = value_start; |
| 11122 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 11123 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11124 | } |
| 11125 | |
| 11126 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11127 | 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] | 11128 | const struct arg *args, struct sample *smp, const char *kw) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11129 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11130 | char delim = '?'; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11131 | struct http_txn *txn = l7; |
| 11132 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11133 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11134 | if (!args || args[0].type != ARGT_STR || |
| 11135 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11136 | return 0; |
| 11137 | |
| 11138 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11139 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11140 | if (args[1].type) |
| 11141 | delim = *args[1].data.str.str; |
| 11142 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11143 | 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] | 11144 | args->data.str.str, args->data.str.len, |
| 11145 | &smp->data.str.str, &smp->data.str.len, |
| 11146 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11147 | return 0; |
| 11148 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11149 | smp->type = SMP_T_STR; |
| 11150 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11151 | return 1; |
| 11152 | } |
| 11153 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11154 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11155 | * above). |
| 11156 | */ |
| 11157 | static int |
| 11158 | 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] | 11159 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11160 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 11161 | 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] | 11162 | |
| 11163 | if (ret > 0) { |
| 11164 | smp->type = SMP_T_UINT; |
| 11165 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 11166 | } |
| 11167 | |
| 11168 | return ret; |
| 11169 | } |
| 11170 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11171 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11172 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11173 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11174 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11175 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11176 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11177 | * high-traffic sites without having to store whole paths. |
| 11178 | * this differs from the base32 functions in that it includes the url parameters |
| 11179 | * as well as the path |
| 11180 | */ |
| 11181 | static int |
| 11182 | 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] | 11183 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11184 | { |
| 11185 | struct http_txn *txn = l7; |
| 11186 | struct hdr_ctx ctx; |
| 11187 | unsigned int hash = 0; |
| 11188 | char *ptr, *beg, *end; |
| 11189 | int len; |
| 11190 | |
| 11191 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11192 | |
| 11193 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11194 | 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] | 11195 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11196 | ptr = ctx.line + ctx.val; |
| 11197 | len = ctx.vlen; |
| 11198 | while (len--) |
| 11199 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11200 | } |
| 11201 | |
| 11202 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11203 | 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] | 11204 | beg = http_get_path(txn); |
| 11205 | if (!beg) |
| 11206 | beg = end; |
| 11207 | |
| 11208 | for (ptr = beg; ptr < end ; ptr++); |
| 11209 | |
| 11210 | if (beg < ptr && *beg == '/') { |
| 11211 | while (beg < ptr) |
| 11212 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11213 | } |
| 11214 | hash = full_hash(hash); |
| 11215 | |
| 11216 | smp->type = SMP_T_UINT; |
| 11217 | smp->data.uint = hash; |
| 11218 | smp->flags = SMP_F_VOL_1ST; |
| 11219 | return 1; |
| 11220 | } |
| 11221 | |
| 11222 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11223 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11224 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11225 | * on the source address length. The URL hash is stored before the address so |
| 11226 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11227 | * 8 bytes would still work. |
| 11228 | */ |
| 11229 | static int |
| 11230 | 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] | 11231 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11232 | { |
| 11233 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11234 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11235 | |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 11236 | if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11237 | return 0; |
| 11238 | |
| 11239 | temp = get_trash_chunk(); |
| 11240 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 11241 | temp->len += sizeof(smp->data.uint); |
| 11242 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11243 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11244 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11245 | 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] | 11246 | temp->len += 4; |
| 11247 | break; |
| 11248 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11249 | 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] | 11250 | temp->len += 16; |
| 11251 | break; |
| 11252 | default: |
| 11253 | return 0; |
| 11254 | } |
| 11255 | |
| 11256 | smp->data.str = *temp; |
| 11257 | smp->type = SMP_T_BIN; |
| 11258 | return 1; |
| 11259 | } |
| 11260 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11261 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11262 | * keyword. These keywords support an optional positive or negative occurrence |
| 11263 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11264 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11265 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11266 | * error message in case of error, that the caller is responsible for freeing. |
| 11267 | * The initial location must either be freeable or NULL. |
| 11268 | */ |
| 11269 | static int val_hdr(struct arg *arg, char **err_msg) |
| 11270 | { |
| 11271 | 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] | 11272 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11273 | return 0; |
| 11274 | } |
| 11275 | return 1; |
| 11276 | } |
| 11277 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11278 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11279 | * adds an optional offset found in args[0] and emits a string representing |
| 11280 | * the date in RFC-1123/5322 format. |
| 11281 | */ |
| 11282 | static int sample_conv_http_date(const struct arg *args, struct sample *smp) |
| 11283 | { |
| 11284 | const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; |
| 11285 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11286 | struct chunk *temp; |
| 11287 | struct tm *tm; |
| 11288 | time_t curr_date = smp->data.uint; |
| 11289 | |
| 11290 | /* add offset */ |
| 11291 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 11292 | curr_date += args[0].data.sint; |
| 11293 | |
| 11294 | tm = gmtime(&curr_date); |
| 11295 | |
| 11296 | temp = get_trash_chunk(); |
| 11297 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11298 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11299 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11300 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11301 | |
| 11302 | smp->data.str = *temp; |
| 11303 | smp->type = SMP_T_STR; |
| 11304 | return 1; |
| 11305 | } |
| 11306 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11307 | /* Match language range with language tag. RFC2616 14.4: |
| 11308 | * |
| 11309 | * A language-range matches a language-tag if it exactly equals |
| 11310 | * the tag, or if it exactly equals a prefix of the tag such |
| 11311 | * that the first tag character following the prefix is "-". |
| 11312 | * |
| 11313 | * Return 1 if the strings match, else return 0. |
| 11314 | */ |
| 11315 | static inline int language_range_match(const char *range, int range_len, |
| 11316 | const char *tag, int tag_len) |
| 11317 | { |
| 11318 | const char *end = range + range_len; |
| 11319 | const char *tend = tag + tag_len; |
| 11320 | while (range < end) { |
| 11321 | if (*range == '-' && tag == tend) |
| 11322 | return 1; |
| 11323 | if (*range != *tag || tag == tend) |
| 11324 | return 0; |
| 11325 | range++; |
| 11326 | tag++; |
| 11327 | } |
| 11328 | /* Return true only if the last char of the tag is matched. */ |
| 11329 | return tag == tend; |
| 11330 | } |
| 11331 | |
| 11332 | /* Arguments: The list of expected value, the number of parts returned and the separator */ |
| 11333 | static int sample_conv_q_prefered(const struct arg *args, struct sample *smp) |
| 11334 | { |
| 11335 | const char *al = smp->data.str.str; |
| 11336 | const char *end = al + smp->data.str.len; |
| 11337 | const char *token; |
| 11338 | int toklen; |
| 11339 | int qvalue; |
| 11340 | const char *str; |
| 11341 | const char *w; |
| 11342 | int best_q = 0; |
| 11343 | |
| 11344 | /* Set the constant to the sample, because the output of the |
| 11345 | * function will be peek in the constant configuration string. |
| 11346 | */ |
| 11347 | smp->flags |= SMP_F_CONST; |
| 11348 | smp->data.str.size = 0; |
| 11349 | smp->data.str.str = ""; |
| 11350 | smp->data.str.len = 0; |
| 11351 | |
| 11352 | /* Parse the accept language */ |
| 11353 | while (1) { |
| 11354 | |
| 11355 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11356 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11357 | al++; |
| 11358 | if (al >= end) |
| 11359 | break; |
| 11360 | |
| 11361 | /* Start of the fisrt word. */ |
| 11362 | token = al; |
| 11363 | |
| 11364 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11365 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11366 | al++; |
| 11367 | if (al == token) |
| 11368 | goto expect_comma; |
| 11369 | |
| 11370 | /* Length of the token. */ |
| 11371 | toklen = al - token; |
| 11372 | qvalue = 1000; |
| 11373 | |
| 11374 | /* Check if the token exists in the list. If the token not exists, |
| 11375 | * jump to the next token. |
| 11376 | */ |
| 11377 | str = args[0].data.str.str; |
| 11378 | w = str; |
| 11379 | while (1) { |
| 11380 | if (*str == ';' || *str == '\0') { |
| 11381 | if (language_range_match(token, toklen, w, str-w)) |
| 11382 | goto look_for_q; |
| 11383 | if (*str == '\0') |
| 11384 | goto expect_comma; |
| 11385 | w = str + 1; |
| 11386 | } |
| 11387 | str++; |
| 11388 | } |
| 11389 | goto expect_comma; |
| 11390 | |
| 11391 | look_for_q: |
| 11392 | |
| 11393 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11394 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11395 | al++; |
| 11396 | if (al >= end) |
| 11397 | goto process_value; |
| 11398 | |
| 11399 | /* If ',' is found, process the result */ |
| 11400 | if (*al == ',') |
| 11401 | goto process_value; |
| 11402 | |
| 11403 | /* If the character is different from ';', look |
| 11404 | * for the end of the header part in best effort. |
| 11405 | */ |
| 11406 | if (*al != ';') |
| 11407 | goto expect_comma; |
| 11408 | |
| 11409 | /* Assumes that the char is ';', now expect "q=". */ |
| 11410 | al++; |
| 11411 | |
| 11412 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11413 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11414 | al++; |
| 11415 | if (al >= end) |
| 11416 | goto process_value; |
| 11417 | |
| 11418 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11419 | if (*al != 'q') |
| 11420 | goto process_value; |
| 11421 | al++; |
| 11422 | |
| 11423 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11424 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11425 | al++; |
| 11426 | if (al >= end) |
| 11427 | goto process_value; |
| 11428 | |
| 11429 | /* Expect '='. If no '=', continue in best effort */ |
| 11430 | if (*al != '=') |
| 11431 | goto process_value; |
| 11432 | al++; |
| 11433 | |
| 11434 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11435 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11436 | al++; |
| 11437 | if (al >= end) |
| 11438 | goto process_value; |
| 11439 | |
| 11440 | /* Parse the q value. */ |
| 11441 | qvalue = parse_qvalue(al, &al); |
| 11442 | |
| 11443 | process_value: |
| 11444 | |
| 11445 | /* If the new q value is the best q value, then store the associated |
| 11446 | * language in the response. If qvalue is the biggest value (1000), |
| 11447 | * break the process. |
| 11448 | */ |
| 11449 | if (qvalue > best_q) { |
| 11450 | smp->data.str.str = (char *)w; |
| 11451 | smp->data.str.len = str - w; |
| 11452 | if (qvalue >= 1000) |
| 11453 | break; |
| 11454 | best_q = qvalue; |
| 11455 | } |
| 11456 | |
| 11457 | expect_comma: |
| 11458 | |
| 11459 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11460 | while (al < end && *al != ',') |
| 11461 | al++; |
| 11462 | if (al >= end) |
| 11463 | break; |
| 11464 | |
| 11465 | /* Comma is found, jump it and restart the analyzer. */ |
| 11466 | al++; |
| 11467 | } |
| 11468 | |
| 11469 | /* Set default value if required. */ |
| 11470 | if (smp->data.str.len == 0 && args[1].type == ARGT_STR) { |
| 11471 | smp->data.str.str = args[1].data.str.str; |
| 11472 | smp->data.str.len = args[1].data.str.len; |
| 11473 | } |
| 11474 | |
| 11475 | /* Return true only if a matching language was found. */ |
| 11476 | return smp->data.str.len != 0; |
| 11477 | } |
| 11478 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 11479 | /* |
| 11480 | * Return the struct http_req_action_kw associated to a keyword. |
| 11481 | */ |
| 11482 | struct http_req_action_kw *action_http_req_custom(const char *kw) |
| 11483 | { |
| 11484 | if (!LIST_ISEMPTY(&http_req_keywords.list)) { |
| 11485 | struct http_req_action_kw_list *kw_list; |
| 11486 | int i; |
| 11487 | |
| 11488 | list_for_each_entry(kw_list, &http_req_keywords.list, list) { |
| 11489 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11490 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11491 | return &kw_list->kw[i]; |
| 11492 | } |
| 11493 | } |
| 11494 | } |
| 11495 | return NULL; |
| 11496 | } |
| 11497 | |
| 11498 | /* |
| 11499 | * Return the struct http_res_action_kw associated to a keyword. |
| 11500 | */ |
| 11501 | struct http_res_action_kw *action_http_res_custom(const char *kw) |
| 11502 | { |
| 11503 | if (!LIST_ISEMPTY(&http_res_keywords.list)) { |
| 11504 | struct http_res_action_kw_list *kw_list; |
| 11505 | int i; |
| 11506 | |
| 11507 | list_for_each_entry(kw_list, &http_res_keywords.list, list) { |
| 11508 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11509 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11510 | return &kw_list->kw[i]; |
| 11511 | } |
| 11512 | } |
| 11513 | } |
| 11514 | return NULL; |
| 11515 | } |
| 11516 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11517 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11518 | /* All supported ACL keywords must be declared here. */ |
| 11519 | /************************************************************************/ |
| 11520 | |
| 11521 | /* Note: must not be declared <const> as its list will be overwritten. |
| 11522 | * Please take care of keeping this list alphabetically sorted. |
| 11523 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11524 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11525 | { "base", "base", PAT_MATCH_STR }, |
| 11526 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 11527 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 11528 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 11529 | { "base_end", "base", PAT_MATCH_END }, |
| 11530 | { "base_len", "base", PAT_MATCH_LEN }, |
| 11531 | { "base_reg", "base", PAT_MATCH_REG }, |
| 11532 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11533 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11534 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 11535 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 11536 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 11537 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 11538 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 11539 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 11540 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 11541 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11542 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11543 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 11544 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 11545 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 11546 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 11547 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 11548 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 11549 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 11550 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11551 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11552 | /* these two declarations uses strings with list storage (in place |
| 11553 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 11554 | * and delete functions are relative to the list management. The parse |
| 11555 | * and match method are related to the corresponding fetch methods. This |
| 11556 | * is very particular ACL declaration mode. |
| 11557 | */ |
| 11558 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 11559 | { "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] | 11560 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11561 | { "path", "path", PAT_MATCH_STR }, |
| 11562 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 11563 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 11564 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 11565 | { "path_end", "path", PAT_MATCH_END }, |
| 11566 | { "path_len", "path", PAT_MATCH_LEN }, |
| 11567 | { "path_reg", "path", PAT_MATCH_REG }, |
| 11568 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11569 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11570 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 11571 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11572 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11573 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 11574 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 11575 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 11576 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 11577 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 11578 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 11579 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 11580 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11581 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11582 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 11583 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 11584 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 11585 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 11586 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 11587 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 11588 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 11589 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11590 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11591 | { "url", "url", PAT_MATCH_STR }, |
| 11592 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 11593 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 11594 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 11595 | { "url_end", "url", PAT_MATCH_END }, |
| 11596 | { "url_len", "url", PAT_MATCH_LEN }, |
| 11597 | { "url_reg", "url", PAT_MATCH_REG }, |
| 11598 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11599 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11600 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 11601 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 11602 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 11603 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 11604 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 11605 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 11606 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 11607 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11608 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 11609 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11610 | }}; |
| 11611 | |
| 11612 | /************************************************************************/ |
| 11613 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11614 | /************************************************************************/ |
| 11615 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11616 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11617 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11618 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11619 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 11620 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11621 | /* capture are allocated and are permanent in the session */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11622 | { "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] | 11623 | |
| 11624 | /* retrieve these captures from the HTTP logs */ |
| 11625 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11626 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11627 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11628 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11629 | { "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] | 11630 | { "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] | 11631 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11632 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 11633 | * are only here to match the ACL's name, are request-only and are used |
| 11634 | * for ACL compatibility only. |
| 11635 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11636 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11637 | { "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] | 11638 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11639 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11640 | |
| 11641 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 11642 | * only here to match the ACL's name, are request-only and are used for |
| 11643 | * ACL compatibility only. |
| 11644 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11645 | { "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] | 11646 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11647 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11648 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11649 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 11650 | { "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] | 11651 | { "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] | 11652 | { "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] | 11653 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11654 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11655 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11656 | |
| 11657 | /* HTTP protocol on the request path */ |
| 11658 | { "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] | 11659 | { "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] | 11660 | |
| 11661 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11662 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11663 | { "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] | 11664 | |
| 11665 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11666 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 11667 | { "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] | 11668 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11669 | /* 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] | 11670 | { "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] | 11671 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11672 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11673 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11674 | { "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] | 11675 | { "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] | 11676 | { "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] | 11677 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11678 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11679 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11680 | |
| 11681 | /* 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] | 11682 | { "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] | 11683 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11684 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11685 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11686 | { "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] | 11687 | { "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] | 11688 | { "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] | 11689 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11690 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 11691 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11692 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11693 | /* 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] | 11694 | { "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] | 11695 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11696 | { "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] | 11697 | { "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] | 11698 | |
| 11699 | /* 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] | 11700 | { "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] | 11701 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11702 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 11703 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11704 | |
| 11705 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11706 | { "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] | 11707 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11708 | { "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] | 11709 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11710 | { "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] | 11711 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11712 | { "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] | 11713 | { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11714 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11715 | }}; |
| 11716 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11717 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11718 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 11719 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11720 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR}, |
| 11721 | { "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] | 11722 | { NULL, NULL, 0, 0, 0 }, |
| 11723 | }}; |
| 11724 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11725 | __attribute__((constructor)) |
| 11726 | static void __http_protocol_init(void) |
| 11727 | { |
| 11728 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11729 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11730 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11731 | } |
| 11732 | |
| 11733 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 11734 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 11735 | * Local variables: |
| 11736 | * c-indent-level: 8 |
| 11737 | * c-basic-offset: 8 |
| 11738 | * End: |
| 11739 | */ |