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 | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 590 | /* Find the first or next header field in message buffer <sol> using headers |
| 591 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 592 | * everything necessary to use the header and find next occurrence. If its |
| 593 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 594 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 595 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 596 | * no header name. |
| 597 | */ |
| 598 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 599 | { |
| 600 | char *eol, *sov; |
| 601 | int cur_idx, old_idx; |
| 602 | int len; |
| 603 | |
| 604 | cur_idx = ctx->idx; |
| 605 | if (cur_idx) { |
| 606 | /* We have previously returned a header, let's search another one */ |
| 607 | sol = ctx->line; |
| 608 | eol = sol + idx->v[cur_idx].len; |
| 609 | goto next_hdr; |
| 610 | } |
| 611 | |
| 612 | /* first request for this header */ |
| 613 | sol += hdr_idx_first_pos(idx); |
| 614 | old_idx = 0; |
| 615 | cur_idx = hdr_idx_first_idx(idx); |
| 616 | while (cur_idx) { |
| 617 | eol = sol + idx->v[cur_idx].len; |
| 618 | |
| 619 | len = 0; |
| 620 | while (1) { |
| 621 | if (len >= eol - sol) |
| 622 | goto next_hdr; |
| 623 | if (sol[len] == ':') |
| 624 | break; |
| 625 | len++; |
| 626 | } |
| 627 | |
| 628 | ctx->del = len; |
| 629 | sov = sol + len + 1; |
| 630 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 631 | sov++; |
| 632 | |
| 633 | ctx->line = sol; |
| 634 | ctx->prev = old_idx; |
| 635 | ctx->idx = cur_idx; |
| 636 | ctx->val = sov - sol; |
| 637 | ctx->tws = 0; |
| 638 | |
| 639 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 640 | eol--; |
| 641 | ctx->tws++; |
| 642 | } |
| 643 | ctx->vlen = eol - sov; |
| 644 | return 1; |
| 645 | |
| 646 | next_hdr: |
| 647 | sol = eol + idx->v[cur_idx].cr + 1; |
| 648 | old_idx = cur_idx; |
| 649 | cur_idx = idx->v[cur_idx].next; |
| 650 | } |
| 651 | return 0; |
| 652 | } |
| 653 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 654 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 655 | * par 2.2 for more information. Note that it requires a valid header to return |
| 656 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 657 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 658 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 659 | { |
| 660 | int quoted, qdpair; |
| 661 | |
| 662 | quoted = qdpair = 0; |
| 663 | for (; s < e; s++) { |
| 664 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 665 | else if (quoted) { |
| 666 | if (*s == '\\') qdpair = 1; |
| 667 | else if (*s == '"') quoted = 0; |
| 668 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 669 | else if (*s == '"') quoted = 1; |
| 670 | else if (*s == ',') return s; |
| 671 | } |
| 672 | return s; |
| 673 | } |
| 674 | |
| 675 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 676 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 677 | * structure holds everything necessary to use the header and find next |
| 678 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 679 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 680 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 681 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 682 | * is NULL when searching for a new values of a header, the current header is |
| 683 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 684 | */ |
| 685 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 686 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 687 | struct hdr_ctx *ctx) |
| 688 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 689 | char *eol, *sov; |
| 690 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 691 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 692 | cur_idx = ctx->idx; |
| 693 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 694 | /* We have previously returned a value, let's search |
| 695 | * another one on the same line. |
| 696 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 697 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 698 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 699 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 700 | eol = sol + idx->v[cur_idx].len; |
| 701 | |
| 702 | if (sov >= eol) |
| 703 | /* no more values in this header */ |
| 704 | goto next_hdr; |
| 705 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 706 | /* values remaining for this header, skip the comma but save it |
| 707 | * for later use (eg: for header deletion). |
| 708 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 709 | sov++; |
| 710 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 711 | sov++; |
| 712 | |
| 713 | goto return_hdr; |
| 714 | } |
| 715 | |
| 716 | /* first request for this header */ |
| 717 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 718 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 719 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 720 | while (cur_idx) { |
| 721 | eol = sol + idx->v[cur_idx].len; |
| 722 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 723 | if (len == 0) { |
| 724 | /* No argument was passed, we want any header. |
| 725 | * To achieve this, we simply build a fake request. */ |
| 726 | while (sol + len < eol && sol[len] != ':') |
| 727 | len++; |
| 728 | name = sol; |
| 729 | } |
| 730 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 731 | if ((len < eol - sol) && |
| 732 | (sol[len] == ':') && |
| 733 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 734 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 735 | sov = sol + len + 1; |
| 736 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 737 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 738 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 739 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 740 | ctx->prev = old_idx; |
| 741 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 742 | ctx->idx = cur_idx; |
| 743 | ctx->val = sov - sol; |
| 744 | |
| 745 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 746 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 747 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 748 | eol--; |
| 749 | ctx->tws++; |
| 750 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 751 | ctx->vlen = eol - sov; |
| 752 | return 1; |
| 753 | } |
| 754 | next_hdr: |
| 755 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 756 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 757 | cur_idx = idx->v[cur_idx].next; |
| 758 | } |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 763 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 764 | struct hdr_ctx *ctx) |
| 765 | { |
| 766 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 767 | } |
| 768 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 769 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 770 | * the http_find_header functions. The value is removed, as well as surrounding |
| 771 | * 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] | 772 | * 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] | 773 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 774 | * no more header, so any processing may stop. The ctx is always left in a form |
| 775 | * that can be handled by http_find_header2() to find next occurrence. |
| 776 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 777 | 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] | 778 | { |
| 779 | int cur_idx = ctx->idx; |
| 780 | char *sol = ctx->line; |
| 781 | struct hdr_idx_elem *hdr; |
| 782 | int delta, skip_comma; |
| 783 | |
| 784 | if (!cur_idx) |
| 785 | return 0; |
| 786 | |
| 787 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 788 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 789 | /* 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] | 790 | 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] | 791 | http_msg_move_end(msg, delta); |
| 792 | idx->used--; |
| 793 | hdr->len = 0; /* unused entry */ |
| 794 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 795 | if (idx->tail == ctx->idx) |
| 796 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 797 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 798 | 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] | 799 | 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] | 800 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 801 | return ctx->idx; |
| 802 | } |
| 803 | |
| 804 | /* 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] | 805 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 806 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 807 | */ |
| 808 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 809 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 810 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 811 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 812 | NULL, 0); |
| 813 | hdr->len += delta; |
| 814 | http_msg_move_end(msg, delta); |
| 815 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 816 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 817 | return ctx->idx; |
| 818 | } |
| 819 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 820 | /* This function handles a server error at the stream interface level. The |
| 821 | * stream interface is assumed to be already in a closed state. An optional |
| 822 | * message is copied into the input buffer, and an HTTP status code stored. |
| 823 | * 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] | 824 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 825 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 826 | static void http_server_error(struct session *s, struct stream_interface *si, |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 827 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 828 | { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 829 | channel_auto_read(si->ob); |
| 830 | channel_abort(si->ob); |
| 831 | channel_auto_close(si->ob); |
| 832 | channel_erase(si->ob); |
| 833 | channel_auto_close(si->ib); |
| 834 | channel_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 835 | if (status > 0 && msg) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 836 | s->txn.status = status; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 837 | bo_inject(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 838 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 839 | if (!(s->flags & SN_ERR_MASK)) |
| 840 | s->flags |= err; |
| 841 | if (!(s->flags & SN_FINST_MASK)) |
| 842 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 843 | } |
| 844 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 845 | /* This function returns the appropriate error location for the given session |
| 846 | * and message. |
| 847 | */ |
| 848 | |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 849 | struct chunk *http_error_message(struct session *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 850 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 851 | if (s->be->errmsg[msgnum].str) |
| 852 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 853 | else if (s->fe->errmsg[msgnum].str) |
| 854 | return &s->fe->errmsg[msgnum]; |
| 855 | else |
| 856 | return &http_err_chunks[msgnum]; |
| 857 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 858 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 859 | /* |
| 860 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 861 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 862 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 863 | 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] | 864 | { |
| 865 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 866 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 867 | |
| 868 | m = ((unsigned)*str - 'A'); |
| 869 | |
| 870 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 871 | for (h = http_methods[m]; h->len > 0; h++) { |
| 872 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 873 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 874 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 875 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 876 | }; |
| 877 | return HTTP_METH_OTHER; |
| 878 | } |
| 879 | return HTTP_METH_NONE; |
| 880 | |
| 881 | } |
| 882 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 883 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 884 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 885 | * It is returned otherwise. |
| 886 | */ |
| 887 | static char * |
| 888 | http_get_path(struct http_txn *txn) |
| 889 | { |
| 890 | char *ptr, *end; |
| 891 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 892 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 893 | end = ptr + txn->req.sl.rq.u_l; |
| 894 | |
| 895 | if (ptr >= end) |
| 896 | return NULL; |
| 897 | |
| 898 | /* RFC2616, par. 5.1.2 : |
| 899 | * Request-URI = "*" | absuri | abspath | authority |
| 900 | */ |
| 901 | |
| 902 | if (*ptr == '*') |
| 903 | return NULL; |
| 904 | |
| 905 | if (isalpha((unsigned char)*ptr)) { |
| 906 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 907 | ptr++; |
| 908 | while (ptr < end && |
| 909 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 910 | ptr++; |
| 911 | /* skip '://' */ |
| 912 | if (ptr == end || *ptr++ != ':') |
| 913 | return NULL; |
| 914 | if (ptr == end || *ptr++ != '/') |
| 915 | return NULL; |
| 916 | if (ptr == end || *ptr++ != '/') |
| 917 | return NULL; |
| 918 | } |
| 919 | /* skip [user[:passwd]@]host[:[port]] */ |
| 920 | |
| 921 | while (ptr < end && *ptr != '/') |
| 922 | ptr++; |
| 923 | |
| 924 | if (ptr == end) |
| 925 | return NULL; |
| 926 | |
| 927 | /* OK, we got the '/' ! */ |
| 928 | return ptr; |
| 929 | } |
| 930 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 931 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 932 | * If not found, return NULL. It is returned otherwise. |
| 933 | */ |
| 934 | static char * |
| 935 | http_get_path_from_string(char *str) |
| 936 | { |
| 937 | char *ptr = str; |
| 938 | |
| 939 | /* RFC2616, par. 5.1.2 : |
| 940 | * Request-URI = "*" | absuri | abspath | authority |
| 941 | */ |
| 942 | |
| 943 | if (*ptr == '*') |
| 944 | return NULL; |
| 945 | |
| 946 | if (isalpha((unsigned char)*ptr)) { |
| 947 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 948 | ptr++; |
| 949 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 950 | ptr++; |
| 951 | /* skip '://' */ |
| 952 | if (*ptr == '\0' || *ptr++ != ':') |
| 953 | return NULL; |
| 954 | if (*ptr == '\0' || *ptr++ != '/') |
| 955 | return NULL; |
| 956 | if (*ptr == '\0' || *ptr++ != '/') |
| 957 | return NULL; |
| 958 | } |
| 959 | /* skip [user[:passwd]@]host[:[port]] */ |
| 960 | |
| 961 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 962 | ptr++; |
| 963 | |
| 964 | if (*ptr == '\0' || *ptr == ' ') |
| 965 | return NULL; |
| 966 | |
| 967 | /* OK, we got the '/' ! */ |
| 968 | return ptr; |
| 969 | } |
| 970 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 971 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 972 | * in redirect mode. This may only be called just after the stream interface |
| 973 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 974 | * follow normal proxy processing. NOTE: this function is designed to support |
| 975 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 976 | */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 977 | void http_perform_server_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 978 | { |
| 979 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 980 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 981 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 982 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 983 | |
| 984 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 985 | trash.len = strlen(HTTP_302); |
| 986 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 987 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 988 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 989 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 990 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 991 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 992 | return; |
| 993 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 994 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 995 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 996 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 997 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 998 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 999 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1000 | /* 3: add the request URI. Since it was already forwarded, we need |
| 1001 | * to temporarily rewind the buffer. |
| 1002 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1003 | txn = &s->txn; |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 1004 | b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1005 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1006 | path = http_get_path(txn); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1007 | 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] | 1008 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1009 | b_adv(s->req->buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1010 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1011 | if (!path) |
| 1012 | return; |
| 1013 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1014 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1015 | return; |
| 1016 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1017 | memcpy(trash.str + trash.len, path, len); |
| 1018 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1019 | |
| 1020 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1021 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 1022 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1023 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1024 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 1025 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1026 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1027 | |
| 1028 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1029 | si_shutr(si); |
| 1030 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1031 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1032 | si->state = SI_ST_CLO; |
| 1033 | |
| 1034 | /* send the message */ |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 1035 | 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] | 1036 | |
| 1037 | /* 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] | 1038 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 1039 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1040 | } |
| 1041 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1042 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1043 | * that the server side is closed. Note that err_type is actually a |
| 1044 | * bitmask, where almost only aborts may be cumulated with other |
| 1045 | * values. We consider that aborted operations are more important |
| 1046 | * than timeouts or errors due to the fact that nobody else in the |
| 1047 | * logs might explain incomplete retries. All others should avoid |
| 1048 | * being cumulated. It should normally not be possible to have multiple |
| 1049 | * 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] | 1050 | * Note that connection errors appearing on the second request of a keep-alive |
| 1051 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1052 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1053 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1054 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1055 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1056 | |
| 1057 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1058 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1059 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1060 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1061 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1062 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 1063 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1064 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1065 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1066 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1067 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1068 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1069 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1070 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1071 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1072 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 1073 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1074 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1075 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
Willy Tarreau | 3634624 | 2014-02-24 18:26:30 +0100 | [diff] [blame] | 1076 | 503, (s->flags & SN_SRV_REUSED) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1077 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1078 | else if (err_type & SI_ET_CONN_RES) |
| 1079 | http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1080 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 1081 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1082 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1083 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1084 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1085 | } |
| 1086 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1087 | extern const char sess_term_cond[8]; |
| 1088 | extern const char sess_fin_state[8]; |
| 1089 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1090 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1091 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1092 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1093 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1094 | /* |
| 1095 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1096 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1097 | */ |
| 1098 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1099 | char **cap, struct cap_hdr *cap_hdr) |
| 1100 | { |
| 1101 | char *eol, *sol, *col, *sov; |
| 1102 | int cur_idx; |
| 1103 | struct cap_hdr *h; |
| 1104 | int len; |
| 1105 | |
| 1106 | sol = som + hdr_idx_first_pos(idx); |
| 1107 | cur_idx = hdr_idx_first_idx(idx); |
| 1108 | |
| 1109 | while (cur_idx) { |
| 1110 | eol = sol + idx->v[cur_idx].len; |
| 1111 | |
| 1112 | col = sol; |
| 1113 | while (col < eol && *col != ':') |
| 1114 | col++; |
| 1115 | |
| 1116 | sov = col + 1; |
| 1117 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1118 | sov++; |
| 1119 | |
| 1120 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1121 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1122 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1123 | if (cap[h->index] == NULL) |
| 1124 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1125 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1126 | |
| 1127 | if (cap[h->index] == NULL) { |
| 1128 | Alert("HTTP capture : out of memory.\n"); |
| 1129 | continue; |
| 1130 | } |
| 1131 | |
| 1132 | len = eol - sov; |
| 1133 | if (len > h->len) |
| 1134 | len = h->len; |
| 1135 | |
| 1136 | memcpy(cap[h->index], sov, len); |
| 1137 | cap[h->index][len]=0; |
| 1138 | } |
| 1139 | } |
| 1140 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1141 | cur_idx = idx->v[cur_idx].next; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1146 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1147 | */ |
| 1148 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1149 | |
| 1150 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1151 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1152 | */ |
| 1153 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1154 | ptr++; \ |
| 1155 | if (likely(ptr < end)) \ |
| 1156 | goto good; \ |
| 1157 | else { \ |
| 1158 | state = (st); \ |
| 1159 | goto http_msg_ood; \ |
| 1160 | } \ |
| 1161 | } while (0) |
| 1162 | |
| 1163 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1164 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1165 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1166 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1167 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1168 | * will give undefined results. |
| 1169 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1170 | * and that msg->sol points to the beginning of the response. |
| 1171 | * If a complete line is found (which implies that at least one CR or LF is |
| 1172 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1173 | * returned indicating an incomplete line (which does not mean that parts have |
| 1174 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1175 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1176 | * upon next call. |
| 1177 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1178 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1179 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1180 | * 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] | 1181 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1182 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1183 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1184 | enum ht_state state, const char *ptr, const char *end, |
| 1185 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1186 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1187 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1188 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1189 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1190 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1191 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1192 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1193 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1194 | |
| 1195 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1196 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1197 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1198 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1199 | state = HTTP_MSG_ERROR; |
| 1200 | break; |
| 1201 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1202 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1203 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1204 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1205 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1206 | goto http_msg_rpcode; |
| 1207 | } |
| 1208 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1209 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1210 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1211 | state = HTTP_MSG_ERROR; |
| 1212 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1213 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1214 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1215 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1216 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1217 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1218 | |
| 1219 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1220 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1221 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1222 | } |
| 1223 | |
| 1224 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1225 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1226 | http_msg_rsp_reason: |
| 1227 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1228 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1229 | msg->sl.st.r_l = 0; |
| 1230 | goto http_msg_rpline_eol; |
| 1231 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1232 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1233 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1234 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1235 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1236 | goto http_msg_rpreason; |
| 1237 | } |
| 1238 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1239 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1240 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1241 | goto http_msg_rsp_reason; |
| 1242 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1243 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1244 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1245 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1246 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1247 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1248 | http_msg_rpline_eol: |
| 1249 | /* We have seen the end of line. Note that we do not |
| 1250 | * necessarily have the \n yet, but at least we know that we |
| 1251 | * have EITHER \r OR \n, otherwise the response would not be |
| 1252 | * complete. We can then record the response length and return |
| 1253 | * to the caller which will be able to register it. |
| 1254 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1255 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1256 | return ptr; |
| 1257 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1258 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1259 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1260 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1261 | exit(1); |
| 1262 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1263 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1267 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1268 | if (ret_state) |
| 1269 | *ret_state = state; |
| 1270 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1271 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1272 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1273 | } |
| 1274 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1275 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1276 | * This function parses a request line between <ptr> and <end>, starting with |
| 1277 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1278 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1279 | * will give undefined results. |
| 1280 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1281 | * and that msg->sol points to the beginning of the request. |
| 1282 | * If a complete line is found (which implies that at least one CR or LF is |
| 1283 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1284 | * returned indicating an incomplete line (which does not mean that parts have |
| 1285 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1286 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1287 | * upon next call. |
| 1288 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1289 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1290 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1291 | * 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] | 1292 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1293 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1294 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1295 | enum ht_state state, const char *ptr, const char *end, |
| 1296 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1297 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1298 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1299 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1300 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1301 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1302 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1303 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1304 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
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 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1307 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1308 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1309 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1310 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1311 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1312 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1313 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1314 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1315 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1316 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1317 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1318 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1319 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1320 | msg->sl.rq.v_l = 0; |
| 1321 | goto http_msg_rqline_eol; |
| 1322 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1323 | state = HTTP_MSG_ERROR; |
| 1324 | break; |
| 1325 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1326 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1327 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1328 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1329 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1330 | goto http_msg_rquri; |
| 1331 | } |
| 1332 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1333 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1334 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1335 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1336 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1337 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1338 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1339 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1340 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1341 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1342 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1343 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1344 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1345 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1346 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1347 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1348 | /* non-ASCII chars are forbidden unless option |
| 1349 | * accept-invalid-http-request is enabled in the frontend. |
| 1350 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1351 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1352 | if (msg->err_pos < -1) |
| 1353 | goto invalid_char; |
| 1354 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1355 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1356 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1357 | } |
| 1358 | |
| 1359 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1360 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1361 | goto http_msg_req09_uri_e; |
| 1362 | } |
| 1363 | |
| 1364 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1365 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1366 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1367 | state = HTTP_MSG_ERROR; |
| 1368 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1369 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1370 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1371 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1372 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1373 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1374 | goto http_msg_rqver; |
| 1375 | } |
| 1376 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1377 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1378 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1379 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1380 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1381 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1382 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1383 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1384 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1385 | |
| 1386 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1387 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1388 | http_msg_rqline_eol: |
| 1389 | /* We have seen the end of line. Note that we do not |
| 1390 | * necessarily have the \n yet, but at least we know that we |
| 1391 | * have EITHER \r OR \n, otherwise the request would not be |
| 1392 | * complete. We can then record the request length and return |
| 1393 | * to the caller which will be able to register it. |
| 1394 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1395 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1396 | return ptr; |
| 1397 | } |
| 1398 | |
| 1399 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1400 | state = HTTP_MSG_ERROR; |
| 1401 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1402 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1403 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1404 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1405 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1406 | exit(1); |
| 1407 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1408 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1409 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1410 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1411 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1412 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1413 | if (ret_state) |
| 1414 | *ret_state = state; |
| 1415 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1416 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1417 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1418 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1419 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1420 | /* |
| 1421 | * Returns the data from Authorization header. Function may be called more |
| 1422 | * than once so data is stored in txn->auth_data. When no header is found |
| 1423 | * 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] | 1424 | * searching again for something we are unable to find anyway. However, if |
| 1425 | * the result if valid, the cache is not reused because we would risk to |
| 1426 | * have the credentials overwritten by another session in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1427 | */ |
| 1428 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 1429 | /* This bufffer is initialized in the file 'src/haproxy.c'. This length is |
| 1430 | * set according to global.tune.bufsize. |
| 1431 | */ |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1432 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1433 | |
| 1434 | int |
| 1435 | get_http_auth(struct session *s) |
| 1436 | { |
| 1437 | |
| 1438 | struct http_txn *txn = &s->txn; |
| 1439 | struct chunk auth_method; |
| 1440 | struct hdr_ctx ctx; |
| 1441 | char *h, *p; |
| 1442 | int len; |
| 1443 | |
| 1444 | #ifdef DEBUG_AUTH |
| 1445 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1446 | #endif |
| 1447 | |
| 1448 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1449 | return 0; |
| 1450 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1451 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1452 | |
| 1453 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1454 | |
| 1455 | if (txn->flags & TX_USE_PX_CONN) { |
| 1456 | h = "Proxy-Authorization"; |
| 1457 | len = strlen(h); |
| 1458 | } else { |
| 1459 | h = "Authorization"; |
| 1460 | len = strlen(h); |
| 1461 | } |
| 1462 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1463 | 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] | 1464 | return 0; |
| 1465 | |
| 1466 | h = ctx.line + ctx.val; |
| 1467 | |
| 1468 | p = memchr(h, ' ', ctx.vlen); |
| 1469 | if (!p || p == h) |
| 1470 | return 0; |
| 1471 | |
| 1472 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1473 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1474 | |
| 1475 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1476 | |
| 1477 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1478 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1479 | |
| 1480 | if (len < 0) |
| 1481 | return 0; |
| 1482 | |
| 1483 | |
| 1484 | get_http_auth_buff[len] = '\0'; |
| 1485 | |
| 1486 | p = strchr(get_http_auth_buff, ':'); |
| 1487 | |
| 1488 | if (!p) |
| 1489 | return 0; |
| 1490 | |
| 1491 | txn->auth.user = get_http_auth_buff; |
| 1492 | *p = '\0'; |
| 1493 | txn->auth.pass = p+1; |
| 1494 | |
| 1495 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1496 | return 1; |
| 1497 | } |
| 1498 | |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1502 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1503 | /* |
| 1504 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1505 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1506 | * ensuring that the message does not wrap. The function can be preempted |
| 1507 | * everywhere when data are missing and recalled at the exact same location |
| 1508 | * with no information loss. The message may even be realigned between two |
| 1509 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1510 | * 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] | 1511 | * fields. Note that msg->sol will be initialized after completing the first |
| 1512 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1513 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1514 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1515 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1516 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1517 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1518 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1519 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1520 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1521 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1522 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1523 | ptr = buf->p + msg->next; |
| 1524 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1525 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1526 | if (unlikely(ptr >= end)) |
| 1527 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1528 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1529 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1530 | /* |
| 1531 | * First, states that are specific to the response only. |
| 1532 | * We check them first so that request and headers are |
| 1533 | * closer to each other (accessed more often). |
| 1534 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1535 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1536 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1537 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1538 | /* we have a start of message, but we have to check |
| 1539 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1540 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1541 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1542 | if (unlikely(ptr != buf->p)) { |
| 1543 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1544 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1545 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1546 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1547 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1548 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1549 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1550 | hdr_idx_init(idx); |
| 1551 | state = HTTP_MSG_RPVER; |
| 1552 | goto http_msg_rpver; |
| 1553 | } |
| 1554 | |
| 1555 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1556 | goto http_msg_invalid; |
| 1557 | |
| 1558 | if (unlikely(*ptr == '\n')) |
| 1559 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1560 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1561 | /* stop here */ |
| 1562 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1563 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1564 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1565 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1566 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1567 | /* stop here */ |
| 1568 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1569 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1570 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1571 | case HTTP_MSG_RPVER_SP: |
| 1572 | case HTTP_MSG_RPCODE: |
| 1573 | case HTTP_MSG_RPCODE_SP: |
| 1574 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1575 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1576 | state, ptr, end, |
| 1577 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1578 | if (unlikely(!ptr)) |
| 1579 | return; |
| 1580 | |
| 1581 | /* we have a full response and we know that we have either a CR |
| 1582 | * or an LF at <ptr>. |
| 1583 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1584 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1585 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1586 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1587 | if (likely(*ptr == '\r')) |
| 1588 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1589 | goto http_msg_rpline_end; |
| 1590 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1591 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1592 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1593 | /* msg->sol must point to the first of CR or LF. */ |
| 1594 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1595 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1596 | /* stop here */ |
| 1597 | |
| 1598 | /* |
| 1599 | * Second, states that are specific to the request only |
| 1600 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1601 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1602 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1603 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1604 | /* we have a start of message, but we have to check |
| 1605 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1606 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1607 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1608 | if (likely(ptr != buf->p)) { |
| 1609 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1610 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1611 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1612 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1613 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1614 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1615 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1616 | state = HTTP_MSG_RQMETH; |
| 1617 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1618 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1619 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1620 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1621 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1622 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1623 | if (unlikely(*ptr == '\n')) |
| 1624 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1625 | 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] | 1626 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1627 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1628 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1629 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1630 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1631 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1632 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1633 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1634 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1635 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1636 | case HTTP_MSG_RQMETH_SP: |
| 1637 | case HTTP_MSG_RQURI: |
| 1638 | case HTTP_MSG_RQURI_SP: |
| 1639 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1640 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1641 | state, ptr, end, |
| 1642 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1643 | if (unlikely(!ptr)) |
| 1644 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1645 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1646 | /* we have a full request and we know that we have either a CR |
| 1647 | * or an LF at <ptr>. |
| 1648 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1649 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1650 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1651 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1652 | if (likely(*ptr == '\r')) |
| 1653 | 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] | 1654 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1655 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1656 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1657 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1658 | /* check for HTTP/0.9 request : no version information available. |
| 1659 | * msg->sol must point to the first of CR or LF. |
| 1660 | */ |
| 1661 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1662 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1663 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1664 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1665 | 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] | 1666 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1667 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1668 | /* |
| 1669 | * Common states below |
| 1670 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1671 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1672 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1673 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1674 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1675 | goto http_msg_hdr_name; |
| 1676 | } |
| 1677 | |
| 1678 | if (likely(*ptr == '\r')) |
| 1679 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1680 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1681 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1682 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1683 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1684 | /* assumes msg->sol points to the first char */ |
| 1685 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1686 | 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] | 1687 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1688 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1689 | 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] | 1690 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1691 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1692 | goto http_msg_invalid; |
| 1693 | |
| 1694 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1695 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1696 | |
| 1697 | /* and we still accept this non-token character */ |
| 1698 | 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] | 1699 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1700 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1701 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1702 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1703 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1704 | 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] | 1705 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1706 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1707 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1708 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1709 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1710 | goto http_msg_hdr_val; |
| 1711 | } |
| 1712 | |
| 1713 | if (likely(*ptr == '\r')) |
| 1714 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1715 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1716 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1717 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1718 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1719 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1720 | 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] | 1721 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1722 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1723 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1724 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1725 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1726 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1727 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1728 | goto http_msg_hdr_l1_sp; |
| 1729 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1730 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1731 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1732 | goto http_msg_complete_header; |
| 1733 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1734 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1735 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1736 | /* assumes msg->sol points to the first char, and msg->sov |
| 1737 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1738 | */ |
| 1739 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1740 | 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] | 1741 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1742 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1743 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1744 | * real header end in case it ends with lots of LWS, but is this |
| 1745 | * really needed ? |
| 1746 | */ |
| 1747 | if (likely(*ptr == '\r')) |
| 1748 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1749 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1750 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1751 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1752 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1753 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1754 | 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] | 1755 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1756 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1757 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1758 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1759 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1760 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1761 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1762 | goto http_msg_hdr_val; |
| 1763 | } |
| 1764 | http_msg_complete_header: |
| 1765 | /* |
| 1766 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1767 | * Assumes msg->sol points to the first char, msg->sov points |
| 1768 | * to the first character of the value and msg->eol to the |
| 1769 | * first CR or LF so we know how the line ends. We insert last |
| 1770 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1771 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1772 | 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] | 1773 | idx, idx->tail) < 0)) |
| 1774 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1775 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1776 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1777 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1778 | goto http_msg_hdr_name; |
| 1779 | } |
| 1780 | |
| 1781 | if (likely(*ptr == '\r')) |
| 1782 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1783 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1784 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1785 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1786 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1787 | /* Assumes msg->sol points to the first of either CR or LF. |
| 1788 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 1789 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 1790 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1791 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1792 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1793 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1794 | msg->eoh = msg->sol; |
| 1795 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1796 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1797 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1798 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1799 | |
| 1800 | case HTTP_MSG_ERROR: |
| 1801 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1802 | break; |
| 1803 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1804 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1805 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1806 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1807 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1808 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1809 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1810 | } |
| 1811 | http_msg_ood: |
| 1812 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1813 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1814 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1815 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1816 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1817 | http_msg_invalid: |
| 1818 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1819 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1820 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1821 | return; |
| 1822 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1823 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1824 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1825 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1826 | * nothing is done and 1 is returned. |
| 1827 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1828 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1829 | { |
| 1830 | int delta; |
| 1831 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1832 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1833 | |
| 1834 | if (msg->sl.rq.v_l != 0) |
| 1835 | return 1; |
| 1836 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1837 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1838 | if (txn->meth != HTTP_METH_GET) |
| 1839 | return 0; |
| 1840 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1841 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1842 | delta = 0; |
| 1843 | |
| 1844 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1845 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1846 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1847 | } |
| 1848 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1849 | 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] | 1850 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1851 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1852 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1853 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1854 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1855 | NULL, NULL); |
| 1856 | if (unlikely(!cur_end)) |
| 1857 | return 0; |
| 1858 | |
| 1859 | /* we have a full HTTP/1.0 request now and we know that |
| 1860 | * we have either a CR or an LF at <ptr>. |
| 1861 | */ |
| 1862 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1863 | return 1; |
| 1864 | } |
| 1865 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1866 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1867 | * and "keep-alive" values. If we already know that some headers may safely |
| 1868 | * 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] | 1869 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1870 | * - 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] | 1871 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1872 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1873 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1874 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1875 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1876 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1877 | 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] | 1878 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1879 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1880 | const char *hdr_val = "Connection"; |
| 1881 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1882 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1883 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1884 | return; |
| 1885 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1886 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1887 | hdr_val = "Proxy-Connection"; |
| 1888 | hdr_len = 16; |
| 1889 | } |
| 1890 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1891 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1892 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1893 | 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] | 1894 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1895 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1896 | if (to_del & 2) |
| 1897 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1898 | else |
| 1899 | txn->flags |= TX_CON_KAL_SET; |
| 1900 | } |
| 1901 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1902 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1903 | if (to_del & 1) |
| 1904 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1905 | else |
| 1906 | txn->flags |= TX_CON_CLO_SET; |
| 1907 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1908 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1909 | txn->flags |= TX_HDR_CONN_UPG; |
| 1910 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1911 | } |
| 1912 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1913 | txn->flags |= TX_HDR_CONN_PRS; |
| 1914 | return; |
| 1915 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1916 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1917 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1918 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1919 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1920 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1921 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1922 | 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] | 1923 | { |
| 1924 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1925 | const char *hdr_val = "Connection"; |
| 1926 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1927 | |
| 1928 | ctx.idx = 0; |
| 1929 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1930 | |
| 1931 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1932 | hdr_val = "Proxy-Connection"; |
| 1933 | hdr_len = 16; |
| 1934 | } |
| 1935 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1936 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1937 | 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] | 1938 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1939 | if (wanted & TX_CON_KAL_SET) |
| 1940 | txn->flags |= TX_CON_KAL_SET; |
| 1941 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1942 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1943 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1944 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1945 | if (wanted & TX_CON_CLO_SET) |
| 1946 | txn->flags |= TX_CON_CLO_SET; |
| 1947 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1948 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1949 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1950 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1951 | |
| 1952 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1953 | return; |
| 1954 | |
| 1955 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1956 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1957 | hdr_val = "Connection: close"; |
| 1958 | hdr_len = 17; |
| 1959 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1960 | hdr_val = "Proxy-Connection: close"; |
| 1961 | hdr_len = 23; |
| 1962 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1963 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1967 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1968 | hdr_val = "Connection: keep-alive"; |
| 1969 | hdr_len = 22; |
| 1970 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1971 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1972 | hdr_len = 28; |
| 1973 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1974 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1975 | } |
| 1976 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1977 | } |
| 1978 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 1979 | /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to |
| 1980 | * the first byte of data after the chunk size, so that we know we can forward |
| 1981 | * exactly msg->next bytes. msg->sol contains the exact number of bytes forming |
| 1982 | * the chunk size. That way it is always possible to differentiate between the |
| 1983 | * start of the body and the start of the data. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1984 | * 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] | 1985 | * 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] | 1986 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1987 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1988 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1989 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1990 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1991 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1992 | const char *end = buf->data + buf->size; |
| 1993 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1994 | unsigned int chunk = 0; |
| 1995 | |
| 1996 | /* The chunk size is in the following form, though we are only |
| 1997 | * interested in the size and CRLF : |
| 1998 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1999 | */ |
| 2000 | while (1) { |
| 2001 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2002 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2003 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2004 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2005 | if (c < 0) /* not a hex digit anymore */ |
| 2006 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2007 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2008 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 2009 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2010 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2011 | chunk = (chunk << 4) + c; |
| 2012 | } |
| 2013 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2014 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2015 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2016 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2017 | |
| 2018 | while (http_is_spht[(unsigned char)*ptr]) { |
| 2019 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2020 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2021 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2022 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2023 | } |
| 2024 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2025 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2026 | * for the end of chunk size. |
| 2027 | */ |
| 2028 | while (1) { |
| 2029 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2030 | /* we now have a CR or an LF at ptr */ |
| 2031 | if (likely(*ptr == '\r')) { |
| 2032 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2033 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2034 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2035 | return 0; |
| 2036 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2037 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2038 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2039 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2040 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2041 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2042 | /* done */ |
| 2043 | break; |
| 2044 | } |
| 2045 | else if (*ptr == ';') { |
| 2046 | /* chunk extension, ends at next CRLF */ |
| 2047 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2048 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2049 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2050 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2051 | |
| 2052 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2053 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2054 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2055 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2056 | return 0; |
| 2057 | } |
| 2058 | /* we have a CRLF now, loop above */ |
| 2059 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2060 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2061 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2062 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2063 | } |
| 2064 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2065 | /* 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] | 2066 | * which may or may not be present. We save that into ->next, |
| 2067 | * and the number of bytes parsed into msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2068 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2069 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2070 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2071 | msg->sol += buf->size; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2072 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2073 | msg->chunk_len = chunk; |
| 2074 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2075 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2076 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2077 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2078 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2079 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2080 | } |
| 2081 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2082 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2083 | * 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] | 2084 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2085 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2086 | * If not enough data are available, the function does not change anything |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2087 | * except maybe msg->next if it could parse some lines, and returns zero. |
| 2088 | * If a parse error is encountered, the function returns < 0 and does not |
| 2089 | * change anything except maybe msg->next. Note that the message must |
| 2090 | * already be in HTTP_MSG_TRAILERS state before calling this function, |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2091 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2092 | * forwarding, and that msg->next exactly matches the length of trailers |
| 2093 | * already parsed and not forwarded. It is also important to note that this |
| 2094 | * 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] | 2095 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2096 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2097 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2098 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2099 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2100 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2101 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2102 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2103 | const char *ptr = b_ptr(buf, msg->next); |
| 2104 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2105 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2106 | |
| 2107 | /* scan current line and stop at LF or CRLF */ |
| 2108 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2109 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2110 | return 0; |
| 2111 | |
| 2112 | if (*ptr == '\n') { |
| 2113 | if (!p1) |
| 2114 | p1 = ptr; |
| 2115 | p2 = ptr; |
| 2116 | break; |
| 2117 | } |
| 2118 | |
| 2119 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2120 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2121 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2122 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2123 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2124 | p1 = ptr; |
| 2125 | } |
| 2126 | |
| 2127 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2128 | if (ptr >= buf->data + buf->size) |
| 2129 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | /* after LF; point to beginning of next line */ |
| 2133 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2134 | if (p2 >= buf->data + buf->size) |
| 2135 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2136 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2137 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2138 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2139 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2140 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2141 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2142 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2143 | * Everything was scheduled for forwarding, there's nothing |
| 2144 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2145 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2146 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2147 | msg->msg_state = HTTP_MSG_DONE; |
| 2148 | return 1; |
| 2149 | } |
| 2150 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2151 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2152 | } |
| 2153 | } |
| 2154 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2155 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF |
| 2156 | * or a possible LF alone at the end of a chunk. It automatically adjusts |
| 2157 | * 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] | 2158 | * 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] | 2159 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2160 | * not enough data are available, the function does not change anything and |
| 2161 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2162 | * does not change anything. Note: this function is designed to parse wrapped |
| 2163 | * CRLF at the end of the buffer. |
| 2164 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2165 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2166 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2167 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2168 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2169 | int bytes; |
| 2170 | |
| 2171 | /* NB: we'll check data availabilty at the end. It's not a |
| 2172 | * problem because whatever we match first will be checked |
| 2173 | * against the correct length. |
| 2174 | */ |
| 2175 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2176 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2177 | if (*ptr == '\r') { |
| 2178 | bytes++; |
| 2179 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2180 | if (ptr >= buf->data + buf->size) |
| 2181 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2182 | } |
| 2183 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2184 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2185 | return 0; |
| 2186 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2187 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2188 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2189 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2190 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2191 | |
| 2192 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2193 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2194 | ptr = buf->data; |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2195 | /* Advance ->next to allow the CRLF to be forwarded */ |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2196 | msg->next += bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2197 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2198 | return 1; |
| 2199 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2200 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2201 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2202 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2203 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2204 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2205 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2206 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2207 | { |
| 2208 | int q = 1000; |
| 2209 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2210 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2211 | goto out; |
| 2212 | q = (*qvalue++ - '0') * 1000; |
| 2213 | |
| 2214 | if (*qvalue++ != '.') |
| 2215 | goto out; |
| 2216 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2217 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2218 | goto out; |
| 2219 | q += (*qvalue++ - '0') * 100; |
| 2220 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2221 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2222 | goto out; |
| 2223 | q += (*qvalue++ - '0') * 10; |
| 2224 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2225 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2226 | goto out; |
| 2227 | q += (*qvalue++ - '0') * 1; |
| 2228 | out: |
| 2229 | if (q > 1000) |
| 2230 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2231 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2232 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2233 | return q; |
| 2234 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2235 | |
| 2236 | /* |
| 2237 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2238 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2239 | int select_compression_request_header(struct session *s, struct buffer *req) |
| 2240 | { |
| 2241 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2242 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2243 | struct hdr_ctx ctx; |
| 2244 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2245 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2246 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2247 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 2248 | * 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] | 2249 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 2250 | */ |
| 2251 | ctx.idx = 0; |
| 2252 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2253 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2254 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2255 | (ctx.vlen < 31 || |
| 2256 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2257 | ctx.line[ctx.val + 30] < '6' || |
| 2258 | (ctx.line[ctx.val + 30] == '6' && |
| 2259 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2260 | s->comp_algo = NULL; |
| 2261 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2262 | } |
| 2263 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2264 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2265 | 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] | 2266 | int best_q = 0; |
| 2267 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2268 | ctx.idx = 0; |
| 2269 | 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] | 2270 | const char *qval; |
| 2271 | int q; |
| 2272 | int toklen; |
| 2273 | |
| 2274 | /* try to isolate the token from the optional q-value */ |
| 2275 | toklen = 0; |
| 2276 | while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)]) |
| 2277 | toklen++; |
| 2278 | |
| 2279 | qval = ctx.line + ctx.val + toklen; |
| 2280 | while (1) { |
| 2281 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2282 | qval++; |
| 2283 | |
| 2284 | if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') { |
| 2285 | qval = NULL; |
| 2286 | break; |
| 2287 | } |
| 2288 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2289 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2290 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2291 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2292 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2293 | if (qval >= ctx.line + ctx.val + ctx.vlen) { |
| 2294 | qval = NULL; |
| 2295 | break; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2296 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2297 | if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0) |
| 2298 | break; |
| 2299 | |
| 2300 | while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';') |
| 2301 | qval++; |
| 2302 | } |
| 2303 | |
| 2304 | /* 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] | 2305 | q = qval ? parse_qvalue(qval + 2, NULL) : 1000; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2306 | |
| 2307 | if (q <= best_q) |
| 2308 | continue; |
| 2309 | |
| 2310 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 2311 | if (*(ctx.line + ctx.val) == '*' || |
| 2312 | word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) { |
| 2313 | s->comp_algo = comp_algo; |
| 2314 | best_q = q; |
| 2315 | break; |
| 2316 | } |
| 2317 | } |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2322 | if (s->comp_algo) { |
| 2323 | if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) { |
| 2324 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2325 | ctx.idx = 0; |
| 2326 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2327 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2328 | } |
| 2329 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2330 | return 1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | /* identity is implicit does not require headers */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2334 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
| 2335 | 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] | 2336 | if (comp_algo->add_data == identity_add_data) { |
| 2337 | s->comp_algo = comp_algo; |
| 2338 | return 1; |
| 2339 | } |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2344 | return 0; |
| 2345 | } |
| 2346 | |
| 2347 | /* |
| 2348 | * Selects a comression algorithm depending of the server response. |
| 2349 | */ |
| 2350 | int select_compression_response_header(struct session *s, struct buffer *res) |
| 2351 | { |
| 2352 | struct http_txn *txn = &s->txn; |
| 2353 | struct http_msg *msg = &txn->rsp; |
| 2354 | struct hdr_ctx ctx; |
| 2355 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2356 | |
| 2357 | /* no common compression algorithm was found in request header */ |
| 2358 | if (s->comp_algo == NULL) |
| 2359 | goto fail; |
| 2360 | |
| 2361 | /* HTTP < 1.1 should not be compressed */ |
Willy Tarreau | 7257550 | 2013-12-24 14:41:35 +0100 | [diff] [blame] | 2362 | 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] | 2363 | goto fail; |
| 2364 | |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2365 | /* 200 only */ |
| 2366 | if (txn->status != 200) |
| 2367 | goto fail; |
| 2368 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2369 | /* Content-Length is null */ |
| 2370 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2371 | goto fail; |
| 2372 | |
| 2373 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2374 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2375 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2376 | goto fail; |
| 2377 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2378 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2379 | ctx.idx = 0; |
| 2380 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2381 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2382 | goto fail; |
| 2383 | } |
| 2384 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2385 | comp_type = NULL; |
| 2386 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2387 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2388 | * not listed in the "compression type" directive if any. If no content-type was |
| 2389 | * found but configuration requires one, we don't compress either. Backend has |
| 2390 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2391 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2392 | ctx.idx = 0; |
| 2393 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2394 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2395 | goto fail; |
| 2396 | |
| 2397 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 2398 | (s->fe->comp && (comp_type = s->fe->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2399 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2400 | if (ctx.vlen >= comp_type->name_len && |
| 2401 | 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] | 2402 | /* this Content-Type should be compressed */ |
| 2403 | break; |
| 2404 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2405 | /* this Content-Type should not be compressed */ |
| 2406 | if (comp_type == NULL) |
| 2407 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2408 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2409 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2410 | else { /* no content-type header */ |
| 2411 | if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types)) |
| 2412 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2413 | } |
| 2414 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2415 | /* limit compression rate */ |
| 2416 | if (global.comp_rate_lim > 0) |
| 2417 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2418 | goto fail; |
| 2419 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2420 | /* limit cpu usage */ |
| 2421 | if (idle_pct < compress_min_idle) |
| 2422 | goto fail; |
| 2423 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2424 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2425 | 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] | 2426 | goto fail; |
| 2427 | |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2428 | s->flags |= SN_COMP_READY; |
| 2429 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2430 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2431 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2432 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2433 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2434 | |
| 2435 | /* add Transfer-Encoding header */ |
| 2436 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2437 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2438 | |
| 2439 | /* |
| 2440 | * Add Content-Encoding header when it's not identity encoding. |
| 2441 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2442 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2443 | * header. |
| 2444 | */ |
| 2445 | if (s->comp_algo->add_data != identity_add_data) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2446 | trash.len = 18; |
| 2447 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
| 2448 | memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len); |
| 2449 | trash.len += s->comp_algo->name_len; |
| 2450 | trash.str[trash.len] = '\0'; |
| 2451 | 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] | 2452 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2453 | return 1; |
| 2454 | |
| 2455 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2456 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2457 | return 0; |
| 2458 | } |
| 2459 | |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2460 | void http_adjust_conn_mode(struct session *s, struct http_txn *txn, struct http_msg *msg) |
| 2461 | { |
| 2462 | int tmp = TX_CON_WANT_KAL; |
| 2463 | |
| 2464 | if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 2465 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
| 2466 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2467 | tmp = TX_CON_WANT_TUN; |
| 2468 | |
| 2469 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 2470 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2471 | tmp = TX_CON_WANT_TUN; |
| 2472 | } |
| 2473 | |
| 2474 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 2475 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 2476 | /* option httpclose + server_close => forceclose */ |
| 2477 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 2478 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2479 | tmp = TX_CON_WANT_CLO; |
| 2480 | else |
| 2481 | tmp = TX_CON_WANT_SCL; |
| 2482 | } |
| 2483 | |
| 2484 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
| 2485 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 2486 | tmp = TX_CON_WANT_CLO; |
| 2487 | |
| 2488 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2489 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2490 | |
| 2491 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 2492 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2493 | /* parse the Connection header and possibly clean it */ |
| 2494 | int to_del = 0; |
| 2495 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 2496 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 2497 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
| 2498 | to_del |= 2; /* remove "keep-alive" */ |
| 2499 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2500 | to_del |= 1; /* remove "close" */ |
| 2501 | http_parse_connection_header(txn, msg, to_del); |
| 2502 | } |
| 2503 | |
| 2504 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2505 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2506 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2507 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2508 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 2509 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
| 2510 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
| 2511 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2512 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2513 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2514 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2515 | * processing can continue on next analysers, or zero if it either needs more |
| 2516 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2517 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2518 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2519 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2520 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2521 | 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] | 2522 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2523 | /* |
| 2524 | * We will parse the partial (or complete) lines. |
| 2525 | * We will check the request syntax, and also join multi-line |
| 2526 | * headers. An index of all the lines will be elaborated while |
| 2527 | * parsing. |
| 2528 | * |
| 2529 | * For the parsing, we use a 28 states FSM. |
| 2530 | * |
| 2531 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2532 | * req->buf->p = beginning of request |
| 2533 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2534 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2535 | * msg->eol = end of current header or line (LF or CRLF) |
| 2536 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2537 | * |
| 2538 | * 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] | 2539 | * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2540 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2541 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2542 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2543 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2544 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2545 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2546 | struct http_txn *txn = &s->txn; |
| 2547 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2548 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2549 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2550 | 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] | 2551 | now_ms, __FUNCTION__, |
| 2552 | s, |
| 2553 | req, |
| 2554 | req->rex, req->wex, |
| 2555 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2556 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2557 | req->analysers); |
| 2558 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2559 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2560 | s->srv_error = http_return_srv_error; |
| 2561 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2562 | /* There's a protected area at the end of the buffer for rewriting |
| 2563 | * purposes. We don't want to start to parse the request if the |
| 2564 | * protected area is affected, because we may have to move processed |
| 2565 | * data later, which is much more complicated. |
| 2566 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2567 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2568 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 2569 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2570 | 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] | 2571 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2572 | /* 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] | 2573 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2574 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2575 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2576 | return 0; |
| 2577 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2578 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2579 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2580 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2581 | } |
| 2582 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2583 | /* Note that we have the same problem with the response ; we |
| 2584 | * may want to send a redirect, error or anything which requires |
| 2585 | * some spare space. So we'll ensure that we have at least |
| 2586 | * maxrewrite bytes available in the response buffer before |
| 2587 | * processing that one. This will only affect pipelined |
| 2588 | * keep-alive requests. |
| 2589 | */ |
| 2590 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 2591 | unlikely(!channel_is_rewritable(s->rep) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2592 | bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) || |
| 2593 | bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) { |
| 2594 | if (s->rep->buf->o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2595 | 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] | 2596 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2597 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2598 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2599 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2600 | s->rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2601 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2602 | return 0; |
| 2603 | } |
| 2604 | } |
| 2605 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2606 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2607 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2608 | } |
| 2609 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2610 | /* 1: we might have to print this header in debug mode */ |
| 2611 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2612 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2613 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2614 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2615 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2616 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2617 | /* this is a bit complex : in case of error on the request line, |
| 2618 | * we know that rq.l is still zero, so we display only the part |
| 2619 | * up to the end of the line (truncated by debug_hdr). |
| 2620 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2621 | 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] | 2622 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2623 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2624 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2625 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2626 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2627 | while (cur_idx) { |
| 2628 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2629 | debug_hdr("clihdr", s, sol, eol); |
| 2630 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2631 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2632 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2633 | } |
| 2634 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2635 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2636 | /* |
| 2637 | * Now we quickly check if we have found a full valid request. |
| 2638 | * If not so, we check the FD and buffer states before leaving. |
| 2639 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2640 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2641 | * requests are checked first. When waiting for a second request |
| 2642 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2643 | * we note the error in the session flags but don't set any state. |
| 2644 | * Since the error will be noted there, it will not be counted by |
| 2645 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2646 | * Last, we may increase some tracked counters' http request errors on |
| 2647 | * the cases that are deliberately the client's fault. For instance, |
| 2648 | * a timeout or connection reset is not counted as an error. However |
| 2649 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2650 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2651 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2652 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2653 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2654 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2655 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2656 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2657 | session_inc_http_req_ctr(s); |
| 2658 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2659 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2660 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2661 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2662 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2663 | /* 1: Since we are in header mode, if there's no space |
| 2664 | * left for headers, we won't be able to free more |
| 2665 | * later, so the session will never terminate. We |
| 2666 | * must terminate it now. |
| 2667 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2668 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2669 | /* FIXME: check if URI is set and return Status |
| 2670 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2671 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2672 | session_inc_http_req_ctr(s); |
| 2673 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2674 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2675 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2676 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2677 | goto return_bad_req; |
| 2678 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2679 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2680 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2681 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2682 | if (!(s->flags & SN_ERR_MASK)) |
| 2683 | s->flags |= SN_ERR_CLICL; |
| 2684 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2685 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2686 | goto failed_keep_alive; |
| 2687 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2688 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2689 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2690 | 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] | 2691 | session_inc_http_err_ctr(s); |
| 2692 | } |
| 2693 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2694 | txn->status = 400; |
| 2695 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2696 | msg->msg_state = HTTP_MSG_ERROR; |
| 2697 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2698 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2699 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2700 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2701 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2702 | if (s->listener->counters) |
| 2703 | s->listener->counters->failed_req++; |
| 2704 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2705 | if (!(s->flags & SN_FINST_MASK)) |
| 2706 | s->flags |= SN_FINST_R; |
| 2707 | return 0; |
| 2708 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2709 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2710 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2711 | 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] | 2712 | if (!(s->flags & SN_ERR_MASK)) |
| 2713 | s->flags |= SN_ERR_CLITO; |
| 2714 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2715 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2716 | goto failed_keep_alive; |
| 2717 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2718 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2719 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2720 | 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] | 2721 | session_inc_http_err_ctr(s); |
| 2722 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2723 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2724 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2725 | msg->msg_state = HTTP_MSG_ERROR; |
| 2726 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2727 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2728 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2729 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2730 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2731 | if (s->listener->counters) |
| 2732 | s->listener->counters->failed_req++; |
| 2733 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2734 | if (!(s->flags & SN_FINST_MASK)) |
| 2735 | s->flags |= SN_FINST_R; |
| 2736 | return 0; |
| 2737 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2738 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2739 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2740 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2741 | if (!(s->flags & SN_ERR_MASK)) |
| 2742 | s->flags |= SN_ERR_CLICL; |
| 2743 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2744 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2745 | goto failed_keep_alive; |
| 2746 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2747 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2748 | 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] | 2749 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2750 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2751 | msg->msg_state = HTTP_MSG_ERROR; |
| 2752 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2753 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2754 | session_inc_http_err_ctr(s); |
| 2755 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2756 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2757 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2758 | if (s->listener->counters) |
| 2759 | s->listener->counters->failed_req++; |
| 2760 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2761 | if (!(s->flags & SN_FINST_MASK)) |
| 2762 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2763 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2764 | } |
| 2765 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2766 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2767 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2768 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2769 | #ifdef TCP_QUICKACK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 2770 | 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] | 2771 | /* We need more data, we have to re-enable quick-ack in case we |
| 2772 | * previously disabled it, otherwise we might cause the client |
| 2773 | * to delay next data. |
| 2774 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2775 | 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] | 2776 | } |
| 2777 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2778 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2779 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2780 | /* If the client starts to talk, let's fall back to |
| 2781 | * request timeout processing. |
| 2782 | */ |
| 2783 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2784 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2785 | } |
| 2786 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2787 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2788 | if (!tick_isset(req->analyse_exp)) { |
| 2789 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2790 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2791 | tick_isset(s->be->timeout.httpka)) |
| 2792 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2793 | else |
| 2794 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2795 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2796 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2797 | /* we're not ready yet */ |
| 2798 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2799 | |
| 2800 | failed_keep_alive: |
| 2801 | /* Here we process low-level errors for keep-alive requests. In |
| 2802 | * short, if the request is not the first one and it experiences |
| 2803 | * a timeout, read error or shutdown, we just silently close so |
| 2804 | * that the client can try again. |
| 2805 | */ |
| 2806 | txn->status = 0; |
| 2807 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2808 | req->analysers = 0; |
| 2809 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2810 | s->logs.level = 0; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2811 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2812 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2813 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2814 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2815 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2816 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2817 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2818 | * 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] | 2819 | * 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] | 2820 | * 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] | 2821 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2822 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2823 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2824 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2825 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2826 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2827 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2828 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2829 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2830 | /* kill the pending keep-alive timeout */ |
| 2831 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2832 | req->analyse_exp = TICK_ETERNITY; |
| 2833 | } |
| 2834 | |
| 2835 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2836 | /* Maybe we found in invalid header name while we were configured not |
| 2837 | * to block on that, so we have to capture it now. |
| 2838 | */ |
| 2839 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2840 | 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] | 2841 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2842 | /* |
| 2843 | * 1: identify the method |
| 2844 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2845 | 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] | 2846 | |
| 2847 | /* we can make use of server redirect on GET and HEAD */ |
| 2848 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2849 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2850 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2851 | /* |
| 2852 | * 2: check if the URI matches the monitor_uri. |
| 2853 | * We have to do this for every request which gets in, because |
| 2854 | * the monitor-uri is defined by the frontend. |
| 2855 | */ |
| 2856 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2857 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2858 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2859 | s->fe->monitor_uri, |
| 2860 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2861 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2862 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2863 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2864 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2865 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2866 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2867 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2868 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2869 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2870 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2871 | 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] | 2872 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2873 | ret = acl_pass(ret); |
| 2874 | if (cond->pol == ACL_COND_UNLESS) |
| 2875 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2876 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2877 | if (ret) { |
| 2878 | /* we fail this request, let's return 503 service unavail */ |
| 2879 | txn->status = 503; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2880 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2881 | if (!(s->flags & SN_ERR_MASK)) |
| 2882 | 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] | 2883 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2884 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2885 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2886 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2887 | /* nothing to fail, let's reply normaly */ |
| 2888 | txn->status = 200; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2889 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2890 | if (!(s->flags & SN_ERR_MASK)) |
| 2891 | 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] | 2892 | goto return_prx_cond; |
| 2893 | } |
| 2894 | |
| 2895 | /* |
| 2896 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2897 | * Note: we cannot log anymore if the request has been |
| 2898 | * classified as invalid. |
| 2899 | */ |
| 2900 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2901 | /* we have a complete HTTP request that we must log */ |
| 2902 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2903 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2904 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2905 | if (urilen >= REQURI_LEN) |
| 2906 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2907 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2908 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2909 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2910 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2911 | s->do_log(s); |
| 2912 | } else { |
| 2913 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2914 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2915 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2916 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2917 | /* 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] | 2918 | 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] | 2919 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2920 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2921 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2922 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2923 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2924 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2925 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2926 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2927 | |
| 2928 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2929 | 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] | 2930 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2931 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2932 | * we have what looks like a proxied connection instead of a connection, |
| 2933 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2934 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2935 | * happen to do that despite being non-standard :-( |
| 2936 | * We consider that a request not beginning with either '/' or '*' is |
| 2937 | * a proxied connection, which covers both "scheme://location" and |
| 2938 | * CONNECT ip:port. |
| 2939 | */ |
| 2940 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2941 | 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] | 2942 | txn->flags |= TX_USE_PX_CONN; |
| 2943 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2944 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2945 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2946 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2947 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2948 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2949 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2950 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2951 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2952 | /* 6: determine the transfer-length. |
| 2953 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2954 | * the presence of a message-body in a REQUEST and its transfer length |
| 2955 | * must be determined that way (in order of precedence) : |
| 2956 | * 1. The presence of a message-body in a request is signaled by the |
| 2957 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2958 | * in the request's header fields. When a request message contains |
| 2959 | * both a message-body of non-zero length and a method that does |
| 2960 | * not define any semantics for that request message-body, then an |
| 2961 | * origin server SHOULD either ignore the message-body or respond |
| 2962 | * with an appropriate error message (e.g., 413). A proxy or |
| 2963 | * gateway, when presented the same request, SHOULD either forward |
| 2964 | * the request inbound with the message- body or ignore the |
| 2965 | * message-body when determining a response. |
| 2966 | * |
| 2967 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2968 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2969 | * transfer-length is defined by the use of this transfer-coding. |
| 2970 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2971 | * transfer-coding is not present, the transfer-length is defined |
| 2972 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2973 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2974 | * 3. If a Content-Length header field is present, its decimal value in |
| 2975 | * OCTETs represents both the entity-length and the transfer-length. |
| 2976 | * If a message is received with both a Transfer-Encoding header |
| 2977 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2978 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2979 | * 4. By the server closing the connection. (Closing the connection |
| 2980 | * cannot be used to indicate the end of a request body, since that |
| 2981 | * would leave no possibility for the server to send back a response.) |
| 2982 | * |
| 2983 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2984 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2985 | * it is terminated by closing the connection. When the "chunked" |
| 2986 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2987 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2988 | */ |
| 2989 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2990 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2991 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2992 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2993 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2994 | 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] | 2995 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2996 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2997 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2998 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2999 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3000 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3001 | break; |
| 3002 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3003 | } |
| 3004 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3005 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3006 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3007 | 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] | 3008 | signed long long cl; |
| 3009 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3010 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3011 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3012 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3013 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3014 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3015 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3016 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3017 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3018 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3019 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3020 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3021 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3022 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3023 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3024 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3025 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3026 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3027 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3028 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3029 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3030 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3031 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3032 | } |
| 3033 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3034 | /* bodyless requests have a known length */ |
| 3035 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3036 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3037 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3038 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 3039 | * only change if both the request and the config reference something else. |
| 3040 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3041 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3042 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3043 | * if FE and BE have the same settings (common). The method consists in |
| 3044 | * checking if options changed between the two calls (implying that either |
| 3045 | * one is non-null, or one of them is non-null and we are there for the first |
| 3046 | * time. |
| 3047 | */ |
| 3048 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 3049 | ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) |
| 3050 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3051 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3052 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3053 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3054 | req->analyse_exp = TICK_ETERNITY; |
| 3055 | return 1; |
| 3056 | |
| 3057 | return_bad_req: |
| 3058 | /* We centralize bad requests processing here */ |
| 3059 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3060 | /* we detected a parsing error. We want to archive this request |
| 3061 | * in the dedicated proxy area for later troubleshooting. |
| 3062 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3063 | 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] | 3064 | } |
| 3065 | |
| 3066 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3067 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3068 | 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] | 3069 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3070 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3071 | if (s->listener->counters) |
| 3072 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3073 | |
| 3074 | return_prx_cond: |
| 3075 | if (!(s->flags & SN_ERR_MASK)) |
| 3076 | s->flags |= SN_ERR_PRXCOND; |
| 3077 | if (!(s->flags & SN_FINST_MASK)) |
| 3078 | s->flags |= SN_FINST_R; |
| 3079 | |
| 3080 | req->analysers = 0; |
| 3081 | req->analyse_exp = TICK_ETERNITY; |
| 3082 | return 0; |
| 3083 | } |
| 3084 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3085 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3086 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3087 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3088 | * and program a response message if something was unexpected. It cannot fail |
| 3089 | * 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] | 3090 | * 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] | 3091 | * s->target which is supposed to already point to the stats applet. The caller |
| 3092 | * is expected to have already assigned an appctx to the session. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3093 | */ |
| 3094 | int http_handle_stats(struct session *s, struct channel *req) |
| 3095 | { |
| 3096 | struct stats_admin_rule *stats_admin_rule; |
| 3097 | struct stream_interface *si = s->rep->prod; |
| 3098 | struct http_txn *txn = &s->txn; |
| 3099 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3100 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3101 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3102 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3103 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3104 | appctx = si_appctx(si); |
| 3105 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3106 | appctx->st1 = appctx->st2 = 0; |
| 3107 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3108 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | af3cf70 | 2014-04-22 22:19:53 +0200 | [diff] [blame] | 3109 | if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD)) |
| 3110 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3111 | |
| 3112 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3113 | lookup = uri + uri_auth->uri_len; |
| 3114 | |
| 3115 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3116 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3117 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3118 | break; |
| 3119 | } |
| 3120 | } |
| 3121 | |
| 3122 | if (uri_auth->refresh) { |
| 3123 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3124 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3125 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3126 | break; |
| 3127 | } |
| 3128 | } |
| 3129 | } |
| 3130 | |
| 3131 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3132 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3133 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3134 | break; |
| 3135 | } |
| 3136 | } |
| 3137 | |
| 3138 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3139 | if (memcmp(h, ";st=", 4) == 0) { |
| 3140 | int i; |
| 3141 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3142 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3143 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3144 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3145 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3146 | break; |
| 3147 | } |
| 3148 | } |
| 3149 | break; |
| 3150 | } |
| 3151 | } |
| 3152 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3153 | appctx->ctx.stats.scope_str = 0; |
| 3154 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3155 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3156 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3157 | int itx = 0; |
| 3158 | const char *h2; |
| 3159 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3160 | const char *err; |
| 3161 | |
| 3162 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3163 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3164 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3165 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3166 | itx++; |
| 3167 | h++; |
| 3168 | } |
| 3169 | |
| 3170 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3171 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3172 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3173 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3174 | /* 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] | 3175 | memcpy(scope_txt, h2, itx); |
| 3176 | scope_txt[itx] = '\0'; |
| 3177 | err = invalid_char(scope_txt); |
| 3178 | if (err) { |
| 3179 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3180 | appctx->ctx.stats.scope_str = 0; |
| 3181 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3182 | } |
| 3183 | break; |
| 3184 | } |
| 3185 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3186 | |
| 3187 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3188 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3189 | int ret = 1; |
| 3190 | |
| 3191 | if (stats_admin_rule->cond) { |
| 3192 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 3193 | ret = acl_pass(ret); |
| 3194 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3195 | ret = !ret; |
| 3196 | } |
| 3197 | |
| 3198 | if (ret) { |
| 3199 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3200 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3201 | break; |
| 3202 | } |
| 3203 | } |
| 3204 | |
| 3205 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3206 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3207 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3208 | /* we'll need the request body, possibly after sending 100-continue */ |
| 3209 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3210 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3211 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3212 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3213 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3214 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3215 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3216 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3217 | else { |
| 3218 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3219 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3220 | } |
| 3221 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3222 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3223 | return 1; |
| 3224 | } |
| 3225 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3226 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3227 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3228 | */ |
| 3229 | static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
| 3230 | { |
| 3231 | #ifdef IP_TOS |
| 3232 | if (from.ss_family == AF_INET) |
| 3233 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3234 | #endif |
| 3235 | #ifdef IPV6_TCLASS |
| 3236 | if (from.ss_family == AF_INET6) { |
| 3237 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3238 | /* v4-mapped addresses need IP_TOS */ |
| 3239 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3240 | else |
| 3241 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3242 | } |
| 3243 | #endif |
| 3244 | } |
| 3245 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3246 | /* Returns the number of characters written to destination, |
| 3247 | * -1 on internal error and -2 if no replacement took place. |
| 3248 | */ |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3249 | 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] | 3250 | const char *rep_str) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3251 | { |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 3252 | if (!regex_exec_match2(re, val, len, MAX_MATCH, pmatch, 0)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3253 | return -2; |
| 3254 | |
| 3255 | return exp_replace(dst, dst_size, val, rep_str, pmatch); |
| 3256 | } |
| 3257 | |
| 3258 | /* Returns the number of characters written to destination, |
| 3259 | * -1 on internal error and -2 if no replacement took place. |
| 3260 | */ |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3261 | 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] | 3262 | const char *rep_str) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3263 | { |
| 3264 | char* p = val; |
| 3265 | char* dst_end = dst + dst_size; |
| 3266 | char* dst_p = dst; |
| 3267 | |
| 3268 | for (;;) { |
| 3269 | char *p_delim; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3270 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3271 | /* look for delim. */ |
| 3272 | p_delim = p; |
| 3273 | while (p_delim < p + len && *p_delim != delim) |
| 3274 | p_delim++; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3275 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 3276 | if (regex_exec_match2(re, p, p_delim-p, MAX_MATCH, pmatch, 0)) { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3277 | int replace_n = exp_replace(dst_p, dst_end - dst_p, p, rep_str, pmatch); |
| 3278 | |
| 3279 | if (replace_n < 0) |
| 3280 | return -1; |
| 3281 | |
| 3282 | dst_p += replace_n; |
| 3283 | } else { |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3284 | uint len = p_delim - p; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3285 | |
| 3286 | if (dst_p + len >= dst_end) |
| 3287 | return -1; |
| 3288 | |
| 3289 | memcpy(dst_p, p, len); |
| 3290 | dst_p += len; |
| 3291 | } |
| 3292 | |
| 3293 | if (dst_p >= dst_end) |
| 3294 | return -1; |
| 3295 | |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3296 | /* end of the replacements. */ |
| 3297 | if (p_delim >= p + len) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3298 | break; |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3299 | |
| 3300 | /* Next part. */ |
| 3301 | *dst_p++ = delim; |
| 3302 | p = p_delim + 1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3303 | } |
| 3304 | |
| 3305 | return dst_p - dst; |
| 3306 | } |
| 3307 | |
| 3308 | 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] | 3309 | 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] | 3310 | struct hdr_ctx* ctx, int action) |
| 3311 | { |
| 3312 | ctx->idx = 0; |
| 3313 | |
| 3314 | while (http_find_full_header2(name, name_len, buf, idx, ctx)) { |
| 3315 | struct hdr_idx_elem *hdr = idx->v + ctx->idx; |
| 3316 | int delta; |
Willy Tarreau | aa435e7 | 2015-01-29 14:01:34 +0100 | [diff] [blame] | 3317 | char* val = (char*)ctx->line + ctx->val; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3318 | char* val_end = (char*)ctx->line + hdr->len; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3319 | char* reg_dst_buf; |
| 3320 | uint reg_dst_buf_size; |
| 3321 | int n_replaced; |
| 3322 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3323 | trash.len = build_logline(s, trash.str, trash.size, fmt); |
| 3324 | |
| 3325 | if (trash.len >= trash.size - 1) |
| 3326 | return -1; |
| 3327 | |
| 3328 | reg_dst_buf = trash.str + trash.len + 1; |
| 3329 | reg_dst_buf_size = trash.size - trash.len - 1; |
| 3330 | |
| 3331 | switch (action) { |
| 3332 | case HTTP_REQ_ACT_REPLACE_VAL: |
| 3333 | case HTTP_RES_ACT_REPLACE_VAL: |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3334 | 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] | 3335 | break; |
| 3336 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3337 | case HTTP_RES_ACT_REPLACE_HDR: |
Thierry FOURNIER | c9c2daf | 2014-06-18 11:53:08 +0200 | [diff] [blame] | 3338 | 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] | 3339 | break; |
| 3340 | default: /* impossible */ |
| 3341 | return -1; |
| 3342 | } |
| 3343 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3344 | switch (n_replaced) { |
| 3345 | case -1: return -1; |
| 3346 | case -2: continue; |
| 3347 | } |
| 3348 | |
| 3349 | delta = buffer_replace2(msg->chn->buf, val, val_end, reg_dst_buf, n_replaced); |
| 3350 | |
| 3351 | hdr->len += delta; |
| 3352 | http_msg_move_end(msg, delta); |
| 3353 | } |
| 3354 | |
| 3355 | return 0; |
| 3356 | } |
| 3357 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3358 | /* Executes the http-request rules <rules> for session <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3359 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3360 | * further processing of the request (auth, deny, ...), and defaults to |
| 3361 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3362 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 3363 | * on txn->flags if it encounters a tarpit rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3364 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3365 | enum rule_result |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3366 | 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] | 3367 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3368 | struct connection *cli_conn; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3369 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3370 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3371 | const char *auth_realm; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3372 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3373 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3374 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3375 | continue; |
| 3376 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3377 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3378 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3379 | int ret; |
| 3380 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3381 | 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] | 3382 | ret = acl_pass(ret); |
| 3383 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3384 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3385 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3386 | |
| 3387 | if (!ret) /* condition not matched */ |
| 3388 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3389 | } |
| 3390 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3391 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3392 | switch (rule->action) { |
| 3393 | case HTTP_REQ_ACT_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3394 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3395 | |
| 3396 | case HTTP_REQ_ACT_DENY: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3397 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3398 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3399 | case HTTP_REQ_ACT_TARPIT: |
| 3400 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3401 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3402 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3403 | case HTTP_REQ_ACT_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3404 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3405 | auth_realm = rule->arg.auth.realm; |
| 3406 | if (!auth_realm) { |
| 3407 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3408 | auth_realm = STATS_DEFAULT_REALM; |
| 3409 | else |
| 3410 | auth_realm = px->id; |
| 3411 | } |
| 3412 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3413 | * count one error, because normal browsing won't significantly |
| 3414 | * increase the counter but brute force attempts will. |
| 3415 | */ |
| 3416 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3417 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
| 3418 | stream_int_retnclose(&s->si[0], &trash); |
| 3419 | session_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3420 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3421 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3422 | case HTTP_REQ_ACT_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3423 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3424 | return HTTP_RULE_RES_BADREQ; |
| 3425 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3426 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3427 | case HTTP_REQ_ACT_SET_NICE: |
| 3428 | s->task->nice = rule->arg.nice; |
| 3429 | break; |
| 3430 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3431 | case HTTP_REQ_ACT_SET_TOS: |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3432 | 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] | 3433 | 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] | 3434 | break; |
| 3435 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3436 | case HTTP_REQ_ACT_SET_MARK: |
| 3437 | #ifdef SO_MARK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3438 | 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] | 3439 | 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] | 3440 | #endif |
| 3441 | break; |
| 3442 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3443 | case HTTP_REQ_ACT_SET_LOGL: |
| 3444 | s->logs.level = rule->arg.loglevel; |
| 3445 | break; |
| 3446 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3447 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3448 | case HTTP_REQ_ACT_REPLACE_VAL: |
| 3449 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3450 | 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] | 3451 | &rule->arg.hdr_add.re, &ctx, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3452 | return HTTP_RULE_RES_BADREQ; |
| 3453 | break; |
| 3454 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3455 | case HTTP_REQ_ACT_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3456 | ctx.idx = 0; |
| 3457 | /* remove all occurrences of the header */ |
| 3458 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3459 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3460 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3461 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3462 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3463 | |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3464 | case HTTP_REQ_ACT_SET_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3465 | case HTTP_REQ_ACT_ADD_HDR: |
| 3466 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3467 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3468 | trash.len = rule->arg.hdr_add.name_len; |
| 3469 | trash.str[trash.len++] = ':'; |
| 3470 | trash.str[trash.len++] = ' '; |
| 3471 | 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] | 3472 | |
| 3473 | if (rule->action == HTTP_REQ_ACT_SET_HDR) { |
| 3474 | /* remove all occurrences of the header */ |
| 3475 | ctx.idx = 0; |
| 3476 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3477 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3478 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3479 | } |
| 3480 | } |
| 3481 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3482 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3483 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3484 | |
| 3485 | case HTTP_REQ_ACT_DEL_ACL: |
| 3486 | case HTTP_REQ_ACT_DEL_MAP: { |
| 3487 | struct pat_ref *ref; |
| 3488 | char *key; |
| 3489 | int len; |
| 3490 | |
| 3491 | /* collect reference */ |
| 3492 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3493 | if (!ref) |
| 3494 | continue; |
| 3495 | |
| 3496 | /* collect key */ |
| 3497 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3498 | key = trash.str; |
| 3499 | key[len] = '\0'; |
| 3500 | |
| 3501 | /* perform update */ |
| 3502 | /* returned code: 1=ok, 0=ko */ |
| 3503 | pat_ref_delete(ref, key); |
| 3504 | |
| 3505 | break; |
| 3506 | } |
| 3507 | |
| 3508 | case HTTP_REQ_ACT_ADD_ACL: { |
| 3509 | struct pat_ref *ref; |
| 3510 | char *key; |
| 3511 | struct chunk *trash_key; |
| 3512 | int len; |
| 3513 | |
| 3514 | trash_key = get_trash_chunk(); |
| 3515 | |
| 3516 | /* collect reference */ |
| 3517 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3518 | if (!ref) |
| 3519 | continue; |
| 3520 | |
| 3521 | /* collect key */ |
| 3522 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3523 | key = trash_key->str; |
| 3524 | key[len] = '\0'; |
| 3525 | |
| 3526 | /* perform update */ |
| 3527 | /* add entry only if it does not already exist */ |
| 3528 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3529 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3530 | |
| 3531 | break; |
| 3532 | } |
| 3533 | |
| 3534 | case HTTP_REQ_ACT_SET_MAP: { |
| 3535 | struct pat_ref *ref; |
| 3536 | char *key, *value; |
| 3537 | struct chunk *trash_key, *trash_value; |
| 3538 | int len; |
| 3539 | |
| 3540 | trash_key = get_trash_chunk(); |
| 3541 | trash_value = get_trash_chunk(); |
| 3542 | |
| 3543 | /* collect reference */ |
| 3544 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3545 | if (!ref) |
| 3546 | continue; |
| 3547 | |
| 3548 | /* collect key */ |
| 3549 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3550 | key = trash_key->str; |
| 3551 | key[len] = '\0'; |
| 3552 | |
| 3553 | /* collect value */ |
| 3554 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3555 | value = trash_value->str; |
| 3556 | value[len] = '\0'; |
| 3557 | |
| 3558 | /* perform update */ |
| 3559 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3560 | /* update entry if it exists */ |
| 3561 | pat_ref_set(ref, key, value, NULL); |
| 3562 | else |
| 3563 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3564 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3565 | |
| 3566 | break; |
| 3567 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3568 | |
| 3569 | case HTTP_REQ_ACT_CUSTOM_CONT: |
| 3570 | rule->action_ptr(rule, px, s, txn); |
| 3571 | break; |
| 3572 | |
| 3573 | case HTTP_REQ_ACT_CUSTOM_STOP: |
| 3574 | rule->action_ptr(rule, px, s, txn); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3575 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3576 | |
| 3577 | case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX: |
| 3578 | /* Note: only the first valid tracking parameter of each |
| 3579 | * applies. |
| 3580 | */ |
| 3581 | |
| 3582 | if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) { |
| 3583 | struct stktable *t; |
| 3584 | struct stksess *ts; |
| 3585 | struct stktable_key *key; |
| 3586 | void *ptr; |
| 3587 | |
| 3588 | t = rule->act_prm.trk_ctr.table.t; |
| 3589 | key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL); |
| 3590 | |
| 3591 | if (key && (ts = stktable_get_entry(t, key))) { |
| 3592 | session_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts); |
| 3593 | |
| 3594 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3595 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3596 | if (ptr) |
| 3597 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3598 | |
| 3599 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3600 | if (ptr) |
| 3601 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3602 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3603 | |
| 3604 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 3605 | if (s->fe != s->be) |
| 3606 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3607 | } |
| 3608 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3609 | } |
| 3610 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3611 | |
| 3612 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3613 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3614 | } |
| 3615 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3616 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3617 | /* Executes the http-response rules <rules> for session <s>, proxy <px> and |
| 3618 | * transaction <txn>. Returns the first rule that prevents further processing |
| 3619 | * of the response (deny, ...) or NULL if it executed all rules or stopped |
| 3620 | * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny |
| 3621 | * rule. |
| 3622 | */ |
| 3623 | static struct http_res_rule * |
| 3624 | http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 3625 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3626 | struct connection *cli_conn; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3627 | struct http_res_rule *rule; |
| 3628 | struct hdr_ctx ctx; |
| 3629 | |
| 3630 | list_for_each_entry(rule, rules, list) { |
| 3631 | if (rule->action >= HTTP_RES_ACT_MAX) |
| 3632 | continue; |
| 3633 | |
| 3634 | /* check optional condition */ |
| 3635 | if (rule->cond) { |
| 3636 | int ret; |
| 3637 | |
| 3638 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 3639 | ret = acl_pass(ret); |
| 3640 | |
| 3641 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3642 | ret = !ret; |
| 3643 | |
| 3644 | if (!ret) /* condition not matched */ |
| 3645 | continue; |
| 3646 | } |
| 3647 | |
| 3648 | |
| 3649 | switch (rule->action) { |
| 3650 | case HTTP_RES_ACT_ALLOW: |
| 3651 | return NULL; /* "allow" rules are OK */ |
| 3652 | |
| 3653 | case HTTP_RES_ACT_DENY: |
| 3654 | txn->flags |= TX_SVDENY; |
| 3655 | return rule; |
| 3656 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3657 | case HTTP_RES_ACT_SET_NICE: |
| 3658 | s->task->nice = rule->arg.nice; |
| 3659 | break; |
| 3660 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3661 | case HTTP_RES_ACT_SET_TOS: |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3662 | 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] | 3663 | 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] | 3664 | break; |
| 3665 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3666 | case HTTP_RES_ACT_SET_MARK: |
| 3667 | #ifdef SO_MARK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3668 | 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] | 3669 | 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] | 3670 | #endif |
| 3671 | break; |
| 3672 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3673 | case HTTP_RES_ACT_SET_LOGL: |
| 3674 | s->logs.level = rule->arg.loglevel; |
| 3675 | break; |
| 3676 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3677 | case HTTP_RES_ACT_REPLACE_HDR: |
| 3678 | case HTTP_RES_ACT_REPLACE_VAL: |
| 3679 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3680 | 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] | 3681 | &rule->arg.hdr_add.re, &ctx, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3682 | return NULL; /* note: we should report an error here */ |
| 3683 | break; |
| 3684 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3685 | case HTTP_RES_ACT_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3686 | ctx.idx = 0; |
| 3687 | /* remove all occurrences of the header */ |
| 3688 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3689 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3690 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3691 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3692 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3693 | |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3694 | case HTTP_RES_ACT_SET_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3695 | case HTTP_RES_ACT_ADD_HDR: |
| 3696 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3697 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3698 | trash.len = rule->arg.hdr_add.name_len; |
| 3699 | trash.str[trash.len++] = ':'; |
| 3700 | trash.str[trash.len++] = ' '; |
| 3701 | 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] | 3702 | |
| 3703 | if (rule->action == HTTP_RES_ACT_SET_HDR) { |
| 3704 | /* remove all occurrences of the header */ |
| 3705 | ctx.idx = 0; |
| 3706 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3707 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3708 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3709 | } |
| 3710 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3711 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3712 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3713 | |
| 3714 | case HTTP_RES_ACT_DEL_ACL: |
| 3715 | case HTTP_RES_ACT_DEL_MAP: { |
| 3716 | struct pat_ref *ref; |
| 3717 | char *key; |
| 3718 | int len; |
| 3719 | |
| 3720 | /* collect reference */ |
| 3721 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3722 | if (!ref) |
| 3723 | continue; |
| 3724 | |
| 3725 | /* collect key */ |
| 3726 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3727 | key = trash.str; |
| 3728 | key[len] = '\0'; |
| 3729 | |
| 3730 | /* perform update */ |
| 3731 | /* returned code: 1=ok, 0=ko */ |
| 3732 | pat_ref_delete(ref, key); |
| 3733 | |
| 3734 | break; |
| 3735 | } |
| 3736 | |
| 3737 | case HTTP_RES_ACT_ADD_ACL: { |
| 3738 | struct pat_ref *ref; |
| 3739 | char *key; |
| 3740 | struct chunk *trash_key; |
| 3741 | int len; |
| 3742 | |
| 3743 | trash_key = get_trash_chunk(); |
| 3744 | |
| 3745 | /* collect reference */ |
| 3746 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3747 | if (!ref) |
| 3748 | continue; |
| 3749 | |
| 3750 | /* collect key */ |
| 3751 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3752 | key = trash_key->str; |
| 3753 | key[len] = '\0'; |
| 3754 | |
| 3755 | /* perform update */ |
| 3756 | /* check if the entry already exists */ |
| 3757 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3758 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3759 | |
| 3760 | break; |
| 3761 | } |
| 3762 | |
| 3763 | case HTTP_RES_ACT_SET_MAP: { |
| 3764 | struct pat_ref *ref; |
| 3765 | char *key, *value; |
| 3766 | struct chunk *trash_key, *trash_value; |
| 3767 | int len; |
| 3768 | |
| 3769 | trash_key = get_trash_chunk(); |
| 3770 | trash_value = get_trash_chunk(); |
| 3771 | |
| 3772 | /* collect reference */ |
| 3773 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3774 | if (!ref) |
| 3775 | continue; |
| 3776 | |
| 3777 | /* collect key */ |
| 3778 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3779 | key = trash_key->str; |
| 3780 | key[len] = '\0'; |
| 3781 | |
| 3782 | /* collect value */ |
| 3783 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3784 | value = trash_value->str; |
| 3785 | value[len] = '\0'; |
| 3786 | |
| 3787 | /* perform update */ |
| 3788 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3789 | /* update entry if it exists */ |
| 3790 | pat_ref_set(ref, key, value, NULL); |
| 3791 | else |
| 3792 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3793 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3794 | |
| 3795 | break; |
| 3796 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3797 | |
| 3798 | case HTTP_RES_ACT_CUSTOM_CONT: |
| 3799 | rule->action_ptr(rule, px, s, txn); |
| 3800 | break; |
| 3801 | |
| 3802 | case HTTP_RES_ACT_CUSTOM_STOP: |
| 3803 | rule->action_ptr(rule, px, s, txn); |
| 3804 | return rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3805 | } |
| 3806 | } |
| 3807 | |
| 3808 | /* we reached the end of the rules, nothing to report */ |
| 3809 | return NULL; |
| 3810 | } |
| 3811 | |
| 3812 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3813 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3814 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3815 | * as too large a request to build a valid response. |
| 3816 | */ |
| 3817 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn) |
| 3818 | { |
| 3819 | struct http_msg *msg = &txn->req; |
| 3820 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3821 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3822 | |
| 3823 | /* build redirect message */ |
| 3824 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3825 | case 308: |
| 3826 | msg_fmt = HTTP_308; |
| 3827 | break; |
| 3828 | case 307: |
| 3829 | msg_fmt = HTTP_307; |
| 3830 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3831 | case 303: |
| 3832 | msg_fmt = HTTP_303; |
| 3833 | break; |
| 3834 | case 301: |
| 3835 | msg_fmt = HTTP_301; |
| 3836 | break; |
| 3837 | case 302: |
| 3838 | default: |
| 3839 | msg_fmt = HTTP_302; |
| 3840 | break; |
| 3841 | } |
| 3842 | |
| 3843 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3844 | return 0; |
| 3845 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3846 | location = trash.str + trash.len; |
| 3847 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3848 | switch(rule->type) { |
| 3849 | case REDIRECT_TYPE_SCHEME: { |
| 3850 | const char *path; |
| 3851 | const char *host; |
| 3852 | struct hdr_ctx ctx; |
| 3853 | int pathlen; |
| 3854 | int hostlen; |
| 3855 | |
| 3856 | host = ""; |
| 3857 | hostlen = 0; |
| 3858 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3859 | 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] | 3860 | host = ctx.line + ctx.val; |
| 3861 | hostlen = ctx.vlen; |
| 3862 | } |
| 3863 | |
| 3864 | path = http_get_path(txn); |
| 3865 | /* build message using path */ |
| 3866 | if (path) { |
| 3867 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3868 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3869 | int qs = 0; |
| 3870 | while (qs < pathlen) { |
| 3871 | if (path[qs] == '?') { |
| 3872 | pathlen = qs; |
| 3873 | break; |
| 3874 | } |
| 3875 | qs++; |
| 3876 | } |
| 3877 | } |
| 3878 | } else { |
| 3879 | path = "/"; |
| 3880 | pathlen = 1; |
| 3881 | } |
| 3882 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3883 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3884 | /* check if we can add scheme + "://" + host + path */ |
| 3885 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3886 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3887 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3888 | /* add scheme */ |
| 3889 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3890 | trash.len += rule->rdr_len; |
| 3891 | } |
| 3892 | else { |
| 3893 | /* add scheme with executing log format */ |
| 3894 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3895 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3896 | /* check if we can add scheme + "://" + host + path */ |
| 3897 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 3898 | return 0; |
| 3899 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3900 | /* add "://" */ |
| 3901 | memcpy(trash.str + trash.len, "://", 3); |
| 3902 | trash.len += 3; |
| 3903 | |
| 3904 | /* add host */ |
| 3905 | memcpy(trash.str + trash.len, host, hostlen); |
| 3906 | trash.len += hostlen; |
| 3907 | |
| 3908 | /* add path */ |
| 3909 | memcpy(trash.str + trash.len, path, pathlen); |
| 3910 | trash.len += pathlen; |
| 3911 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3912 | /* 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] | 3913 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3914 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3915 | if (trash.len > trash.size - 5) |
| 3916 | return 0; |
| 3917 | trash.str[trash.len] = '/'; |
| 3918 | trash.len++; |
| 3919 | } |
| 3920 | |
| 3921 | break; |
| 3922 | } |
| 3923 | case REDIRECT_TYPE_PREFIX: { |
| 3924 | const char *path; |
| 3925 | int pathlen; |
| 3926 | |
| 3927 | path = http_get_path(txn); |
| 3928 | /* build message using path */ |
| 3929 | if (path) { |
| 3930 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3931 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3932 | int qs = 0; |
| 3933 | while (qs < pathlen) { |
| 3934 | if (path[qs] == '?') { |
| 3935 | pathlen = qs; |
| 3936 | break; |
| 3937 | } |
| 3938 | qs++; |
| 3939 | } |
| 3940 | } |
| 3941 | } else { |
| 3942 | path = "/"; |
| 3943 | pathlen = 1; |
| 3944 | } |
| 3945 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3946 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3947 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 3948 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3949 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3950 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3951 | * add anything, otherwise it makes it hard for the user to |
| 3952 | * configure a self-redirection. |
| 3953 | */ |
| 3954 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 3955 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3956 | trash.len += rule->rdr_len; |
| 3957 | } |
| 3958 | } |
| 3959 | else { |
| 3960 | /* add prefix with executing log format */ |
| 3961 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 3962 | |
| 3963 | /* Check length */ |
| 3964 | if (trash.len + pathlen > trash.size - 4) |
| 3965 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3966 | } |
| 3967 | |
| 3968 | /* add path */ |
| 3969 | memcpy(trash.str + trash.len, path, pathlen); |
| 3970 | trash.len += pathlen; |
| 3971 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3972 | /* 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] | 3973 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3974 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3975 | if (trash.len > trash.size - 5) |
| 3976 | return 0; |
| 3977 | trash.str[trash.len] = '/'; |
| 3978 | trash.len++; |
| 3979 | } |
| 3980 | |
| 3981 | break; |
| 3982 | } |
| 3983 | case REDIRECT_TYPE_LOCATION: |
| 3984 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3985 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3986 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 3987 | return 0; |
| 3988 | |
| 3989 | /* add location */ |
| 3990 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3991 | trash.len += rule->rdr_len; |
| 3992 | } |
| 3993 | else { |
| 3994 | /* add location with executing log format */ |
| 3995 | 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] | 3996 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3997 | /* Check left length */ |
| 3998 | if (trash.len > trash.size - 4) |
| 3999 | return 0; |
| 4000 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4001 | break; |
| 4002 | } |
| 4003 | |
| 4004 | if (rule->cookie_len) { |
| 4005 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 4006 | trash.len += 14; |
| 4007 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 4008 | trash.len += rule->cookie_len; |
| 4009 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 4010 | trash.len += 2; |
| 4011 | } |
| 4012 | |
| 4013 | /* add end of headers and the keep-alive/close status. |
| 4014 | * We may choose to set keep-alive if the Location begins |
| 4015 | * with a slash, because the client will come back to the |
| 4016 | * same server. |
| 4017 | */ |
| 4018 | txn->status = rule->code; |
| 4019 | /* let's log the request time */ |
| 4020 | s->logs.tv_request = now; |
| 4021 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4022 | if (*location == '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4023 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 4024 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
| 4025 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4026 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4027 | /* keep-alive possible */ |
| 4028 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
| 4029 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4030 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 4031 | trash.len += 30; |
| 4032 | } else { |
| 4033 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 4034 | trash.len += 24; |
| 4035 | } |
| 4036 | } |
| 4037 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 4038 | trash.len += 4; |
| 4039 | bo_inject(txn->rsp.chn, trash.str, trash.len); |
| 4040 | /* "eat" the request */ |
| 4041 | bi_fast_delete(txn->req.chn->buf, msg->sov); |
Willy Tarreau | 6d8bac7 | 2014-04-25 12:19:32 +0200 | [diff] [blame] | 4042 | msg->next -= msg->sov; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4043 | msg->sov = 0; |
| 4044 | txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY; |
| 4045 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 4046 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4047 | txn->rsp.msg_state = HTTP_MSG_DONE; |
| 4048 | } else { |
| 4049 | /* keep-alive not possible */ |
| 4050 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4051 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 4052 | trash.len += 29; |
| 4053 | } else { |
| 4054 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 4055 | trash.len += 23; |
| 4056 | } |
| 4057 | stream_int_retnclose(txn->req.chn->prod, &trash); |
| 4058 | txn->req.chn->analysers = 0; |
| 4059 | } |
| 4060 | |
| 4061 | if (!(s->flags & SN_ERR_MASK)) |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 4062 | s->flags |= SN_ERR_LOCAL; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4063 | if (!(s->flags & SN_FINST_MASK)) |
| 4064 | s->flags |= SN_FINST_R; |
| 4065 | |
| 4066 | return 1; |
| 4067 | } |
| 4068 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4069 | /* This stream analyser runs all HTTP request processing which is common to |
| 4070 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 4071 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4072 | * 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] | 4073 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 4074 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4075 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4076 | 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] | 4077 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4078 | struct http_txn *txn = &s->txn; |
| 4079 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4080 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4081 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4082 | enum rule_result verdict; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4083 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4084 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4085 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4086 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4087 | return 0; |
| 4088 | } |
| 4089 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4090 | 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] | 4091 | now_ms, __FUNCTION__, |
| 4092 | s, |
| 4093 | req, |
| 4094 | req->rex, req->wex, |
| 4095 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4096 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4097 | req->analysers); |
| 4098 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4099 | /* just in case we have some per-backend tracking */ |
| 4100 | session_inc_be_http_req_ctr(s); |
| 4101 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4102 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4103 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
| 4104 | 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] | 4105 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4106 | switch (verdict) { |
| 4107 | case HTTP_RULE_RES_CONT: |
| 4108 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4109 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4110 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4111 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4112 | if (txn->flags & TX_CLTARPIT) |
| 4113 | goto tarpit; |
| 4114 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4115 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4116 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4117 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4118 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4119 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4120 | goto done; |
| 4121 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4122 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4123 | goto return_bad_req; |
| 4124 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4125 | } |
| 4126 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4127 | /* OK at this stage, we know that the request was accepted according to |
| 4128 | * the http-request rules, we can check for the stats. Note that the |
| 4129 | * URI is detected *before* the req* rules in order not to be affected |
| 4130 | * by a possible reqrep, while they are processed *after* so that a |
| 4131 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4132 | */ |
| 4133 | if (stats_check_uri(s->rep->prod, txn, px)) { |
| 4134 | s->target = &http_stats_applet.obj_type; |
| 4135 | if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) { |
| 4136 | txn->status = 500; |
| 4137 | s->logs.tv_request = now; |
| 4138 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 4139 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4140 | if (!(s->flags & SN_ERR_MASK)) |
| 4141 | s->flags |= SN_ERR_RESOURCE; |
| 4142 | goto return_prx_cond; |
| 4143 | } |
| 4144 | |
| 4145 | /* parse the whole stats request and extract the relevant information */ |
| 4146 | http_handle_stats(s, req); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4147 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn); |
| 4148 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4149 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4150 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4151 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4152 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4153 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4154 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4155 | } |
| 4156 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4157 | /* evaluate the req* rules except reqadd */ |
| 4158 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4159 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4160 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4161 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4162 | if (txn->flags & TX_CLDENY) |
| 4163 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4164 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4165 | if (txn->flags & TX_CLTARPIT) |
| 4166 | goto tarpit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4167 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4168 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4169 | /* add request headers from the rule sets in the same order */ |
| 4170 | list_for_each_entry(wl, &px->req_add, list) { |
| 4171 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 4172 | 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] | 4173 | ret = acl_pass(ret); |
| 4174 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4175 | ret = !ret; |
| 4176 | if (!ret) |
| 4177 | continue; |
| 4178 | } |
| 4179 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4180 | 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] | 4181 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4182 | } |
| 4183 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4184 | |
| 4185 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4186 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4187 | /* process the stats request now */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4188 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4189 | s->fe->fe_counters.intercepted_req++; |
| 4190 | |
| 4191 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 4192 | s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy |
| 4193 | if (!(s->flags & SN_FINST_MASK)) |
| 4194 | s->flags |= SN_FINST_R; |
| 4195 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4196 | /* we may want to compress the stats page */ |
| 4197 | if (s->fe->comp || s->be->comp) |
| 4198 | select_compression_request_header(s, req->buf); |
| 4199 | |
| 4200 | /* 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] | 4201 | 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] | 4202 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4203 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4204 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4205 | /* check whether we have some ACLs set to redirect this request */ |
| 4206 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4207 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4208 | int ret; |
| 4209 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 4210 | 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] | 4211 | ret = acl_pass(ret); |
| 4212 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4213 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4214 | if (!ret) |
| 4215 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4216 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4217 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4218 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4219 | goto done; |
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 | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4222 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4223 | * If this happens, then the data will not come immediately, so we must |
| 4224 | * send all what we have without waiting. Note that due to the small gain |
| 4225 | * 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] | 4226 | * 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] | 4227 | * itself once used. |
| 4228 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4229 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4230 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4231 | done: /* done with this analyser, continue with next ones that the calling |
| 4232 | * points will have set, if any. |
| 4233 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4234 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4235 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4236 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4237 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4238 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4239 | tarpit: |
| 4240 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4241 | * which may be the same as the connect timeout if unspecified. |
| 4242 | * If unset, then set it to zero because we really want it to |
| 4243 | * eventually expire. We build the tarpit as an analyser. |
| 4244 | */ |
| 4245 | channel_erase(s->req); |
| 4246 | |
| 4247 | /* wipe the request out so that we can drop the connection early |
| 4248 | * if the client closes first. |
| 4249 | */ |
| 4250 | channel_dont_connect(req); |
| 4251 | req->analysers = 0; /* remove switching rules etc... */ |
| 4252 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4253 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4254 | if (!req->analyse_exp) |
| 4255 | req->analyse_exp = tick_add(now_ms, 0); |
| 4256 | session_inc_http_err_ctr(s); |
| 4257 | s->fe->fe_counters.denied_req++; |
| 4258 | if (s->fe != s->be) |
| 4259 | s->be->be_counters.denied_req++; |
| 4260 | if (s->listener->counters) |
| 4261 | s->listener->counters->denied_req++; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4262 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4263 | |
| 4264 | deny: /* this request was blocked (denied) */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4265 | txn->flags |= TX_CLDENY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4266 | txn->status = 403; |
| 4267 | s->logs.tv_request = now; |
| 4268 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
| 4269 | session_inc_http_err_ctr(s); |
| 4270 | s->fe->fe_counters.denied_req++; |
| 4271 | if (s->fe != s->be) |
| 4272 | s->be->be_counters.denied_req++; |
| 4273 | if (s->listener->counters) |
| 4274 | s->listener->counters->denied_req++; |
| 4275 | goto return_prx_cond; |
| 4276 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4277 | return_bad_req: |
| 4278 | /* We centralize bad requests processing here */ |
| 4279 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4280 | /* we detected a parsing error. We want to archive this request |
| 4281 | * in the dedicated proxy area for later troubleshooting. |
| 4282 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4283 | 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] | 4284 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4285 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4286 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4287 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4288 | 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] | 4289 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4290 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4291 | if (s->listener->counters) |
| 4292 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4293 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4294 | return_prx_cond: |
| 4295 | if (!(s->flags & SN_ERR_MASK)) |
| 4296 | s->flags |= SN_ERR_PRXCOND; |
| 4297 | if (!(s->flags & SN_FINST_MASK)) |
| 4298 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4299 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4300 | req->analysers = 0; |
| 4301 | req->analyse_exp = TICK_ETERNITY; |
| 4302 | return 0; |
| 4303 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4304 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4305 | /* This function performs all the processing enabled for the current request. |
| 4306 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4307 | * needs more data, encounters an error, or wants to immediately abort the |
| 4308 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 4309 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4310 | 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] | 4311 | { |
| 4312 | struct http_txn *txn = &s->txn; |
| 4313 | struct http_msg *msg = &txn->req; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4314 | struct connection *cli_conn = objt_conn(req->prod->end); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4315 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4316 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4317 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4318 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4319 | return 0; |
| 4320 | } |
| 4321 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4322 | 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] | 4323 | now_ms, __FUNCTION__, |
| 4324 | s, |
| 4325 | req, |
| 4326 | req->rex, req->wex, |
| 4327 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4328 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4329 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4330 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 4331 | if (s->fe->comp || s->be->comp) |
| 4332 | select_compression_request_header(s, req->buf); |
| 4333 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4334 | /* |
| 4335 | * Right now, we know that we have processed the entire headers |
| 4336 | * and that unwanted requests have been filtered out. We can do |
| 4337 | * whatever we want with the remaining request. Also, now we |
| 4338 | * may have separate values for ->fe, ->be. |
| 4339 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4340 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4341 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4342 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4343 | * incoming request. Note that this requires that a connection is |
| 4344 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4345 | */ |
| 4346 | 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] | 4347 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4348 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4349 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4350 | /* Note that for now we don't reuse existing proxy connections */ |
| 4351 | if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4352 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4353 | txn->status = 500; |
| 4354 | req->analysers = 0; |
| 4355 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
| 4356 | |
| 4357 | if (!(s->flags & SN_ERR_MASK)) |
| 4358 | s->flags |= SN_ERR_RESOURCE; |
| 4359 | if (!(s->flags & SN_FINST_MASK)) |
| 4360 | s->flags |= SN_FINST_R; |
| 4361 | |
| 4362 | return 0; |
| 4363 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4364 | |
| 4365 | path = http_get_path(txn); |
| 4366 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4367 | 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] | 4368 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4369 | /* if the path was found, we have to remove everything between |
| 4370 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4371 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4372 | * u_l characters by a single "/". |
| 4373 | */ |
| 4374 | if (path) { |
| 4375 | char *cur_ptr = req->buf->p; |
| 4376 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4377 | int delta; |
| 4378 | |
| 4379 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4380 | http_msg_move_end(&txn->req, delta); |
| 4381 | cur_end += delta; |
| 4382 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4383 | goto return_bad_req; |
| 4384 | } |
| 4385 | else { |
| 4386 | char *cur_ptr = req->buf->p; |
| 4387 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4388 | int delta; |
| 4389 | |
| 4390 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4391 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4392 | http_msg_move_end(&txn->req, delta); |
| 4393 | cur_end += delta; |
| 4394 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4395 | goto return_bad_req; |
| 4396 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4397 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4398 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4399 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4400 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4401 | * Note that doing so might move headers in the request, but |
| 4402 | * the fields will stay coherent and the URI will not move. |
| 4403 | * This should only be performed in the backend. |
| 4404 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 4405 | 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] | 4406 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 4407 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4408 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4409 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4410 | * 8: the appsession cookie was looked up very early in 1.2, |
| 4411 | * so let's do the same now. |
| 4412 | */ |
| 4413 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4414 | /* It needs to look into the URI unless persistence must be ignored */ |
| 4415 | 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] | 4416 | 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] | 4417 | } |
| 4418 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4419 | /* add unique-id if "header-unique-id" is specified */ |
| 4420 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4421 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 4422 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4423 | goto return_bad_req; |
| 4424 | s->unique_id[0] = '\0'; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4425 | 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] | 4426 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4427 | |
| 4428 | if (s->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4429 | chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 4430 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4431 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4432 | 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] | 4433 | goto return_bad_req; |
| 4434 | } |
| 4435 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4436 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4437 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4438 | * asks for it. |
| 4439 | */ |
| 4440 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4441 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4442 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Cyril Bonté | a32d275 | 2012-05-29 23:27:41 +0200 | [diff] [blame] | 4443 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name, |
| 4444 | 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] | 4445 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4446 | /* The header is set to be added only if none is present |
| 4447 | * and we found it, so don't do anything. |
| 4448 | */ |
| 4449 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4450 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4451 | /* Add an X-Forwarded-For header unless the source IP is |
| 4452 | * in the 'except' network range. |
| 4453 | */ |
| 4454 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4455 | (((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] | 4456 | != s->fe->except_net.s_addr) && |
| 4457 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4458 | (((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] | 4459 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4460 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4461 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4462 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4463 | |
| 4464 | /* Note: we rely on the backend to get the header name to be used for |
| 4465 | * x-forwarded-for, because the header is really meant for the backends. |
| 4466 | * However, if the backend did not specify any option, we have to rely |
| 4467 | * on the frontend's header name. |
| 4468 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4469 | if (s->be->fwdfor_hdr_len) { |
| 4470 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4471 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4472 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4473 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4474 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4475 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4476 | 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] | 4477 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4478 | 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] | 4479 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4480 | } |
| 4481 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4482 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4483 | /* FIXME: for the sake of completeness, we should also support |
| 4484 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4485 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4486 | int len; |
| 4487 | char pn[INET6_ADDRSTRLEN]; |
| 4488 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4489 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4490 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4491 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4492 | /* Note: we rely on the backend to get the header name to be used for |
| 4493 | * x-forwarded-for, because the header is really meant for the backends. |
| 4494 | * However, if the backend did not specify any option, we have to rely |
| 4495 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4496 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4497 | if (s->be->fwdfor_hdr_len) { |
| 4498 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4499 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4500 | } else { |
| 4501 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4502 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4503 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4504 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4505 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4506 | 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] | 4507 | goto return_bad_req; |
| 4508 | } |
| 4509 | } |
| 4510 | |
| 4511 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4512 | * 10: add X-Original-To if either the frontend or the backend |
| 4513 | * asks for it. |
| 4514 | */ |
| 4515 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 4516 | |
| 4517 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4518 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4519 | /* Add an X-Original-To header unless the destination IP is |
| 4520 | * in the 'except' network range. |
| 4521 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4522 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4523 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4524 | if (cli_conn->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4525 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4526 | (((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] | 4527 | != s->fe->except_to.s_addr) && |
| 4528 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4529 | (((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] | 4530 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4531 | int len; |
| 4532 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4533 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4534 | |
| 4535 | /* Note: we rely on the backend to get the header name to be used for |
| 4536 | * x-original-to, because the header is really meant for the backends. |
| 4537 | * However, if the backend did not specify any option, we have to rely |
| 4538 | * on the frontend's header name. |
| 4539 | */ |
| 4540 | if (s->be->orgto_hdr_len) { |
| 4541 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4542 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4543 | } else { |
| 4544 | len = s->fe->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4545 | memcpy(trash.str, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4546 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4547 | 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] | 4548 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4549 | 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] | 4550 | goto return_bad_req; |
| 4551 | } |
| 4552 | } |
| 4553 | } |
| 4554 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4555 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4556 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4557 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4558 | * "Upgrade" is present in the Connection header. |
| 4559 | */ |
| 4560 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4561 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4562 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4563 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4564 | unsigned int want_flags = 0; |
| 4565 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4566 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4567 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4568 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4569 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4570 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4571 | want_flags |= TX_CON_CLO_SET; |
| 4572 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4573 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4574 | ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
| 4575 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4576 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4577 | want_flags |= TX_CON_KAL_SET; |
| 4578 | } |
| 4579 | |
| 4580 | 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] | 4581 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4582 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4583 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4584 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4585 | /* If we have no server assigned yet and we're balancing on url_param |
| 4586 | * with a POST request, we may be interested in checking the body for |
| 4587 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4588 | */ |
| 4589 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 4590 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4591 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4592 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4593 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4594 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4595 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4596 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4597 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4598 | #ifdef TCP_QUICKACK |
| 4599 | /* We expect some data from the client. Unless we know for sure |
| 4600 | * we already have a full request, we have to re-enable quick-ack |
| 4601 | * in case we previously disabled it, otherwise we might cause |
| 4602 | * the client to delay further data. |
| 4603 | */ |
| 4604 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4605 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4606 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4607 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4608 | 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] | 4609 | #endif |
| 4610 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4611 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4612 | /************************************************************* |
| 4613 | * OK, that's finished for the headers. We have done what we * |
| 4614 | * could. Let's switch to the DATA state. * |
| 4615 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4616 | req->analyse_exp = TICK_ETERNITY; |
| 4617 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4618 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4619 | /* if the server closes the connection, we want to immediately react |
| 4620 | * and close the socket to save packets and syscalls. |
| 4621 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4622 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
| 4623 | req->cons->flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4624 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4625 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4626 | /* OK let's go on with the BODY now */ |
| 4627 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4628 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4629 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4630 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4631 | /* we detected a parsing error. We want to archive this request |
| 4632 | * in the dedicated proxy area for later troubleshooting. |
| 4633 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4634 | 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] | 4635 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4636 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4637 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4638 | txn->status = 400; |
| 4639 | req->analysers = 0; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4640 | 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] | 4641 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4642 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4643 | if (s->listener->counters) |
| 4644 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4645 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4646 | if (!(s->flags & SN_ERR_MASK)) |
| 4647 | s->flags |= SN_ERR_PRXCOND; |
| 4648 | if (!(s->flags & SN_FINST_MASK)) |
| 4649 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4650 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4651 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4652 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4653 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4654 | * returns zero, at the beginning because it prevents any other processing |
| 4655 | * from occurring, and at the end because it terminates the request. |
| 4656 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4657 | 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] | 4658 | { |
| 4659 | struct http_txn *txn = &s->txn; |
| 4660 | |
| 4661 | /* This connection is being tarpitted. The CLIENT side has |
| 4662 | * already set the connect expiration date to the right |
| 4663 | * timeout. We just have to check that the client is still |
| 4664 | * there and that the timeout has not expired. |
| 4665 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4666 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4667 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4668 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4669 | return 0; |
| 4670 | |
| 4671 | /* We will set the queue timer to the time spent, just for |
| 4672 | * logging purposes. We fake a 500 server error, so that the |
| 4673 | * attacker will not suspect his connection has been tarpitted. |
| 4674 | * It will not cause trouble to the logs because we can exclude |
| 4675 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4676 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4677 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4678 | |
| 4679 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4680 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4681 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4682 | |
| 4683 | req->analysers = 0; |
| 4684 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4685 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4686 | if (!(s->flags & SN_ERR_MASK)) |
| 4687 | s->flags |= SN_ERR_PRXCOND; |
| 4688 | if (!(s->flags & SN_FINST_MASK)) |
| 4689 | s->flags |= SN_FINST_T; |
| 4690 | return 0; |
| 4691 | } |
| 4692 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4693 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4694 | * for either the buffer to be full, or the full advertised contents to have |
| 4695 | * reached the buffer. It must only be called after the standard HTTP request |
| 4696 | * processing has occurred, because it expects the request to be parsed and will |
| 4697 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4698 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4699 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4700 | * 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] | 4701 | */ |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4702 | 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] | 4703 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4704 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4705 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4706 | |
| 4707 | /* We have to parse the HTTP request body to find any required data. |
| 4708 | * "balance url_param check_post" should have been the only way to get |
| 4709 | * into this. We were brought here after HTTP header analysis, so all |
| 4710 | * related structures are ready. |
| 4711 | */ |
| 4712 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4713 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4714 | /* This is the first call */ |
| 4715 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4716 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4717 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4718 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4719 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4720 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4721 | */ |
| 4722 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4723 | struct hdr_ctx ctx; |
| 4724 | ctx.idx = 0; |
| 4725 | /* Expect is allowed in 1.1, look for it */ |
| 4726 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4727 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 4728 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 4729 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4730 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4731 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4732 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4733 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4734 | /* 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] | 4735 | * req->buf->p still points to the beginning of the message. We |
| 4736 | * must save the body in msg->next because it survives buffer |
| 4737 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4738 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4739 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4740 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4741 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4742 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4743 | else |
| 4744 | msg->msg_state = HTTP_MSG_DATA; |
| 4745 | } |
| 4746 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4747 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4748 | /* We're in content-length mode, we just have to wait for enough data. */ |
| 4749 | if (req->buf->i - msg->sov < msg->body_len) |
| 4750 | goto missing_data; |
| 4751 | |
| 4752 | /* OK we have everything we need now */ |
| 4753 | goto http_end; |
| 4754 | } |
| 4755 | |
| 4756 | /* OK here we're parsing a chunked-encoded message */ |
| 4757 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4758 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4759 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4760 | * 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] | 4761 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4762 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4763 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4764 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4765 | if (!ret) |
| 4766 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4767 | else if (ret < 0) { |
| 4768 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4769 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4770 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4771 | } |
| 4772 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4773 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4774 | * 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] | 4775 | * 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] | 4776 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4777 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4778 | goto http_end; |
| 4779 | |
Willy Tarreau | 226071e | 2014-04-10 11:55:45 +0200 | [diff] [blame] | 4780 | 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] | 4781 | goto http_end; |
| 4782 | |
| 4783 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4784 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4785 | * we have the maximum we can expect. |
| 4786 | */ |
| 4787 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4788 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4789 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4790 | 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] | 4791 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4792 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4793 | |
| 4794 | if (!(s->flags & SN_ERR_MASK)) |
| 4795 | s->flags |= SN_ERR_CLITO; |
| 4796 | if (!(s->flags & SN_FINST_MASK)) |
| 4797 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4798 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4799 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4800 | |
| 4801 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4802 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4803 | /* Not enough data. We'll re-use the http-request |
| 4804 | * timeout here. Ideally, we should set the timeout |
| 4805 | * relative to the accept() date. We just set the |
| 4806 | * request timeout once at the beginning of the |
| 4807 | * request. |
| 4808 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4809 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4810 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4811 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4812 | return 0; |
| 4813 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4814 | |
| 4815 | http_end: |
| 4816 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4817 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4818 | req->analysers &= ~an_bit; |
| 4819 | req->analyse_exp = TICK_ETERNITY; |
| 4820 | return 1; |
| 4821 | |
| 4822 | return_bad_req: /* let's centralize all bad requests */ |
| 4823 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4824 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4825 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4826 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4827 | if (!(s->flags & SN_ERR_MASK)) |
| 4828 | s->flags |= SN_ERR_PRXCOND; |
| 4829 | if (!(s->flags & SN_FINST_MASK)) |
| 4830 | s->flags |= SN_FINST_R; |
| 4831 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4832 | return_err_msg: |
| 4833 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4834 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4835 | if (s->listener->counters) |
| 4836 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4837 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4838 | } |
| 4839 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4840 | /* send a server's name with an outgoing request over an established connection. |
| 4841 | * Note: this function is designed to be called once the request has been scheduled |
| 4842 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4843 | * proceeding. |
| 4844 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4845 | 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] | 4846 | |
| 4847 | struct hdr_ctx ctx; |
| 4848 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4849 | char *hdr_name = be->server_id_hdr_name; |
| 4850 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4851 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4852 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4853 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4854 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4855 | ctx.idx = 0; |
| 4856 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4857 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4858 | if (old_o) { |
| 4859 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4860 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4861 | txn->req.next += old_o; |
| 4862 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4863 | } |
| 4864 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4865 | old_i = chn->buf->i; |
| 4866 | 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] | 4867 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4868 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4869 | } |
| 4870 | |
| 4871 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4872 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4873 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4874 | hdr_val += hdr_name_len; |
| 4875 | *hdr_val++ = ':'; |
| 4876 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4877 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4878 | 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] | 4879 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4880 | if (old_o) { |
| 4881 | /* If this was a forwarded request, we must readjust the amount of |
| 4882 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4883 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4884 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4885 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4886 | old_o += chn->buf->i - old_i; |
| 4887 | b_adv(chn->buf, old_o); |
| 4888 | txn->req.next -= old_o; |
| 4889 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4890 | } |
| 4891 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4892 | return 0; |
| 4893 | } |
| 4894 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4895 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4896 | * right now but it works. |
| 4897 | */ |
| 4898 | void http_end_txn_clean_session(struct session *s) |
| 4899 | { |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4900 | int prev_status = s->txn.status; |
| 4901 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4902 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4903 | * server's connections. We need a safer way to reinitialize buffer |
| 4904 | * flags. We also need a more accurate method for computing per-request |
| 4905 | * data. |
| 4906 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4907 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4908 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4909 | * to the server. |
| 4910 | */ |
| 4911 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4912 | !si_conn_ready(s->req->cons)) { |
| 4913 | s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4914 | si_shutr(s->req->cons); |
| 4915 | si_shutw(s->req->cons); |
| 4916 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4917 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4918 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4919 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4920 | if (unlikely(s->srv_conn)) |
| 4921 | sess_change_server(s, NULL); |
| 4922 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4923 | |
| 4924 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4925 | session_process_counters(s); |
| 4926 | |
| 4927 | if (s->txn.status) { |
| 4928 | int n; |
| 4929 | |
| 4930 | n = s->txn.status / 100; |
| 4931 | if (n < 1 || n > 5) |
| 4932 | n = 0; |
| 4933 | |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4934 | if (s->fe->mode == PR_MODE_HTTP) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4935 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4936 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4937 | s->fe->fe_counters.p.http.comp_rsp++; |
| 4938 | } |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 4939 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4940 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4941 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4942 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4943 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4944 | s->be->be_counters.p.http.comp_rsp++; |
| 4945 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4946 | } |
| 4947 | |
| 4948 | /* don't count other requests' data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4949 | s->logs.bytes_in -= s->req->buf->i; |
| 4950 | s->logs.bytes_out -= s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4951 | |
| 4952 | /* let's do a final log if we need it */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 4953 | if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4954 | !(s->flags & SN_MONITOR) && |
| 4955 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 4956 | s->do_log(s); |
| 4957 | } |
| 4958 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 4959 | /* stop tracking content-based counters */ |
| 4960 | session_stop_content_counters(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 4961 | session_update_time_stats(s); |
| 4962 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4963 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4964 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4965 | tv_zero(&s->logs.tv_request); |
| 4966 | s->logs.t_queue = -1; |
| 4967 | s->logs.t_connect = -1; |
| 4968 | s->logs.t_data = -1; |
| 4969 | s->logs.t_close = 0; |
| 4970 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4971 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4972 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4973 | s->logs.bytes_in = s->req->total = s->req->buf->i; |
| 4974 | s->logs.bytes_out = s->rep->total = s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4975 | |
| 4976 | if (s->pend_pos) |
| 4977 | pendconn_free(s->pend_pos); |
| 4978 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4979 | if (objt_server(s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4980 | if (s->flags & SN_CURR_SESS) { |
| 4981 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4982 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4983 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4984 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 4985 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4986 | } |
| 4987 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4988 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4989 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4990 | /* only release our endpoint if we don't intend to reuse the |
| 4991 | * connection. |
| 4992 | */ |
| 4993 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4994 | !si_conn_ready(s->req->cons)) { |
| 4995 | si_release_endpoint(s->req->cons); |
| 4996 | } |
| 4997 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4998 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4999 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 5000 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5001 | s->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | c920096 | 2013-12-31 23:03:09 +0100 | [diff] [blame] | 5002 | 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] | 5003 | 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); |
| 5004 | 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] | 5005 | 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] | 5006 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED); |
Cyril Bonté | a83a50b | 2014-10-22 22:30:13 +0200 | [diff] [blame] | 5007 | s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 5008 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5009 | s->txn.meth = 0; |
| 5010 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 5011 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5012 | |
| 5013 | if (prev_status == 401 || prev_status == 407) { |
| 5014 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 5015 | * a chance of being able to send the visitor again to the same |
| 5016 | * server over the same connection. This is required by some |
| 5017 | * broken protocols such as NTLM, and anyway whenever there is |
| 5018 | * an opportunity for sending the challenge to the proper place, |
| 5019 | * it's better to do it (at least it helps with debugging). |
| 5020 | */ |
| 5021 | s->txn.flags |= TX_PREFER_LAST; |
| 5022 | } |
| 5023 | |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 5024 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5025 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 5026 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 5027 | if (s->fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5028 | s->req->flags |= CF_NEVER_WAIT; |
| 5029 | s->rep->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 5030 | } |
| 5031 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5032 | /* if the request buffer is not empty, it means we're |
| 5033 | * about to process another request, so send pending |
| 5034 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5035 | * Just don't do this if the buffer is close to be full, |
| 5036 | * because the request will wait for it to flush a little |
| 5037 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5038 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5039 | if (s->req->buf->i) { |
| 5040 | if (s->rep->buf->o && |
| 5041 | !buffer_full(s->rep->buf, global.tune.maxrewrite) && |
| 5042 | 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] | 5043 | s->rep->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5044 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5045 | |
| 5046 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5047 | channel_auto_read(s->req); |
| 5048 | channel_auto_close(s->req); |
| 5049 | channel_auto_read(s->rep); |
| 5050 | channel_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5051 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5052 | /* we're in keep-alive with an idle connection, monitor it */ |
| 5053 | si_idle_conn(s->req->cons); |
| 5054 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 5055 | s->req->analysers = s->listener->analysers; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5056 | s->rep->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5057 | } |
| 5058 | |
| 5059 | |
| 5060 | /* This function updates the request state machine according to the response |
| 5061 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5062 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5063 | * it is only used to find when a request/response couple is complete. Both |
| 5064 | * this function and its equivalent should loop until both return zero. It |
| 5065 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5066 | */ |
| 5067 | int http_sync_req_state(struct session *s) |
| 5068 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5069 | struct channel *chn = s->req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5070 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5071 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5072 | unsigned int old_state = txn->req.msg_state; |
| 5073 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5074 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 5075 | return 0; |
| 5076 | |
| 5077 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5078 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5079 | * We can shut the read side unless we want to abort_on_close, |
| 5080 | * or we have a POST request. The issue with POST requests is |
| 5081 | * that some browsers still send a CRLF after the request, and |
| 5082 | * this CRLF must be read so that it does not remain in the kernel |
| 5083 | * buffers, otherwise a close could cause an RST on some systems |
| 5084 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5085 | * Note that if we're using keep-alive on the client side, we'd |
| 5086 | * rather poll now and keep the polling enabled for the whole |
| 5087 | * session's life than enabling/disabling it between each |
| 5088 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5089 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5090 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5091 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5092 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 5093 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5094 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5095 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5096 | /* if the server closes the connection, we want to immediately react |
| 5097 | * and close the socket to save packets and syscalls. |
| 5098 | */ |
| 5099 | chn->cons->flags |= SI_FL_NOHALF; |
| 5100 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5101 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5102 | goto wait_other_side; |
| 5103 | |
| 5104 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5105 | /* The server has not finished to respond, so we |
| 5106 | * don't want to move in order not to upset it. |
| 5107 | */ |
| 5108 | goto wait_other_side; |
| 5109 | } |
| 5110 | |
| 5111 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5112 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5113 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5114 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5115 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5116 | goto wait_other_side; |
| 5117 | } |
| 5118 | |
| 5119 | /* When we get here, it means that both the request and the |
| 5120 | * response have finished receiving. Depending on the connection |
| 5121 | * mode, we'll have to wait for the last bytes to leave in either |
| 5122 | * direction, and sometimes for a close to be effective. |
| 5123 | */ |
| 5124 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5125 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5126 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5127 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5128 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5129 | } |
| 5130 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5131 | /* Option forceclose is set, or either side wants to close, |
| 5132 | * let's enforce it now that we're not expecting any new |
| 5133 | * data to come. The caller knows the session is complete |
| 5134 | * once both states are CLOSED. |
| 5135 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5136 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5137 | channel_shutr_now(chn); |
| 5138 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5139 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5140 | } |
| 5141 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5142 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5143 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5144 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5145 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5146 | channel_auto_read(chn); |
| 5147 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5148 | chn->flags |= CF_NEVER_WAIT; |
| 5149 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5150 | } |
| 5151 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5152 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5153 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5154 | chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5155 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5156 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5157 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5158 | goto http_msg_closing; |
| 5159 | } |
| 5160 | else { |
| 5161 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5162 | goto http_msg_closed; |
| 5163 | } |
| 5164 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5165 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5166 | } |
| 5167 | |
| 5168 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5169 | http_msg_closing: |
| 5170 | /* nothing else to forward, just waiting for the output buffer |
| 5171 | * to be empty and for the shutw_now to take effect. |
| 5172 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5173 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5174 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5175 | goto http_msg_closed; |
| 5176 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5177 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5178 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5179 | goto wait_other_side; |
| 5180 | } |
| 5181 | } |
| 5182 | |
| 5183 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5184 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5185 | /* see above in MSG_DONE why we only do this in these states */ |
| 5186 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5187 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5188 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5189 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5190 | goto wait_other_side; |
| 5191 | } |
| 5192 | |
| 5193 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5194 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5195 | } |
| 5196 | |
| 5197 | |
| 5198 | /* This function updates the response state machine according to the request |
| 5199 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5200 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5201 | * it is only used to find when a request/response couple is complete. Both |
| 5202 | * this function and its equivalent should loop until both return zero. It |
| 5203 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5204 | */ |
| 5205 | int http_sync_res_state(struct session *s) |
| 5206 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5207 | struct channel *chn = s->rep; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5208 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5209 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5210 | unsigned int old_state = txn->rsp.msg_state; |
| 5211 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5212 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5213 | return 0; |
| 5214 | |
| 5215 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5216 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5217 | * still monitor the server connection for a possible close |
| 5218 | * while the request is being uploaded, so we don't disable |
| 5219 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5220 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5221 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5222 | |
| 5223 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5224 | goto wait_other_side; |
| 5225 | |
| 5226 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5227 | /* The client seems to still be sending data, probably |
| 5228 | * because we got an error response during an upload. |
| 5229 | * We have the choice of either breaking the connection |
| 5230 | * or letting it pass through. Let's do the later. |
| 5231 | */ |
| 5232 | goto wait_other_side; |
| 5233 | } |
| 5234 | |
| 5235 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5236 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5237 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5238 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5239 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5240 | goto wait_other_side; |
| 5241 | } |
| 5242 | |
| 5243 | /* When we get here, it means that both the request and the |
| 5244 | * response have finished receiving. Depending on the connection |
| 5245 | * mode, we'll have to wait for the last bytes to leave in either |
| 5246 | * direction, and sometimes for a close to be effective. |
| 5247 | */ |
| 5248 | |
| 5249 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5250 | /* Server-close mode : shut read and wait for the request |
| 5251 | * side to close its output buffer. The caller will detect |
| 5252 | * when we're in DONE and the other is in CLOSED and will |
| 5253 | * catch that for the final cleanup. |
| 5254 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5255 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5256 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5257 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5258 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5259 | /* Option forceclose is set, or either side wants to close, |
| 5260 | * let's enforce it now that we're not expecting any new |
| 5261 | * data to come. The caller knows the session is complete |
| 5262 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5263 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5264 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5265 | channel_shutr_now(chn); |
| 5266 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5267 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5268 | } |
| 5269 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5270 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5271 | * need to forward remaining data. Keep-alive will need to monitor |
| 5272 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5273 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5274 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5275 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5276 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5277 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5278 | } |
| 5279 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5280 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5281 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5282 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5283 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5284 | goto http_msg_closing; |
| 5285 | } |
| 5286 | else { |
| 5287 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5288 | goto http_msg_closed; |
| 5289 | } |
| 5290 | } |
| 5291 | goto wait_other_side; |
| 5292 | } |
| 5293 | |
| 5294 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5295 | http_msg_closing: |
| 5296 | /* nothing else to forward, just waiting for the output buffer |
| 5297 | * to be empty and for the shutw_now to take effect. |
| 5298 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5299 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5300 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5301 | goto http_msg_closed; |
| 5302 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5303 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5304 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5305 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5306 | if (objt_server(s->target)) |
| 5307 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5308 | goto wait_other_side; |
| 5309 | } |
| 5310 | } |
| 5311 | |
| 5312 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5313 | http_msg_closed: |
| 5314 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5315 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5316 | channel_auto_close(chn); |
| 5317 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5318 | goto wait_other_side; |
| 5319 | } |
| 5320 | |
| 5321 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5322 | /* We force the response to leave immediately if we're waiting for the |
| 5323 | * other side, since there is no pending shutdown to push it out. |
| 5324 | */ |
| 5325 | if (!channel_is_empty(chn)) |
| 5326 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5327 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5328 | } |
| 5329 | |
| 5330 | |
| 5331 | /* Resync the request and response state machines. Return 1 if either state |
| 5332 | * changes. |
| 5333 | */ |
| 5334 | int http_resync_states(struct session *s) |
| 5335 | { |
| 5336 | struct http_txn *txn = &s->txn; |
| 5337 | int old_req_state = txn->req.msg_state; |
| 5338 | int old_res_state = txn->rsp.msg_state; |
| 5339 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5340 | http_sync_req_state(s); |
| 5341 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5342 | if (!http_sync_res_state(s)) |
| 5343 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5344 | if (!http_sync_req_state(s)) |
| 5345 | break; |
| 5346 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5347 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5348 | /* OK, both state machines agree on a compatible state. |
| 5349 | * There are a few cases we're interested in : |
| 5350 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5351 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5352 | * directions, so let's simply disable both analysers. |
| 5353 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5354 | * request. |
| 5355 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5356 | * with server-close mode means we've completed one request and we |
| 5357 | * must re-initialize the server connection. |
| 5358 | */ |
| 5359 | |
| 5360 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5361 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5362 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5363 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 5364 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5365 | channel_auto_close(s->req); |
| 5366 | channel_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5367 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5368 | channel_auto_close(s->rep); |
| 5369 | channel_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5370 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5371 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
| 5372 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5373 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5374 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5375 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5376 | channel_auto_close(s->rep); |
| 5377 | channel_auto_read(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5378 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5379 | channel_abort(s->req); |
| 5380 | channel_auto_close(s->req); |
| 5381 | channel_auto_read(s->req); |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5382 | channel_truncate(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5383 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5384 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5385 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5386 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5387 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5388 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5389 | /* server-close/keep-alive: terminate this transaction, |
| 5390 | * possibly killing the server connection and reinitialize |
| 5391 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5392 | */ |
| 5393 | http_end_txn_clean_session(s); |
| 5394 | } |
| 5395 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5396 | return txn->req.msg_state != old_req_state || |
| 5397 | txn->rsp.msg_state != old_res_state; |
| 5398 | } |
| 5399 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5400 | /* This function is an analyser which forwards request body (including chunk |
| 5401 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5402 | * zero byte. The only situation where it must not be called is when we're in |
| 5403 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5404 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5405 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5406 | * 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] | 5407 | * 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] | 5408 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5409 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5410 | 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] | 5411 | { |
| 5412 | struct http_txn *txn = &s->txn; |
| 5413 | struct http_msg *msg = &s->txn.req; |
| 5414 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5415 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5416 | return 0; |
| 5417 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5418 | 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] | 5419 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5420 | /* Output closed while we were sending data. We must abort and |
| 5421 | * wake the other side up. |
| 5422 | */ |
| 5423 | msg->msg_state = HTTP_MSG_ERROR; |
| 5424 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5425 | return 1; |
| 5426 | } |
| 5427 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5428 | /* Note that we don't have to send 100-continue back because we don't |
| 5429 | * need the data to complete our job, and it's up to the server to |
| 5430 | * decide whether to return 100, 417 or anything else in return of |
| 5431 | * an "Expect: 100-continue" header. |
| 5432 | */ |
| 5433 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5434 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5435 | /* we have msg->sov which points to the first byte of message |
| 5436 | * body, and req->buf.p still points to the beginning of the |
| 5437 | * message. We forward the headers now, as we don't need them |
| 5438 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5439 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5440 | b_adv(req->buf, msg->sov); |
| 5441 | msg->next -= msg->sov; |
| 5442 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5443 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5444 | /* The previous analysers guarantee that the state is somewhere |
| 5445 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 5446 | * msg->next are always correct. |
| 5447 | */ |
| 5448 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5449 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 5450 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5451 | else |
| 5452 | msg->msg_state = HTTP_MSG_DATA; |
| 5453 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5454 | } |
| 5455 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5456 | /* Some post-connect processing might want us to refrain from starting to |
| 5457 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5458 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5459 | */ |
| 5460 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
| 5461 | if (!(s->rep->flags & CF_READ_ATTACHED)) { |
| 5462 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5463 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5464 | goto missing_data; |
| 5465 | } |
| 5466 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5467 | } |
| 5468 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5469 | /* in most states, we should abort in case of early close */ |
| 5470 | channel_auto_close(req); |
| 5471 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5472 | if (req->to_forward) { |
| 5473 | /* We can't process the buffer's contents yet */ |
| 5474 | req->flags |= CF_WAKE_WRITE; |
| 5475 | goto missing_data; |
| 5476 | } |
| 5477 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5478 | while (1) { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5479 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5480 | /* must still forward */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5481 | /* we may have some pending data starting at req->buf->p */ |
| 5482 | if (msg->chunk_len > req->buf->i - msg->next) { |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5483 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5484 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5485 | } |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5486 | msg->next += msg->chunk_len; |
| 5487 | msg->chunk_len = 0; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5488 | |
| 5489 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5490 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5491 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5492 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5493 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5494 | } |
| 5495 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5496 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 5497 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5498 | * TRAILERS state. |
| 5499 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5500 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5501 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5502 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5503 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5504 | else if (ret < 0) { |
| 5505 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5506 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5507 | 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] | 5508 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5509 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5510 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5511 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5512 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5513 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5514 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5515 | |
| 5516 | if (ret == 0) |
| 5517 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5518 | else if (ret < 0) { |
| 5519 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5520 | if (msg->err_pos >= 0) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5521 | 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] | 5522 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5523 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5524 | /* we're in MSG_CHUNK_SIZE now */ |
| 5525 | } |
| 5526 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5527 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5528 | |
| 5529 | if (ret == 0) |
| 5530 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5531 | else if (ret < 0) { |
| 5532 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5533 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5534 | 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] | 5535 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5536 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5537 | /* we're in HTTP_MSG_DONE now */ |
| 5538 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5539 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5540 | int old_state = msg->msg_state; |
| 5541 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5542 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5543 | |
| 5544 | /* we may have some pending data starting at req->buf->p |
| 5545 | * such as last chunk of data or trailers. |
| 5546 | */ |
| 5547 | b_adv(req->buf, msg->next); |
Willy Tarreau | 9dc1c61 | 2014-09-01 20:35:55 +0200 | [diff] [blame] | 5548 | if (unlikely(!(s->req->flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5549 | msg->sov -= msg->next; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5550 | msg->next = 0; |
| 5551 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5552 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5553 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5554 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5555 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5556 | if (http_resync_states(s)) { |
| 5557 | /* some state changes occurred, maybe the analyser |
| 5558 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 5559 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5560 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5561 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5562 | /* request errors are most likely due to |
| 5563 | * the server aborting the transfer. |
| 5564 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5565 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5566 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5567 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5568 | 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] | 5569 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5570 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5571 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5572 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5573 | |
| 5574 | /* If "option abortonclose" is set on the backend, we |
| 5575 | * want to monitor the client's connection and forward |
| 5576 | * any shutdown notification to the server, which will |
| 5577 | * decide whether to close or to go on processing the |
| 5578 | * request. |
| 5579 | */ |
| 5580 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5581 | channel_auto_read(req); |
| 5582 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5583 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5584 | else if (s->txn.meth == HTTP_METH_POST) { |
| 5585 | /* POST requests may require to read extra CRLF |
| 5586 | * sent by broken browsers and which could cause |
| 5587 | * an RST to be sent upon close on some systems |
| 5588 | * (eg: Linux). |
| 5589 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5590 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5591 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5592 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5593 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5594 | } |
| 5595 | } |
| 5596 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5597 | missing_data: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5598 | /* we may have some pending data starting at req->buf->p */ |
| 5599 | b_adv(req->buf, msg->next); |
Willy Tarreau | 9dc1c61 | 2014-09-01 20:35:55 +0200 | [diff] [blame] | 5600 | if (unlikely(!(s->req->flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5601 | msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i); |
| 5602 | |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5603 | msg->next = 0; |
| 5604 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
| 5605 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5606 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5607 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5608 | if (!(s->flags & SN_ERR_MASK)) |
| 5609 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5610 | if (!(s->flags & SN_FINST_MASK)) { |
| 5611 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5612 | s->flags |= SN_FINST_H; |
| 5613 | else |
| 5614 | s->flags |= SN_FINST_D; |
| 5615 | } |
| 5616 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5617 | s->fe->fe_counters.cli_aborts++; |
| 5618 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5619 | if (objt_server(s->target)) |
| 5620 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5621 | |
| 5622 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5623 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5624 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5625 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5626 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5627 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5628 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5629 | /* 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] | 5630 | * 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] | 5631 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5632 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5633 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5634 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5635 | /* 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] | 5636 | * 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] | 5637 | * 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] | 5638 | * modes are already handled by the stream sock layer. We must not do |
| 5639 | * this in content-length mode because it could present the MSG_MORE |
| 5640 | * flag with the last block of forwarded data, which would cause an |
| 5641 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5642 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5643 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5644 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5645 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5646 | return 0; |
| 5647 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5648 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5649 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5650 | if (s->listener->counters) |
| 5651 | s->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5652 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5653 | return_bad_req_stats_ok: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5654 | /* we may have some pending data starting at req->buf->p */ |
| 5655 | b_adv(req->buf, msg->next); |
| 5656 | msg->next = 0; |
| 5657 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5658 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5659 | if (txn->status) { |
| 5660 | /* Note: we don't send any error if some data were already sent */ |
| 5661 | stream_int_retnclose(req->prod, NULL); |
| 5662 | } else { |
| 5663 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5664 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5665 | } |
| 5666 | req->analysers = 0; |
| 5667 | 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] | 5668 | |
| 5669 | if (!(s->flags & SN_ERR_MASK)) |
| 5670 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5671 | if (!(s->flags & SN_FINST_MASK)) { |
| 5672 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5673 | s->flags |= SN_FINST_H; |
| 5674 | else |
| 5675 | s->flags |= SN_FINST_D; |
| 5676 | } |
| 5677 | return 0; |
| 5678 | |
| 5679 | aborted_xfer: |
| 5680 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5681 | if (txn->status) { |
| 5682 | /* Note: we don't send any error if some data were already sent */ |
| 5683 | stream_int_retnclose(req->prod, NULL); |
| 5684 | } else { |
| 5685 | txn->status = 502; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5686 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5687 | } |
| 5688 | req->analysers = 0; |
| 5689 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5690 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5691 | s->fe->fe_counters.srv_aborts++; |
| 5692 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5693 | if (objt_server(s->target)) |
| 5694 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5695 | |
| 5696 | if (!(s->flags & SN_ERR_MASK)) |
| 5697 | s->flags |= SN_ERR_SRVCL; |
| 5698 | if (!(s->flags & SN_FINST_MASK)) { |
| 5699 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5700 | s->flags |= SN_FINST_H; |
| 5701 | else |
| 5702 | s->flags |= SN_FINST_D; |
| 5703 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5704 | return 0; |
| 5705 | } |
| 5706 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5707 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5708 | * processing can continue on next analysers, or zero if it either needs more |
| 5709 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 5710 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 5711 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5712 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5713 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5714 | 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] | 5715 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5716 | struct http_txn *txn = &s->txn; |
| 5717 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5718 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5719 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5720 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5721 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5722 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5723 | 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] | 5724 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5725 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5726 | rep, |
| 5727 | rep->rex, rep->wex, |
| 5728 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5729 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5730 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5731 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5732 | /* |
| 5733 | * Now parse the partial (or complete) lines. |
| 5734 | * We will check the response syntax, and also join multi-line |
| 5735 | * headers. An index of all the lines will be elaborated while |
| 5736 | * parsing. |
| 5737 | * |
| 5738 | * For the parsing, we use a 28 states FSM. |
| 5739 | * |
| 5740 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5741 | * rep->buf->p = beginning of response |
| 5742 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5743 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5744 | * msg->eol = end of current header or line (LF or CRLF) |
| 5745 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5746 | */ |
| 5747 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5748 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5749 | /* There's a protected area at the end of the buffer for rewriting |
| 5750 | * purposes. We don't want to start to parse the request if the |
| 5751 | * protected area is affected, because we may have to move processed |
| 5752 | * data later, which is much more complicated. |
| 5753 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5754 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5755 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5756 | /* some data has still not left the buffer, wake us once that's done */ |
| 5757 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5758 | goto abort_response; |
| 5759 | channel_dont_close(rep); |
| 5760 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5761 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5762 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5763 | } |
| 5764 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5765 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5766 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5767 | buffer_slow_realign(rep->buf); |
| 5768 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5769 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5770 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5771 | } |
| 5772 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5773 | /* 1: we might have to print this header in debug mode */ |
| 5774 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5775 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5776 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5777 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5778 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5779 | sol = rep->buf->p; |
| 5780 | 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] | 5781 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5782 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5783 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5784 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5785 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5786 | while (cur_idx) { |
| 5787 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5788 | debug_hdr("srvhdr", s, sol, eol); |
| 5789 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5790 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5791 | } |
| 5792 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5793 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5794 | /* |
| 5795 | * Now we quickly check if we have found a full valid response. |
| 5796 | * If not so, we check the FD and buffer states before leaving. |
| 5797 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5798 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5799 | * responses are checked first. |
| 5800 | * |
| 5801 | * Depending on whether the client is still there or not, we |
| 5802 | * may send an error response back or not. Note that normally |
| 5803 | * we should only check for HTTP status there, and check I/O |
| 5804 | * errors somewhere else. |
| 5805 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5806 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5807 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5808 | /* Invalid response */ |
| 5809 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5810 | /* we detected a parsing error. We want to archive this response |
| 5811 | * in the dedicated proxy area for later troubleshooting. |
| 5812 | */ |
| 5813 | hdr_response_bad: |
| 5814 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5815 | 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] | 5816 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5817 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5818 | if (objt_server(s->target)) { |
| 5819 | objt_server(s->target)->counters.failed_resp++; |
| 5820 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5821 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5822 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5823 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5824 | rep->analysers = 0; |
| 5825 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5826 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5827 | channel_truncate(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5828 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5829 | |
| 5830 | if (!(s->flags & SN_ERR_MASK)) |
| 5831 | s->flags |= SN_ERR_PRXCOND; |
| 5832 | if (!(s->flags & SN_FINST_MASK)) |
| 5833 | s->flags |= SN_FINST_H; |
| 5834 | |
| 5835 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5836 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5837 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5838 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5839 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5840 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5841 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5842 | goto hdr_response_bad; |
| 5843 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5844 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5845 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5846 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5847 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5848 | 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] | 5849 | else if (txn->flags & TX_NOT_FIRST) |
| 5850 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5851 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5852 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5853 | if (objt_server(s->target)) { |
| 5854 | objt_server(s->target)->counters.failed_resp++; |
| 5855 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5856 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5857 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5858 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5859 | rep->analysers = 0; |
| 5860 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5861 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5862 | channel_truncate(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5863 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5864 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5865 | if (!(s->flags & SN_ERR_MASK)) |
| 5866 | s->flags |= SN_ERR_SRVCL; |
| 5867 | if (!(s->flags & SN_FINST_MASK)) |
| 5868 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5869 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5870 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5871 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 5872 | /* read timeout : return a 504 to the client. */ |
| 5873 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5874 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5875 | 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] | 5876 | else if (txn->flags & TX_NOT_FIRST) |
| 5877 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5878 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5879 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5880 | if (objt_server(s->target)) { |
| 5881 | objt_server(s->target)->counters.failed_resp++; |
| 5882 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5883 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5884 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5885 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5886 | rep->analysers = 0; |
| 5887 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5888 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5889 | channel_truncate(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5890 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5891 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5892 | if (!(s->flags & SN_ERR_MASK)) |
| 5893 | s->flags |= SN_ERR_SRVTO; |
| 5894 | if (!(s->flags & SN_FINST_MASK)) |
| 5895 | s->flags |= SN_FINST_H; |
| 5896 | return 0; |
| 5897 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5898 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5899 | /* client abort with an abortonclose */ |
| 5900 | else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
| 5901 | s->fe->fe_counters.cli_aborts++; |
| 5902 | s->be->be_counters.cli_aborts++; |
| 5903 | if (objt_server(s->target)) |
| 5904 | objt_server(s->target)->counters.cli_aborts++; |
| 5905 | |
| 5906 | rep->analysers = 0; |
| 5907 | channel_auto_close(rep); |
| 5908 | |
| 5909 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5910 | channel_truncate(rep); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5911 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400)); |
| 5912 | |
| 5913 | if (!(s->flags & SN_ERR_MASK)) |
| 5914 | s->flags |= SN_ERR_CLICL; |
| 5915 | if (!(s->flags & SN_FINST_MASK)) |
| 5916 | s->flags |= SN_FINST_H; |
| 5917 | |
| 5918 | /* process_session() will take care of the error */ |
| 5919 | return 0; |
| 5920 | } |
| 5921 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5922 | /* 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] | 5923 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5924 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5925 | 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] | 5926 | else if (txn->flags & TX_NOT_FIRST) |
| 5927 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5928 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5929 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5930 | if (objt_server(s->target)) { |
| 5931 | objt_server(s->target)->counters.failed_resp++; |
| 5932 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5933 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5934 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5935 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5936 | rep->analysers = 0; |
| 5937 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5938 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5939 | channel_truncate(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5940 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5941 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5942 | if (!(s->flags & SN_ERR_MASK)) |
| 5943 | s->flags |= SN_ERR_SRVCL; |
| 5944 | if (!(s->flags & SN_FINST_MASK)) |
| 5945 | s->flags |= SN_FINST_H; |
| 5946 | return 0; |
| 5947 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5948 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5949 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5950 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5951 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5952 | 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] | 5953 | else if (txn->flags & TX_NOT_FIRST) |
| 5954 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5955 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5956 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5957 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5958 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5959 | |
| 5960 | if (!(s->flags & SN_ERR_MASK)) |
| 5961 | s->flags |= SN_ERR_CLICL; |
| 5962 | if (!(s->flags & SN_FINST_MASK)) |
| 5963 | s->flags |= SN_FINST_H; |
| 5964 | |
| 5965 | /* process_session() will take care of the error */ |
| 5966 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5967 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5968 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5969 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5970 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5971 | return 0; |
| 5972 | } |
| 5973 | |
| 5974 | /* More interesting part now : we know that we have a complete |
| 5975 | * response which at least looks like HTTP. We have an indicator |
| 5976 | * of each header's length, so we can parse them quickly. |
| 5977 | */ |
| 5978 | |
| 5979 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5980 | 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] | 5981 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5982 | /* |
| 5983 | * 1: get the status code |
| 5984 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5985 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5986 | if (n < 1 || n > 5) |
| 5987 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5988 | /* when the client triggers a 4xx from the server, it's most often due |
| 5989 | * to a missing object or permission. These events should be tracked |
| 5990 | * because if they happen often, it may indicate a brute force or a |
| 5991 | * vulnerability scan. |
| 5992 | */ |
| 5993 | if (n == 4) |
| 5994 | session_inc_http_err_ctr(s); |
| 5995 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5996 | if (objt_server(s->target)) |
| 5997 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5998 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5999 | /* check if the response is HTTP/1.1 or above */ |
| 6000 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6001 | ((rep->buf->p[5] > '1') || |
| 6002 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6003 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6004 | |
| 6005 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 6006 | 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] | 6007 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6008 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6009 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6010 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6011 | 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] | 6012 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 6013 | /* Adjust server's health based on status code. Note: status codes 501 |
| 6014 | * and 505 are triggered on demand by client request, so we must not |
| 6015 | * count them as server failures. |
| 6016 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6017 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6018 | 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] | 6019 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6020 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6021 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6022 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6023 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6024 | /* |
| 6025 | * 2: check for cacheability. |
| 6026 | */ |
| 6027 | |
| 6028 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6029 | case 100: |
| 6030 | /* |
| 6031 | * We may be facing a 100-continue response, in which case this |
| 6032 | * is not the right response, and we're waiting for the next one. |
| 6033 | * Let's allow this response to go to the client and wait for the |
| 6034 | * next one. |
| 6035 | */ |
| 6036 | hdr_idx_init(&txn->hdr_idx); |
| 6037 | msg->next -= channel_forward(rep, msg->next); |
| 6038 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 6039 | txn->status = 0; |
| 6040 | s->logs.t_data = -1; /* was not a response yet */ |
| 6041 | goto next_one; |
| 6042 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6043 | case 200: |
| 6044 | case 203: |
| 6045 | case 206: |
| 6046 | case 300: |
| 6047 | case 301: |
| 6048 | case 410: |
| 6049 | /* RFC2616 @13.4: |
| 6050 | * "A response received with a status code of |
| 6051 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 6052 | * by a cache (...) unless a cache-control |
| 6053 | * directive prohibits caching." |
| 6054 | * |
| 6055 | * RFC2616 @9.5: POST method : |
| 6056 | * "Responses to this method are not cacheable, |
| 6057 | * unless the response includes appropriate |
| 6058 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6059 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6060 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6061 | ((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] | 6062 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 6063 | break; |
| 6064 | default: |
| 6065 | break; |
| 6066 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6067 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6068 | /* |
| 6069 | * 3: we may need to capture headers |
| 6070 | */ |
| 6071 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 6072 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6073 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6074 | txn->rsp.cap, s->fe->rsp_cap); |
| 6075 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6076 | /* 4: determine the transfer-length. |
| 6077 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 6078 | * the presence of a message-body in a RESPONSE and its transfer length |
| 6079 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6080 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6081 | * All responses to the HEAD request method MUST NOT include a |
| 6082 | * message-body, even though the presence of entity-header fields |
| 6083 | * might lead one to believe they do. All 1xx (informational), 204 |
| 6084 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 6085 | * message-body. All other responses do include a message-body, |
| 6086 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6087 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6088 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 6089 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 6090 | * always terminated by the first empty line after the header fields, |
| 6091 | * regardless of the entity-header fields present in the message. |
| 6092 | * |
| 6093 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 6094 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 6095 | * transfer-length is defined by the use of this transfer-coding. |
| 6096 | * If a Transfer-Encoding header field is present and the "chunked" |
| 6097 | * transfer-coding is not present, the transfer-length is defined by |
| 6098 | * the sender closing the connection. |
| 6099 | * |
| 6100 | * 3. If a Content-Length header field is present, its decimal value in |
| 6101 | * OCTETs represents both the entity-length and the transfer-length. |
| 6102 | * If a message is received with both a Transfer-Encoding header |
| 6103 | * field and a Content-Length header field, the latter MUST be ignored. |
| 6104 | * |
| 6105 | * 4. If the message uses the media type "multipart/byteranges", and |
| 6106 | * the transfer-length is not otherwise specified, then this self- |
| 6107 | * delimiting media type defines the transfer-length. This media |
| 6108 | * type MUST NOT be used unless the sender knows that the recipient |
| 6109 | * can parse it; the presence in a request of a Range header with |
| 6110 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 6111 | * client can parse multipart/byteranges responses. |
| 6112 | * |
| 6113 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6114 | */ |
| 6115 | |
| 6116 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6117 | * 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] | 6118 | * 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] | 6119 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6120 | */ |
| 6121 | if (txn->meth == HTTP_METH_HEAD || |
| 6122 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6123 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6124 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 6125 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6126 | goto skip_content_length; |
| 6127 | } |
| 6128 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6129 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6130 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6131 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6132 | 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] | 6133 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6134 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6135 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6136 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6137 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6138 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6139 | break; |
| 6140 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6141 | } |
| 6142 | |
| 6143 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 6144 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6145 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6146 | 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] | 6147 | signed long long cl; |
| 6148 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6149 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6150 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6151 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6152 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6153 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6154 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6155 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6156 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6157 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6158 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6159 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6160 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6161 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6162 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6163 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6164 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6165 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6166 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6167 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6168 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6169 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6170 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6171 | } |
| 6172 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6173 | if (s->fe->comp || s->be->comp) |
| 6174 | select_compression_response_header(s, rep->buf); |
| 6175 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6176 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6177 | /* Now we have to check if we need to modify the Connection header. |
| 6178 | * This is more difficult on the response than it is on the request, |
| 6179 | * because we can have two different HTTP versions and we don't know |
| 6180 | * how the client will interprete a response. For instance, let's say |
| 6181 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6182 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6183 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6184 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6185 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6186 | * indicating how a request MAY be understood by the client. In case |
| 6187 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6188 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6189 | * will fall back to explicit close. Note that we won't take risks with |
| 6190 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6191 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6192 | */ |
| 6193 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6194 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6195 | txn->status == 101)) { |
| 6196 | /* Either we've established an explicit tunnel, or we're |
| 6197 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6198 | * to understand the next protocols. We have to switch to tunnel |
| 6199 | * mode, so that we transfer the request and responses then let |
| 6200 | * this protocol pass unmodified. When we later implement specific |
| 6201 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6202 | * header which contains information about that protocol for |
| 6203 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6204 | */ |
| 6205 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6206 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6207 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6208 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6209 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6210 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6211 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6212 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6213 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6214 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6215 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6216 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6217 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6218 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6219 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6220 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6221 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6222 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6223 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6224 | /* now adjust header transformations depending on current state */ |
| 6225 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6226 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6227 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6228 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6229 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6230 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6231 | else { /* SCL / KAL */ |
| 6232 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6233 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6234 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6235 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6236 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6237 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6238 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6239 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6240 | /* Some keep-alive responses are converted to Server-close if |
| 6241 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6242 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6243 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6244 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6245 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6246 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6247 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6248 | } |
| 6249 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6250 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6251 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6252 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6253 | /* end of job, return OK */ |
| 6254 | rep->analysers &= ~an_bit; |
| 6255 | rep->analyse_exp = TICK_ETERNITY; |
| 6256 | channel_auto_close(rep); |
| 6257 | return 1; |
| 6258 | |
| 6259 | abort_keep_alive: |
| 6260 | /* A keep-alive request to the server failed on a network error. |
| 6261 | * The client is required to retry. We need to close without returning |
| 6262 | * any other information so that the client retries. |
| 6263 | */ |
| 6264 | txn->status = 0; |
| 6265 | rep->analysers = 0; |
| 6266 | s->req->analysers = 0; |
| 6267 | channel_auto_close(rep); |
| 6268 | s->logs.logwait = 0; |
| 6269 | s->logs.level = 0; |
| 6270 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6271 | channel_truncate(rep); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6272 | stream_int_retnclose(rep->cons, NULL); |
| 6273 | return 0; |
| 6274 | } |
| 6275 | |
| 6276 | /* This function performs all the processing enabled for the current response. |
| 6277 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 6278 | * and updates s->rep->analysers. It might make sense to explode it into several |
| 6279 | * other functions. It works like process_request (see indications above). |
| 6280 | */ |
| 6281 | int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px) |
| 6282 | { |
| 6283 | struct http_txn *txn = &s->txn; |
| 6284 | struct http_msg *msg = &txn->rsp; |
| 6285 | struct proxy *cur_proxy; |
| 6286 | struct cond_wordlist *wl; |
| 6287 | struct http_res_rule *http_res_last_rule = NULL; |
| 6288 | |
| 6289 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 6290 | now_ms, __FUNCTION__, |
| 6291 | s, |
| 6292 | rep, |
| 6293 | rep->rex, rep->wex, |
| 6294 | rep->flags, |
| 6295 | rep->buf->i, |
| 6296 | rep->analysers); |
| 6297 | |
| 6298 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6299 | return 0; |
| 6300 | |
| 6301 | rep->analysers &= ~an_bit; |
| 6302 | rep->analyse_exp = TICK_ETERNITY; |
| 6303 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6304 | /* The stats applet needs to adjust the Connection header but we don't |
| 6305 | * apply any filter there. |
| 6306 | */ |
| 6307 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) |
| 6308 | goto skip_filters; |
| 6309 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6310 | /* |
| 6311 | * We will have to evaluate the filters. |
| 6312 | * As opposed to version 1.2, now they will be evaluated in the |
| 6313 | * filters order and not in the header order. This means that |
| 6314 | * each filter has to be validated among all headers. |
| 6315 | * |
| 6316 | * Filters are tried with ->be first, then with ->fe if it is |
| 6317 | * different from ->be. |
| 6318 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6319 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6320 | cur_proxy = s->be; |
| 6321 | while (1) { |
| 6322 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6323 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6324 | /* evaluate http-response rules */ |
| 6325 | if (!http_res_last_rule) |
| 6326 | 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] | 6327 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6328 | /* try headers filters */ |
| 6329 | if (rule_set->rsp_exp != NULL) { |
| 6330 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6331 | return_bad_resp: |
| 6332 | if (objt_server(s->target)) { |
| 6333 | objt_server(s->target)->counters.failed_resp++; |
| 6334 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6335 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6336 | s->be->be_counters.failed_resp++; |
| 6337 | return_srv_prx_502: |
| 6338 | rep->analysers = 0; |
| 6339 | txn->status = 502; |
| 6340 | s->logs.t_data = -1; /* was not a valid response */ |
| 6341 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6342 | channel_truncate(rep); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6343 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
| 6344 | if (!(s->flags & SN_ERR_MASK)) |
| 6345 | s->flags |= SN_ERR_PRXCOND; |
| 6346 | if (!(s->flags & SN_FINST_MASK)) |
| 6347 | s->flags |= SN_FINST_H; |
| 6348 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6349 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6350 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6351 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6352 | /* has the response been denied ? */ |
| 6353 | if (txn->flags & TX_SVDENY) { |
| 6354 | if (objt_server(s->target)) |
| 6355 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6356 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6357 | s->be->be_counters.denied_resp++; |
| 6358 | s->fe->fe_counters.denied_resp++; |
| 6359 | if (s->listener->counters) |
| 6360 | s->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6361 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6362 | goto return_srv_prx_502; |
| 6363 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6364 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6365 | /* add response headers from the rule sets in the same order */ |
| 6366 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6367 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6368 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6369 | if (wl->cond) { |
| 6370 | int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 6371 | ret = acl_pass(ret); |
| 6372 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6373 | ret = !ret; |
| 6374 | if (!ret) |
| 6375 | continue; |
| 6376 | } |
| 6377 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6378 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6379 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6380 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6381 | /* check whether we're already working on the frontend */ |
| 6382 | if (cur_proxy == s->fe) |
| 6383 | break; |
| 6384 | cur_proxy = s->fe; |
| 6385 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6386 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6387 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6388 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6389 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6390 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6391 | /* |
| 6392 | * Now check for a server cookie. |
| 6393 | */ |
| 6394 | if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name || |
| 6395 | (s->be->options & PR_O_CHK_CACHE)) |
| 6396 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6397 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6398 | /* |
| 6399 | * Check for cache-control or pragma headers if required. |
| 6400 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6401 | 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] | 6402 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6403 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6404 | /* |
| 6405 | * Add server cookie in the response if needed |
| 6406 | */ |
| 6407 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6408 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 6409 | (!(s->flags & SN_DIRECT) || |
| 6410 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6411 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6412 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6413 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6414 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 6415 | !(s->flags & SN_IGNORE_PRST)) { |
| 6416 | /* the server is known, it's not the one the client requested, or the |
| 6417 | * cookie's last seen date needs to be refreshed. We have to |
| 6418 | * insert a set-cookie here, except if we want to insert only on POST |
| 6419 | * requests and this one isn't. Note that servers which don't have cookies |
| 6420 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6421 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6422 | if (!objt_server(s->target)->cookie) { |
| 6423 | chunk_printf(&trash, |
| 6424 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6425 | s->be->cookie_name); |
| 6426 | } |
| 6427 | else { |
| 6428 | 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] | 6429 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6430 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6431 | /* emit last_date, which is mandatory */ |
| 6432 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6433 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6434 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6435 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6436 | if (s->be->cookie_maxlife) { |
| 6437 | /* emit first_date, which is either the original one or |
| 6438 | * the current date. |
| 6439 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6440 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6441 | s30tob64(txn->cookie_first_date ? |
| 6442 | txn->cookie_first_date >> 2 : |
| 6443 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6444 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6445 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6446 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6447 | chunk_appendf(&trash, "; path=/"); |
| 6448 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6449 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6450 | if (s->be->cookie_domain) |
| 6451 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6452 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6453 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6454 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6455 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6456 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6457 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6458 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6459 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6460 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6461 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6462 | txn->flags &= ~TX_SCK_MASK; |
| 6463 | if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT)) |
| 6464 | /* the server did not change, only the date was updated */ |
| 6465 | txn->flags |= TX_SCK_UPDATED; |
| 6466 | else |
| 6467 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6468 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6469 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6470 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6471 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6472 | * 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] | 6473 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6474 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6475 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6476 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6477 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6478 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6479 | "Cache-control: private", 22) < 0)) |
| 6480 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6481 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6482 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6483 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6484 | /* |
| 6485 | * Check if result will be cacheable with a cookie. |
| 6486 | * We'll block the response if security checks have caught |
| 6487 | * nasty things such as a cacheable cookie. |
| 6488 | */ |
| 6489 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6490 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6491 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6492 | /* we're in presence of a cacheable response containing |
| 6493 | * a set-cookie header. We'll block it as requested by |
| 6494 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6495 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6496 | if (objt_server(s->target)) |
| 6497 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6498 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6499 | s->be->be_counters.denied_resp++; |
| 6500 | s->fe->fe_counters.denied_resp++; |
| 6501 | if (s->listener->counters) |
| 6502 | s->listener->counters->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6503 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6504 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6505 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6506 | send_log(s->be, LOG_ALERT, |
| 6507 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6508 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6509 | goto return_srv_prx_502; |
| 6510 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6511 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6512 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6513 | /* |
| 6514 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6515 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6516 | * 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] | 6517 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6518 | * want to touch any 101 response either since it's switching to another |
| 6519 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6520 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6521 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6522 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 6523 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6524 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6525 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6526 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6527 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6528 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6529 | /* we want a keep-alive response here. Keep-alive header |
| 6530 | * required if either side is not 1.1. |
| 6531 | */ |
| 6532 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6533 | want_flags |= TX_CON_KAL_SET; |
| 6534 | } |
| 6535 | else { |
| 6536 | /* we want a close response here. Close header required if |
| 6537 | * the server is 1.1, regardless of the client. |
| 6538 | */ |
| 6539 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6540 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6541 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6542 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6543 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6544 | http_change_connection_header(txn, msg, want_flags); |
| 6545 | } |
| 6546 | |
| 6547 | skip_header_mangling: |
| 6548 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
| 6549 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 6550 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6551 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6552 | /* if the user wants to log as soon as possible, without counting |
| 6553 | * bytes from the server, then this is the right moment. We have |
| 6554 | * to temporarily assign bytes_out to log what we currently have. |
| 6555 | */ |
| 6556 | if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 6557 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6558 | s->logs.bytes_out = txn->rsp.eoh; |
| 6559 | s->do_log(s); |
| 6560 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6561 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6562 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6563 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6564 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6565 | /* This function is an analyser which forwards response body (including chunk |
| 6566 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6567 | * zero byte. The only situation where it must not be called is when we're in |
| 6568 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6569 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6570 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 6571 | * 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] | 6572 | * |
| 6573 | * It is capable of compressing response data both in content-length mode and |
| 6574 | * in chunked mode. The state machines follows different flows depending on |
| 6575 | * whether content-length and chunked modes are used, since there are no |
| 6576 | * trailers in content-length : |
| 6577 | * |
| 6578 | * chk-mode cl-mode |
| 6579 | * ,----- BODY -----. |
| 6580 | * / \ |
| 6581 | * V size > 0 V chk-mode |
| 6582 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6583 | * | | size == 0 | last byte | |
| 6584 | * | v final crlf v inspected | |
| 6585 | * | TRAILERS -----------> DONE | |
| 6586 | * | | |
| 6587 | * `----------------------------------------------' |
| 6588 | * |
| 6589 | * Compression only happens in the DATA state, and must be flushed in final |
| 6590 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6591 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6592 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6593 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6594 | 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] | 6595 | { |
| 6596 | struct http_txn *txn = &s->txn; |
| 6597 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6598 | static struct buffer *tmpbuf = &buf_empty; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6599 | int compressing = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6600 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6601 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6602 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6603 | return 0; |
| 6604 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6605 | 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] | 6606 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 6607 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6608 | /* Output closed while we were sending data. We must abort and |
| 6609 | * wake the other side up. |
| 6610 | */ |
| 6611 | msg->msg_state = HTTP_MSG_ERROR; |
| 6612 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6613 | return 1; |
| 6614 | } |
| 6615 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6616 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6617 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6618 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 6619 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6620 | /* we have msg->sov which points to the first byte of message |
| 6621 | * body, and res->buf.p still points to the beginning of the |
| 6622 | * message. We forward the headers now, as we don't need them |
| 6623 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6624 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6625 | b_adv(res->buf, msg->sov); |
| 6626 | msg->next -= msg->sov; |
| 6627 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6628 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6629 | /* The previous analysers guarantee that the state is somewhere |
| 6630 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 6631 | * msg->next are always correct. |
| 6632 | */ |
| 6633 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 6634 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 6635 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6636 | else |
| 6637 | msg->msg_state = HTTP_MSG_DATA; |
| 6638 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6639 | } |
| 6640 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6641 | if (res->to_forward) { |
| 6642 | /* We can't process the buffer's contents yet */ |
| 6643 | res->flags |= CF_WAKE_WRITE; |
| 6644 | goto missing_data; |
| 6645 | } |
| 6646 | |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6647 | if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) { |
| 6648 | /* We need a compression buffer in the DATA state to put the |
| 6649 | * output of compressed data, and in CRLF state to let the |
| 6650 | * TRAILERS state finish the job of removing the trailing CRLF. |
| 6651 | */ |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6652 | if (unlikely(!tmpbuf->size)) { |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6653 | /* this is the first time we need the compression buffer */ |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 6654 | if (b_alloc(&tmpbuf) == NULL) |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6655 | goto aborted_xfer; /* no memory */ |
| 6656 | } |
| 6657 | |
| 6658 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6659 | if (ret < 0) { |
| 6660 | res->flags |= CF_WAKE_WRITE; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6661 | goto missing_data; /* not enough spaces in buffers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6662 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6663 | compressing = 1; |
| 6664 | } |
| 6665 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6666 | while (1) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6667 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6668 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6669 | /* we may have some pending data starting at res->buf->p */ |
| 6670 | if (unlikely(s->comp_algo)) { |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 6671 | ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6672 | if (ret < 0) |
| 6673 | goto aborted_xfer; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6674 | |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6675 | if (msg->chunk_len) { |
| 6676 | /* input empty or output full */ |
| 6677 | if (res->buf->i > msg->next) |
| 6678 | res->flags |= CF_WAKE_WRITE; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6679 | goto missing_data; |
| 6680 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6681 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6682 | else { |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6683 | if (msg->chunk_len > res->buf->i - msg->next) { |
| 6684 | /* output full */ |
| 6685 | res->flags |= CF_WAKE_WRITE; |
| 6686 | goto missing_data; |
| 6687 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6688 | msg->next += msg->chunk_len; |
| 6689 | msg->chunk_len = 0; |
| 6690 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6691 | |
| 6692 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6693 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6694 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6695 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6696 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6697 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6698 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6699 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6700 | |
| 6701 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 6702 | /* we want the CRLF after the data */ |
| 6703 | |
| 6704 | ret = http_skip_chunk_crlf(msg); |
| 6705 | if (ret == 0) |
| 6706 | goto missing_data; |
| 6707 | else if (ret < 0) { |
| 6708 | if (msg->err_pos >= 0) |
| 6709 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe); |
| 6710 | goto return_bad_res; |
| 6711 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6712 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 6713 | |
| 6714 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6715 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 6716 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6717 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6718 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6719 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6720 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6721 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6722 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6723 | else if (ret < 0) { |
| 6724 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6725 | 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] | 6726 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6727 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 6728 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6729 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6730 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6731 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6732 | if (unlikely(compressing)) { |
| 6733 | /* we need to flush output contents before syncing FSMs */ |
| 6734 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6735 | compressing = 0; |
| 6736 | } |
| 6737 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6738 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6739 | if (ret == 0) |
| 6740 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6741 | else if (ret < 0) { |
| 6742 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6743 | 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] | 6744 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6745 | } |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6746 | /* we're in HTTP_MSG_DONE now, fall through */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6747 | |
| 6748 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6749 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6750 | if (unlikely(compressing)) { |
| 6751 | /* we need to flush output contents before syncing FSMs */ |
| 6752 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6753 | compressing = 0; |
| 6754 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6755 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6756 | /* we may have some pending data starting at res->buf->p |
| 6757 | * such as a last chunk of data or trailers. |
| 6758 | */ |
| 6759 | b_adv(res->buf, msg->next); |
| 6760 | msg->next = 0; |
| 6761 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6762 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6763 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6764 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6765 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6766 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6767 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6768 | if (http_resync_states(s)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6769 | /* some state changes occurred, maybe the analyser |
| 6770 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6771 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6772 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6773 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6774 | /* response errors are most likely due to |
| 6775 | * the client aborting the transfer. |
| 6776 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6777 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6778 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6779 | if (msg->err_pos >= 0) |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6780 | 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] | 6781 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6782 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6783 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6784 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6785 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6786 | } |
| 6787 | } |
| 6788 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6789 | missing_data: |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6790 | /* we may have some pending data starting at res->buf->p */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6791 | if (unlikely(compressing)) { |
| 6792 | 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] | 6793 | compressing = 0; |
| 6794 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6795 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6796 | if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) { |
| 6797 | b_adv(res->buf, msg->next); |
| 6798 | msg->next = 0; |
| 6799 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6800 | } |
| 6801 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6802 | if (res->flags & CF_SHUTW) |
| 6803 | goto aborted_xfer; |
| 6804 | |
| 6805 | /* stop waiting for data if the input is closed before the end. If the |
| 6806 | * client side was already closed, it means that the client has aborted, |
| 6807 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6808 | * server abort. |
| 6809 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6810 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | b2c6a78 | 2014-04-23 20:29:01 +0200 | [diff] [blame] | 6811 | if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6812 | goto aborted_xfer; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6813 | if (!(s->flags & SN_ERR_MASK)) |
| 6814 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6815 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6816 | if (objt_server(s->target)) |
| 6817 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6818 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6819 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6820 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6821 | /* 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] | 6822 | if (!s->req->analysers) |
| 6823 | goto return_bad_res; |
| 6824 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6825 | /* 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] | 6826 | * 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] | 6827 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 6828 | * close. |
| 6829 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6830 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6831 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6832 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6833 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6834 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6835 | /* 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] | 6836 | * 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] | 6837 | * 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] | 6838 | * modes are already handled by the stream sock layer. We must not do |
| 6839 | * this in content-length mode because it could present the MSG_MORE |
| 6840 | * flag with the last block of forwarded data, which would cause an |
| 6841 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6842 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6843 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6844 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6845 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6846 | /* the session handler will take care of timeouts and errors */ |
| 6847 | return 0; |
| 6848 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6849 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6850 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6851 | if (objt_server(s->target)) |
| 6852 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6853 | |
| 6854 | return_bad_res_stats_ok: |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 6855 | if (unlikely(compressing)) { |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6856 | 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] | 6857 | compressing = 0; |
| 6858 | } |
| 6859 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6860 | /* we may have some pending data starting at res->buf->p */ |
| 6861 | if (s->comp_algo == NULL) { |
| 6862 | b_adv(res->buf, msg->next); |
| 6863 | msg->next = 0; |
| 6864 | } |
| 6865 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6866 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6867 | /* don't send any error message as we're in the body */ |
| 6868 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6869 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6870 | 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] | 6871 | if (objt_server(s->target)) |
| 6872 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6873 | |
| 6874 | if (!(s->flags & SN_ERR_MASK)) |
| 6875 | s->flags |= SN_ERR_PRXCOND; |
| 6876 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6877 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6878 | return 0; |
| 6879 | |
| 6880 | aborted_xfer: |
Willy Tarreau | 6fef8ae | 2014-04-22 21:22:06 +0200 | [diff] [blame] | 6881 | if (unlikely(compressing)) { |
| 6882 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
| 6883 | compressing = 0; |
| 6884 | } |
| 6885 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6886 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6887 | /* don't send any error message as we're in the body */ |
| 6888 | stream_int_retnclose(res->cons, NULL); |
| 6889 | res->analysers = 0; |
| 6890 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 6891 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6892 | s->fe->fe_counters.cli_aborts++; |
| 6893 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6894 | if (objt_server(s->target)) |
| 6895 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6896 | |
| 6897 | if (!(s->flags & SN_ERR_MASK)) |
| 6898 | s->flags |= SN_ERR_CLICL; |
| 6899 | if (!(s->flags & SN_FINST_MASK)) |
| 6900 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6901 | return 0; |
| 6902 | } |
| 6903 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6904 | /* Iterate the same filter through all request headers. |
| 6905 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6906 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6907 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6908 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6909 | 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] | 6910 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6911 | char *cur_ptr, *cur_end, *cur_next; |
| 6912 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6913 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6914 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6915 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6916 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6917 | last_hdr = 0; |
| 6918 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6919 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6920 | old_idx = 0; |
| 6921 | |
| 6922 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6923 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6924 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6925 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6926 | (exp->action == ACT_ALLOW || |
| 6927 | exp->action == ACT_DENY || |
| 6928 | exp->action == ACT_TARPIT)) |
| 6929 | return 0; |
| 6930 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6931 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6932 | if (!cur_idx) |
| 6933 | break; |
| 6934 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6935 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6936 | cur_ptr = cur_next; |
| 6937 | cur_end = cur_ptr + cur_hdr->len; |
| 6938 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6939 | |
| 6940 | /* Now we have one header between cur_ptr and cur_end, |
| 6941 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6942 | */ |
| 6943 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6944 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6945 | switch (exp->action) { |
| 6946 | case ACT_SETBE: |
| 6947 | /* It is not possible to jump a second time. |
| 6948 | * FIXME: should we return an HTTP/500 here so that |
| 6949 | * the admin knows there's a problem ? |
| 6950 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6951 | if (s->be != s->fe) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6952 | break; |
| 6953 | |
| 6954 | /* Swithing Proxy */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6955 | session_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6956 | last_hdr = 1; |
| 6957 | break; |
| 6958 | |
| 6959 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6960 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6961 | last_hdr = 1; |
| 6962 | break; |
| 6963 | |
| 6964 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6965 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6966 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6967 | break; |
| 6968 | |
| 6969 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6970 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6971 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6972 | break; |
| 6973 | |
| 6974 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6975 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6976 | if (trash.len < 0) |
| 6977 | return -1; |
| 6978 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6979 | 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] | 6980 | /* FIXME: if the user adds a newline in the replacement, the |
| 6981 | * index will not be recalculated for now, and the new line |
| 6982 | * will not be counted as a new header. |
| 6983 | */ |
| 6984 | |
| 6985 | cur_end += delta; |
| 6986 | cur_next += delta; |
| 6987 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6988 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6989 | break; |
| 6990 | |
| 6991 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6992 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6993 | cur_next += delta; |
| 6994 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6995 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6996 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6997 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6998 | cur_hdr->len = 0; |
| 6999 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7000 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7001 | break; |
| 7002 | |
| 7003 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7004 | } |
| 7005 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7006 | /* keep the link from this header to next one in case of later |
| 7007 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7008 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7009 | old_idx = cur_idx; |
| 7010 | } |
| 7011 | return 0; |
| 7012 | } |
| 7013 | |
| 7014 | |
| 7015 | /* Apply the filter to the request line. |
| 7016 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7017 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7018 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7019 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7020 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7021 | 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] | 7022 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7023 | char *cur_ptr, *cur_end; |
| 7024 | int done; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7025 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7026 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7027 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7028 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7029 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7030 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7031 | (exp->action == ACT_ALLOW || |
| 7032 | exp->action == ACT_DENY || |
| 7033 | exp->action == ACT_TARPIT)) |
| 7034 | return 0; |
| 7035 | else if (exp->action == ACT_REMOVE) |
| 7036 | return 0; |
| 7037 | |
| 7038 | done = 0; |
| 7039 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7040 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7041 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7042 | |
| 7043 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7044 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7045 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7046 | switch (exp->action) { |
| 7047 | case ACT_SETBE: |
| 7048 | /* It is not possible to jump a second time. |
| 7049 | * FIXME: should we return an HTTP/500 here so that |
| 7050 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7051 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7052 | if (s->be != s->fe) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7053 | break; |
| 7054 | |
| 7055 | /* Swithing Proxy */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7056 | session_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7057 | done = 1; |
| 7058 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7059 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7060 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7061 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7062 | done = 1; |
| 7063 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7064 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7065 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7066 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7067 | done = 1; |
| 7068 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7069 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7070 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7071 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7072 | done = 1; |
| 7073 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7074 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7075 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7076 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7077 | if (trash.len < 0) |
| 7078 | return -1; |
| 7079 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7080 | 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] | 7081 | /* FIXME: if the user adds a newline in the replacement, the |
| 7082 | * index will not be recalculated for now, and the new line |
| 7083 | * will not be counted as a new header. |
| 7084 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7085 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7086 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7087 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7088 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7089 | HTTP_MSG_RQMETH, |
| 7090 | cur_ptr, cur_end + 1, |
| 7091 | NULL, NULL); |
| 7092 | if (unlikely(!cur_end)) |
| 7093 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7094 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7095 | /* we have a full request and we know that we have either a CR |
| 7096 | * or an LF at <ptr>. |
| 7097 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7098 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7099 | 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] | 7100 | /* there is no point trying this regex on headers */ |
| 7101 | return 1; |
| 7102 | } |
| 7103 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7104 | return done; |
| 7105 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7106 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7107 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7108 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7109 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7110 | * 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] | 7111 | * 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] | 7112 | * unparsable request. Since it can manage the switch to another backend, it |
| 7113 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7114 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7115 | 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] | 7116 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7117 | struct http_txn *txn = &s->txn; |
| 7118 | struct hdr_exp *exp; |
| 7119 | |
| 7120 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7121 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7122 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7123 | /* |
| 7124 | * The interleaving of transformations and verdicts |
| 7125 | * makes it difficult to decide to continue or stop |
| 7126 | * the evaluation. |
| 7127 | */ |
| 7128 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7129 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7130 | break; |
| 7131 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7132 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7133 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7134 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7135 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7136 | |
| 7137 | /* if this filter had a condition, evaluate it now and skip to |
| 7138 | * next filter if the condition does not match. |
| 7139 | */ |
| 7140 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7141 | 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] | 7142 | ret = acl_pass(ret); |
| 7143 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7144 | ret = !ret; |
| 7145 | |
| 7146 | if (!ret) |
| 7147 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7148 | } |
| 7149 | |
| 7150 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7151 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7152 | if (unlikely(ret < 0)) |
| 7153 | return -1; |
| 7154 | |
| 7155 | if (likely(ret == 0)) { |
| 7156 | /* The filter did not match the request, it can be |
| 7157 | * iterated through all headers. |
| 7158 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7159 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7160 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7161 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7162 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7163 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7164 | } |
| 7165 | |
| 7166 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7167 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7168 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7169 | * Try to retrieve the server associated to the appsession. |
| 7170 | * If the server is found, it's assigned to the session. |
| 7171 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7172 | void manage_client_side_appsession(struct session *s, const char *buf, int len) { |
| 7173 | struct http_txn *txn = &s->txn; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7174 | appsess *asession = NULL; |
| 7175 | char *sessid_temp = NULL; |
| 7176 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7177 | if (len > s->be->appsession_len) { |
| 7178 | len = s->be->appsession_len; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7179 | } |
| 7180 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7181 | if (s->be->options2 & PR_O2_AS_REQL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7182 | /* 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] | 7183 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7184 | /* 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] | 7185 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7186 | } |
| 7187 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7188 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7189 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7190 | 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] | 7191 | return; |
| 7192 | } |
| 7193 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7194 | memcpy(txn->sessid, buf, len); |
| 7195 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7196 | } |
| 7197 | |
| 7198 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 7199 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7200 | 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] | 7201 | return; |
| 7202 | } |
| 7203 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7204 | memcpy(sessid_temp, buf, len); |
| 7205 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7206 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7207 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7208 | /* free previously allocated memory */ |
| 7209 | pool_free2(apools.sessid, sessid_temp); |
| 7210 | |
| 7211 | if (asession != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7212 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
| 7213 | if (!(s->be->options2 & PR_O2_AS_REQL)) |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7214 | asession->request_count++; |
| 7215 | |
| 7216 | if (asession->serverid != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7217 | struct server *srv = s->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7218 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7219 | while (srv) { |
| 7220 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7221 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7222 | (s->be->options & PR_O_PERSIST) || |
| 7223 | (s->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7224 | /* we found the server and it's usable */ |
| 7225 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7226 | 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] | 7227 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 7228 | s->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 7229 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7230 | break; |
| 7231 | } else { |
| 7232 | txn->flags &= ~TX_CK_MASK; |
| 7233 | txn->flags |= TX_CK_DOWN; |
| 7234 | } |
| 7235 | } |
| 7236 | srv = srv->next; |
| 7237 | } |
| 7238 | } |
| 7239 | } |
| 7240 | } |
| 7241 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7242 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7243 | * same way as with headers above except that the semi-colon also ends a token. |
| 7244 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7245 | * return a valid result. |
| 7246 | */ |
| 7247 | char *find_cookie_value_end(char *s, const char *e) |
| 7248 | { |
| 7249 | int quoted, qdpair; |
| 7250 | |
| 7251 | quoted = qdpair = 0; |
| 7252 | for (; s < e; s++) { |
| 7253 | if (qdpair) qdpair = 0; |
| 7254 | else if (quoted) { |
| 7255 | if (*s == '\\') qdpair = 1; |
| 7256 | else if (*s == '"') quoted = 0; |
| 7257 | } |
| 7258 | else if (*s == '"') quoted = 1; |
| 7259 | else if (*s == ',' || *s == ';') return s; |
| 7260 | } |
| 7261 | return s; |
| 7262 | } |
| 7263 | |
| 7264 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7265 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7266 | * the first delimiter is updated if required. The function tries as much as |
| 7267 | * possible to respect the following principles : |
| 7268 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7269 | * colon, in which case <next> is simply removed |
| 7270 | * - set exactly one space character after the new first delimiter, unless |
| 7271 | * there are not enough characters in the block being moved to do so. |
| 7272 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7273 | * one. |
| 7274 | * |
| 7275 | * It is the caller's responsibility to ensure that : |
| 7276 | * - <from> points to a valid delimiter or the colon ; |
| 7277 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7278 | * - there are non-space chars before <from> ; |
| 7279 | * - there is a CR/LF at or after <next>. |
| 7280 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7281 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7282 | { |
| 7283 | char *prev = *from; |
| 7284 | |
| 7285 | if (*prev == ':') { |
| 7286 | /* We're removing the first value, preserve the colon and add a |
| 7287 | * space if possible. |
| 7288 | */ |
| 7289 | if (!http_is_crlf[(unsigned char)*next]) |
| 7290 | next++; |
| 7291 | prev++; |
| 7292 | if (prev < next) |
| 7293 | *prev++ = ' '; |
| 7294 | |
| 7295 | while (http_is_spht[(unsigned char)*next]) |
| 7296 | next++; |
| 7297 | } else { |
| 7298 | /* Remove useless spaces before the old delimiter. */ |
| 7299 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 7300 | prev--; |
| 7301 | *from = prev; |
| 7302 | |
| 7303 | /* copy the delimiter and if possible a space if we're |
| 7304 | * not at the end of the line. |
| 7305 | */ |
| 7306 | if (!http_is_crlf[(unsigned char)*next]) { |
| 7307 | *prev++ = *next++; |
| 7308 | if (prev + 1 < next) |
| 7309 | *prev++ = ' '; |
| 7310 | while (http_is_spht[(unsigned char)*next]) |
| 7311 | next++; |
| 7312 | } |
| 7313 | } |
| 7314 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7315 | } |
| 7316 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7317 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7318 | * 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] | 7319 | * desirable to call it only when needed. This code is quite complex because |
| 7320 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7321 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7322 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7323 | void manage_client_side_cookies(struct session *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7324 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7325 | struct http_txn *txn = &s->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7326 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7327 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7328 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7329 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7330 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7331 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7332 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7333 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7334 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7335 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7336 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7337 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7338 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7339 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7340 | hdr_beg = hdr_next; |
| 7341 | hdr_end = hdr_beg + cur_hdr->len; |
| 7342 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7343 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7344 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7345 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7346 | * "Cookie:" headers. |
| 7347 | */ |
| 7348 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7349 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7350 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7351 | old_idx = cur_idx; |
| 7352 | continue; |
| 7353 | } |
| 7354 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7355 | del_from = NULL; /* nothing to be deleted */ |
| 7356 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7357 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7358 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7359 | * attributes whose name begin with a '$', and associate them with |
| 7360 | * the right cookie, if we want to delete this cookie. |
| 7361 | * So there are 3 cases for each cookie read : |
| 7362 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7363 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7364 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7365 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7366 | * "special" cookie. |
| 7367 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7368 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7369 | * *MUST* delete it. |
| 7370 | * |
| 7371 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7372 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7373 | * the RFC explicitly allows spaces before it, and not within the |
| 7374 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7375 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7376 | * after the equal sign too, resulting in some (rare) buggy |
| 7377 | * implementations trying to do that. So let's do what servers do. |
| 7378 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7379 | * allowed quoted strings in values, with any possible character |
| 7380 | * after a backslash, including control chars and delimitors, which |
| 7381 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7382 | * within values even without quotes. |
| 7383 | * |
| 7384 | * We have to keep multiple pointers in order to support cookie |
| 7385 | * removal at the beginning, middle or end of header without |
| 7386 | * corrupting the header. All of these headers are valid : |
| 7387 | * |
| 7388 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7389 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7390 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7391 | * | | | | | | | | | |
| 7392 | * | | | | | | | | hdr_end <--+ |
| 7393 | * | | | | | | | +--> next |
| 7394 | * | | | | | | +----> val_end |
| 7395 | * | | | | | +-----------> val_beg |
| 7396 | * | | | | +--------------> equal |
| 7397 | * | | | +----------------> att_end |
| 7398 | * | | +---------------------> att_beg |
| 7399 | * | +--------------------------> prev |
| 7400 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7401 | */ |
| 7402 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7403 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7404 | /* Iterate through all cookies on this line */ |
| 7405 | |
| 7406 | /* find att_beg */ |
| 7407 | att_beg = prev + 1; |
| 7408 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7409 | att_beg++; |
| 7410 | |
| 7411 | /* find att_end : this is the first character after the last non |
| 7412 | * space before the equal. It may be equal to hdr_end. |
| 7413 | */ |
| 7414 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7415 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7416 | while (equal < hdr_end) { |
| 7417 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7418 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7419 | if (http_is_spht[(unsigned char)*equal++]) |
| 7420 | continue; |
| 7421 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7422 | } |
| 7423 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7424 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7425 | * is between <att_beg> and <equal>, both may be identical. |
| 7426 | */ |
| 7427 | |
| 7428 | /* look for end of cookie if there is an equal sign */ |
| 7429 | if (equal < hdr_end && *equal == '=') { |
| 7430 | /* look for the beginning of the value */ |
| 7431 | val_beg = equal + 1; |
| 7432 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7433 | val_beg++; |
| 7434 | |
| 7435 | /* find the end of the value, respecting quotes */ |
| 7436 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7437 | |
| 7438 | /* make val_end point to the first white space or delimitor after the value */ |
| 7439 | val_end = next; |
| 7440 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7441 | val_end--; |
| 7442 | } else { |
| 7443 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7444 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7445 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7446 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7447 | * they will automatically be removed if a header before them is removed, |
| 7448 | * since they're supposed to be linked together. |
| 7449 | */ |
| 7450 | if (*att_beg == '$') |
| 7451 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7452 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7453 | /* Ignore cookies with no equal sign */ |
| 7454 | if (equal == next) { |
| 7455 | /* This is not our cookie, so we must preserve it. But if we already |
| 7456 | * scheduled another cookie for removal, we cannot remove the |
| 7457 | * complete header, but we can remove the previous block itself. |
| 7458 | */ |
| 7459 | preserve_hdr = 1; |
| 7460 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7461 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7462 | val_end += delta; |
| 7463 | next += delta; |
| 7464 | hdr_end += delta; |
| 7465 | hdr_next += delta; |
| 7466 | cur_hdr->len += delta; |
| 7467 | http_msg_move_end(&txn->req, delta); |
| 7468 | prev = del_from; |
| 7469 | del_from = NULL; |
| 7470 | } |
| 7471 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7472 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7473 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7474 | /* if there are spaces around the equal sign, we need to |
| 7475 | * strip them otherwise we'll get trouble for cookie captures, |
| 7476 | * or even for rewrites. Since this happens extremely rarely, |
| 7477 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7478 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7479 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7480 | int stripped_before = 0; |
| 7481 | int stripped_after = 0; |
| 7482 | |
| 7483 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7484 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7485 | equal += stripped_before; |
| 7486 | val_beg += stripped_before; |
| 7487 | } |
| 7488 | |
| 7489 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7490 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7491 | val_beg += stripped_after; |
| 7492 | stripped_before += stripped_after; |
| 7493 | } |
| 7494 | |
| 7495 | val_end += stripped_before; |
| 7496 | next += stripped_before; |
| 7497 | hdr_end += stripped_before; |
| 7498 | hdr_next += stripped_before; |
| 7499 | cur_hdr->len += stripped_before; |
| 7500 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7501 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7502 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7503 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7504 | /* First, let's see if we want to capture this cookie. We check |
| 7505 | * that we don't already have a client side cookie, because we |
| 7506 | * can only capture one. Also as an optimisation, we ignore |
| 7507 | * cookies shorter than the declared name. |
| 7508 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7509 | if (s->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7510 | (val_end - att_beg >= s->fe->capture_namelen) && |
| 7511 | memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7512 | int log_len = val_end - att_beg; |
| 7513 | |
| 7514 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7515 | Alert("HTTP logging : out of memory.\n"); |
| 7516 | } else { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7517 | if (log_len > s->fe->capture_len) |
| 7518 | log_len = s->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7519 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7520 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7521 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7522 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7523 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7524 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7525 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7526 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7527 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7528 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7529 | * For cookies in prefix mode, the form is : |
| 7530 | * |
| 7531 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7532 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7533 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7534 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7535 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7536 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7537 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7538 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7539 | * have the server ID between val_beg and delim, and the original cookie between |
| 7540 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7541 | * |
| 7542 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7543 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7544 | * | || || | |+-> next |
| 7545 | * | || || | +--> val_end |
| 7546 | * | || || +---------> delim |
| 7547 | * | || |+------------> val_beg |
| 7548 | * | || +-------------> att_end = equal |
| 7549 | * | |+-----------------> att_beg |
| 7550 | * | +------------------> prev |
| 7551 | * +-------------------------> hdr_beg |
| 7552 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7553 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7554 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7555 | for (delim = val_beg; delim < val_end; delim++) |
| 7556 | if (*delim == COOKIE_DELIM) |
| 7557 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7558 | } else { |
| 7559 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7560 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7561 | /* Now check if the cookie contains a date field, which would |
| 7562 | * appear after a vertical bar ('|') just after the server name |
| 7563 | * and before the delimiter. |
| 7564 | */ |
| 7565 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7566 | if (vbar1) { |
| 7567 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7568 | * right is the last seen date. It is a base64 encoded |
| 7569 | * 30-bit value representing the UNIX date since the |
| 7570 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7571 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7572 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7573 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7574 | if (val_end - vbar1 >= 5) { |
| 7575 | val = b64tos30(vbar1); |
| 7576 | if (val > 0) |
| 7577 | txn->cookie_last_date = val << 2; |
| 7578 | } |
| 7579 | /* look for a second vertical bar */ |
| 7580 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7581 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7582 | val = b64tos30(vbar1 + 1); |
| 7583 | if (val > 0) |
| 7584 | txn->cookie_first_date = val << 2; |
| 7585 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7586 | } |
| 7587 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7588 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7589 | /* if the cookie has an expiration date and the proxy wants to check |
| 7590 | * it, then we do that now. We first check if the cookie is too old, |
| 7591 | * then only if it has expired. We detect strict overflow because the |
| 7592 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7593 | * in the future are ignored if their offset is beyond one day. This |
| 7594 | * allows an admin to fix timezone issues without expiring everyone |
| 7595 | * and at the same time avoids keeping unwanted side effects for too |
| 7596 | * long. |
| 7597 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7598 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7599 | (((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] | 7600 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7601 | txn->flags &= ~TX_CK_MASK; |
| 7602 | txn->flags |= TX_CK_OLD; |
| 7603 | delim = val_beg; // let's pretend we have not found the cookie |
| 7604 | txn->cookie_first_date = 0; |
| 7605 | txn->cookie_last_date = 0; |
| 7606 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7607 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7608 | (((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] | 7609 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7610 | txn->flags &= ~TX_CK_MASK; |
| 7611 | txn->flags |= TX_CK_EXPIRED; |
| 7612 | delim = val_beg; // let's pretend we have not found the cookie |
| 7613 | txn->cookie_first_date = 0; |
| 7614 | txn->cookie_last_date = 0; |
| 7615 | } |
| 7616 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7617 | /* Here, we'll look for the first running server which supports the cookie. |
| 7618 | * This allows to share a same cookie between several servers, for example |
| 7619 | * to dedicate backup servers to specific servers only. |
| 7620 | * However, to prevent clients from sticking to cookie-less backup server |
| 7621 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7622 | * empty cookies and mark them as invalid. |
| 7623 | * The same behaviour is applied when persistence must be ignored. |
| 7624 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7625 | if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7626 | srv = NULL; |
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 | while (srv) { |
| 7629 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7630 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7631 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7632 | (s->be->options & PR_O_PERSIST) || |
| 7633 | (s->flags & SN_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7634 | /* we found the server and we can use it */ |
| 7635 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7636 | 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] | 7637 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 7638 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7639 | break; |
| 7640 | } else { |
| 7641 | /* we found a server, but it's down, |
| 7642 | * mark it as such and go on in case |
| 7643 | * another one is available. |
| 7644 | */ |
| 7645 | txn->flags &= ~TX_CK_MASK; |
| 7646 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7647 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7648 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7649 | srv = srv->next; |
| 7650 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7651 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7652 | 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] | 7653 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7654 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7655 | if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7656 | txn->flags |= TX_CK_UNUSED; |
| 7657 | else |
| 7658 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7659 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7660 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7661 | /* depending on the cookie mode, we may have to either : |
| 7662 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7663 | * the server never sees it ; |
| 7664 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7665 | * application cookie so that it does not get accidentely removed later, |
| 7666 | * if we're in cookie prefix mode |
| 7667 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7668 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7669 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7670 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7671 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7672 | val_end += delta; |
| 7673 | next += delta; |
| 7674 | hdr_end += delta; |
| 7675 | hdr_next += delta; |
| 7676 | cur_hdr->len += delta; |
| 7677 | http_msg_move_end(&txn->req, delta); |
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 | del_from = NULL; |
| 7680 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7681 | } |
| 7682 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7683 | (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] | 7684 | del_from = prev; |
| 7685 | } |
| 7686 | } else { |
| 7687 | /* This is not our cookie, so we must preserve it. But if we already |
| 7688 | * scheduled another cookie for removal, we cannot remove the |
| 7689 | * complete header, but we can remove the previous block itself. |
| 7690 | */ |
| 7691 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7692 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7693 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7694 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7695 | if (att_beg >= del_from) |
| 7696 | att_beg += delta; |
| 7697 | if (att_end >= del_from) |
| 7698 | att_end += delta; |
| 7699 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7700 | val_end += delta; |
| 7701 | next += delta; |
| 7702 | hdr_end += delta; |
| 7703 | hdr_next += delta; |
| 7704 | cur_hdr->len += delta; |
| 7705 | http_msg_move_end(&txn->req, delta); |
| 7706 | prev = del_from; |
| 7707 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7708 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7709 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7710 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7711 | /* Look for the appsession cookie unless persistence must be ignored */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7712 | if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7713 | int cmp_len, value_len; |
| 7714 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7715 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7716 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 7717 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 7718 | value_begin = att_beg + s->be->appsession_name_len; |
| 7719 | value_len = val_end - att_beg - s->be->appsession_name_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7720 | } else { |
| 7721 | cmp_len = att_end - att_beg; |
| 7722 | value_begin = val_beg; |
| 7723 | value_len = val_end - val_beg; |
| 7724 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7725 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7726 | /* let's see if the cookie is our appcookie */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7727 | if (cmp_len == s->be->appsession_name_len && |
| 7728 | memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) { |
| 7729 | manage_client_side_appsession(s, value_begin, value_len); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7730 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7731 | } |
| 7732 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7733 | /* continue with next cookie on this header line */ |
| 7734 | att_beg = next; |
| 7735 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7736 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7737 | /* There are no more cookies on this line. |
| 7738 | * We may still have one (or several) marked for deletion at the |
| 7739 | * end of the line. We must do this now in two ways : |
| 7740 | * - if some cookies must be preserved, we only delete from the |
| 7741 | * mark to the end of line ; |
| 7742 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7743 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7744 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7745 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7746 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7747 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7748 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7749 | cur_hdr->len += delta; |
| 7750 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7751 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7752 | |
| 7753 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7754 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7755 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7756 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7757 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7758 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7759 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7760 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7761 | } |
| 7762 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7763 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7764 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7765 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7766 | } |
| 7767 | |
| 7768 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7769 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7770 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7771 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7772 | 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] | 7773 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7774 | char *cur_ptr, *cur_end, *cur_next; |
| 7775 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7776 | struct http_txn *txn = &s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7777 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7778 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7779 | |
| 7780 | last_hdr = 0; |
| 7781 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7782 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7783 | old_idx = 0; |
| 7784 | |
| 7785 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7786 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7787 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7788 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7789 | (exp->action == ACT_ALLOW || |
| 7790 | exp->action == ACT_DENY)) |
| 7791 | return 0; |
| 7792 | |
| 7793 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7794 | if (!cur_idx) |
| 7795 | break; |
| 7796 | |
| 7797 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7798 | cur_ptr = cur_next; |
| 7799 | cur_end = cur_ptr + cur_hdr->len; |
| 7800 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7801 | |
| 7802 | /* Now we have one header between cur_ptr and cur_end, |
| 7803 | * and the next header starts at cur_next. |
| 7804 | */ |
| 7805 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7806 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7807 | switch (exp->action) { |
| 7808 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7809 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7810 | last_hdr = 1; |
| 7811 | break; |
| 7812 | |
| 7813 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7814 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7815 | last_hdr = 1; |
| 7816 | break; |
| 7817 | |
| 7818 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7819 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7820 | if (trash.len < 0) |
| 7821 | return -1; |
| 7822 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7823 | 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] | 7824 | /* FIXME: if the user adds a newline in the replacement, the |
| 7825 | * index will not be recalculated for now, and the new line |
| 7826 | * will not be counted as a new header. |
| 7827 | */ |
| 7828 | |
| 7829 | cur_end += delta; |
| 7830 | cur_next += delta; |
| 7831 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7832 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7833 | break; |
| 7834 | |
| 7835 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7836 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7837 | cur_next += delta; |
| 7838 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7839 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7840 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7841 | txn->hdr_idx.used--; |
| 7842 | cur_hdr->len = 0; |
| 7843 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7844 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7845 | break; |
| 7846 | |
| 7847 | } |
| 7848 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7849 | |
| 7850 | /* keep the link from this header to next one in case of later |
| 7851 | * removal of next header. |
| 7852 | */ |
| 7853 | old_idx = cur_idx; |
| 7854 | } |
| 7855 | return 0; |
| 7856 | } |
| 7857 | |
| 7858 | |
| 7859 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7860 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7861 | * or -1 if a replacement resulted in an invalid status line. |
| 7862 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7863 | 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] | 7864 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7865 | char *cur_ptr, *cur_end; |
| 7866 | int done; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7867 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7868 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7869 | |
| 7870 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7871 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7872 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7873 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7874 | (exp->action == ACT_ALLOW || |
| 7875 | exp->action == ACT_DENY)) |
| 7876 | return 0; |
| 7877 | else if (exp->action == ACT_REMOVE) |
| 7878 | return 0; |
| 7879 | |
| 7880 | done = 0; |
| 7881 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7882 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7883 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7884 | |
| 7885 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7886 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7887 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7888 | switch (exp->action) { |
| 7889 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7890 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7891 | done = 1; |
| 7892 | break; |
| 7893 | |
| 7894 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7895 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7896 | done = 1; |
| 7897 | break; |
| 7898 | |
| 7899 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7900 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7901 | if (trash.len < 0) |
| 7902 | return -1; |
| 7903 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7904 | 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] | 7905 | /* FIXME: if the user adds a newline in the replacement, the |
| 7906 | * index will not be recalculated for now, and the new line |
| 7907 | * will not be counted as a new header. |
| 7908 | */ |
| 7909 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7910 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7911 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7912 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7913 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7914 | cur_ptr, cur_end + 1, |
| 7915 | NULL, NULL); |
| 7916 | if (unlikely(!cur_end)) |
| 7917 | return -1; |
| 7918 | |
| 7919 | /* we have a full respnse and we know that we have either a CR |
| 7920 | * or an LF at <ptr>. |
| 7921 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7922 | 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] | 7923 | 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] | 7924 | /* there is no point trying this regex on headers */ |
| 7925 | return 1; |
| 7926 | } |
| 7927 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7928 | return done; |
| 7929 | } |
| 7930 | |
| 7931 | |
| 7932 | |
| 7933 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7934 | * 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] | 7935 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7936 | * unparsable response. |
| 7937 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7938 | 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] | 7939 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7940 | struct http_txn *txn = &s->txn; |
| 7941 | struct hdr_exp *exp; |
| 7942 | |
| 7943 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7944 | int ret; |
| 7945 | |
| 7946 | /* |
| 7947 | * The interleaving of transformations and verdicts |
| 7948 | * makes it difficult to decide to continue or stop |
| 7949 | * the evaluation. |
| 7950 | */ |
| 7951 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7952 | if (txn->flags & TX_SVDENY) |
| 7953 | break; |
| 7954 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7955 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7956 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7957 | exp->action == ACT_PASS)) { |
| 7958 | exp = exp->next; |
| 7959 | continue; |
| 7960 | } |
| 7961 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7962 | /* if this filter had a condition, evaluate it now and skip to |
| 7963 | * next filter if the condition does not match. |
| 7964 | */ |
| 7965 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7966 | 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] | 7967 | ret = acl_pass(ret); |
| 7968 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7969 | ret = !ret; |
| 7970 | if (!ret) |
| 7971 | continue; |
| 7972 | } |
| 7973 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7974 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7975 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7976 | if (unlikely(ret < 0)) |
| 7977 | return -1; |
| 7978 | |
| 7979 | if (likely(ret == 0)) { |
| 7980 | /* The filter did not match the response, it can be |
| 7981 | * iterated through all headers. |
| 7982 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7983 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 7984 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7985 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7986 | } |
| 7987 | return 0; |
| 7988 | } |
| 7989 | |
| 7990 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7991 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7992 | * 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] | 7993 | * desirable to call it only when needed. This function is also used when we |
| 7994 | * 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] | 7995 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7996 | void manage_server_side_cookies(struct session *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7997 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7998 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7999 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8000 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8001 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8002 | char *hdr_beg, *hdr_end, *hdr_next; |
| 8003 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8004 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8005 | /* Iterate through the headers. |
| 8006 | * we start with the start line. |
| 8007 | */ |
| 8008 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8009 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8010 | |
| 8011 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 8012 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8013 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8014 | |
| 8015 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8016 | hdr_beg = hdr_next; |
| 8017 | hdr_end = hdr_beg + cur_hdr->len; |
| 8018 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8019 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8020 | /* We have one full header between hdr_beg and hdr_end, and the |
| 8021 | * next header starts at hdr_next. We're only interested in |
| 8022 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8023 | */ |
| 8024 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8025 | is_cookie2 = 0; |
| 8026 | prev = hdr_beg + 10; |
| 8027 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8028 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8029 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 8030 | if (!val) { |
| 8031 | old_idx = cur_idx; |
| 8032 | continue; |
| 8033 | } |
| 8034 | is_cookie2 = 1; |
| 8035 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8036 | } |
| 8037 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8038 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 8039 | * <prev> points to the colon. |
| 8040 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8041 | txn->flags |= TX_SCK_PRESENT; |
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 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8044 | * check-cache is enabled) and we are not interested in checking |
| 8045 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8046 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8047 | if (s->be->cookie_name == NULL && |
| 8048 | s->be->appsession_name == NULL && |
| 8049 | s->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8050 | return; |
| 8051 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8052 | /* OK so now we know we have to process this response cookie. |
| 8053 | * The format of the Set-Cookie header is slightly different |
| 8054 | * from the format of the Cookie header in that it does not |
| 8055 | * support the comma as a cookie delimiter (thus the header |
| 8056 | * cannot be folded) because the Expires attribute described in |
| 8057 | * the original Netscape's spec may contain an unquoted date |
| 8058 | * with a comma inside. We have to live with this because |
| 8059 | * many browsers don't support Max-Age and some browsers don't |
| 8060 | * support quoted strings. However the Set-Cookie2 header is |
| 8061 | * clean. |
| 8062 | * |
| 8063 | * We have to keep multiple pointers in order to support cookie |
| 8064 | * removal at the beginning, middle or end of header without |
| 8065 | * corrupting the header (in case of set-cookie2). A special |
| 8066 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8067 | * fields after the first semi-colon. The <next> pointer points |
| 8068 | * either to the end of line (set-cookie) or next unquoted comma |
| 8069 | * (set-cookie2). All of these headers are valid : |
| 8070 | * |
| 8071 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8072 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8073 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8074 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8075 | * | | | | | | | | | | |
| 8076 | * | | | | | | | | +-> next hdr_end <--+ |
| 8077 | * | | | | | | | +------------> scav |
| 8078 | * | | | | | | +--------------> val_end |
| 8079 | * | | | | | +--------------------> val_beg |
| 8080 | * | | | | +----------------------> equal |
| 8081 | * | | | +------------------------> att_end |
| 8082 | * | | +----------------------------> att_beg |
| 8083 | * | +------------------------------> prev |
| 8084 | * +-----------------------------------------> hdr_beg |
| 8085 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8086 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8087 | for (; prev < hdr_end; prev = next) { |
| 8088 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8089 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8090 | /* find att_beg */ |
| 8091 | att_beg = prev + 1; |
| 8092 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8093 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8094 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8095 | /* find att_end : this is the first character after the last non |
| 8096 | * space before the equal. It may be equal to hdr_end. |
| 8097 | */ |
| 8098 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8099 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8100 | while (equal < hdr_end) { |
| 8101 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8102 | break; |
| 8103 | if (http_is_spht[(unsigned char)*equal++]) |
| 8104 | continue; |
| 8105 | att_end = equal; |
| 8106 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8107 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8108 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8109 | * is between <att_beg> and <equal>, both may be identical. |
| 8110 | */ |
| 8111 | |
| 8112 | /* look for end of cookie if there is an equal sign */ |
| 8113 | if (equal < hdr_end && *equal == '=') { |
| 8114 | /* look for the beginning of the value */ |
| 8115 | val_beg = equal + 1; |
| 8116 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8117 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8118 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8119 | /* find the end of the value, respecting quotes */ |
| 8120 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8121 | |
| 8122 | /* make val_end point to the first white space or delimitor after the value */ |
| 8123 | val_end = next; |
| 8124 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8125 | val_end--; |
| 8126 | } else { |
| 8127 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8128 | val_beg = val_end = next = equal; |
| 8129 | } |
| 8130 | |
| 8131 | if (next < hdr_end) { |
| 8132 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8133 | * a colon or semi-colon before the end. So skip all attr-value |
| 8134 | * pairs and look for the next comma. For Set-Cookie, since |
| 8135 | * commas are permitted in values, skip to the end. |
| 8136 | */ |
| 8137 | if (is_cookie2) |
| 8138 | next = find_hdr_value_end(next, hdr_end); |
| 8139 | else |
| 8140 | next = hdr_end; |
| 8141 | } |
| 8142 | |
| 8143 | /* Now everything is as on the diagram above */ |
| 8144 | |
| 8145 | /* Ignore cookies with no equal sign */ |
| 8146 | if (equal == val_end) |
| 8147 | continue; |
| 8148 | |
| 8149 | /* If there are spaces around the equal sign, we need to |
| 8150 | * strip them otherwise we'll get trouble for cookie captures, |
| 8151 | * or even for rewrites. Since this happens extremely rarely, |
| 8152 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8153 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8154 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8155 | int stripped_before = 0; |
| 8156 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8157 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8158 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8159 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8160 | equal += stripped_before; |
| 8161 | val_beg += stripped_before; |
| 8162 | } |
| 8163 | |
| 8164 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8165 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8166 | val_beg += stripped_after; |
| 8167 | stripped_before += stripped_after; |
| 8168 | } |
| 8169 | |
| 8170 | val_end += stripped_before; |
| 8171 | next += stripped_before; |
| 8172 | hdr_end += stripped_before; |
| 8173 | hdr_next += stripped_before; |
| 8174 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8175 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8176 | } |
| 8177 | |
| 8178 | /* First, let's see if we want to capture this cookie. We check |
| 8179 | * that we don't already have a server side cookie, because we |
| 8180 | * can only capture one. Also as an optimisation, we ignore |
| 8181 | * cookies shorter than the declared name. |
| 8182 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8183 | if (s->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8184 | txn->srv_cookie == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8185 | (val_end - att_beg >= s->fe->capture_namelen) && |
| 8186 | memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8187 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8188 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8189 | Alert("HTTP logging : out of memory.\n"); |
| 8190 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8191 | else { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8192 | if (log_len > s->fe->capture_len) |
| 8193 | log_len = s->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8194 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8195 | txn->srv_cookie[log_len] = 0; |
| 8196 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8197 | } |
| 8198 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8199 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8200 | /* now check if we need to process it for persistence */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8201 | if (!(s->flags & SN_IGNORE_PRST) && |
| 8202 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8203 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8204 | /* assume passive cookie by default */ |
| 8205 | txn->flags &= ~TX_SCK_MASK; |
| 8206 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8207 | |
| 8208 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8209 | * this occurrence because we'll insert another one later. |
| 8210 | * 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] | 8211 | * a direct access. |
| 8212 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8213 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8214 | /* The "preserve" flag was set, we don't want to touch the |
| 8215 | * server's cookie. |
| 8216 | */ |
| 8217 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8218 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 8219 | ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8220 | /* this cookie must be deleted */ |
| 8221 | if (*prev == ':' && next == hdr_end) { |
| 8222 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8223 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8224 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8225 | txn->hdr_idx.used--; |
| 8226 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8227 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8228 | hdr_next += delta; |
| 8229 | http_msg_move_end(&txn->rsp, delta); |
| 8230 | /* note: while both invalid now, <next> and <hdr_end> |
| 8231 | * are still equal, so the for() will stop as expected. |
| 8232 | */ |
| 8233 | } else { |
| 8234 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8235 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8236 | next = prev; |
| 8237 | hdr_end += delta; |
| 8238 | hdr_next += delta; |
| 8239 | cur_hdr->len += delta; |
| 8240 | http_msg_move_end(&txn->rsp, delta); |
| 8241 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8242 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8243 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8244 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8245 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8246 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8247 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8248 | * with this server since we know it. |
| 8249 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8250 | 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] | 8251 | next += delta; |
| 8252 | hdr_end += delta; |
| 8253 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8254 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8255 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8256 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8257 | txn->flags &= ~TX_SCK_MASK; |
| 8258 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8259 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8260 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8261 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8262 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8263 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8264 | 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] | 8265 | next += delta; |
| 8266 | hdr_end += delta; |
| 8267 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8268 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8269 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8270 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8271 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8272 | txn->flags &= ~TX_SCK_MASK; |
| 8273 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8274 | } |
| 8275 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 8276 | /* 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] | 8277 | else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8278 | int cmp_len, value_len; |
| 8279 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8280 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8281 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 8282 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 8283 | value_begin = att_beg + s->be->appsession_name_len; |
| 8284 | 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] | 8285 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8286 | cmp_len = att_end - att_beg; |
| 8287 | value_begin = val_beg; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8288 | value_len = MIN(s->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8289 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8290 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8291 | if ((cmp_len == s->be->appsession_name_len) && |
| 8292 | (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] | 8293 | /* free a possibly previously allocated memory */ |
| 8294 | pool_free2(apools.sessid, txn->sessid); |
| 8295 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8296 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8297 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8298 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8299 | 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] | 8300 | return; |
| 8301 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8302 | memcpy(txn->sessid, value_begin, value_len); |
| 8303 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8304 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8305 | } |
| 8306 | /* that's done for this cookie, check the next one on the same |
| 8307 | * line when next != hdr_end (only if is_cookie2). |
| 8308 | */ |
| 8309 | } |
| 8310 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8311 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8312 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8313 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8314 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8315 | appsess *asession = NULL; |
| 8316 | /* only do insert, if lookup fails */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8317 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8318 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 8319 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8320 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 8321 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8322 | 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] | 8323 | return; |
| 8324 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8325 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 8326 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8327 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 8328 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8329 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8330 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8331 | return; |
| 8332 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8333 | memcpy(asession->sessid, txn->sessid, s->be->appsession_len); |
| 8334 | asession->sessid[s->be->appsession_len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8335 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8336 | server_id_len = strlen(objt_server(s->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8337 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8338 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8339 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8340 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8341 | return; |
| 8342 | } |
| 8343 | asession->serverid[0] = '\0'; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8344 | memcpy(asession->serverid, objt_server(s->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8345 | |
| 8346 | asession->request_count = 0; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8347 | appsession_hash_insert(&(s->be->htbl_proxy), asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8348 | } |
| 8349 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8350 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8351 | asession->request_count++; |
| 8352 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8353 | } |
| 8354 | |
| 8355 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8356 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8357 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8358 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8359 | void check_response_for_cacheability(struct session *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8360 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8361 | struct http_txn *txn = &s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8362 | char *p1, *p2; |
| 8363 | |
| 8364 | char *cur_ptr, *cur_end, *cur_next; |
| 8365 | int cur_idx; |
| 8366 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8367 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8368 | return; |
| 8369 | |
| 8370 | /* Iterate through the headers. |
| 8371 | * we start with the start line. |
| 8372 | */ |
| 8373 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8374 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8375 | |
| 8376 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8377 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8378 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8379 | |
| 8380 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8381 | cur_ptr = cur_next; |
| 8382 | cur_end = cur_ptr + cur_hdr->len; |
| 8383 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8384 | |
| 8385 | /* We have one full header between cur_ptr and cur_end, and the |
| 8386 | * next header starts at cur_next. We're only interested in |
| 8387 | * "Cookie:" headers. |
| 8388 | */ |
| 8389 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8390 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8391 | if (val) { |
| 8392 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8393 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8394 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8395 | return; |
| 8396 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8397 | } |
| 8398 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8399 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8400 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8401 | continue; |
| 8402 | |
| 8403 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8404 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8405 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8406 | |
| 8407 | if (p1 >= cur_end) /* no more info */ |
| 8408 | continue; |
| 8409 | |
| 8410 | /* p1 is at the beginning of the value */ |
| 8411 | p2 = p1; |
| 8412 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8413 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8414 | p2++; |
| 8415 | |
| 8416 | /* we have a complete value between p1 and p2 */ |
| 8417 | if (p2 < cur_end && *p2 == '=') { |
| 8418 | /* we have something of the form no-cache="set-cookie" */ |
| 8419 | if ((cur_end - p1 >= 21) && |
| 8420 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8421 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8422 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8423 | continue; |
| 8424 | } |
| 8425 | |
| 8426 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8427 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8428 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8429 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8430 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8431 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8432 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8433 | return; |
| 8434 | } |
| 8435 | |
| 8436 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8437 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8438 | continue; |
| 8439 | } |
| 8440 | } |
| 8441 | } |
| 8442 | |
| 8443 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8444 | /* |
| 8445 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 8446 | * If the server is found, it's assigned to the session. |
| 8447 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8448 | 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] | 8449 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8450 | char *end_params, *first_param, *cur_param, *next_param; |
| 8451 | char separator; |
| 8452 | int value_len; |
| 8453 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8454 | int mode = s->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8455 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8456 | if (s->be->appsession_name == NULL || |
| 8457 | (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] | 8458 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8459 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8460 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8461 | first_param = NULL; |
| 8462 | switch (mode) { |
| 8463 | case PR_O2_AS_M_PP: |
| 8464 | first_param = memchr(begin, ';', len); |
| 8465 | break; |
| 8466 | case PR_O2_AS_M_QS: |
| 8467 | first_param = memchr(begin, '?', len); |
| 8468 | break; |
| 8469 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8470 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8471 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8472 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8473 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8474 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8475 | switch (mode) { |
| 8476 | case PR_O2_AS_M_PP: |
| 8477 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 8478 | end_params = (char *) begin + len; |
| 8479 | } |
| 8480 | separator = ';'; |
| 8481 | break; |
| 8482 | case PR_O2_AS_M_QS: |
| 8483 | end_params = (char *) begin + len; |
| 8484 | separator = '&'; |
| 8485 | break; |
| 8486 | default: |
| 8487 | /* unknown mode, shouldn't happen */ |
| 8488 | return; |
| 8489 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8490 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8491 | cur_param = next_param = end_params; |
| 8492 | while (cur_param > first_param) { |
| 8493 | cur_param--; |
| 8494 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 8495 | /* let's see if this is the appsession parameter */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8496 | if ((cur_param + s->be->appsession_name_len + 1 < next_param) && |
| 8497 | ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') && |
| 8498 | (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] | 8499 | /* Cool... it's the right one */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8500 | cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 8501 | value_len = MIN(s->be->appsession_len, next_param - cur_param); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8502 | if (value_len > 0) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8503 | manage_client_side_appsession(s, cur_param, value_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8504 | } |
| 8505 | break; |
| 8506 | } |
| 8507 | next_param = cur_param; |
| 8508 | } |
| 8509 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8510 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 8511 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8512 | appsession_hash_dump(&(s->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8513 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8514 | } |
| 8515 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8516 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8517 | * 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] | 8518 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8519 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8520 | * 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] | 8521 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8522 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8523 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8524 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8525 | 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] | 8526 | { |
| 8527 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8528 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8529 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8530 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8531 | if (!uri_auth) |
| 8532 | return 0; |
| 8533 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8534 | 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] | 8535 | return 0; |
| 8536 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8537 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8538 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8539 | return 0; |
| 8540 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8541 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8542 | return 0; |
| 8543 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8544 | return 1; |
| 8545 | } |
| 8546 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8547 | /* |
| 8548 | * 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] | 8549 | * By default it tries to report the error position as msg->err_pos. However if |
| 8550 | * this one is not set, it will then report msg->next, which is the last known |
| 8551 | * 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] | 8552 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8553 | */ |
| 8554 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8555 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8556 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8557 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8558 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8559 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8560 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8561 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8562 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8563 | len1 = MIN(len1, es->len); |
| 8564 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8565 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8566 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8567 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8568 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8569 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8570 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8571 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8572 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8573 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8574 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8575 | es->when = date; // user-visible date |
| 8576 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8577 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8578 | es->oe = other_end; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8579 | if (objt_conn(s->req->prod->end)) |
| 8580 | es->src = __objt_conn(s->req->prod->end)->addr.from; |
| 8581 | else |
| 8582 | memset(&es->src, 0, sizeof(es->src)); |
| 8583 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8584 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8585 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8586 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8587 | es->s_flags = s->flags; |
| 8588 | es->t_flags = s->txn.flags; |
| 8589 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8590 | es->b_out = chn->buf->o; |
| 8591 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8592 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8593 | es->m_clen = msg->chunk_len; |
| 8594 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8595 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8596 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8597 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8598 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8599 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8600 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8601 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8602 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8603 | * 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] | 8604 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8605 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8606 | * 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] | 8607 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8608 | 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] | 8609 | struct hdr_idx *idx, int occ, |
| 8610 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8611 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8612 | struct hdr_ctx local_ctx; |
| 8613 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8614 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8615 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8616 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8617 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8618 | if (!ctx) { |
| 8619 | local_ctx.idx = 0; |
| 8620 | ctx = &local_ctx; |
| 8621 | } |
| 8622 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8623 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8624 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8625 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8626 | occ--; |
| 8627 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8628 | *vptr = ctx->line + ctx->val; |
| 8629 | *vlen = ctx->vlen; |
| 8630 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8631 | } |
| 8632 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8633 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8634 | } |
| 8635 | |
| 8636 | /* negative occurrence, we scan all the list then walk back */ |
| 8637 | if (-occ > MAX_HDR_HISTORY) |
| 8638 | return 0; |
| 8639 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8640 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8641 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8642 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8643 | len_hist[hist_ptr] = ctx->vlen; |
| 8644 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8645 | hist_ptr = 0; |
| 8646 | found++; |
| 8647 | } |
| 8648 | if (-occ > found) |
| 8649 | return 0; |
| 8650 | /* 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] | 8651 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8652 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8653 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8654 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8655 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8656 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8657 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8658 | *vptr = ptr_hist[hist_ptr]; |
| 8659 | *vlen = len_hist[hist_ptr]; |
| 8660 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8661 | } |
| 8662 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8663 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8664 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8665 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8666 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8667 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8668 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8669 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8670 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8671 | * line header values and does not stop at commas. |
| 8672 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8673 | */ |
| 8674 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8675 | struct hdr_idx *idx, int occ, |
| 8676 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8677 | { |
| 8678 | struct hdr_ctx local_ctx; |
| 8679 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8680 | int len_hist[MAX_HDR_HISTORY]; |
| 8681 | unsigned int hist_ptr; |
| 8682 | int found; |
| 8683 | |
| 8684 | if (!ctx) { |
| 8685 | local_ctx.idx = 0; |
| 8686 | ctx = &local_ctx; |
| 8687 | } |
| 8688 | |
| 8689 | if (occ >= 0) { |
| 8690 | /* search from the beginning */ |
| 8691 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8692 | occ--; |
| 8693 | if (occ <= 0) { |
| 8694 | *vptr = ctx->line + ctx->val; |
| 8695 | *vlen = ctx->vlen; |
| 8696 | return 1; |
| 8697 | } |
| 8698 | } |
| 8699 | return 0; |
| 8700 | } |
| 8701 | |
| 8702 | /* negative occurrence, we scan all the list then walk back */ |
| 8703 | if (-occ > MAX_HDR_HISTORY) |
| 8704 | return 0; |
| 8705 | |
| 8706 | found = hist_ptr = 0; |
| 8707 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8708 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8709 | len_hist[hist_ptr] = ctx->vlen; |
| 8710 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8711 | hist_ptr = 0; |
| 8712 | found++; |
| 8713 | } |
| 8714 | if (-occ > found) |
| 8715 | return 0; |
| 8716 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8717 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8718 | */ |
| 8719 | hist_ptr += occ; |
| 8720 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8721 | hist_ptr -= MAX_HDR_HISTORY; |
| 8722 | *vptr = ptr_hist[hist_ptr]; |
| 8723 | *vlen = len_hist[hist_ptr]; |
| 8724 | return 1; |
| 8725 | } |
| 8726 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8727 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8728 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8729 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8730 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8731 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8732 | 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] | 8733 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8734 | int max; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8735 | 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] | 8736 | dir, |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8737 | objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1, |
| 8738 | 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] | 8739 | |
| 8740 | for (max = 0; start + max < end; max++) |
| 8741 | if (start[max] == '\r' || start[max] == '\n') |
| 8742 | break; |
| 8743 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8744 | UBOUND(max, trash.size - trash.len - 3); |
| 8745 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8746 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8747 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8748 | } |
| 8749 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8750 | /* |
| 8751 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 8752 | * the required fields are properly allocated and that we only need to (re)init |
| 8753 | * them. This should be used before processing any new request. |
| 8754 | */ |
| 8755 | void http_init_txn(struct session *s) |
| 8756 | { |
| 8757 | struct http_txn *txn = &s->txn; |
| 8758 | struct proxy *fe = s->fe; |
| 8759 | |
| 8760 | txn->flags = 0; |
| 8761 | txn->status = -1; |
| 8762 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8763 | txn->cookie_first_date = 0; |
| 8764 | txn->cookie_last_date = 0; |
| 8765 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8766 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8767 | 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] | 8768 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8769 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8770 | 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] | 8771 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 8772 | txn->req.chunk_len = 0LL; |
| 8773 | txn->req.body_len = 0LL; |
| 8774 | txn->rsp.chunk_len = 0LL; |
| 8775 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8776 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8777 | 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] | 8778 | txn->req.chn = s->req; |
| 8779 | txn->rsp.chn = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8780 | |
| 8781 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8782 | |
| 8783 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8784 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8785 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8786 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8787 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8788 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 8789 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8790 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8791 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 8792 | |
| 8793 | if (txn->hdr_idx.v) |
| 8794 | hdr_idx_init(&txn->hdr_idx); |
| 8795 | } |
| 8796 | |
| 8797 | /* to be used at the end of a transaction */ |
| 8798 | void http_end_txn(struct session *s) |
| 8799 | { |
| 8800 | struct http_txn *txn = &s->txn; |
| 8801 | |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8802 | /* release any possible compression context */ |
| 8803 | if (s->flags & SN_COMP_READY) |
| 8804 | s->comp_algo->end(&s->comp_ctx); |
| 8805 | s->comp_algo = NULL; |
| 8806 | s->flags &= ~SN_COMP_READY; |
| 8807 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8808 | /* these ones will have been dynamically allocated */ |
| 8809 | pool_free2(pool2_requri, txn->uri); |
| 8810 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8811 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8812 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8813 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8814 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8815 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8816 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8817 | txn->uri = NULL; |
| 8818 | txn->srv_cookie = NULL; |
| 8819 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8820 | |
| 8821 | if (txn->req.cap) { |
| 8822 | struct cap_hdr *h; |
| 8823 | for (h = s->fe->req_cap; h; h = h->next) |
| 8824 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 8825 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 8826 | } |
| 8827 | |
| 8828 | if (txn->rsp.cap) { |
| 8829 | struct cap_hdr *h; |
| 8830 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 8831 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 8832 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 8833 | } |
| 8834 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8835 | } |
| 8836 | |
| 8837 | /* to be used at the end of a transaction to prepare a new one */ |
| 8838 | void http_reset_txn(struct session *s) |
| 8839 | { |
| 8840 | http_end_txn(s); |
| 8841 | http_init_txn(s); |
| 8842 | |
| 8843 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8844 | s->logs.logwait = s->fe->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8845 | s->logs.level = 0; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 8846 | session_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8847 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8848 | /* re-init store persistence */ |
| 8849 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8850 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8851 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8852 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8853 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 8854 | s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8855 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8856 | /* We must trim any excess data from the response buffer, because we |
| 8857 | * may have blocked an invalid response from a server that we don't |
| 8858 | * want to accidentely forward once we disable the analysers, nor do |
| 8859 | * we want those data to come along with next response. A typical |
| 8860 | * example of such data would be from a buggy server responding to |
| 8861 | * a HEAD with some data, or sending more than the advertised |
| 8862 | * content-length. |
| 8863 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8864 | if (unlikely(s->rep->buf->i)) |
| 8865 | s->rep->buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8866 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8867 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8868 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8869 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8870 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8871 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8872 | |
| 8873 | s->req->rex = TICK_ETERNITY; |
| 8874 | s->req->wex = TICK_ETERNITY; |
| 8875 | s->req->analyse_exp = TICK_ETERNITY; |
| 8876 | s->rep->rex = TICK_ETERNITY; |
| 8877 | s->rep->wex = TICK_ETERNITY; |
| 8878 | s->rep->analyse_exp = TICK_ETERNITY; |
| 8879 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8880 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8881 | void free_http_res_rules(struct list *r) |
| 8882 | { |
| 8883 | struct http_res_rule *tr, *pr; |
| 8884 | |
| 8885 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8886 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8887 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8888 | free(pr); |
| 8889 | } |
| 8890 | } |
| 8891 | |
| 8892 | void free_http_req_rules(struct list *r) |
| 8893 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8894 | struct http_req_rule *tr, *pr; |
| 8895 | |
| 8896 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8897 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8898 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8899 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8900 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8901 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8902 | free(pr); |
| 8903 | } |
| 8904 | } |
| 8905 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8906 | /* parse an "http-request" rule */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8907 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8908 | { |
| 8909 | struct http_req_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8910 | struct http_req_action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8911 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8912 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8913 | |
| 8914 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 8915 | if (!rule) { |
| 8916 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8917 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8918 | } |
| 8919 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8920 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8921 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 8922 | cur_arg = 1; |
Willy Tarreau | 5bd6759 | 2014-04-28 22:00:46 +0200 | [diff] [blame] | 8923 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8924 | rule->action = HTTP_REQ_ACT_DENY; |
| 8925 | cur_arg = 1; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8926 | } else if (!strcmp(args[0], "tarpit")) { |
| 8927 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 8928 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8929 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8930 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8931 | cur_arg = 1; |
| 8932 | |
| 8933 | while(*args[cur_arg]) { |
| 8934 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8935 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8936 | cur_arg+=2; |
| 8937 | continue; |
| 8938 | } else |
| 8939 | break; |
| 8940 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8941 | } else if (!strcmp(args[0], "set-nice")) { |
| 8942 | rule->action = HTTP_REQ_ACT_SET_NICE; |
| 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 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8948 | file, linenum, args[0]); |
| 8949 | goto out_err; |
| 8950 | } |
| 8951 | rule->arg.nice = atoi(args[cur_arg]); |
| 8952 | if (rule->arg.nice < -1024) |
| 8953 | rule->arg.nice = -1024; |
| 8954 | else if (rule->arg.nice > 1024) |
| 8955 | rule->arg.nice = 1024; |
| 8956 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8957 | } else if (!strcmp(args[0], "set-tos")) { |
| 8958 | #ifdef IP_TOS |
| 8959 | char *err; |
| 8960 | rule->action = HTTP_REQ_ACT_SET_TOS; |
| 8961 | cur_arg = 1; |
| 8962 | |
| 8963 | if (!*args[cur_arg] || |
| 8964 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8965 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8966 | file, linenum, args[0]); |
| 8967 | goto out_err; |
| 8968 | } |
| 8969 | |
| 8970 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8971 | if (err && *err != '\0') { |
| 8972 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8973 | file, linenum, err, args[0]); |
| 8974 | goto out_err; |
| 8975 | } |
| 8976 | cur_arg++; |
| 8977 | #else |
| 8978 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8979 | goto out_err; |
| 8980 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8981 | } else if (!strcmp(args[0], "set-mark")) { |
| 8982 | #ifdef SO_MARK |
| 8983 | char *err; |
| 8984 | rule->action = HTTP_REQ_ACT_SET_MARK; |
| 8985 | cur_arg = 1; |
| 8986 | |
| 8987 | if (!*args[cur_arg] || |
| 8988 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8989 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8990 | file, linenum, args[0]); |
| 8991 | goto out_err; |
| 8992 | } |
| 8993 | |
| 8994 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8995 | if (err && *err != '\0') { |
| 8996 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8997 | file, linenum, err, args[0]); |
| 8998 | goto out_err; |
| 8999 | } |
| 9000 | cur_arg++; |
| 9001 | global.last_checks |= LSTCHK_NETADM; |
| 9002 | #else |
| 9003 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9004 | goto out_err; |
| 9005 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9006 | } else if (!strcmp(args[0], "set-log-level")) { |
| 9007 | rule->action = HTTP_REQ_ACT_SET_LOGL; |
| 9008 | cur_arg = 1; |
| 9009 | |
| 9010 | if (!*args[cur_arg] || |
| 9011 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9012 | bad_log_level: |
| 9013 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9014 | file, linenum, args[0]); |
| 9015 | goto out_err; |
| 9016 | } |
| 9017 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9018 | rule->arg.loglevel = -1; |
| 9019 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 9020 | goto bad_log_level; |
| 9021 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9022 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9023 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 9024 | cur_arg = 1; |
| 9025 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 9026 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9027 | (*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] | 9028 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9029 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9030 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9031 | } |
| 9032 | |
| 9033 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9034 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9035 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 9036 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9037 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9038 | 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] | 9039 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9040 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9041 | free(proxy->conf.lfs_file); |
| 9042 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9043 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9044 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9045 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
| 9046 | 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] | 9047 | cur_arg = 1; |
| 9048 | |
| 9049 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 9050 | (*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] | 9051 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 9052 | file, linenum, args[0]); |
| 9053 | goto out_err; |
| 9054 | } |
| 9055 | |
| 9056 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9057 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9058 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9059 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9060 | error = NULL; |
| 9061 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9062 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9063 | args[cur_arg + 1], error); |
| 9064 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9065 | goto out_err; |
| 9066 | } |
| 9067 | |
| 9068 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9069 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9070 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9071 | file, linenum); |
| 9072 | |
| 9073 | free(proxy->conf.lfs_file); |
| 9074 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9075 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9076 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9077 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9078 | rule->action = HTTP_REQ_ACT_DEL_HDR; |
| 9079 | cur_arg = 1; |
| 9080 | |
| 9081 | if (!*args[cur_arg] || |
| 9082 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9083 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9084 | file, linenum, args[0]); |
| 9085 | goto out_err; |
| 9086 | } |
| 9087 | |
| 9088 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9089 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9090 | |
| 9091 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9092 | free(proxy->conf.lfs_file); |
| 9093 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9094 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9095 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9096 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9097 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 9098 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9099 | struct sample_expr *expr; |
| 9100 | unsigned int where; |
| 9101 | char *err = NULL; |
| 9102 | |
| 9103 | cur_arg = 1; |
| 9104 | proxy->conf.args.ctx = ARGC_TRK; |
| 9105 | |
| 9106 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9107 | if (!expr) { |
| 9108 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9109 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9110 | free(err); |
| 9111 | goto out_err; |
| 9112 | } |
| 9113 | |
| 9114 | where = 0; |
| 9115 | if (proxy->cap & PR_CAP_FE) |
| 9116 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9117 | if (proxy->cap & PR_CAP_BE) |
| 9118 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9119 | |
| 9120 | if (!(expr->fetch->val & where)) { |
| 9121 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9122 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9123 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9124 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9125 | free(expr); |
| 9126 | goto out_err; |
| 9127 | } |
| 9128 | |
| 9129 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9130 | cur_arg++; |
| 9131 | if (!args[cur_arg]) { |
| 9132 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 9133 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9134 | free(expr); |
| 9135 | goto out_err; |
| 9136 | } |
| 9137 | /* we copy the table name for now, it will be resolved later */ |
| 9138 | rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]); |
| 9139 | cur_arg++; |
| 9140 | } |
| 9141 | rule->act_prm.trk_ctr.expr = expr; |
| 9142 | rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0'; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9143 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9144 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9145 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9146 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9147 | 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] | 9148 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9149 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9150 | goto out_err; |
| 9151 | } |
| 9152 | |
| 9153 | /* this redirect rule might already contain a parsed condition which |
| 9154 | * we'll pass to the http-request rule. |
| 9155 | */ |
| 9156 | rule->action = HTTP_REQ_ACT_REDIR; |
| 9157 | rule->arg.redir = redir; |
| 9158 | rule->cond = redir->cond; |
| 9159 | redir->cond = NULL; |
| 9160 | cur_arg = 2; |
| 9161 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9162 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9163 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9164 | rule->action = HTTP_REQ_ACT_ADD_ACL; |
| 9165 | /* |
| 9166 | * '+ 8' for 'add-acl(' |
| 9167 | * '- 9' for 'add-acl(' + trailing ')' |
| 9168 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9169 | 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] | 9170 | |
| 9171 | cur_arg = 1; |
| 9172 | |
| 9173 | if (!*args[cur_arg] || |
| 9174 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9175 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9176 | file, linenum, args[0]); |
| 9177 | goto out_err; |
| 9178 | } |
| 9179 | |
| 9180 | LIST_INIT(&rule->arg.map.key); |
| 9181 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9182 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9183 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9184 | file, linenum); |
| 9185 | free(proxy->conf.lfs_file); |
| 9186 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9187 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9188 | cur_arg += 1; |
| 9189 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9190 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
| 9191 | rule->action = HTTP_REQ_ACT_DEL_ACL; |
| 9192 | /* |
| 9193 | * '+ 8' for 'del-acl(' |
| 9194 | * '- 9' for 'del-acl(' + trailing ')' |
| 9195 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9196 | 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] | 9197 | |
| 9198 | cur_arg = 1; |
| 9199 | |
| 9200 | if (!*args[cur_arg] || |
| 9201 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9202 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9203 | file, linenum, args[0]); |
| 9204 | goto out_err; |
| 9205 | } |
| 9206 | |
| 9207 | LIST_INIT(&rule->arg.map.key); |
| 9208 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9209 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9210 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9211 | file, linenum); |
| 9212 | free(proxy->conf.lfs_file); |
| 9213 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9214 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9215 | cur_arg += 1; |
| 9216 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9217 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
| 9218 | rule->action = HTTP_REQ_ACT_DEL_MAP; |
| 9219 | /* |
| 9220 | * '+ 8' for 'del-map(' |
| 9221 | * '- 9' for 'del-map(' + trailing ')' |
| 9222 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9223 | 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] | 9224 | |
| 9225 | cur_arg = 1; |
| 9226 | |
| 9227 | if (!*args[cur_arg] || |
| 9228 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9229 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9230 | file, linenum, args[0]); |
| 9231 | goto out_err; |
| 9232 | } |
| 9233 | |
| 9234 | LIST_INIT(&rule->arg.map.key); |
| 9235 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9236 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9237 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9238 | file, linenum); |
| 9239 | free(proxy->conf.lfs_file); |
| 9240 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9241 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9242 | cur_arg += 1; |
| 9243 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9244 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9245 | rule->action = HTTP_REQ_ACT_SET_MAP; |
| 9246 | /* |
| 9247 | * '+ 8' for 'set-map(' |
| 9248 | * '- 9' for 'set-map(' + trailing ')' |
| 9249 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9250 | 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] | 9251 | |
| 9252 | cur_arg = 1; |
| 9253 | |
| 9254 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9255 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9256 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9257 | file, linenum, args[0]); |
| 9258 | goto out_err; |
| 9259 | } |
| 9260 | |
| 9261 | LIST_INIT(&rule->arg.map.key); |
| 9262 | LIST_INIT(&rule->arg.map.value); |
| 9263 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9264 | |
| 9265 | /* key pattern */ |
| 9266 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9267 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9268 | file, linenum); |
| 9269 | |
| 9270 | /* value pattern */ |
| 9271 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9272 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9273 | file, linenum); |
| 9274 | free(proxy->conf.lfs_file); |
| 9275 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9276 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9277 | |
| 9278 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9279 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9280 | char *errmsg = NULL; |
| 9281 | cur_arg = 1; |
| 9282 | /* try in the module list */ |
| 9283 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9284 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9285 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9286 | free(errmsg); |
| 9287 | goto out_err; |
| 9288 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9289 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9290 | 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] | 9291 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9292 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9293 | } |
| 9294 | |
| 9295 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9296 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9297 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9298 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9299 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9300 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9301 | file, linenum, args[0], errmsg); |
| 9302 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9303 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9304 | } |
| 9305 | rule->cond = cond; |
| 9306 | } |
| 9307 | else if (*args[cur_arg]) { |
| 9308 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9309 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9310 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9311 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9312 | } |
| 9313 | |
| 9314 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9315 | out_err: |
| 9316 | free(rule); |
| 9317 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9318 | } |
| 9319 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9320 | /* parse an "http-respose" rule */ |
| 9321 | struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 9322 | { |
| 9323 | struct http_res_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9324 | struct http_res_action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9325 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9326 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9327 | |
| 9328 | rule = calloc(1, sizeof(*rule)); |
| 9329 | if (!rule) { |
| 9330 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9331 | goto out_err; |
| 9332 | } |
| 9333 | |
| 9334 | if (!strcmp(args[0], "allow")) { |
| 9335 | rule->action = HTTP_RES_ACT_ALLOW; |
| 9336 | cur_arg = 1; |
| 9337 | } else if (!strcmp(args[0], "deny")) { |
| 9338 | rule->action = HTTP_RES_ACT_DENY; |
| 9339 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9340 | } else if (!strcmp(args[0], "set-nice")) { |
| 9341 | rule->action = HTTP_RES_ACT_SET_NICE; |
| 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 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9347 | file, linenum, args[0]); |
| 9348 | goto out_err; |
| 9349 | } |
| 9350 | rule->arg.nice = atoi(args[cur_arg]); |
| 9351 | if (rule->arg.nice < -1024) |
| 9352 | rule->arg.nice = -1024; |
| 9353 | else if (rule->arg.nice > 1024) |
| 9354 | rule->arg.nice = 1024; |
| 9355 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9356 | } else if (!strcmp(args[0], "set-tos")) { |
| 9357 | #ifdef IP_TOS |
| 9358 | char *err; |
| 9359 | rule->action = HTTP_RES_ACT_SET_TOS; |
| 9360 | cur_arg = 1; |
| 9361 | |
| 9362 | if (!*args[cur_arg] || |
| 9363 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9364 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9365 | file, linenum, args[0]); |
| 9366 | goto out_err; |
| 9367 | } |
| 9368 | |
| 9369 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9370 | if (err && *err != '\0') { |
| 9371 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9372 | file, linenum, err, args[0]); |
| 9373 | goto out_err; |
| 9374 | } |
| 9375 | cur_arg++; |
| 9376 | #else |
| 9377 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9378 | goto out_err; |
| 9379 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9380 | } else if (!strcmp(args[0], "set-mark")) { |
| 9381 | #ifdef SO_MARK |
| 9382 | char *err; |
| 9383 | rule->action = HTTP_RES_ACT_SET_MARK; |
| 9384 | cur_arg = 1; |
| 9385 | |
| 9386 | if (!*args[cur_arg] || |
| 9387 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9388 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9389 | file, linenum, args[0]); |
| 9390 | goto out_err; |
| 9391 | } |
| 9392 | |
| 9393 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9394 | if (err && *err != '\0') { |
| 9395 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9396 | file, linenum, err, args[0]); |
| 9397 | goto out_err; |
| 9398 | } |
| 9399 | cur_arg++; |
| 9400 | global.last_checks |= LSTCHK_NETADM; |
| 9401 | #else |
| 9402 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9403 | goto out_err; |
| 9404 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9405 | } else if (!strcmp(args[0], "set-log-level")) { |
| 9406 | rule->action = HTTP_RES_ACT_SET_LOGL; |
| 9407 | cur_arg = 1; |
| 9408 | |
| 9409 | if (!*args[cur_arg] || |
| 9410 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9411 | bad_log_level: |
| 9412 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9413 | file, linenum, args[0]); |
| 9414 | goto out_err; |
| 9415 | } |
| 9416 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9417 | rule->arg.loglevel = -1; |
| 9418 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 9419 | goto bad_log_level; |
| 9420 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9421 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9422 | rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR; |
| 9423 | cur_arg = 1; |
| 9424 | |
| 9425 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9426 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9427 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9428 | file, linenum, args[0]); |
| 9429 | goto out_err; |
| 9430 | } |
| 9431 | |
| 9432 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9433 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9434 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9435 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9436 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9437 | 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] | 9438 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9439 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9440 | free(proxy->conf.lfs_file); |
| 9441 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9442 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9443 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9444 | } 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] | 9445 | 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] | 9446 | cur_arg = 1; |
| 9447 | |
| 9448 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9449 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9450 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9451 | file, linenum, args[0]); |
| 9452 | goto out_err; |
| 9453 | } |
| 9454 | |
| 9455 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9456 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9457 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9458 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9459 | error = NULL; |
| 9460 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9461 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9462 | args[cur_arg + 1], error); |
| 9463 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9464 | goto out_err; |
| 9465 | } |
| 9466 | |
| 9467 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9468 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9469 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9470 | file, linenum); |
| 9471 | |
| 9472 | free(proxy->conf.lfs_file); |
| 9473 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9474 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9475 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9476 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9477 | rule->action = HTTP_RES_ACT_DEL_HDR; |
| 9478 | cur_arg = 1; |
| 9479 | |
| 9480 | if (!*args[cur_arg] || |
| 9481 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9482 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9483 | file, linenum, args[0]); |
| 9484 | goto out_err; |
| 9485 | } |
| 9486 | |
| 9487 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9488 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9489 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9490 | proxy->conf.args.ctx = ARGC_HRS; |
| 9491 | free(proxy->conf.lfs_file); |
| 9492 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9493 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9494 | cur_arg += 1; |
| 9495 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9496 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9497 | rule->action = HTTP_RES_ACT_ADD_ACL; |
| 9498 | /* |
| 9499 | * '+ 8' for 'add-acl(' |
| 9500 | * '- 9' for 'add-acl(' + trailing ')' |
| 9501 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9502 | 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] | 9503 | |
| 9504 | cur_arg = 1; |
| 9505 | |
| 9506 | if (!*args[cur_arg] || |
| 9507 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9508 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9509 | file, linenum, args[0]); |
| 9510 | goto out_err; |
| 9511 | } |
| 9512 | |
| 9513 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9514 | proxy->conf.args.ctx = ARGC_HRS; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9515 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9516 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9517 | file, linenum); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9518 | free(proxy->conf.lfs_file); |
| 9519 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9520 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9521 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9522 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9523 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9524 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
| 9525 | rule->action = HTTP_RES_ACT_DEL_ACL; |
| 9526 | /* |
| 9527 | * '+ 8' for 'del-acl(' |
| 9528 | * '- 9' for 'del-acl(' + trailing ')' |
| 9529 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9530 | 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] | 9531 | |
| 9532 | cur_arg = 1; |
| 9533 | |
| 9534 | if (!*args[cur_arg] || |
| 9535 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9536 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9537 | file, linenum, args[0]); |
| 9538 | goto out_err; |
| 9539 | } |
| 9540 | |
| 9541 | LIST_INIT(&rule->arg.map.key); |
| 9542 | proxy->conf.args.ctx = ARGC_HRS; |
| 9543 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9544 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9545 | file, linenum); |
| 9546 | free(proxy->conf.lfs_file); |
| 9547 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9548 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9549 | cur_arg += 1; |
| 9550 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9551 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
| 9552 | rule->action = HTTP_RES_ACT_DEL_MAP; |
| 9553 | /* |
| 9554 | * '+ 8' for 'del-map(' |
| 9555 | * '- 9' for 'del-map(' + trailing ')' |
| 9556 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9557 | 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] | 9558 | |
| 9559 | cur_arg = 1; |
| 9560 | |
| 9561 | if (!*args[cur_arg] || |
| 9562 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9563 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9564 | file, linenum, args[0]); |
| 9565 | goto out_err; |
| 9566 | } |
| 9567 | |
| 9568 | LIST_INIT(&rule->arg.map.key); |
| 9569 | proxy->conf.args.ctx = ARGC_HRS; |
| 9570 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9571 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9572 | file, linenum); |
| 9573 | free(proxy->conf.lfs_file); |
| 9574 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9575 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9576 | cur_arg += 1; |
| 9577 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9578 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9579 | rule->action = HTTP_RES_ACT_SET_MAP; |
| 9580 | /* |
| 9581 | * '+ 8' for 'set-map(' |
| 9582 | * '- 9' for 'set-map(' + trailing ')' |
| 9583 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9584 | 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] | 9585 | |
| 9586 | cur_arg = 1; |
| 9587 | |
| 9588 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9589 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9590 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9591 | file, linenum, args[0]); |
| 9592 | goto out_err; |
| 9593 | } |
| 9594 | |
| 9595 | LIST_INIT(&rule->arg.map.key); |
| 9596 | LIST_INIT(&rule->arg.map.value); |
| 9597 | |
| 9598 | proxy->conf.args.ctx = ARGC_HRS; |
| 9599 | |
| 9600 | /* key pattern */ |
| 9601 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9602 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9603 | file, linenum); |
| 9604 | |
| 9605 | /* value pattern */ |
| 9606 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9607 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9608 | file, linenum); |
| 9609 | |
| 9610 | free(proxy->conf.lfs_file); |
| 9611 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9612 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9613 | |
| 9614 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9615 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9616 | char *errmsg = NULL; |
| 9617 | cur_arg = 1; |
| 9618 | /* try in the module list */ |
| 9619 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9620 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9621 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9622 | free(errmsg); |
| 9623 | goto out_err; |
| 9624 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9625 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9626 | 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] | 9627 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 9628 | goto out_err; |
| 9629 | } |
| 9630 | |
| 9631 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9632 | struct acl_cond *cond; |
| 9633 | char *errmsg = NULL; |
| 9634 | |
| 9635 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9636 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9637 | file, linenum, args[0], errmsg); |
| 9638 | free(errmsg); |
| 9639 | goto out_err; |
| 9640 | } |
| 9641 | rule->cond = cond; |
| 9642 | } |
| 9643 | else if (*args[cur_arg]) { |
| 9644 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9645 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9646 | file, linenum, args[0], args[cur_arg]); |
| 9647 | goto out_err; |
| 9648 | } |
| 9649 | |
| 9650 | return rule; |
| 9651 | out_err: |
| 9652 | free(rule); |
| 9653 | return NULL; |
| 9654 | } |
| 9655 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9656 | /* 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] | 9657 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
| 9658 | * dynamic log-format rule instead of a static string. |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9659 | */ |
| 9660 | 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] | 9661 | const char **args, char **errmsg, int use_fmt) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9662 | { |
| 9663 | struct redirect_rule *rule; |
| 9664 | int cur_arg; |
| 9665 | int type = REDIRECT_TYPE_NONE; |
| 9666 | int code = 302; |
| 9667 | const char *destination = NULL; |
| 9668 | const char *cookie = NULL; |
| 9669 | int cookie_set = 0; |
| 9670 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9671 | struct acl_cond *cond = NULL; |
| 9672 | |
| 9673 | cur_arg = 0; |
| 9674 | while (*(args[cur_arg])) { |
| 9675 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9676 | if (!*args[cur_arg + 1]) |
| 9677 | goto missing_arg; |
| 9678 | |
| 9679 | type = REDIRECT_TYPE_LOCATION; |
| 9680 | cur_arg++; |
| 9681 | destination = args[cur_arg]; |
| 9682 | } |
| 9683 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9684 | if (!*args[cur_arg + 1]) |
| 9685 | goto missing_arg; |
| 9686 | |
| 9687 | type = REDIRECT_TYPE_PREFIX; |
| 9688 | cur_arg++; |
| 9689 | destination = args[cur_arg]; |
| 9690 | } |
| 9691 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9692 | if (!*args[cur_arg + 1]) |
| 9693 | goto missing_arg; |
| 9694 | |
| 9695 | type = REDIRECT_TYPE_SCHEME; |
| 9696 | cur_arg++; |
| 9697 | destination = args[cur_arg]; |
| 9698 | } |
| 9699 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9700 | if (!*args[cur_arg + 1]) |
| 9701 | goto missing_arg; |
| 9702 | |
| 9703 | cur_arg++; |
| 9704 | cookie = args[cur_arg]; |
| 9705 | cookie_set = 1; |
| 9706 | } |
| 9707 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9708 | if (!*args[cur_arg + 1]) |
| 9709 | goto missing_arg; |
| 9710 | |
| 9711 | cur_arg++; |
| 9712 | cookie = args[cur_arg]; |
| 9713 | cookie_set = 0; |
| 9714 | } |
| 9715 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9716 | if (!*args[cur_arg + 1]) |
| 9717 | goto missing_arg; |
| 9718 | |
| 9719 | cur_arg++; |
| 9720 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9721 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9722 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9723 | "'%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] | 9724 | args[cur_arg - 1], args[cur_arg]); |
| 9725 | return NULL; |
| 9726 | } |
| 9727 | } |
| 9728 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9729 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9730 | } |
| 9731 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9732 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9733 | } |
| 9734 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9735 | strcmp(args[cur_arg], "unless") == 0) { |
| 9736 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 9737 | if (!cond) { |
| 9738 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9739 | return NULL; |
| 9740 | } |
| 9741 | break; |
| 9742 | } |
| 9743 | else { |
| 9744 | memprintf(errmsg, |
| 9745 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9746 | args[cur_arg]); |
| 9747 | return NULL; |
| 9748 | } |
| 9749 | cur_arg++; |
| 9750 | } |
| 9751 | |
| 9752 | if (type == REDIRECT_TYPE_NONE) { |
| 9753 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9754 | return NULL; |
| 9755 | } |
| 9756 | |
| 9757 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 9758 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9759 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9760 | |
| 9761 | if (!use_fmt) { |
| 9762 | /* old-style static redirect rule */ |
| 9763 | rule->rdr_str = strdup(destination); |
| 9764 | rule->rdr_len = strlen(destination); |
| 9765 | } |
| 9766 | else { |
| 9767 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9768 | |
| 9769 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9770 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9771 | * makes it hard for the user to configure a self-redirection. |
| 9772 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9773 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9774 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9775 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9776 | (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9777 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9778 | free(curproxy->conf.lfs_file); |
| 9779 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9780 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9781 | } |
| 9782 | } |
| 9783 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9784 | if (cookie) { |
| 9785 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9786 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9787 | */ |
| 9788 | rule->cookie_len = strlen(cookie); |
| 9789 | if (cookie_set) { |
| 9790 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9791 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9792 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9793 | rule->cookie_len += 9; |
| 9794 | } else { |
| 9795 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9796 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9797 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9798 | rule->cookie_len += 20; |
| 9799 | } |
| 9800 | } |
| 9801 | rule->type = type; |
| 9802 | rule->code = code; |
| 9803 | rule->flags = flags; |
| 9804 | LIST_INIT(&rule->list); |
| 9805 | return rule; |
| 9806 | |
| 9807 | missing_arg: |
| 9808 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9809 | return NULL; |
| 9810 | } |
| 9811 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9812 | /************************************************************************/ |
| 9813 | /* The code below is dedicated to ACL parsing and matching */ |
| 9814 | /************************************************************************/ |
| 9815 | |
| 9816 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9817 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9818 | * which means that a request or response is ready. If some data is missing, |
| 9819 | * 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] | 9820 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9821 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9822 | * |
| 9823 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9824 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9825 | * decide whether or not an HTTP message is present ; |
| 9826 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9827 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9828 | * 1 if an HTTP message is ready |
| 9829 | */ |
| 9830 | static int |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9831 | 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] | 9832 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9833 | { |
| 9834 | struct http_txn *txn = l7; |
| 9835 | struct http_msg *msg = &txn->req; |
| 9836 | |
| 9837 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 9838 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 9839 | */ |
| 9840 | |
| 9841 | if (unlikely(!s || !txn)) |
| 9842 | return 0; |
| 9843 | |
| 9844 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9845 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9846 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9847 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9848 | if (unlikely(!s->req)) |
| 9849 | return 0; |
| 9850 | |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9851 | /* If the buffer does not leave enough free space at the end, |
| 9852 | * we must first realign it. |
| 9853 | */ |
| 9854 | if (s->req->buf->p > s->req->buf->data && |
| 9855 | s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite) |
| 9856 | buffer_slow_realign(s->req->buf); |
| 9857 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9858 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9859 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9860 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9861 | |
| 9862 | /* Try to decode HTTP request */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9863 | if (likely(msg->next < s->req->buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9864 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9865 | |
| 9866 | /* Still no valid request ? */ |
| 9867 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9868 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9869 | buffer_full(s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9870 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9871 | } |
| 9872 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9873 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9874 | return 0; |
| 9875 | } |
| 9876 | |
| 9877 | /* OK we just got a valid HTTP request. We have some minor |
| 9878 | * preparation to perform so that further checks can rely |
| 9879 | * on HTTP tests. |
| 9880 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9881 | |
| 9882 | /* If the request was parsed but was too large, we must absolutely |
| 9883 | * return an error so that it is not processed. At the moment this |
| 9884 | * cannot happen, but if the parsers are to change in the future, |
| 9885 | * we want this check to be maintained. |
| 9886 | */ |
| 9887 | if (unlikely(s->req->buf->i + s->req->buf->p > |
| 9888 | s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) { |
| 9889 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9890 | smp->data.uint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9891 | return 1; |
| 9892 | } |
| 9893 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9894 | 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] | 9895 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 9896 | s->flags |= SN_REDIRECTABLE; |
| 9897 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9898 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9899 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9900 | } |
| 9901 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9902 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9903 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9904 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9905 | |
| 9906 | /* otherwise everything's ready for the request */ |
| 9907 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9908 | else { |
| 9909 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9910 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9911 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9912 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9913 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9914 | } |
| 9915 | |
| 9916 | /* everything's OK */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9917 | smp->data.uint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9918 | return 1; |
| 9919 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9920 | |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 9921 | /* Note: these functinos *do* modify the sample. Even in case of success, at |
| 9922 | * least the type and uint value are modified. |
| 9923 | */ |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9924 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9925 | 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] | 9926 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9927 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9928 | 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] | 9929 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9930 | |
| 9931 | /* 1. Check on METHOD |
| 9932 | * We use the pre-parsed method if it is known, and store its number as an |
| 9933 | * integer. If it is unknown, we use the pointer and the length. |
| 9934 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9935 | 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] | 9936 | { |
| 9937 | int len, meth; |
| 9938 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9939 | len = strlen(text); |
| 9940 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9941 | |
| 9942 | pattern->val.i = meth; |
| 9943 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 9944 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9945 | pattern->len = len; |
| 9946 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9947 | else { |
| 9948 | pattern->ptr.str = NULL; |
| 9949 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9950 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9951 | return 1; |
| 9952 | } |
| 9953 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9954 | /* This function fetches the method of current HTTP request and stores |
| 9955 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 9956 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 9957 | * in <len> and <ptr> is NULL ; |
| 9958 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 9959 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9960 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9961 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9962 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9963 | smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 9964 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9965 | { |
| 9966 | int meth; |
| 9967 | struct http_txn *txn = l7; |
| 9968 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9969 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9970 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9971 | meth = txn->meth; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9972 | smp->type = SMP_T_METH; |
| 9973 | smp->data.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9974 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9975 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9976 | /* ensure the indexes are not affected */ |
| 9977 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9978 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9979 | smp->data.meth.str.len = txn->req.sl.rq.m_l; |
| 9980 | smp->data.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9981 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9982 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9983 | return 1; |
| 9984 | } |
| 9985 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9986 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9987 | 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] | 9988 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9989 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9990 | struct pattern_list *lst; |
| 9991 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9992 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9993 | list_for_each_entry(lst, &expr->patterns, list) { |
| 9994 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9995 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9996 | /* well-known method */ |
| 9997 | if (pattern->val.i != HTTP_METH_OTHER) { |
| 9998 | if (smp->data.meth.meth == pattern->val.i) |
| 9999 | return pattern; |
| 10000 | else |
| 10001 | continue; |
| 10002 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10003 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10004 | /* Other method, we must compare the strings */ |
| 10005 | if (pattern->len != smp->data.meth.str.len) |
| 10006 | continue; |
| 10007 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10008 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | 4de2a94 | 2014-08-28 20:42:57 +0200 | [diff] [blame] | 10009 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) || |
| 10010 | (!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] | 10011 | return pattern; |
| 10012 | } |
| 10013 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10014 | } |
| 10015 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10016 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10017 | smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10018 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10019 | { |
| 10020 | struct http_txn *txn = l7; |
| 10021 | char *ptr; |
| 10022 | int len; |
| 10023 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10024 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10025 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10026 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10027 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10028 | |
| 10029 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10030 | if (len <= 0) |
| 10031 | return 0; |
| 10032 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10033 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10034 | smp->data.str.str = ptr; |
| 10035 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10036 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10037 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10038 | return 1; |
| 10039 | } |
| 10040 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10041 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10042 | smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10043 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10044 | { |
| 10045 | struct http_txn *txn = l7; |
| 10046 | char *ptr; |
| 10047 | int len; |
| 10048 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10049 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10050 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10051 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10052 | return 0; |
| 10053 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10054 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10055 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10056 | |
| 10057 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10058 | if (len <= 0) |
| 10059 | return 0; |
| 10060 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10061 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10062 | smp->data.str.str = ptr; |
| 10063 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10064 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10065 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10066 | return 1; |
| 10067 | } |
| 10068 | |
| 10069 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10070 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10071 | smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10072 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10073 | { |
| 10074 | struct http_txn *txn = l7; |
| 10075 | char *ptr; |
| 10076 | int len; |
| 10077 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10078 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10079 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10080 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10081 | return 0; |
| 10082 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10083 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10084 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10085 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10086 | smp->type = SMP_T_UINT; |
| 10087 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10088 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10089 | return 1; |
| 10090 | } |
| 10091 | |
| 10092 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10093 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10094 | smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10095 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10096 | { |
| 10097 | struct http_txn *txn = l7; |
| 10098 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10099 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10100 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10101 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10102 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10103 | 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] | 10104 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10105 | return 1; |
| 10106 | } |
| 10107 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10108 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10109 | smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10110 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10111 | { |
| 10112 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10113 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10114 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10115 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10116 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10117 | 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] | 10118 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10119 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10120 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10121 | smp->type = SMP_T_IPV4; |
| 10122 | smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10123 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10124 | return 1; |
| 10125 | } |
| 10126 | |
| 10127 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10128 | smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10129 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10130 | { |
| 10131 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10132 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10133 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10134 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10135 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10136 | 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] | 10137 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10138 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10139 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10140 | smp->type = SMP_T_UINT; |
| 10141 | smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10142 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10143 | return 1; |
| 10144 | } |
| 10145 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10146 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10147 | * Accepts an optional argument of type string containing the header field name, |
| 10148 | * and an optional argument of type signed or unsigned integer to request an |
| 10149 | * 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] | 10150 | * headers are considered from the first one. It does not stop on commas and |
| 10151 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10152 | */ |
| 10153 | static int |
| 10154 | smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10155 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10156 | { |
| 10157 | struct http_txn *txn = l7; |
| 10158 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10159 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
| 10160 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 10161 | int occ = 0; |
| 10162 | const char *name_str = NULL; |
| 10163 | int name_len = 0; |
| 10164 | |
| 10165 | if (!ctx) { |
| 10166 | /* first call */ |
| 10167 | ctx = &static_hdr_ctx; |
| 10168 | ctx->idx = 0; |
| 10169 | smp->ctx.a[0] = ctx; |
| 10170 | } |
| 10171 | |
| 10172 | if (args) { |
| 10173 | if (args[0].type != ARGT_STR) |
| 10174 | return 0; |
| 10175 | name_str = args[0].data.str.str; |
| 10176 | name_len = args[0].data.str.len; |
| 10177 | |
| 10178 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10179 | occ = args[1].data.uint; |
| 10180 | } |
| 10181 | |
| 10182 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10183 | |
| 10184 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10185 | /* search for header from the beginning */ |
| 10186 | ctx->idx = 0; |
| 10187 | |
| 10188 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10189 | /* no explicit occurrence and single fetch => last header by default */ |
| 10190 | occ = -1; |
| 10191 | |
| 10192 | if (!occ) |
| 10193 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10194 | smp->flags |= SMP_F_NOT_LAST; |
| 10195 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10196 | smp->type = SMP_T_STR; |
| 10197 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10198 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
| 10199 | return 1; |
| 10200 | |
| 10201 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10202 | return 0; |
| 10203 | } |
| 10204 | |
| 10205 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10206 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10207 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10208 | */ |
| 10209 | static int |
| 10210 | smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10211 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10212 | { |
| 10213 | struct http_txn *txn = l7; |
| 10214 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10215 | struct hdr_ctx ctx; |
| 10216 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 10217 | int cnt; |
| 10218 | |
| 10219 | if (!args || args->type != ARGT_STR) |
| 10220 | return 0; |
| 10221 | |
| 10222 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10223 | |
| 10224 | ctx.idx = 0; |
| 10225 | cnt = 0; |
| 10226 | while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx)) |
| 10227 | cnt++; |
| 10228 | |
| 10229 | smp->type = SMP_T_UINT; |
| 10230 | smp->data.uint = cnt; |
| 10231 | smp->flags = SMP_F_VOL_HDR; |
| 10232 | return 1; |
| 10233 | } |
| 10234 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10235 | static int |
| 10236 | smp_fetch_hdr_names(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10237 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10238 | { |
| 10239 | struct http_txn *txn = l7; |
| 10240 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10241 | struct hdr_ctx ctx; |
| 10242 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 10243 | struct chunk *temp; |
| 10244 | char del = ','; |
| 10245 | |
| 10246 | if (args && args->type == ARGT_STR) |
| 10247 | del = *args[0].data.str.str; |
| 10248 | |
| 10249 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10250 | |
| 10251 | temp = get_trash_chunk(); |
| 10252 | |
| 10253 | ctx.idx = 0; |
| 10254 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 10255 | if (temp->len) |
| 10256 | temp->str[temp->len++] = del; |
| 10257 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 10258 | temp->len += ctx.del; |
| 10259 | } |
| 10260 | |
| 10261 | smp->type = SMP_T_STR; |
| 10262 | smp->data.str.str = temp->str; |
| 10263 | smp->data.str.len = temp->len; |
| 10264 | smp->flags = SMP_F_VOL_HDR; |
| 10265 | return 1; |
| 10266 | } |
| 10267 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10268 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10269 | * Accepts an optional argument of type string containing the header field name, |
| 10270 | * and an optional argument of type signed or unsigned integer to request an |
| 10271 | * 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] | 10272 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10273 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10274 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10275 | smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10276 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10277 | { |
| 10278 | struct http_txn *txn = l7; |
| 10279 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10280 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10281 | 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] | 10282 | int occ = 0; |
| 10283 | const char *name_str = NULL; |
| 10284 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10285 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10286 | if (!ctx) { |
| 10287 | /* first call */ |
| 10288 | ctx = &static_hdr_ctx; |
| 10289 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10290 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10291 | } |
| 10292 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10293 | if (args) { |
| 10294 | if (args[0].type != ARGT_STR) |
| 10295 | return 0; |
| 10296 | name_str = args[0].data.str.str; |
| 10297 | name_len = args[0].data.str.len; |
| 10298 | |
| 10299 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10300 | occ = args[1].data.uint; |
| 10301 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10302 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10303 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10304 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10305 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10306 | /* search for header from the beginning */ |
| 10307 | ctx->idx = 0; |
| 10308 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10309 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10310 | /* no explicit occurrence and single fetch => last header by default */ |
| 10311 | occ = -1; |
| 10312 | |
| 10313 | if (!occ) |
| 10314 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10315 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10316 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10317 | smp->type = SMP_T_STR; |
| 10318 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10319 | 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] | 10320 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10321 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10322 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10323 | return 0; |
| 10324 | } |
| 10325 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10326 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10327 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10328 | */ |
| 10329 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10330 | smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10331 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10332 | { |
| 10333 | struct http_txn *txn = l7; |
| 10334 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10335 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10336 | 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] | 10337 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10338 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10339 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10340 | return 0; |
| 10341 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10342 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10343 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10344 | ctx.idx = 0; |
| 10345 | cnt = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10346 | 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] | 10347 | cnt++; |
| 10348 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10349 | smp->type = SMP_T_UINT; |
| 10350 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10351 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10352 | return 1; |
| 10353 | } |
| 10354 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10355 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10356 | * takes a mandatory argument of type string and an optional one of type int |
| 10357 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10358 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10359 | */ |
| 10360 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10361 | smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10362 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10363 | { |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10364 | int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10365 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10366 | if (ret > 0) { |
| 10367 | smp->type = SMP_T_UINT; |
| 10368 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10369 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10370 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10371 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10372 | } |
| 10373 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10374 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10375 | * and an optional one of type int to designate a specific occurrence. |
| 10376 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10377 | */ |
| 10378 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10379 | smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10380 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10381 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10382 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10383 | |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10384 | while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw, private)) > 0) { |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10385 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 10386 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10387 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10388 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10389 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10390 | if (smp->data.str.len < temp->size - 1) { |
| 10391 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 10392 | temp->str[smp->data.str.len] = '\0'; |
| 10393 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 10394 | smp->type = SMP_T_IPV6; |
| 10395 | break; |
| 10396 | } |
| 10397 | } |
| 10398 | } |
| 10399 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10400 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10401 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10402 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10403 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10404 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10405 | } |
| 10406 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10407 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10408 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10409 | */ |
| 10410 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10411 | smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10412 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10413 | { |
| 10414 | struct http_txn *txn = l7; |
| 10415 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10416 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10417 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10418 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10419 | 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] | 10420 | ptr = http_get_path(txn); |
| 10421 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10422 | return 0; |
| 10423 | |
| 10424 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10425 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10426 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10427 | |
| 10428 | while (ptr < end && *ptr != '?') |
| 10429 | ptr++; |
| 10430 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10431 | smp->data.str.len = ptr - smp->data.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10432 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10433 | return 1; |
| 10434 | } |
| 10435 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10436 | /* This produces a concatenation of the first occurrence of the Host header |
| 10437 | * followed by the path component if it begins with a slash ('/'). This means |
| 10438 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10439 | * If no Host header is found, then the path is returned as-is. The returned |
| 10440 | * 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] | 10441 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10442 | */ |
| 10443 | static int |
| 10444 | smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10445 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10446 | { |
| 10447 | struct http_txn *txn = l7; |
| 10448 | char *ptr, *end, *beg; |
| 10449 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10450 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10451 | |
| 10452 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10453 | |
| 10454 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10455 | if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen) |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10456 | return smp_fetch_path(px, l4, l7, opt, args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10457 | |
| 10458 | /* 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] | 10459 | temp = get_trash_chunk(); |
| 10460 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10461 | smp->type = SMP_T_STR; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10462 | smp->data.str.str = temp->str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10463 | smp->data.str.len = ctx.vlen; |
| 10464 | |
| 10465 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10466 | 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] | 10467 | beg = http_get_path(txn); |
| 10468 | if (!beg) |
| 10469 | beg = end; |
| 10470 | |
| 10471 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10472 | |
| 10473 | if (beg < ptr && *beg == '/') { |
| 10474 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 10475 | smp->data.str.len += ptr - beg; |
| 10476 | } |
| 10477 | |
| 10478 | smp->flags = SMP_F_VOL_1ST; |
| 10479 | return 1; |
| 10480 | } |
| 10481 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10482 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10483 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10484 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10485 | * 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] | 10486 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10487 | * 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] | 10488 | * high-traffic sites without having to store whole paths. |
| 10489 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10490 | int |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10491 | smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10492 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10493 | { |
| 10494 | struct http_txn *txn = l7; |
| 10495 | struct hdr_ctx ctx; |
| 10496 | unsigned int hash = 0; |
| 10497 | char *ptr, *beg, *end; |
| 10498 | int len; |
| 10499 | |
| 10500 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10501 | |
| 10502 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10503 | 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] | 10504 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10505 | ptr = ctx.line + ctx.val; |
| 10506 | len = ctx.vlen; |
| 10507 | while (len--) |
| 10508 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10509 | } |
| 10510 | |
| 10511 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10512 | 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] | 10513 | beg = http_get_path(txn); |
| 10514 | if (!beg) |
| 10515 | beg = end; |
| 10516 | |
| 10517 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10518 | |
| 10519 | if (beg < ptr && *beg == '/') { |
| 10520 | while (beg < ptr) |
| 10521 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10522 | } |
| 10523 | hash = full_hash(hash); |
| 10524 | |
| 10525 | smp->type = SMP_T_UINT; |
| 10526 | smp->data.uint = hash; |
| 10527 | smp->flags = SMP_F_VOL_1ST; |
| 10528 | return 1; |
| 10529 | } |
| 10530 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10531 | /* 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] | 10532 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10533 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10534 | * 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] | 10535 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10536 | * 8 bytes would still work. |
| 10537 | */ |
| 10538 | static int |
| 10539 | smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10540 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10541 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10542 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10543 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
| 10544 | |
| 10545 | if (!cli_conn) |
| 10546 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10547 | |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10548 | if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10549 | return 0; |
| 10550 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10551 | temp = get_trash_chunk(); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10552 | *(unsigned int *)temp->str = htonl(smp->data.uint); |
| 10553 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10554 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10555 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10556 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10557 | 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] | 10558 | temp->len += 4; |
| 10559 | break; |
| 10560 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10561 | 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] | 10562 | temp->len += 16; |
| 10563 | break; |
| 10564 | default: |
| 10565 | return 0; |
| 10566 | } |
| 10567 | |
| 10568 | smp->data.str = *temp; |
| 10569 | smp->type = SMP_T_BIN; |
| 10570 | return 1; |
| 10571 | } |
| 10572 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10573 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10574 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10575 | * of type string carrying the whole query string. |
| 10576 | */ |
| 10577 | static int |
| 10578 | smp_fetch_query(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10579 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10580 | { |
| 10581 | struct http_txn *txn = l7; |
| 10582 | char *ptr, *end; |
| 10583 | |
| 10584 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10585 | |
| 10586 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 10587 | end = ptr + txn->req.sl.rq.u_l; |
| 10588 | |
| 10589 | /* look up the '?' */ |
| 10590 | do { |
| 10591 | if (ptr == end) |
| 10592 | return 0; |
| 10593 | } while (*ptr++ != '?'); |
| 10594 | |
| 10595 | smp->type = SMP_T_STR; |
| 10596 | smp->data.str.str = ptr; |
| 10597 | smp->data.str.len = end - ptr; |
| 10598 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10599 | return 1; |
| 10600 | } |
| 10601 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10602 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10603 | smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10604 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10605 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10606 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10607 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10608 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10609 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10610 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10611 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10612 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 10613 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10614 | return 1; |
| 10615 | } |
| 10616 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10617 | /* return a valid test if the current request is the first one on the connection */ |
| 10618 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10619 | smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10620 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10621 | { |
| 10622 | if (!s) |
| 10623 | return 0; |
| 10624 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10625 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 10626 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10627 | return 1; |
| 10628 | } |
| 10629 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10630 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10631 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10632 | smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10633 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10634 | { |
| 10635 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10636 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10637 | return 0; |
| 10638 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10639 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10640 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10641 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10642 | return 0; |
| 10643 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10644 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10645 | 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] | 10646 | return 1; |
| 10647 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10648 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10649 | /* Accepts exactly 1 argument of type userlist */ |
| 10650 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10651 | smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10652 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10653 | { |
| 10654 | |
| 10655 | if (!args || args->type != ARGT_USR) |
| 10656 | return 0; |
| 10657 | |
| 10658 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10659 | |
| 10660 | if (!get_http_auth(l4)) |
| 10661 | return 0; |
| 10662 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10663 | /* if the user does not belong to the userlist or has a wrong password, |
| 10664 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10665 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10666 | */ |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10667 | if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass)) |
| 10668 | return 0; |
| 10669 | |
| 10670 | /* pat_match_auth() will need the user list */ |
| 10671 | smp->ctx.a[0] = args->data.usr; |
| 10672 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10673 | smp->type = SMP_T_STR; |
| 10674 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10675 | smp->data.str.str = l4->txn.auth.user; |
| 10676 | smp->data.str.len = strlen(l4->txn.auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10677 | |
| 10678 | return 1; |
| 10679 | } |
| 10680 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10681 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10682 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10683 | * the cookie value is returned into *value and *value_l, and the function |
| 10684 | * returns a pointer to the next pointer to search from if the value was found. |
| 10685 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10686 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10687 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10688 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10689 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10690 | * list is expected. |
| 10691 | */ |
| 10692 | static char * |
| 10693 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10694 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10695 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10696 | { |
| 10697 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10698 | char *next; |
| 10699 | |
| 10700 | /* we search at least a cookie name followed by an equal, and more |
| 10701 | * generally something like this : |
| 10702 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10703 | */ |
| 10704 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10705 | /* Iterate through all cookies on this line */ |
| 10706 | |
| 10707 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 10708 | att_beg++; |
| 10709 | |
| 10710 | /* find att_end : this is the first character after the last non |
| 10711 | * space before the equal. It may be equal to hdr_end. |
| 10712 | */ |
| 10713 | equal = att_end = att_beg; |
| 10714 | |
| 10715 | while (equal < hdr_end) { |
| 10716 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10717 | break; |
| 10718 | if (http_is_spht[(unsigned char)*equal++]) |
| 10719 | continue; |
| 10720 | att_end = equal; |
| 10721 | } |
| 10722 | |
| 10723 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10724 | * is between <att_beg> and <equal>, both may be identical. |
| 10725 | */ |
| 10726 | |
| 10727 | /* look for end of cookie if there is an equal sign */ |
| 10728 | if (equal < hdr_end && *equal == '=') { |
| 10729 | /* look for the beginning of the value */ |
| 10730 | val_beg = equal + 1; |
| 10731 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 10732 | val_beg++; |
| 10733 | |
| 10734 | /* find the end of the value, respecting quotes */ |
| 10735 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10736 | |
| 10737 | /* make val_end point to the first white space or delimitor after the value */ |
| 10738 | val_end = next; |
| 10739 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 10740 | val_end--; |
| 10741 | } else { |
| 10742 | val_beg = val_end = next = equal; |
| 10743 | } |
| 10744 | |
| 10745 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10746 | * they will automatically be removed if a header before them is removed, |
| 10747 | * since they're supposed to be linked together. |
| 10748 | */ |
| 10749 | if (*att_beg == '$') |
| 10750 | continue; |
| 10751 | |
| 10752 | /* Ignore cookies with no equal sign */ |
| 10753 | if (equal == next) |
| 10754 | continue; |
| 10755 | |
| 10756 | /* Now we have the cookie name between att_beg and att_end, and |
| 10757 | * its value between val_beg and val_end. |
| 10758 | */ |
| 10759 | |
| 10760 | if (att_end - att_beg == cookie_name_l && |
| 10761 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10762 | /* let's return this value and indicate where to go on from */ |
| 10763 | *value = val_beg; |
| 10764 | *value_l = val_end - val_beg; |
| 10765 | return next + 1; |
| 10766 | } |
| 10767 | |
| 10768 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10769 | if (!list) |
| 10770 | break; |
| 10771 | } |
| 10772 | |
| 10773 | return NULL; |
| 10774 | } |
| 10775 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10776 | /* Fetch a captured HTTP request header. The index is the position of |
| 10777 | * the "capture" option in the configuration file |
| 10778 | */ |
| 10779 | static int |
| 10780 | smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10781 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10782 | { |
| 10783 | struct proxy *fe = l4->fe; |
| 10784 | struct http_txn *txn = l7; |
| 10785 | int idx; |
| 10786 | |
| 10787 | if (!args || args->type != ARGT_UINT) |
| 10788 | return 0; |
| 10789 | |
| 10790 | idx = args->data.uint; |
| 10791 | |
| 10792 | if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL) |
| 10793 | return 0; |
| 10794 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10795 | smp->type = SMP_T_STR; |
| 10796 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10797 | smp->data.str.str = txn->req.cap[idx]; |
| 10798 | smp->data.str.len = strlen(txn->req.cap[idx]); |
| 10799 | |
| 10800 | return 1; |
| 10801 | } |
| 10802 | |
| 10803 | /* Fetch a captured HTTP response header. The index is the position of |
| 10804 | * the "capture" option in the configuration file |
| 10805 | */ |
| 10806 | static int |
| 10807 | smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10808 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10809 | { |
| 10810 | struct proxy *fe = l4->fe; |
| 10811 | struct http_txn *txn = l7; |
| 10812 | int idx; |
| 10813 | |
| 10814 | if (!args || args->type != ARGT_UINT) |
| 10815 | return 0; |
| 10816 | |
| 10817 | idx = args->data.uint; |
| 10818 | |
| 10819 | if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL) |
| 10820 | return 0; |
| 10821 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10822 | smp->type = SMP_T_STR; |
| 10823 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10824 | smp->data.str.str = txn->rsp.cap[idx]; |
| 10825 | smp->data.str.len = strlen(txn->rsp.cap[idx]); |
| 10826 | |
| 10827 | return 1; |
| 10828 | } |
| 10829 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10830 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 10831 | static int |
| 10832 | smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10833 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10834 | { |
| 10835 | struct chunk *temp; |
| 10836 | struct http_txn *txn = l7; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10837 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10838 | |
| 10839 | if (!txn->uri) |
| 10840 | return 0; |
| 10841 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10842 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10843 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10844 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10845 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10846 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10847 | temp = get_trash_chunk(); |
| 10848 | temp->str = txn->uri; |
| 10849 | temp->len = ptr - txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10850 | smp->data.str = *temp; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10851 | smp->type = SMP_T_STR; |
| 10852 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10853 | |
| 10854 | return 1; |
| 10855 | |
| 10856 | } |
| 10857 | |
| 10858 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 10859 | static int |
| 10860 | smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10861 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10862 | { |
| 10863 | struct chunk *temp; |
| 10864 | struct http_txn *txn = l7; |
| 10865 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10866 | |
| 10867 | if (!txn->uri) |
| 10868 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10869 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10870 | ptr = txn->uri; |
| 10871 | |
| 10872 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10873 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10874 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10875 | if (!*ptr) |
| 10876 | return 0; |
| 10877 | |
| 10878 | ptr++; /* skip the space */ |
| 10879 | |
| 10880 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10881 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10882 | if (!ptr) |
| 10883 | return 0; |
| 10884 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 10885 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10886 | |
| 10887 | smp->data.str = *temp; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10888 | smp->data.str.len = ptr - temp->str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10889 | smp->type = SMP_T_STR; |
| 10890 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10891 | |
| 10892 | return 1; |
| 10893 | } |
| 10894 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10895 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 10896 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10897 | */ |
| 10898 | static int |
| 10899 | smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10900 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10901 | { |
| 10902 | struct http_txn *txn = l7; |
| 10903 | |
| 10904 | if (txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
| 10905 | return 0; |
| 10906 | |
| 10907 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 10908 | smp->data.str.str = "HTTP/1.1"; |
| 10909 | else |
| 10910 | smp->data.str.str = "HTTP/1.0"; |
| 10911 | |
| 10912 | smp->data.str.len = 8; |
| 10913 | smp->type = SMP_T_STR; |
| 10914 | smp->flags = SMP_F_CONST; |
| 10915 | return 1; |
| 10916 | |
| 10917 | } |
| 10918 | |
| 10919 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 10920 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10921 | */ |
| 10922 | static int |
| 10923 | smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10924 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10925 | { |
| 10926 | struct http_txn *txn = l7; |
| 10927 | |
| 10928 | if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
| 10929 | return 0; |
| 10930 | |
| 10931 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
| 10932 | smp->data.str.str = "HTTP/1.1"; |
| 10933 | else |
| 10934 | smp->data.str.str = "HTTP/1.0"; |
| 10935 | |
| 10936 | smp->data.str.len = 8; |
| 10937 | smp->type = SMP_T_STR; |
| 10938 | smp->flags = SMP_F_CONST; |
| 10939 | return 1; |
| 10940 | |
| 10941 | } |
| 10942 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10943 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10944 | /* 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] | 10945 | * 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] | 10946 | * 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] | 10947 | * 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] | 10948 | * 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] | 10949 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 10950 | * 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] | 10951 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 10952 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10953 | */ |
William Lallemand | 07c8b24 | 2014-05-02 17:11:07 +0200 | [diff] [blame] | 10954 | int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10955 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10956 | { |
| 10957 | struct http_txn *txn = l7; |
| 10958 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10959 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10960 | const struct http_msg *msg; |
| 10961 | const char *hdr_name; |
| 10962 | int hdr_name_len; |
| 10963 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10964 | int occ = 0; |
| 10965 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10966 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10967 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10968 | return 0; |
| 10969 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10970 | if (!ctx) { |
| 10971 | /* first call */ |
| 10972 | ctx = &static_hdr_ctx; |
| 10973 | ctx->idx = 0; |
| 10974 | smp->ctx.a[2] = ctx; |
| 10975 | } |
| 10976 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10977 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10978 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10979 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10980 | msg = &txn->req; |
| 10981 | hdr_name = "Cookie"; |
| 10982 | hdr_name_len = 6; |
| 10983 | } else { |
| 10984 | msg = &txn->rsp; |
| 10985 | hdr_name = "Set-Cookie"; |
| 10986 | hdr_name_len = 10; |
| 10987 | } |
| 10988 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10989 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10990 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 10991 | occ = -1; |
| 10992 | |
| 10993 | /* OK so basically here, either we want only one value and it's the |
| 10994 | * last one, or we want to iterate over all of them and we fetch the |
| 10995 | * next one. |
| 10996 | */ |
| 10997 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10998 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10999 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11000 | /* search for the header from the beginning, we must first initialize |
| 11001 | * the search parameters. |
| 11002 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11003 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11004 | ctx->idx = 0; |
| 11005 | } |
| 11006 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11007 | smp->flags |= SMP_F_VOL_HDR; |
| 11008 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11009 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11010 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 11011 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11012 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 11013 | goto out; |
| 11014 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11015 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11016 | continue; |
| 11017 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11018 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 11019 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11020 | } |
| 11021 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11022 | smp->type = SMP_T_STR; |
| 11023 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11024 | 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] | 11025 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 11026 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11027 | &smp->data.str.str, |
| 11028 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11029 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11030 | found = 1; |
| 11031 | if (occ >= 0) { |
| 11032 | /* one value was returned into smp->data.str.{str,len} */ |
| 11033 | smp->flags |= SMP_F_NOT_LAST; |
| 11034 | return 1; |
| 11035 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11036 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11037 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11038 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11039 | /* all cookie headers and values were scanned. If we're looking for the |
| 11040 | * last occurrence, we may return it now. |
| 11041 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11042 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11043 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11044 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11045 | } |
| 11046 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11047 | /* 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] | 11048 | * 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] | 11049 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 11050 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11051 | */ |
| 11052 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11053 | smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 11054 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11055 | { |
| 11056 | struct http_txn *txn = l7; |
| 11057 | struct hdr_idx *idx = &txn->hdr_idx; |
| 11058 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11059 | const struct http_msg *msg; |
| 11060 | const char *hdr_name; |
| 11061 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11062 | int cnt; |
| 11063 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11064 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11065 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11066 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11067 | return 0; |
| 11068 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11069 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11070 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 11071 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11072 | msg = &txn->req; |
| 11073 | hdr_name = "Cookie"; |
| 11074 | hdr_name_len = 6; |
| 11075 | } else { |
| 11076 | msg = &txn->rsp; |
| 11077 | hdr_name = "Set-Cookie"; |
| 11078 | hdr_name_len = 10; |
| 11079 | } |
| 11080 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11081 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 11082 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11083 | ctx.idx = 0; |
| 11084 | cnt = 0; |
| 11085 | |
| 11086 | while (1) { |
| 11087 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 11088 | if (!val_beg) { |
| 11089 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 11090 | break; |
| 11091 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11092 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11093 | continue; |
| 11094 | |
| 11095 | val_beg = ctx.line + ctx.val; |
| 11096 | val_end = val_beg + ctx.vlen; |
| 11097 | } |
| 11098 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11099 | smp->type = SMP_T_STR; |
| 11100 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11101 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11102 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 11103 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11104 | &smp->data.str.str, |
| 11105 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11106 | cnt++; |
| 11107 | } |
| 11108 | } |
| 11109 | |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 11110 | smp->type = SMP_T_UINT; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11111 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11112 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11113 | return 1; |
| 11114 | } |
| 11115 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11116 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 11117 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 11118 | */ |
| 11119 | static int |
| 11120 | smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 11121 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11122 | { |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 11123 | int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11124 | |
| 11125 | if (ret > 0) { |
| 11126 | smp->type = SMP_T_UINT; |
| 11127 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 11128 | } |
| 11129 | |
| 11130 | return ret; |
| 11131 | } |
| 11132 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11133 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11134 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11135 | /************************************************************************/ |
| 11136 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11137 | /* |
| 11138 | * Given a path string and its length, find the position of beginning of the |
| 11139 | * query string. Returns NULL if no query string is found in the path. |
| 11140 | * |
| 11141 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11142 | * |
| 11143 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 11144 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11145 | 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] | 11146 | { |
| 11147 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11148 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11149 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11150 | return p ? p + 1 : NULL; |
| 11151 | } |
| 11152 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11153 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11154 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11155 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11156 | } |
| 11157 | |
| 11158 | /* |
| 11159 | * Given a url parameter, find the starting position of the first occurence, |
| 11160 | * or NULL if the parameter is not found. |
| 11161 | * |
| 11162 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11163 | * the function will return query_string+8. |
| 11164 | */ |
| 11165 | static char* |
| 11166 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11167 | char* url_param_name, size_t url_param_name_l, |
| 11168 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11169 | { |
| 11170 | char *pos, *last; |
| 11171 | |
| 11172 | pos = query_string; |
| 11173 | last = query_string + query_string_l - url_param_name_l - 1; |
| 11174 | |
| 11175 | while (pos <= last) { |
| 11176 | if (pos[url_param_name_l] == '=') { |
| 11177 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 11178 | return pos; |
| 11179 | pos += url_param_name_l + 1; |
| 11180 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11181 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11182 | pos++; |
| 11183 | pos++; |
| 11184 | } |
| 11185 | return NULL; |
| 11186 | } |
| 11187 | |
| 11188 | /* |
| 11189 | * Given a url parameter name, returns its value and size into *value and |
| 11190 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 11191 | * zero is returned and value/value_l are not touched. |
| 11192 | */ |
| 11193 | static int |
| 11194 | find_url_param_value(char* path, size_t path_l, |
| 11195 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11196 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11197 | { |
| 11198 | char *query_string, *qs_end; |
| 11199 | char *arg_start; |
| 11200 | char *value_start, *value_end; |
| 11201 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11202 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11203 | if (!query_string) |
| 11204 | return 0; |
| 11205 | |
| 11206 | qs_end = path + path_l; |
| 11207 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11208 | url_param_name, url_param_name_l, |
| 11209 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11210 | if (!arg_start) |
| 11211 | return 0; |
| 11212 | |
| 11213 | value_start = arg_start + url_param_name_l + 1; |
| 11214 | value_end = value_start; |
| 11215 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11216 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11217 | value_end++; |
| 11218 | |
| 11219 | *value = value_start; |
| 11220 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 11221 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11222 | } |
| 11223 | |
| 11224 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11225 | smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 11226 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11227 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11228 | char delim = '?'; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11229 | struct http_txn *txn = l7; |
| 11230 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11231 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11232 | if (!args || args[0].type != ARGT_STR || |
| 11233 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11234 | return 0; |
| 11235 | |
| 11236 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11237 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11238 | if (args[1].type) |
| 11239 | delim = *args[1].data.str.str; |
| 11240 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11241 | 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] | 11242 | args->data.str.str, args->data.str.len, |
| 11243 | &smp->data.str.str, &smp->data.str.len, |
| 11244 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11245 | return 0; |
| 11246 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11247 | smp->type = SMP_T_STR; |
| 11248 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11249 | return 1; |
| 11250 | } |
| 11251 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11252 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11253 | * above). |
| 11254 | */ |
| 11255 | static int |
| 11256 | smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 11257 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11258 | { |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 11259 | int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11260 | |
| 11261 | if (ret > 0) { |
| 11262 | smp->type = SMP_T_UINT; |
| 11263 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 11264 | } |
| 11265 | |
| 11266 | return ret; |
| 11267 | } |
| 11268 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11269 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11270 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11271 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11272 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11273 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11274 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11275 | * high-traffic sites without having to store whole paths. |
| 11276 | * this differs from the base32 functions in that it includes the url parameters |
| 11277 | * as well as the path |
| 11278 | */ |
| 11279 | static int |
| 11280 | smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 11281 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11282 | { |
| 11283 | struct http_txn *txn = l7; |
| 11284 | struct hdr_ctx ctx; |
| 11285 | unsigned int hash = 0; |
| 11286 | char *ptr, *beg, *end; |
| 11287 | int len; |
| 11288 | |
| 11289 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11290 | |
| 11291 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11292 | 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] | 11293 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11294 | ptr = ctx.line + ctx.val; |
| 11295 | len = ctx.vlen; |
| 11296 | while (len--) |
| 11297 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11298 | } |
| 11299 | |
| 11300 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11301 | 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] | 11302 | beg = http_get_path(txn); |
| 11303 | if (!beg) |
| 11304 | beg = end; |
| 11305 | |
| 11306 | for (ptr = beg; ptr < end ; ptr++); |
| 11307 | |
| 11308 | if (beg < ptr && *beg == '/') { |
| 11309 | while (beg < ptr) |
| 11310 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11311 | } |
| 11312 | hash = full_hash(hash); |
| 11313 | |
| 11314 | smp->type = SMP_T_UINT; |
| 11315 | smp->data.uint = hash; |
| 11316 | smp->flags = SMP_F_VOL_1ST; |
| 11317 | return 1; |
| 11318 | } |
| 11319 | |
| 11320 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11321 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11322 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11323 | * on the source address length. The URL hash is stored before the address so |
| 11324 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11325 | * 8 bytes would still work. |
| 11326 | */ |
| 11327 | static int |
| 11328 | smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 11329 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11330 | { |
| 11331 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11332 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11333 | |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 11334 | if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11335 | return 0; |
| 11336 | |
| 11337 | temp = get_trash_chunk(); |
| 11338 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 11339 | temp->len += sizeof(smp->data.uint); |
| 11340 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11341 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11342 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11343 | 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] | 11344 | temp->len += 4; |
| 11345 | break; |
| 11346 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11347 | 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] | 11348 | temp->len += 16; |
| 11349 | break; |
| 11350 | default: |
| 11351 | return 0; |
| 11352 | } |
| 11353 | |
| 11354 | smp->data.str = *temp; |
| 11355 | smp->type = SMP_T_BIN; |
| 11356 | return 1; |
| 11357 | } |
| 11358 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11359 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11360 | * keyword. These keywords support an optional positive or negative occurrence |
| 11361 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11362 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11363 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11364 | * error message in case of error, that the caller is responsible for freeing. |
| 11365 | * The initial location must either be freeable or NULL. |
| 11366 | */ |
| 11367 | static int val_hdr(struct arg *arg, char **err_msg) |
| 11368 | { |
| 11369 | 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] | 11370 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11371 | return 0; |
| 11372 | } |
| 11373 | return 1; |
| 11374 | } |
| 11375 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11376 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11377 | * adds an optional offset found in args[0] and emits a string representing |
| 11378 | * the date in RFC-1123/5322 format. |
| 11379 | */ |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 11380 | static int sample_conv_http_date(struct session *session, const struct arg *args, |
| 11381 | struct sample *smp, void *private) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11382 | { |
| 11383 | const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; |
| 11384 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11385 | struct chunk *temp; |
| 11386 | struct tm *tm; |
| 11387 | time_t curr_date = smp->data.uint; |
| 11388 | |
| 11389 | /* add offset */ |
| 11390 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 11391 | curr_date += args[0].data.sint; |
| 11392 | |
| 11393 | tm = gmtime(&curr_date); |
| 11394 | |
| 11395 | temp = get_trash_chunk(); |
| 11396 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11397 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11398 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11399 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11400 | |
| 11401 | smp->data.str = *temp; |
| 11402 | smp->type = SMP_T_STR; |
| 11403 | return 1; |
| 11404 | } |
| 11405 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11406 | /* Match language range with language tag. RFC2616 14.4: |
| 11407 | * |
| 11408 | * A language-range matches a language-tag if it exactly equals |
| 11409 | * the tag, or if it exactly equals a prefix of the tag such |
| 11410 | * that the first tag character following the prefix is "-". |
| 11411 | * |
| 11412 | * Return 1 if the strings match, else return 0. |
| 11413 | */ |
| 11414 | static inline int language_range_match(const char *range, int range_len, |
| 11415 | const char *tag, int tag_len) |
| 11416 | { |
| 11417 | const char *end = range + range_len; |
| 11418 | const char *tend = tag + tag_len; |
| 11419 | while (range < end) { |
| 11420 | if (*range == '-' && tag == tend) |
| 11421 | return 1; |
| 11422 | if (*range != *tag || tag == tend) |
| 11423 | return 0; |
| 11424 | range++; |
| 11425 | tag++; |
| 11426 | } |
| 11427 | /* Return true only if the last char of the tag is matched. */ |
| 11428 | return tag == tend; |
| 11429 | } |
| 11430 | |
| 11431 | /* Arguments: The list of expected value, the number of parts returned and the separator */ |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 11432 | static int sample_conv_q_prefered(struct session *session, const struct arg *args, |
| 11433 | struct sample *smp, void *private) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11434 | { |
| 11435 | const char *al = smp->data.str.str; |
| 11436 | const char *end = al + smp->data.str.len; |
| 11437 | const char *token; |
| 11438 | int toklen; |
| 11439 | int qvalue; |
| 11440 | const char *str; |
| 11441 | const char *w; |
| 11442 | int best_q = 0; |
| 11443 | |
| 11444 | /* Set the constant to the sample, because the output of the |
| 11445 | * function will be peek in the constant configuration string. |
| 11446 | */ |
| 11447 | smp->flags |= SMP_F_CONST; |
| 11448 | smp->data.str.size = 0; |
| 11449 | smp->data.str.str = ""; |
| 11450 | smp->data.str.len = 0; |
| 11451 | |
| 11452 | /* Parse the accept language */ |
| 11453 | while (1) { |
| 11454 | |
| 11455 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11456 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11457 | al++; |
| 11458 | if (al >= end) |
| 11459 | break; |
| 11460 | |
| 11461 | /* Start of the fisrt word. */ |
| 11462 | token = al; |
| 11463 | |
| 11464 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11465 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11466 | al++; |
| 11467 | if (al == token) |
| 11468 | goto expect_comma; |
| 11469 | |
| 11470 | /* Length of the token. */ |
| 11471 | toklen = al - token; |
| 11472 | qvalue = 1000; |
| 11473 | |
| 11474 | /* Check if the token exists in the list. If the token not exists, |
| 11475 | * jump to the next token. |
| 11476 | */ |
| 11477 | str = args[0].data.str.str; |
| 11478 | w = str; |
| 11479 | while (1) { |
| 11480 | if (*str == ';' || *str == '\0') { |
| 11481 | if (language_range_match(token, toklen, w, str-w)) |
| 11482 | goto look_for_q; |
| 11483 | if (*str == '\0') |
| 11484 | goto expect_comma; |
| 11485 | w = str + 1; |
| 11486 | } |
| 11487 | str++; |
| 11488 | } |
| 11489 | goto expect_comma; |
| 11490 | |
| 11491 | look_for_q: |
| 11492 | |
| 11493 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11494 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11495 | al++; |
| 11496 | if (al >= end) |
| 11497 | goto process_value; |
| 11498 | |
| 11499 | /* If ',' is found, process the result */ |
| 11500 | if (*al == ',') |
| 11501 | goto process_value; |
| 11502 | |
| 11503 | /* If the character is different from ';', look |
| 11504 | * for the end of the header part in best effort. |
| 11505 | */ |
| 11506 | if (*al != ';') |
| 11507 | goto expect_comma; |
| 11508 | |
| 11509 | /* Assumes that the char is ';', now expect "q=". */ |
| 11510 | al++; |
| 11511 | |
| 11512 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11513 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11514 | al++; |
| 11515 | if (al >= end) |
| 11516 | goto process_value; |
| 11517 | |
| 11518 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11519 | if (*al != 'q') |
| 11520 | goto process_value; |
| 11521 | al++; |
| 11522 | |
| 11523 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11524 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11525 | al++; |
| 11526 | if (al >= end) |
| 11527 | goto process_value; |
| 11528 | |
| 11529 | /* Expect '='. If no '=', continue in best effort */ |
| 11530 | if (*al != '=') |
| 11531 | goto process_value; |
| 11532 | al++; |
| 11533 | |
| 11534 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11535 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11536 | al++; |
| 11537 | if (al >= end) |
| 11538 | goto process_value; |
| 11539 | |
| 11540 | /* Parse the q value. */ |
| 11541 | qvalue = parse_qvalue(al, &al); |
| 11542 | |
| 11543 | process_value: |
| 11544 | |
| 11545 | /* If the new q value is the best q value, then store the associated |
| 11546 | * language in the response. If qvalue is the biggest value (1000), |
| 11547 | * break the process. |
| 11548 | */ |
| 11549 | if (qvalue > best_q) { |
| 11550 | smp->data.str.str = (char *)w; |
| 11551 | smp->data.str.len = str - w; |
| 11552 | if (qvalue >= 1000) |
| 11553 | break; |
| 11554 | best_q = qvalue; |
| 11555 | } |
| 11556 | |
| 11557 | expect_comma: |
| 11558 | |
| 11559 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11560 | while (al < end && *al != ',') |
| 11561 | al++; |
| 11562 | if (al >= end) |
| 11563 | break; |
| 11564 | |
| 11565 | /* Comma is found, jump it and restart the analyzer. */ |
| 11566 | al++; |
| 11567 | } |
| 11568 | |
| 11569 | /* Set default value if required. */ |
| 11570 | if (smp->data.str.len == 0 && args[1].type == ARGT_STR) { |
| 11571 | smp->data.str.str = args[1].data.str.str; |
| 11572 | smp->data.str.len = args[1].data.str.len; |
| 11573 | } |
| 11574 | |
| 11575 | /* Return true only if a matching language was found. */ |
| 11576 | return smp->data.str.len != 0; |
| 11577 | } |
| 11578 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11579 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 11580 | * builds a string in the trash from the specified format string, then modifies |
| 11581 | * the relevant part of the request line accordingly. Then it updates various |
| 11582 | * pointers to the next elements which were moved, and the total buffer length. |
| 11583 | * It finds the action to be performed in p[2], previously filled by function |
| 11584 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 11585 | * error, though this can be revisited when this code is finally exploited. |
| 11586 | */ |
| 11587 | int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct session *s, struct http_txn *txn) |
| 11588 | { |
| 11589 | char *cur_ptr, *cur_end; |
| 11590 | int offset; |
| 11591 | int delta; |
| 11592 | |
| 11593 | chunk_reset(&trash); |
| 11594 | |
| 11595 | /* prepare a '?' just in case we have to create a query string */ |
| 11596 | trash.str[trash.len++] = '?'; |
| 11597 | offset = 1; |
| 11598 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]); |
| 11599 | |
| 11600 | switch (*(int *)&rule->arg.act.p[2]) { |
| 11601 | case 0: // method |
| 11602 | cur_ptr = s->req->buf->p; |
| 11603 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 11604 | |
| 11605 | /* adjust req line offsets and lengths */ |
| 11606 | delta = trash.len - offset - (cur_end - cur_ptr); |
| 11607 | txn->req.sl.rq.m_l += delta; |
| 11608 | txn->req.sl.rq.u += delta; |
| 11609 | txn->req.sl.rq.v += delta; |
| 11610 | break; |
| 11611 | |
| 11612 | case 1: // path |
| 11613 | cur_ptr = http_get_path(txn); |
| 11614 | if (!cur_ptr) |
| 11615 | cur_ptr = s->req->buf->p + txn->req.sl.rq.u; |
| 11616 | |
| 11617 | cur_end = cur_ptr; |
| 11618 | while (cur_end < s->req->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?') |
| 11619 | cur_end++; |
| 11620 | |
| 11621 | /* adjust req line offsets and lengths */ |
| 11622 | delta = trash.len - offset - (cur_end - cur_ptr); |
| 11623 | txn->req.sl.rq.u_l += delta; |
| 11624 | txn->req.sl.rq.v += delta; |
| 11625 | break; |
| 11626 | |
| 11627 | case 2: // query |
| 11628 | cur_ptr = s->req->buf->p + txn->req.sl.rq.u; |
| 11629 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11630 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 11631 | cur_ptr++; |
| 11632 | |
| 11633 | /* skip the question mark or indicate that we must insert it |
| 11634 | * (but only if the format string is not empty then). |
| 11635 | */ |
| 11636 | if (cur_ptr < cur_end) |
| 11637 | cur_ptr++; |
| 11638 | else if (trash.len > 1) |
| 11639 | offset = 0; |
| 11640 | |
| 11641 | /* adjust req line offsets and lengths */ |
| 11642 | delta = trash.len - offset - (cur_end - cur_ptr); |
| 11643 | txn->req.sl.rq.u_l += delta; |
| 11644 | txn->req.sl.rq.v += delta; |
| 11645 | break; |
| 11646 | |
| 11647 | case 3: // uri |
| 11648 | cur_ptr = s->req->buf->p + txn->req.sl.rq.u; |
| 11649 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11650 | |
| 11651 | /* adjust req line offsets and lengths */ |
| 11652 | delta = trash.len - offset - (cur_end - cur_ptr); |
| 11653 | txn->req.sl.rq.u_l += delta; |
| 11654 | txn->req.sl.rq.v += delta; |
| 11655 | break; |
| 11656 | |
| 11657 | default: |
| 11658 | return -1; |
| 11659 | } |
| 11660 | |
| 11661 | /* commit changes and adjust end of message */ |
| 11662 | delta = buffer_replace2(s->req->buf, cur_ptr, cur_end, trash.str + offset, trash.len - offset); |
| 11663 | http_msg_move_end(&txn->req, delta); |
| 11664 | return 0; |
| 11665 | } |
| 11666 | |
| 11667 | /* parse an http-request action among : |
| 11668 | * set-method |
| 11669 | * set-path |
| 11670 | * set-query |
| 11671 | * set-uri |
| 11672 | * |
| 11673 | * All of them accept a single argument of type string representing a log-format. |
| 11674 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 11675 | * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri). |
| 11676 | * It returns 0 on success, < 0 on error. |
| 11677 | */ |
| 11678 | int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err) |
| 11679 | { |
| 11680 | int cur_arg = *orig_arg; |
| 11681 | |
| 11682 | rule->action = HTTP_REQ_ACT_CUSTOM_CONT; |
| 11683 | |
| 11684 | switch (args[0][4]) { |
| 11685 | case 'm' : |
| 11686 | *(int *)&rule->arg.act.p[2] = 0; |
| 11687 | rule->action_ptr = http_action_set_req_line; |
| 11688 | break; |
| 11689 | case 'p' : |
| 11690 | *(int *)&rule->arg.act.p[2] = 1; |
| 11691 | rule->action_ptr = http_action_set_req_line; |
| 11692 | break; |
| 11693 | case 'q' : |
| 11694 | *(int *)&rule->arg.act.p[2] = 2; |
| 11695 | rule->action_ptr = http_action_set_req_line; |
| 11696 | break; |
| 11697 | case 'u' : |
| 11698 | *(int *)&rule->arg.act.p[2] = 3; |
| 11699 | rule->action_ptr = http_action_set_req_line; |
| 11700 | break; |
| 11701 | default: |
| 11702 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
| 11703 | return -1; |
| 11704 | } |
| 11705 | |
| 11706 | if (!*args[cur_arg] || |
| 11707 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 11708 | memprintf(err, "expects exactly 1 argument <format>"); |
| 11709 | return -1; |
| 11710 | } |
| 11711 | |
| 11712 | LIST_INIT((struct list *)&rule->arg.act.p[0]); |
| 11713 | proxy->conf.args.ctx = ARGC_HRQ; |
| 11714 | parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP, |
| 11715 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 11716 | proxy->conf.args.file, proxy->conf.args.line); |
| 11717 | |
| 11718 | (*orig_arg)++; |
| 11719 | return 0; |
| 11720 | } |
| 11721 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 11722 | /* |
| 11723 | * Return the struct http_req_action_kw associated to a keyword. |
| 11724 | */ |
| 11725 | struct http_req_action_kw *action_http_req_custom(const char *kw) |
| 11726 | { |
| 11727 | if (!LIST_ISEMPTY(&http_req_keywords.list)) { |
| 11728 | struct http_req_action_kw_list *kw_list; |
| 11729 | int i; |
| 11730 | |
| 11731 | list_for_each_entry(kw_list, &http_req_keywords.list, list) { |
| 11732 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11733 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11734 | return &kw_list->kw[i]; |
| 11735 | } |
| 11736 | } |
| 11737 | } |
| 11738 | return NULL; |
| 11739 | } |
| 11740 | |
| 11741 | /* |
| 11742 | * Return the struct http_res_action_kw associated to a keyword. |
| 11743 | */ |
| 11744 | struct http_res_action_kw *action_http_res_custom(const char *kw) |
| 11745 | { |
| 11746 | if (!LIST_ISEMPTY(&http_res_keywords.list)) { |
| 11747 | struct http_res_action_kw_list *kw_list; |
| 11748 | int i; |
| 11749 | |
| 11750 | list_for_each_entry(kw_list, &http_res_keywords.list, list) { |
| 11751 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11752 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11753 | return &kw_list->kw[i]; |
| 11754 | } |
| 11755 | } |
| 11756 | } |
| 11757 | return NULL; |
| 11758 | } |
| 11759 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11760 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11761 | /* All supported ACL keywords must be declared here. */ |
| 11762 | /************************************************************************/ |
| 11763 | |
| 11764 | /* Note: must not be declared <const> as its list will be overwritten. |
| 11765 | * Please take care of keeping this list alphabetically sorted. |
| 11766 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11767 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11768 | { "base", "base", PAT_MATCH_STR }, |
| 11769 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 11770 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 11771 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 11772 | { "base_end", "base", PAT_MATCH_END }, |
| 11773 | { "base_len", "base", PAT_MATCH_LEN }, |
| 11774 | { "base_reg", "base", PAT_MATCH_REG }, |
| 11775 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11776 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11777 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 11778 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 11779 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 11780 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 11781 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 11782 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 11783 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 11784 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11785 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11786 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 11787 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 11788 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 11789 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 11790 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 11791 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 11792 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 11793 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11794 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11795 | /* these two declarations uses strings with list storage (in place |
| 11796 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 11797 | * and delete functions are relative to the list management. The parse |
| 11798 | * and match method are related to the corresponding fetch methods. This |
| 11799 | * is very particular ACL declaration mode. |
| 11800 | */ |
| 11801 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 11802 | { "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] | 11803 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11804 | { "path", "path", PAT_MATCH_STR }, |
| 11805 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 11806 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 11807 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 11808 | { "path_end", "path", PAT_MATCH_END }, |
| 11809 | { "path_len", "path", PAT_MATCH_LEN }, |
| 11810 | { "path_reg", "path", PAT_MATCH_REG }, |
| 11811 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11812 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11813 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 11814 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11815 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11816 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 11817 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 11818 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 11819 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 11820 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 11821 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 11822 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 11823 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11824 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11825 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 11826 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 11827 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 11828 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 11829 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 11830 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 11831 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 11832 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11833 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11834 | { "url", "url", PAT_MATCH_STR }, |
| 11835 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 11836 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 11837 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 11838 | { "url_end", "url", PAT_MATCH_END }, |
| 11839 | { "url_len", "url", PAT_MATCH_LEN }, |
| 11840 | { "url_reg", "url", PAT_MATCH_REG }, |
| 11841 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11842 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11843 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 11844 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 11845 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 11846 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 11847 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 11848 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 11849 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 11850 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11851 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 11852 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11853 | }}; |
| 11854 | |
| 11855 | /************************************************************************/ |
| 11856 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11857 | /************************************************************************/ |
| 11858 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11859 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11860 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11861 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11862 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 11863 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11864 | /* capture are allocated and are permanent in the session */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11865 | { "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] | 11866 | |
| 11867 | /* retrieve these captures from the HTTP logs */ |
| 11868 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11869 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11870 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11871 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11872 | { "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] | 11873 | { "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] | 11874 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11875 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 11876 | * are only here to match the ACL's name, are request-only and are used |
| 11877 | * for ACL compatibility only. |
| 11878 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11879 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11880 | { "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] | 11881 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11882 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11883 | |
| 11884 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 11885 | * only here to match the ACL's name, are request-only and are used for |
| 11886 | * ACL compatibility only. |
| 11887 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11888 | { "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] | 11889 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11890 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11891 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11892 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 11893 | { "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] | 11894 | { "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] | 11895 | { "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] | 11896 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11897 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11898 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11899 | |
| 11900 | /* HTTP protocol on the request path */ |
| 11901 | { "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] | 11902 | { "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] | 11903 | |
| 11904 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11905 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11906 | { "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] | 11907 | |
| 11908 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11909 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 11910 | { "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] | 11911 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11912 | /* 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] | 11913 | { "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] | 11914 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11915 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11916 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11917 | { "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] | 11918 | { "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] | 11919 | { "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] | 11920 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11921 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 11922 | { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11923 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11924 | |
| 11925 | /* 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] | 11926 | { "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] | 11927 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11928 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11929 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11930 | { "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] | 11931 | { "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] | 11932 | { "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] | 11933 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11934 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 11935 | { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11936 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11937 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11938 | /* 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] | 11939 | { "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] | 11940 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11941 | { "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] | 11942 | { "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] | 11943 | |
| 11944 | /* 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] | 11945 | { "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] | 11946 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11947 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 11948 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11949 | |
| 11950 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11951 | { "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] | 11952 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11953 | { "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] | 11954 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11955 | { "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] | 11956 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11957 | { "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] | 11958 | { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11959 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11960 | }}; |
| 11961 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11962 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11963 | /************************************************************************/ |
| 11964 | /* All supported converter keywords must be declared here. */ |
| 11965 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11966 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 11967 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11968 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR}, |
| 11969 | { "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] | 11970 | { NULL, NULL, 0, 0, 0 }, |
| 11971 | }}; |
| 11972 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11973 | /************************************************************************/ |
| 11974 | /* All supported http-request action keywords must be declared here. */ |
| 11975 | /************************************************************************/ |
| 11976 | struct http_req_action_kw_list http_req_actions = { |
| 11977 | .scope = "http", |
| 11978 | .kw = { |
| 11979 | { "set-method", parse_set_req_line }, |
| 11980 | { "set-path", parse_set_req_line }, |
| 11981 | { "set-query", parse_set_req_line }, |
| 11982 | { "set-uri", parse_set_req_line }, |
| 11983 | } |
| 11984 | }; |
| 11985 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11986 | __attribute__((constructor)) |
| 11987 | static void __http_protocol_init(void) |
| 11988 | { |
| 11989 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11990 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11991 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11992 | http_req_keywords_register(&http_req_actions); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11993 | } |
| 11994 | |
| 11995 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 11996 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 11997 | * Local variables: |
| 11998 | * c-indent-level: 8 |
| 11999 | * c-basic-offset: 8 |
| 12000 | * End: |
| 12001 | */ |