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 | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 829 | channel_auto_read(si_oc(si)); |
| 830 | channel_abort(si_oc(si)); |
| 831 | channel_auto_close(si_oc(si)); |
| 832 | channel_erase(si_oc(si)); |
| 833 | channel_auto_close(si_ic(si)); |
| 834 | channel_auto_read(si_ic(si)); |
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 | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 837 | bo_inject(si_ic(si), 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 | 22ec1ea | 2014-11-27 20:45:39 +0100 | [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 | 22ec1ea | 2014-11-27 20:45:39 +0100 | [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 | 22ec1ea | 2014-11-27 20:45:39 +0100 | [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 | 22ec1ea | 2014-11-27 20:45:39 +0100 | [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 | |
Jesse Hathaway | 2468d4e | 2015-03-06 20:16:15 +0000 | [diff] [blame] | 2365 | /* compress 200,201,202,203 responses only */ |
| 2366 | if ((txn->status != 200) && |
| 2367 | (txn->status != 201) && |
| 2368 | (txn->status != 202) && |
| 2369 | (txn->status != 203)) |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2370 | goto fail; |
| 2371 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2372 | /* Content-Length is null */ |
| 2373 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2374 | goto fail; |
| 2375 | |
| 2376 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2377 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2378 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2379 | goto fail; |
| 2380 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2381 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2382 | ctx.idx = 0; |
| 2383 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2384 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2385 | goto fail; |
| 2386 | } |
| 2387 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2388 | comp_type = NULL; |
| 2389 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2390 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2391 | * not listed in the "compression type" directive if any. If no content-type was |
| 2392 | * found but configuration requires one, we don't compress either. Backend has |
| 2393 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2394 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2395 | ctx.idx = 0; |
| 2396 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2397 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2398 | goto fail; |
| 2399 | |
| 2400 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 2401 | (s->fe->comp && (comp_type = s->fe->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2402 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2403 | if (ctx.vlen >= comp_type->name_len && |
| 2404 | 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] | 2405 | /* this Content-Type should be compressed */ |
| 2406 | break; |
| 2407 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2408 | /* this Content-Type should not be compressed */ |
| 2409 | if (comp_type == NULL) |
| 2410 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2411 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2412 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2413 | else { /* no content-type header */ |
| 2414 | if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types)) |
| 2415 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2416 | } |
| 2417 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2418 | /* limit compression rate */ |
| 2419 | if (global.comp_rate_lim > 0) |
| 2420 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2421 | goto fail; |
| 2422 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2423 | /* limit cpu usage */ |
| 2424 | if (idle_pct < compress_min_idle) |
| 2425 | goto fail; |
| 2426 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2427 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2428 | 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] | 2429 | goto fail; |
| 2430 | |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2431 | s->flags |= SN_COMP_READY; |
| 2432 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2433 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2434 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2435 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2436 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2437 | |
| 2438 | /* add Transfer-Encoding header */ |
| 2439 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2440 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2441 | |
| 2442 | /* |
| 2443 | * Add Content-Encoding header when it's not identity encoding. |
| 2444 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2445 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2446 | * header. |
| 2447 | */ |
| 2448 | if (s->comp_algo->add_data != identity_add_data) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2449 | trash.len = 18; |
| 2450 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
| 2451 | memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len); |
| 2452 | trash.len += s->comp_algo->name_len; |
| 2453 | trash.str[trash.len] = '\0'; |
| 2454 | 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] | 2455 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2456 | return 1; |
| 2457 | |
| 2458 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2459 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2460 | return 0; |
| 2461 | } |
| 2462 | |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2463 | void http_adjust_conn_mode(struct session *s, struct http_txn *txn, struct http_msg *msg) |
| 2464 | { |
| 2465 | int tmp = TX_CON_WANT_KAL; |
| 2466 | |
| 2467 | if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 2468 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
| 2469 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2470 | tmp = TX_CON_WANT_TUN; |
| 2471 | |
| 2472 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 2473 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2474 | tmp = TX_CON_WANT_TUN; |
| 2475 | } |
| 2476 | |
| 2477 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 2478 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 2479 | /* option httpclose + server_close => forceclose */ |
| 2480 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 2481 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2482 | tmp = TX_CON_WANT_CLO; |
| 2483 | else |
| 2484 | tmp = TX_CON_WANT_SCL; |
| 2485 | } |
| 2486 | |
| 2487 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
| 2488 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 2489 | tmp = TX_CON_WANT_CLO; |
| 2490 | |
| 2491 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2492 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2493 | |
| 2494 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 2495 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2496 | /* parse the Connection header and possibly clean it */ |
| 2497 | int to_del = 0; |
| 2498 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 2499 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 2500 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
| 2501 | to_del |= 2; /* remove "keep-alive" */ |
| 2502 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2503 | to_del |= 1; /* remove "close" */ |
| 2504 | http_parse_connection_header(txn, msg, to_del); |
| 2505 | } |
| 2506 | |
| 2507 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2508 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2509 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2510 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2511 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 2512 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
| 2513 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
| 2514 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2515 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2516 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2517 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2518 | * processing can continue on next analysers, or zero if it either needs more |
| 2519 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2520 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2521 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2522 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2523 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2524 | 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] | 2525 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2526 | /* |
| 2527 | * We will parse the partial (or complete) lines. |
| 2528 | * We will check the request syntax, and also join multi-line |
| 2529 | * headers. An index of all the lines will be elaborated while |
| 2530 | * parsing. |
| 2531 | * |
| 2532 | * For the parsing, we use a 28 states FSM. |
| 2533 | * |
| 2534 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2535 | * req->buf->p = beginning of request |
| 2536 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2537 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2538 | * msg->eol = end of current header or line (LF or CRLF) |
| 2539 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2540 | * |
| 2541 | * 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] | 2542 | * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2543 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2544 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2545 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2546 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2547 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2548 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2549 | struct http_txn *txn = &s->txn; |
| 2550 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2551 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2552 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2553 | 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] | 2554 | now_ms, __FUNCTION__, |
| 2555 | s, |
| 2556 | req, |
| 2557 | req->rex, req->wex, |
| 2558 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2559 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2560 | req->analysers); |
| 2561 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2562 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2563 | s->srv_error = http_return_srv_error; |
| 2564 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2565 | /* There's a protected area at the end of the buffer for rewriting |
| 2566 | * purposes. We don't want to start to parse the request if the |
| 2567 | * protected area is affected, because we may have to move processed |
| 2568 | * data later, which is much more complicated. |
| 2569 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2570 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2571 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 2572 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2573 | 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] | 2574 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2575 | /* 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] | 2576 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2577 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2578 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2579 | return 0; |
| 2580 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2581 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2582 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2583 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2584 | } |
| 2585 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2586 | /* Note that we have the same problem with the response ; we |
| 2587 | * may want to send a redirect, error or anything which requires |
| 2588 | * some spare space. So we'll ensure that we have at least |
| 2589 | * maxrewrite bytes available in the response buffer before |
| 2590 | * processing that one. This will only affect pipelined |
| 2591 | * keep-alive requests. |
| 2592 | */ |
| 2593 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2594 | unlikely(!channel_is_rewritable(&s->res) || |
| 2595 | bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) || |
| 2596 | bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) { |
| 2597 | if (s->res.buf->o) { |
| 2598 | if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2599 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2600 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2601 | channel_dont_connect(req); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2602 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 2603 | s->res.flags |= CF_WAKE_WRITE; |
| 2604 | s->res.analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2605 | return 0; |
| 2606 | } |
| 2607 | } |
| 2608 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2609 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2610 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2611 | } |
| 2612 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2613 | /* 1: we might have to print this header in debug mode */ |
| 2614 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2615 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2616 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2617 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2618 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2619 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2620 | /* this is a bit complex : in case of error on the request line, |
| 2621 | * we know that rq.l is still zero, so we display only the part |
| 2622 | * up to the end of the line (truncated by debug_hdr). |
| 2623 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2624 | 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] | 2625 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2626 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2627 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2628 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2629 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2630 | while (cur_idx) { |
| 2631 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2632 | debug_hdr("clihdr", s, sol, eol); |
| 2633 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2634 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2635 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2636 | } |
| 2637 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2638 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2639 | /* |
| 2640 | * Now we quickly check if we have found a full valid request. |
| 2641 | * If not so, we check the FD and buffer states before leaving. |
| 2642 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2643 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2644 | * requests are checked first. When waiting for a second request |
| 2645 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2646 | * we note the error in the session flags but don't set any state. |
| 2647 | * Since the error will be noted there, it will not be counted by |
| 2648 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2649 | * Last, we may increase some tracked counters' http request errors on |
| 2650 | * the cases that are deliberately the client's fault. For instance, |
| 2651 | * a timeout or connection reset is not counted as an error. However |
| 2652 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2653 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2654 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2655 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2656 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2657 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2658 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2659 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2660 | session_inc_http_req_ctr(s); |
| 2661 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2662 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2663 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2664 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2665 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2666 | /* 1: Since we are in header mode, if there's no space |
| 2667 | * left for headers, we won't be able to free more |
| 2668 | * later, so the session will never terminate. We |
| 2669 | * must terminate it now. |
| 2670 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2671 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2672 | /* FIXME: check if URI is set and return Status |
| 2673 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2674 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2675 | session_inc_http_req_ctr(s); |
| 2676 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2677 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2678 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2679 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2680 | goto return_bad_req; |
| 2681 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2682 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2683 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2684 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2685 | if (!(s->flags & SN_ERR_MASK)) |
| 2686 | s->flags |= SN_ERR_CLICL; |
| 2687 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2688 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2689 | goto failed_keep_alive; |
| 2690 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2691 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2692 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2693 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2694 | session_inc_http_err_ctr(s); |
| 2695 | } |
| 2696 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2697 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2698 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2699 | msg->msg_state = HTTP_MSG_ERROR; |
| 2700 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2701 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2702 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2703 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2704 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2705 | if (s->listener->counters) |
| 2706 | s->listener->counters->failed_req++; |
| 2707 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2708 | if (!(s->flags & SN_FINST_MASK)) |
| 2709 | s->flags |= SN_FINST_R; |
| 2710 | return 0; |
| 2711 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2712 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2713 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2714 | 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] | 2715 | if (!(s->flags & SN_ERR_MASK)) |
| 2716 | s->flags |= SN_ERR_CLITO; |
| 2717 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2718 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2719 | goto failed_keep_alive; |
| 2720 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2721 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2722 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2723 | 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] | 2724 | session_inc_http_err_ctr(s); |
| 2725 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2726 | txn->status = 408; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2727 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2728 | msg->msg_state = HTTP_MSG_ERROR; |
| 2729 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2730 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2731 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2732 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2733 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2734 | if (s->listener->counters) |
| 2735 | s->listener->counters->failed_req++; |
| 2736 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2737 | if (!(s->flags & SN_FINST_MASK)) |
| 2738 | s->flags |= SN_FINST_R; |
| 2739 | return 0; |
| 2740 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2741 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2742 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2743 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2744 | if (!(s->flags & SN_ERR_MASK)) |
| 2745 | s->flags |= SN_ERR_CLICL; |
| 2746 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2747 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2748 | goto failed_keep_alive; |
| 2749 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2750 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2751 | 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] | 2752 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2753 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2754 | msg->msg_state = HTTP_MSG_ERROR; |
| 2755 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2756 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2757 | session_inc_http_err_ctr(s); |
| 2758 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2759 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2760 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2761 | if (s->listener->counters) |
| 2762 | s->listener->counters->failed_req++; |
| 2763 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2764 | if (!(s->flags & SN_FINST_MASK)) |
| 2765 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2766 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2767 | } |
| 2768 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2769 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2770 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2771 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2772 | #ifdef TCP_QUICKACK |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2773 | if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->si[0].end) && conn_ctrl_ready(__objt_conn(s->si[0].end))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2774 | /* We need more data, we have to re-enable quick-ack in case we |
| 2775 | * previously disabled it, otherwise we might cause the client |
| 2776 | * to delay next data. |
| 2777 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2778 | setsockopt(__objt_conn(s->si[0].end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2779 | } |
| 2780 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2781 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2782 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2783 | /* If the client starts to talk, let's fall back to |
| 2784 | * request timeout processing. |
| 2785 | */ |
| 2786 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2787 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2788 | } |
| 2789 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2790 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2791 | if (!tick_isset(req->analyse_exp)) { |
| 2792 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2793 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2794 | tick_isset(s->be->timeout.httpka)) |
| 2795 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2796 | else |
| 2797 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2798 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2799 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2800 | /* we're not ready yet */ |
| 2801 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2802 | |
| 2803 | failed_keep_alive: |
| 2804 | /* Here we process low-level errors for keep-alive requests. In |
| 2805 | * short, if the request is not the first one and it experiences |
| 2806 | * a timeout, read error or shutdown, we just silently close so |
| 2807 | * that the client can try again. |
| 2808 | */ |
| 2809 | txn->status = 0; |
| 2810 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2811 | req->analysers = 0; |
| 2812 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2813 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2814 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2815 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2816 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2817 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2818 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2819 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2820 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2821 | * 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] | 2822 | * 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] | 2823 | * 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] | 2824 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2825 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2826 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2827 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2828 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2829 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2830 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2831 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2832 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2833 | /* kill the pending keep-alive timeout */ |
| 2834 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2835 | req->analyse_exp = TICK_ETERNITY; |
| 2836 | } |
| 2837 | |
| 2838 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2839 | /* Maybe we found in invalid header name while we were configured not |
| 2840 | * to block on that, so we have to capture it now. |
| 2841 | */ |
| 2842 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2843 | 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] | 2844 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2845 | /* |
| 2846 | * 1: identify the method |
| 2847 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2848 | 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] | 2849 | |
| 2850 | /* we can make use of server redirect on GET and HEAD */ |
| 2851 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2852 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2853 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2854 | /* |
| 2855 | * 2: check if the URI matches the monitor_uri. |
| 2856 | * We have to do this for every request which gets in, because |
| 2857 | * the monitor-uri is defined by the frontend. |
| 2858 | */ |
| 2859 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2860 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2861 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2862 | s->fe->monitor_uri, |
| 2863 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2864 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2865 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2866 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2867 | struct acl_cond *cond; |
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 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2870 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2871 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2872 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2873 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2874 | 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] | 2875 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2876 | ret = acl_pass(ret); |
| 2877 | if (cond->pol == ACL_COND_UNLESS) |
| 2878 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2879 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2880 | if (ret) { |
| 2881 | /* we fail this request, let's return 503 service unavail */ |
| 2882 | txn->status = 503; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2883 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2884 | if (!(s->flags & SN_ERR_MASK)) |
| 2885 | 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] | 2886 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2887 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2888 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2889 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2890 | /* nothing to fail, let's reply normaly */ |
| 2891 | txn->status = 200; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2892 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2893 | if (!(s->flags & SN_ERR_MASK)) |
| 2894 | 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] | 2895 | goto return_prx_cond; |
| 2896 | } |
| 2897 | |
| 2898 | /* |
| 2899 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2900 | * Note: we cannot log anymore if the request has been |
| 2901 | * classified as invalid. |
| 2902 | */ |
| 2903 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2904 | /* we have a complete HTTP request that we must log */ |
| 2905 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2906 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2907 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2908 | if (urilen >= REQURI_LEN) |
| 2909 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2910 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2911 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2912 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2913 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2914 | s->do_log(s); |
| 2915 | } else { |
| 2916 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2917 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2918 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2919 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2920 | /* 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] | 2921 | 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] | 2922 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2923 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2924 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2925 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2926 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2927 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2928 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2929 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2930 | |
| 2931 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2932 | 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] | 2933 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2934 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2935 | * we have what looks like a proxied connection instead of a connection, |
| 2936 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2937 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2938 | * happen to do that despite being non-standard :-( |
| 2939 | * We consider that a request not beginning with either '/' or '*' is |
| 2940 | * a proxied connection, which covers both "scheme://location" and |
| 2941 | * CONNECT ip:port. |
| 2942 | */ |
| 2943 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2944 | 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] | 2945 | txn->flags |= TX_USE_PX_CONN; |
| 2946 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2947 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2948 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2949 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2950 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2951 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2952 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2953 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2954 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2955 | /* 6: determine the transfer-length. |
| 2956 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2957 | * the presence of a message-body in a REQUEST and its transfer length |
| 2958 | * must be determined that way (in order of precedence) : |
| 2959 | * 1. The presence of a message-body in a request is signaled by the |
| 2960 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2961 | * in the request's header fields. When a request message contains |
| 2962 | * both a message-body of non-zero length and a method that does |
| 2963 | * not define any semantics for that request message-body, then an |
| 2964 | * origin server SHOULD either ignore the message-body or respond |
| 2965 | * with an appropriate error message (e.g., 413). A proxy or |
| 2966 | * gateway, when presented the same request, SHOULD either forward |
| 2967 | * the request inbound with the message- body or ignore the |
| 2968 | * message-body when determining a response. |
| 2969 | * |
| 2970 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2971 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2972 | * transfer-length is defined by the use of this transfer-coding. |
| 2973 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2974 | * transfer-coding is not present, the transfer-length is defined |
| 2975 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2976 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2977 | * 3. If a Content-Length header field is present, its decimal value in |
| 2978 | * OCTETs represents both the entity-length and the transfer-length. |
| 2979 | * If a message is received with both a Transfer-Encoding header |
| 2980 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2981 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2982 | * 4. By the server closing the connection. (Closing the connection |
| 2983 | * cannot be used to indicate the end of a request body, since that |
| 2984 | * would leave no possibility for the server to send back a response.) |
| 2985 | * |
| 2986 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2987 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2988 | * it is terminated by closing the connection. When the "chunked" |
| 2989 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2990 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2991 | */ |
| 2992 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2993 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2994 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2995 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2996 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2997 | 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] | 2998 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2999 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 3000 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3001 | /* bad transfer-encoding (chunked followed by something else) */ |
| 3002 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3003 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3004 | break; |
| 3005 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3006 | } |
| 3007 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3008 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3009 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3010 | 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] | 3011 | signed long long cl; |
| 3012 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3013 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3014 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3015 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3016 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3017 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3018 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3019 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3020 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3021 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3022 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3023 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3024 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3025 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3026 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3027 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3028 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3029 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3030 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3031 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3032 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3033 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3034 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3035 | } |
| 3036 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3037 | /* bodyless requests have a known length */ |
| 3038 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3039 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3040 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3041 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 3042 | * only change if both the request and the config reference something else. |
| 3043 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3044 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3045 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3046 | * if FE and BE have the same settings (common). The method consists in |
| 3047 | * checking if options changed between the two calls (implying that either |
| 3048 | * one is non-null, or one of them is non-null and we are there for the first |
| 3049 | * time. |
| 3050 | */ |
| 3051 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 3052 | ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) |
| 3053 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3054 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3055 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3056 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3057 | req->analyse_exp = TICK_ETERNITY; |
| 3058 | return 1; |
| 3059 | |
| 3060 | return_bad_req: |
| 3061 | /* We centralize bad requests processing here */ |
| 3062 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3063 | /* we detected a parsing error. We want to archive this request |
| 3064 | * in the dedicated proxy area for later troubleshooting. |
| 3065 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3066 | 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] | 3067 | } |
| 3068 | |
| 3069 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3070 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3071 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3072 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3073 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3074 | if (s->listener->counters) |
| 3075 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3076 | |
| 3077 | return_prx_cond: |
| 3078 | if (!(s->flags & SN_ERR_MASK)) |
| 3079 | s->flags |= SN_ERR_PRXCOND; |
| 3080 | if (!(s->flags & SN_FINST_MASK)) |
| 3081 | s->flags |= SN_FINST_R; |
| 3082 | |
| 3083 | req->analysers = 0; |
| 3084 | req->analyse_exp = TICK_ETERNITY; |
| 3085 | return 0; |
| 3086 | } |
| 3087 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3088 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3089 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3090 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3091 | * and program a response message if something was unexpected. It cannot fail |
| 3092 | * 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] | 3093 | * 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] | 3094 | * s->target which is supposed to already point to the stats applet. The caller |
| 3095 | * is expected to have already assigned an appctx to the session. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3096 | */ |
| 3097 | int http_handle_stats(struct session *s, struct channel *req) |
| 3098 | { |
| 3099 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3100 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3101 | struct http_txn *txn = &s->txn; |
| 3102 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3103 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3104 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3105 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3106 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3107 | appctx = si_appctx(si); |
| 3108 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3109 | appctx->st1 = appctx->st2 = 0; |
| 3110 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3111 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | af3cf70 | 2014-04-22 22:19:53 +0200 | [diff] [blame] | 3112 | if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD)) |
| 3113 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3114 | |
| 3115 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3116 | lookup = uri + uri_auth->uri_len; |
| 3117 | |
| 3118 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3119 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3120 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3121 | break; |
| 3122 | } |
| 3123 | } |
| 3124 | |
| 3125 | if (uri_auth->refresh) { |
| 3126 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3127 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3128 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3129 | break; |
| 3130 | } |
| 3131 | } |
| 3132 | } |
| 3133 | |
| 3134 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3135 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3136 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3137 | break; |
| 3138 | } |
| 3139 | } |
| 3140 | |
| 3141 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3142 | if (memcmp(h, ";st=", 4) == 0) { |
| 3143 | int i; |
| 3144 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3145 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3146 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3147 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3148 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3149 | break; |
| 3150 | } |
| 3151 | } |
| 3152 | break; |
| 3153 | } |
| 3154 | } |
| 3155 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3156 | appctx->ctx.stats.scope_str = 0; |
| 3157 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3158 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3159 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3160 | int itx = 0; |
| 3161 | const char *h2; |
| 3162 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3163 | const char *err; |
| 3164 | |
| 3165 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3166 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3167 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3168 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3169 | itx++; |
| 3170 | h++; |
| 3171 | } |
| 3172 | |
| 3173 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3174 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3175 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3176 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3177 | /* 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] | 3178 | memcpy(scope_txt, h2, itx); |
| 3179 | scope_txt[itx] = '\0'; |
| 3180 | err = invalid_char(scope_txt); |
| 3181 | if (err) { |
| 3182 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3183 | appctx->ctx.stats.scope_str = 0; |
| 3184 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3185 | } |
| 3186 | break; |
| 3187 | } |
| 3188 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3189 | |
| 3190 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3191 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3192 | int ret = 1; |
| 3193 | |
| 3194 | if (stats_admin_rule->cond) { |
| 3195 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 3196 | ret = acl_pass(ret); |
| 3197 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3198 | ret = !ret; |
| 3199 | } |
| 3200 | |
| 3201 | if (ret) { |
| 3202 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3203 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3204 | break; |
| 3205 | } |
| 3206 | } |
| 3207 | |
| 3208 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3209 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3210 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3211 | /* we'll need the request body, possibly after sending 100-continue */ |
| 3212 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3213 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3214 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3215 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3216 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3217 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3218 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3219 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3220 | else { |
| 3221 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3222 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3223 | } |
| 3224 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3225 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3226 | return 1; |
| 3227 | } |
| 3228 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3229 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3230 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3231 | */ |
| 3232 | static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
| 3233 | { |
| 3234 | #ifdef IP_TOS |
| 3235 | if (from.ss_family == AF_INET) |
| 3236 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3237 | #endif |
| 3238 | #ifdef IPV6_TCLASS |
| 3239 | if (from.ss_family == AF_INET6) { |
| 3240 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3241 | /* v4-mapped addresses need IP_TOS */ |
| 3242 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3243 | else |
| 3244 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3245 | } |
| 3246 | #endif |
| 3247 | } |
| 3248 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame^] | 3249 | static int http_transform_header(struct session* s, struct http_msg *msg, |
| 3250 | const char* name, unsigned int name_len, |
| 3251 | struct list *fmt, struct my_regex *re, |
| 3252 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3253 | { |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3254 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 3255 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame^] | 3256 | struct hdr_ctx ctx; |
| 3257 | char *buf = msg->chn->buf->p; |
| 3258 | struct hdr_idx *idx = &s->txn.hdr_idx; |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3259 | struct chunk *replace = get_trash_chunk(); |
| 3260 | struct chunk *output = get_trash_chunk(); |
| 3261 | |
| 3262 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 3263 | if (replace->len >= replace->size - 1) |
| 3264 | return -1; |
| 3265 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame^] | 3266 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3267 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3268 | /* Choose the header browsing function. */ |
| 3269 | switch (action) { |
| 3270 | case HTTP_REQ_ACT_REPLACE_VAL: |
| 3271 | case HTTP_RES_ACT_REPLACE_VAL: |
| 3272 | http_find_hdr_func = http_find_header2; |
| 3273 | break; |
| 3274 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3275 | case HTTP_RES_ACT_REPLACE_HDR: |
| 3276 | http_find_hdr_func = http_find_full_header2; |
| 3277 | break; |
| 3278 | default: /* impossible */ |
| 3279 | return -1; |
| 3280 | } |
| 3281 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame^] | 3282 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 3283 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3284 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame^] | 3285 | char *val = ctx.line + ctx.val; |
| 3286 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3287 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3288 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 3289 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3290 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3291 | output->len = exp_replace(output->str, output->size, val, replace->str, pmatch); |
| 3292 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3293 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3294 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3295 | delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3296 | |
| 3297 | hdr->len += delta; |
| 3298 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3299 | |
| 3300 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame^] | 3301 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3302 | } |
| 3303 | |
| 3304 | return 0; |
| 3305 | } |
| 3306 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3307 | /* Executes the http-request rules <rules> for session <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3308 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3309 | * further processing of the request (auth, deny, ...), and defaults to |
| 3310 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3311 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 3312 | * on txn->flags if it encounters a tarpit rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3313 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3314 | enum rule_result |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3315 | 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] | 3316 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3317 | struct connection *cli_conn; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3318 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3319 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3320 | const char *auth_realm; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3321 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3322 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3323 | * resume condition. If a resume is needed it is always in the action |
| 3324 | * and never in the ACL or converters. In this case, we initialise the |
| 3325 | * current rule, and go to the action execution point. |
| 3326 | */ |
| 3327 | if (s->current_rule_list == rules) { |
| 3328 | rule = LIST_ELEM(s->current_rule, typeof(rule), list); |
| 3329 | goto resume_execution; |
| 3330 | } |
| 3331 | s->current_rule_list = rules; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3332 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3333 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3334 | continue; |
| 3335 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3336 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3337 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3338 | int ret; |
| 3339 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3340 | 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] | 3341 | ret = acl_pass(ret); |
| 3342 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3343 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3344 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3345 | |
| 3346 | if (!ret) /* condition not matched */ |
| 3347 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3348 | } |
| 3349 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3350 | resume_execution: |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3351 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3352 | switch (rule->action) { |
| 3353 | case HTTP_REQ_ACT_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3354 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3355 | |
| 3356 | case HTTP_REQ_ACT_DENY: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3357 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3358 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3359 | case HTTP_REQ_ACT_TARPIT: |
| 3360 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3361 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3362 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3363 | case HTTP_REQ_ACT_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3364 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3365 | auth_realm = rule->arg.auth.realm; |
| 3366 | if (!auth_realm) { |
| 3367 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3368 | auth_realm = STATS_DEFAULT_REALM; |
| 3369 | else |
| 3370 | auth_realm = px->id; |
| 3371 | } |
| 3372 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3373 | * count one error, because normal browsing won't significantly |
| 3374 | * increase the counter but brute force attempts will. |
| 3375 | */ |
| 3376 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3377 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
| 3378 | stream_int_retnclose(&s->si[0], &trash); |
| 3379 | session_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3380 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3381 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3382 | case HTTP_REQ_ACT_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3383 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3384 | return HTTP_RULE_RES_BADREQ; |
| 3385 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3386 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3387 | case HTTP_REQ_ACT_SET_NICE: |
| 3388 | s->task->nice = rule->arg.nice; |
| 3389 | break; |
| 3390 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3391 | case HTTP_REQ_ACT_SET_TOS: |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3392 | if ((cli_conn = objt_conn(s->si[0].end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3393 | 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] | 3394 | break; |
| 3395 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3396 | case HTTP_REQ_ACT_SET_MARK: |
| 3397 | #ifdef SO_MARK |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3398 | if ((cli_conn = objt_conn(s->si[0].end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3399 | 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] | 3400 | #endif |
| 3401 | break; |
| 3402 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3403 | case HTTP_REQ_ACT_SET_LOGL: |
| 3404 | s->logs.level = rule->arg.loglevel; |
| 3405 | break; |
| 3406 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3407 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3408 | case HTTP_REQ_ACT_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame^] | 3409 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 3410 | rule->arg.hdr_add.name_len, |
| 3411 | &rule->arg.hdr_add.fmt, |
| 3412 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3413 | return HTTP_RULE_RES_BADREQ; |
| 3414 | break; |
| 3415 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3416 | case HTTP_REQ_ACT_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3417 | ctx.idx = 0; |
| 3418 | /* remove all occurrences of the header */ |
| 3419 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3420 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3421 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3422 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3423 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3424 | |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3425 | case HTTP_REQ_ACT_SET_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3426 | case HTTP_REQ_ACT_ADD_HDR: |
| 3427 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3428 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3429 | trash.len = rule->arg.hdr_add.name_len; |
| 3430 | trash.str[trash.len++] = ':'; |
| 3431 | trash.str[trash.len++] = ' '; |
| 3432 | 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] | 3433 | |
| 3434 | if (rule->action == HTTP_REQ_ACT_SET_HDR) { |
| 3435 | /* remove all occurrences of the header */ |
| 3436 | ctx.idx = 0; |
| 3437 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3438 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3439 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3440 | } |
| 3441 | } |
| 3442 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3443 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3444 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3445 | |
| 3446 | case HTTP_REQ_ACT_DEL_ACL: |
| 3447 | case HTTP_REQ_ACT_DEL_MAP: { |
| 3448 | struct pat_ref *ref; |
| 3449 | char *key; |
| 3450 | int len; |
| 3451 | |
| 3452 | /* collect reference */ |
| 3453 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3454 | if (!ref) |
| 3455 | continue; |
| 3456 | |
| 3457 | /* collect key */ |
| 3458 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3459 | key = trash.str; |
| 3460 | key[len] = '\0'; |
| 3461 | |
| 3462 | /* perform update */ |
| 3463 | /* returned code: 1=ok, 0=ko */ |
| 3464 | pat_ref_delete(ref, key); |
| 3465 | |
| 3466 | break; |
| 3467 | } |
| 3468 | |
| 3469 | case HTTP_REQ_ACT_ADD_ACL: { |
| 3470 | struct pat_ref *ref; |
| 3471 | char *key; |
| 3472 | struct chunk *trash_key; |
| 3473 | int len; |
| 3474 | |
| 3475 | trash_key = get_trash_chunk(); |
| 3476 | |
| 3477 | /* collect reference */ |
| 3478 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3479 | if (!ref) |
| 3480 | continue; |
| 3481 | |
| 3482 | /* collect key */ |
| 3483 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3484 | key = trash_key->str; |
| 3485 | key[len] = '\0'; |
| 3486 | |
| 3487 | /* perform update */ |
| 3488 | /* add entry only if it does not already exist */ |
| 3489 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3490 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3491 | |
| 3492 | break; |
| 3493 | } |
| 3494 | |
| 3495 | case HTTP_REQ_ACT_SET_MAP: { |
| 3496 | struct pat_ref *ref; |
| 3497 | char *key, *value; |
| 3498 | struct chunk *trash_key, *trash_value; |
| 3499 | int len; |
| 3500 | |
| 3501 | trash_key = get_trash_chunk(); |
| 3502 | trash_value = get_trash_chunk(); |
| 3503 | |
| 3504 | /* collect reference */ |
| 3505 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3506 | if (!ref) |
| 3507 | continue; |
| 3508 | |
| 3509 | /* collect key */ |
| 3510 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3511 | key = trash_key->str; |
| 3512 | key[len] = '\0'; |
| 3513 | |
| 3514 | /* collect value */ |
| 3515 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3516 | value = trash_value->str; |
| 3517 | value[len] = '\0'; |
| 3518 | |
| 3519 | /* perform update */ |
| 3520 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3521 | /* update entry if it exists */ |
| 3522 | pat_ref_set(ref, key, value, NULL); |
| 3523 | else |
| 3524 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3525 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3526 | |
| 3527 | break; |
| 3528 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3529 | |
| 3530 | case HTTP_REQ_ACT_CUSTOM_CONT: |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3531 | if (!rule->action_ptr(rule, px, s, txn)) { |
| 3532 | s->current_rule = &rule->list; |
| 3533 | return HTTP_RULE_RES_YIELD; |
| 3534 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3535 | break; |
| 3536 | |
| 3537 | case HTTP_REQ_ACT_CUSTOM_STOP: |
| 3538 | rule->action_ptr(rule, px, s, txn); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3539 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3540 | |
| 3541 | case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX: |
| 3542 | /* Note: only the first valid tracking parameter of each |
| 3543 | * applies. |
| 3544 | */ |
| 3545 | |
| 3546 | if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) { |
| 3547 | struct stktable *t; |
| 3548 | struct stksess *ts; |
| 3549 | struct stktable_key *key; |
| 3550 | void *ptr; |
| 3551 | |
| 3552 | t = rule->act_prm.trk_ctr.table.t; |
| 3553 | key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL); |
| 3554 | |
| 3555 | if (key && (ts = stktable_get_entry(t, key))) { |
| 3556 | session_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts); |
| 3557 | |
| 3558 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3559 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3560 | if (ptr) |
| 3561 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3562 | |
| 3563 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3564 | if (ptr) |
| 3565 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3566 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3567 | |
| 3568 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 3569 | if (s->fe != s->be) |
| 3570 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3571 | } |
| 3572 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3573 | } |
| 3574 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3575 | |
| 3576 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3577 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3578 | } |
| 3579 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3580 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3581 | /* Executes the http-response rules <rules> for session <s>, proxy <px> and |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3582 | * transaction <txn>. Returns 3 states: HTTP_RULE_RES_CONT, HTTP_RULE_RES_YIELD |
| 3583 | * or HTTP_RULE_RES_STOP. If *CONT is returned, the process can continue the |
| 3584 | * evaluation of next rule list. If *STOP is returned, the process must stop |
| 3585 | * the evaluation. It may set the TX_SVDENY on txn->flags if it encounters a deny |
| 3586 | * rule. If *YIELD is returned, the czller must call again the function with |
| 3587 | * the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3588 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3589 | static enum rule_result |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3590 | http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 3591 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3592 | struct connection *cli_conn; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3593 | struct http_res_rule *rule; |
| 3594 | struct hdr_ctx ctx; |
| 3595 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3596 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3597 | * resume condition. If a resume is needed it is always in the action |
| 3598 | * and never in the ACL or converters. In this case, we initialise the |
| 3599 | * current rule, and go to the action execution point. |
| 3600 | */ |
| 3601 | if (s->current_rule_list == rules) { |
| 3602 | rule = LIST_ELEM(s->current_rule, typeof(rule), list); |
| 3603 | goto resume_execution; |
| 3604 | } |
| 3605 | s->current_rule_list = rules; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3606 | list_for_each_entry(rule, rules, list) { |
| 3607 | if (rule->action >= HTTP_RES_ACT_MAX) |
| 3608 | continue; |
| 3609 | |
| 3610 | /* check optional condition */ |
| 3611 | if (rule->cond) { |
| 3612 | int ret; |
| 3613 | |
| 3614 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 3615 | ret = acl_pass(ret); |
| 3616 | |
| 3617 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3618 | ret = !ret; |
| 3619 | |
| 3620 | if (!ret) /* condition not matched */ |
| 3621 | continue; |
| 3622 | } |
| 3623 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3624 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3625 | |
| 3626 | switch (rule->action) { |
| 3627 | case HTTP_RES_ACT_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3628 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3629 | |
| 3630 | case HTTP_RES_ACT_DENY: |
| 3631 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3632 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3633 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3634 | case HTTP_RES_ACT_SET_NICE: |
| 3635 | s->task->nice = rule->arg.nice; |
| 3636 | break; |
| 3637 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3638 | case HTTP_RES_ACT_SET_TOS: |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3639 | if ((cli_conn = objt_conn(s->si[0].end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3640 | 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] | 3641 | break; |
| 3642 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3643 | case HTTP_RES_ACT_SET_MARK: |
| 3644 | #ifdef SO_MARK |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3645 | if ((cli_conn = objt_conn(s->si[0].end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3646 | 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] | 3647 | #endif |
| 3648 | break; |
| 3649 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3650 | case HTTP_RES_ACT_SET_LOGL: |
| 3651 | s->logs.level = rule->arg.loglevel; |
| 3652 | break; |
| 3653 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3654 | case HTTP_RES_ACT_REPLACE_HDR: |
| 3655 | case HTTP_RES_ACT_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame^] | 3656 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 3657 | rule->arg.hdr_add.name_len, |
| 3658 | &rule->arg.hdr_add.fmt, |
| 3659 | &rule->arg.hdr_add.re, rule->action)) |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3660 | return HTTP_RULE_RES_STOP; /* note: we should report an error here */ |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3661 | break; |
| 3662 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3663 | case HTTP_RES_ACT_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3664 | ctx.idx = 0; |
| 3665 | /* remove all occurrences of the header */ |
| 3666 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3667 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3668 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3669 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3670 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3671 | |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3672 | case HTTP_RES_ACT_SET_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3673 | case HTTP_RES_ACT_ADD_HDR: |
| 3674 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3675 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3676 | trash.len = rule->arg.hdr_add.name_len; |
| 3677 | trash.str[trash.len++] = ':'; |
| 3678 | trash.str[trash.len++] = ' '; |
| 3679 | 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] | 3680 | |
| 3681 | if (rule->action == HTTP_RES_ACT_SET_HDR) { |
| 3682 | /* remove all occurrences of the header */ |
| 3683 | ctx.idx = 0; |
| 3684 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3685 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3686 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3687 | } |
| 3688 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3689 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3690 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3691 | |
| 3692 | case HTTP_RES_ACT_DEL_ACL: |
| 3693 | case HTTP_RES_ACT_DEL_MAP: { |
| 3694 | struct pat_ref *ref; |
| 3695 | char *key; |
| 3696 | int len; |
| 3697 | |
| 3698 | /* collect reference */ |
| 3699 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3700 | if (!ref) |
| 3701 | continue; |
| 3702 | |
| 3703 | /* collect key */ |
| 3704 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3705 | key = trash.str; |
| 3706 | key[len] = '\0'; |
| 3707 | |
| 3708 | /* perform update */ |
| 3709 | /* returned code: 1=ok, 0=ko */ |
| 3710 | pat_ref_delete(ref, key); |
| 3711 | |
| 3712 | break; |
| 3713 | } |
| 3714 | |
| 3715 | case HTTP_RES_ACT_ADD_ACL: { |
| 3716 | struct pat_ref *ref; |
| 3717 | char *key; |
| 3718 | struct chunk *trash_key; |
| 3719 | int len; |
| 3720 | |
| 3721 | trash_key = get_trash_chunk(); |
| 3722 | |
| 3723 | /* collect reference */ |
| 3724 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3725 | if (!ref) |
| 3726 | continue; |
| 3727 | |
| 3728 | /* collect key */ |
| 3729 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3730 | key = trash_key->str; |
| 3731 | key[len] = '\0'; |
| 3732 | |
| 3733 | /* perform update */ |
| 3734 | /* check if the entry already exists */ |
| 3735 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3736 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3737 | |
| 3738 | break; |
| 3739 | } |
| 3740 | |
| 3741 | case HTTP_RES_ACT_SET_MAP: { |
| 3742 | struct pat_ref *ref; |
| 3743 | char *key, *value; |
| 3744 | struct chunk *trash_key, *trash_value; |
| 3745 | int len; |
| 3746 | |
| 3747 | trash_key = get_trash_chunk(); |
| 3748 | trash_value = get_trash_chunk(); |
| 3749 | |
| 3750 | /* collect reference */ |
| 3751 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3752 | if (!ref) |
| 3753 | continue; |
| 3754 | |
| 3755 | /* collect key */ |
| 3756 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3757 | key = trash_key->str; |
| 3758 | key[len] = '\0'; |
| 3759 | |
| 3760 | /* collect value */ |
| 3761 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3762 | value = trash_value->str; |
| 3763 | value[len] = '\0'; |
| 3764 | |
| 3765 | /* perform update */ |
| 3766 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3767 | /* update entry if it exists */ |
| 3768 | pat_ref_set(ref, key, value, NULL); |
| 3769 | else |
| 3770 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3771 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3772 | |
| 3773 | break; |
| 3774 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3775 | |
| 3776 | case HTTP_RES_ACT_CUSTOM_CONT: |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3777 | if (!rule->action_ptr(rule, px, s, txn)) { |
| 3778 | s->current_rule = &rule->list; |
| 3779 | return HTTP_RULE_RES_YIELD; |
| 3780 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3781 | break; |
| 3782 | |
| 3783 | case HTTP_RES_ACT_CUSTOM_STOP: |
| 3784 | rule->action_ptr(rule, px, s, txn); |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3785 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3786 | } |
| 3787 | } |
| 3788 | |
| 3789 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3790 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3791 | } |
| 3792 | |
| 3793 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3794 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3795 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3796 | * as too large a request to build a valid response. |
| 3797 | */ |
| 3798 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn) |
| 3799 | { |
| 3800 | struct http_msg *msg = &txn->req; |
| 3801 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3802 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3803 | |
| 3804 | /* build redirect message */ |
| 3805 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3806 | case 308: |
| 3807 | msg_fmt = HTTP_308; |
| 3808 | break; |
| 3809 | case 307: |
| 3810 | msg_fmt = HTTP_307; |
| 3811 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3812 | case 303: |
| 3813 | msg_fmt = HTTP_303; |
| 3814 | break; |
| 3815 | case 301: |
| 3816 | msg_fmt = HTTP_301; |
| 3817 | break; |
| 3818 | case 302: |
| 3819 | default: |
| 3820 | msg_fmt = HTTP_302; |
| 3821 | break; |
| 3822 | } |
| 3823 | |
| 3824 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3825 | return 0; |
| 3826 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3827 | location = trash.str + trash.len; |
| 3828 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3829 | switch(rule->type) { |
| 3830 | case REDIRECT_TYPE_SCHEME: { |
| 3831 | const char *path; |
| 3832 | const char *host; |
| 3833 | struct hdr_ctx ctx; |
| 3834 | int pathlen; |
| 3835 | int hostlen; |
| 3836 | |
| 3837 | host = ""; |
| 3838 | hostlen = 0; |
| 3839 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3840 | 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] | 3841 | host = ctx.line + ctx.val; |
| 3842 | hostlen = ctx.vlen; |
| 3843 | } |
| 3844 | |
| 3845 | path = http_get_path(txn); |
| 3846 | /* build message using path */ |
| 3847 | if (path) { |
| 3848 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3849 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3850 | int qs = 0; |
| 3851 | while (qs < pathlen) { |
| 3852 | if (path[qs] == '?') { |
| 3853 | pathlen = qs; |
| 3854 | break; |
| 3855 | } |
| 3856 | qs++; |
| 3857 | } |
| 3858 | } |
| 3859 | } else { |
| 3860 | path = "/"; |
| 3861 | pathlen = 1; |
| 3862 | } |
| 3863 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3864 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3865 | /* check if we can add scheme + "://" + host + path */ |
| 3866 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3867 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3868 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3869 | /* add scheme */ |
| 3870 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3871 | trash.len += rule->rdr_len; |
| 3872 | } |
| 3873 | else { |
| 3874 | /* add scheme with executing log format */ |
| 3875 | 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] | 3876 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3877 | /* check if we can add scheme + "://" + host + path */ |
| 3878 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 3879 | return 0; |
| 3880 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3881 | /* add "://" */ |
| 3882 | memcpy(trash.str + trash.len, "://", 3); |
| 3883 | trash.len += 3; |
| 3884 | |
| 3885 | /* add host */ |
| 3886 | memcpy(trash.str + trash.len, host, hostlen); |
| 3887 | trash.len += hostlen; |
| 3888 | |
| 3889 | /* add path */ |
| 3890 | memcpy(trash.str + trash.len, path, pathlen); |
| 3891 | trash.len += pathlen; |
| 3892 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3893 | /* 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] | 3894 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3895 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3896 | if (trash.len > trash.size - 5) |
| 3897 | return 0; |
| 3898 | trash.str[trash.len] = '/'; |
| 3899 | trash.len++; |
| 3900 | } |
| 3901 | |
| 3902 | break; |
| 3903 | } |
| 3904 | case REDIRECT_TYPE_PREFIX: { |
| 3905 | const char *path; |
| 3906 | int pathlen; |
| 3907 | |
| 3908 | path = http_get_path(txn); |
| 3909 | /* build message using path */ |
| 3910 | if (path) { |
| 3911 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3912 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3913 | int qs = 0; |
| 3914 | while (qs < pathlen) { |
| 3915 | if (path[qs] == '?') { |
| 3916 | pathlen = qs; |
| 3917 | break; |
| 3918 | } |
| 3919 | qs++; |
| 3920 | } |
| 3921 | } |
| 3922 | } else { |
| 3923 | path = "/"; |
| 3924 | pathlen = 1; |
| 3925 | } |
| 3926 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3927 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3928 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 3929 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3930 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3931 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3932 | * add anything, otherwise it makes it hard for the user to |
| 3933 | * configure a self-redirection. |
| 3934 | */ |
| 3935 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 3936 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3937 | trash.len += rule->rdr_len; |
| 3938 | } |
| 3939 | } |
| 3940 | else { |
| 3941 | /* add prefix with executing log format */ |
| 3942 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 3943 | |
| 3944 | /* Check length */ |
| 3945 | if (trash.len + pathlen > trash.size - 4) |
| 3946 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3947 | } |
| 3948 | |
| 3949 | /* add path */ |
| 3950 | memcpy(trash.str + trash.len, path, pathlen); |
| 3951 | trash.len += pathlen; |
| 3952 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3953 | /* 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] | 3954 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3955 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3956 | if (trash.len > trash.size - 5) |
| 3957 | return 0; |
| 3958 | trash.str[trash.len] = '/'; |
| 3959 | trash.len++; |
| 3960 | } |
| 3961 | |
| 3962 | break; |
| 3963 | } |
| 3964 | case REDIRECT_TYPE_LOCATION: |
| 3965 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3966 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3967 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 3968 | return 0; |
| 3969 | |
| 3970 | /* add location */ |
| 3971 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3972 | trash.len += rule->rdr_len; |
| 3973 | } |
| 3974 | else { |
| 3975 | /* add location with executing log format */ |
| 3976 | 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] | 3977 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3978 | /* Check left length */ |
| 3979 | if (trash.len > trash.size - 4) |
| 3980 | return 0; |
| 3981 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3982 | break; |
| 3983 | } |
| 3984 | |
| 3985 | if (rule->cookie_len) { |
| 3986 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 3987 | trash.len += 14; |
| 3988 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 3989 | trash.len += rule->cookie_len; |
| 3990 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 3991 | trash.len += 2; |
| 3992 | } |
| 3993 | |
| 3994 | /* add end of headers and the keep-alive/close status. |
| 3995 | * We may choose to set keep-alive if the Location begins |
| 3996 | * with a slash, because the client will come back to the |
| 3997 | * same server. |
| 3998 | */ |
| 3999 | txn->status = rule->code; |
| 4000 | /* let's log the request time */ |
| 4001 | s->logs.tv_request = now; |
| 4002 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4003 | if (*location == '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4004 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 4005 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
| 4006 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4007 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4008 | /* keep-alive possible */ |
| 4009 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
| 4010 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4011 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 4012 | trash.len += 30; |
| 4013 | } else { |
| 4014 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 4015 | trash.len += 24; |
| 4016 | } |
| 4017 | } |
| 4018 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 4019 | trash.len += 4; |
| 4020 | bo_inject(txn->rsp.chn, trash.str, trash.len); |
| 4021 | /* "eat" the request */ |
| 4022 | bi_fast_delete(txn->req.chn->buf, msg->sov); |
Willy Tarreau | 6d8bac7 | 2014-04-25 12:19:32 +0200 | [diff] [blame] | 4023 | msg->next -= msg->sov; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4024 | msg->sov = 0; |
| 4025 | txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4026 | s->res.analysers = AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4027 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4028 | txn->rsp.msg_state = HTTP_MSG_DONE; |
| 4029 | } else { |
| 4030 | /* keep-alive not possible */ |
| 4031 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4032 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 4033 | trash.len += 29; |
| 4034 | } else { |
| 4035 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 4036 | trash.len += 23; |
| 4037 | } |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4038 | stream_int_retnclose(&s->si[0], &trash); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4039 | txn->req.chn->analysers = 0; |
| 4040 | } |
| 4041 | |
| 4042 | if (!(s->flags & SN_ERR_MASK)) |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 4043 | s->flags |= SN_ERR_LOCAL; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4044 | if (!(s->flags & SN_FINST_MASK)) |
| 4045 | s->flags |= SN_FINST_R; |
| 4046 | |
| 4047 | return 1; |
| 4048 | } |
| 4049 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4050 | /* This stream analyser runs all HTTP request processing which is common to |
| 4051 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 4052 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4053 | * 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] | 4054 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 4055 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4056 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4057 | 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] | 4058 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4059 | struct http_txn *txn = &s->txn; |
| 4060 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4061 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4062 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4063 | enum rule_result verdict; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4064 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4065 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4066 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4067 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4068 | } |
| 4069 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4070 | 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] | 4071 | now_ms, __FUNCTION__, |
| 4072 | s, |
| 4073 | req, |
| 4074 | req->rex, req->wex, |
| 4075 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4076 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4077 | req->analysers); |
| 4078 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4079 | /* just in case we have some per-backend tracking */ |
| 4080 | session_inc_be_http_req_ctr(s); |
| 4081 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4082 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4083 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
| 4084 | 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] | 4085 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4086 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4087 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 4088 | goto return_prx_yield; |
| 4089 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4090 | case HTTP_RULE_RES_CONT: |
| 4091 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4092 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4093 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4094 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4095 | if (txn->flags & TX_CLTARPIT) |
| 4096 | goto tarpit; |
| 4097 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4098 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4099 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4100 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4101 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4102 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4103 | goto done; |
| 4104 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4105 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4106 | goto return_bad_req; |
| 4107 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4108 | } |
| 4109 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4110 | /* OK at this stage, we know that the request was accepted according to |
| 4111 | * the http-request rules, we can check for the stats. Note that the |
| 4112 | * URI is detected *before* the req* rules in order not to be affected |
| 4113 | * by a possible reqrep, while they are processed *after* so that a |
| 4114 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4115 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4116 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4117 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4118 | if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4119 | txn->status = 500; |
| 4120 | s->logs.tv_request = now; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4121 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 4122 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4123 | if (!(s->flags & SN_ERR_MASK)) |
| 4124 | s->flags |= SN_ERR_RESOURCE; |
| 4125 | goto return_prx_cond; |
| 4126 | } |
| 4127 | |
| 4128 | /* parse the whole stats request and extract the relevant information */ |
| 4129 | http_handle_stats(s, req); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4130 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn); |
| 4131 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4132 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4133 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4134 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4135 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4136 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4137 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4138 | } |
| 4139 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4140 | /* evaluate the req* rules except reqadd */ |
| 4141 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4142 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4143 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4144 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4145 | if (txn->flags & TX_CLDENY) |
| 4146 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4147 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4148 | if (txn->flags & TX_CLTARPIT) |
| 4149 | goto tarpit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4150 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4151 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4152 | /* add request headers from the rule sets in the same order */ |
| 4153 | list_for_each_entry(wl, &px->req_add, list) { |
| 4154 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 4155 | 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] | 4156 | ret = acl_pass(ret); |
| 4157 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4158 | ret = !ret; |
| 4159 | if (!ret) |
| 4160 | continue; |
| 4161 | } |
| 4162 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4163 | 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] | 4164 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4165 | } |
| 4166 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4167 | |
| 4168 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4169 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4170 | /* process the stats request now */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4171 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4172 | s->fe->fe_counters.intercepted_req++; |
| 4173 | |
| 4174 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 4175 | s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy |
| 4176 | if (!(s->flags & SN_FINST_MASK)) |
| 4177 | s->flags |= SN_FINST_R; |
| 4178 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4179 | /* we may want to compress the stats page */ |
| 4180 | if (s->fe->comp || s->be->comp) |
| 4181 | select_compression_request_header(s, req->buf); |
| 4182 | |
| 4183 | /* 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] | 4184 | 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] | 4185 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4186 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4187 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4188 | /* check whether we have some ACLs set to redirect this request */ |
| 4189 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4190 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4191 | int ret; |
| 4192 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 4193 | 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] | 4194 | ret = acl_pass(ret); |
| 4195 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4196 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4197 | if (!ret) |
| 4198 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4199 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4200 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4201 | goto return_bad_req; |
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 | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4204 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4205 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4206 | * If this happens, then the data will not come immediately, so we must |
| 4207 | * send all what we have without waiting. Note that due to the small gain |
| 4208 | * 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] | 4209 | * 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] | 4210 | * itself once used. |
| 4211 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4212 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4213 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4214 | done: /* done with this analyser, continue with next ones that the calling |
| 4215 | * points will have set, if any. |
| 4216 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4217 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4218 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4219 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4220 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4221 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4222 | tarpit: |
| 4223 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4224 | * which may be the same as the connect timeout if unspecified. |
| 4225 | * If unset, then set it to zero because we really want it to |
| 4226 | * eventually expire. We build the tarpit as an analyser. |
| 4227 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4228 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4229 | |
| 4230 | /* wipe the request out so that we can drop the connection early |
| 4231 | * if the client closes first. |
| 4232 | */ |
| 4233 | channel_dont_connect(req); |
| 4234 | req->analysers = 0; /* remove switching rules etc... */ |
| 4235 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4236 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4237 | if (!req->analyse_exp) |
| 4238 | req->analyse_exp = tick_add(now_ms, 0); |
| 4239 | session_inc_http_err_ctr(s); |
| 4240 | s->fe->fe_counters.denied_req++; |
| 4241 | if (s->fe != s->be) |
| 4242 | s->be->be_counters.denied_req++; |
| 4243 | if (s->listener->counters) |
| 4244 | s->listener->counters->denied_req++; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4245 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4246 | |
| 4247 | deny: /* this request was blocked (denied) */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4248 | txn->flags |= TX_CLDENY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4249 | txn->status = 403; |
| 4250 | s->logs.tv_request = now; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4251 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4252 | session_inc_http_err_ctr(s); |
| 4253 | s->fe->fe_counters.denied_req++; |
| 4254 | if (s->fe != s->be) |
| 4255 | s->be->be_counters.denied_req++; |
| 4256 | if (s->listener->counters) |
| 4257 | s->listener->counters->denied_req++; |
| 4258 | goto return_prx_cond; |
| 4259 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4260 | return_bad_req: |
| 4261 | /* We centralize bad requests processing here */ |
| 4262 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4263 | /* we detected a parsing error. We want to archive this request |
| 4264 | * in the dedicated proxy area for later troubleshooting. |
| 4265 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4266 | 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] | 4267 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4268 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4269 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4270 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4271 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4272 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4273 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4274 | if (s->listener->counters) |
| 4275 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4276 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4277 | return_prx_cond: |
| 4278 | if (!(s->flags & SN_ERR_MASK)) |
| 4279 | s->flags |= SN_ERR_PRXCOND; |
| 4280 | if (!(s->flags & SN_FINST_MASK)) |
| 4281 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4282 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4283 | req->analysers = 0; |
| 4284 | req->analyse_exp = TICK_ETERNITY; |
| 4285 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4286 | |
| 4287 | return_prx_yield: |
| 4288 | channel_dont_connect(req); |
| 4289 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4290 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4291 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4292 | /* This function performs all the processing enabled for the current request. |
| 4293 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4294 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4295 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4296 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4297 | 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] | 4298 | { |
| 4299 | struct http_txn *txn = &s->txn; |
| 4300 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4301 | struct connection *cli_conn = objt_conn(s->si[1].end); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4302 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4303 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4304 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4305 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4306 | return 0; |
| 4307 | } |
| 4308 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4309 | 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] | 4310 | now_ms, __FUNCTION__, |
| 4311 | s, |
| 4312 | req, |
| 4313 | req->rex, req->wex, |
| 4314 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4315 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4316 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4317 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 4318 | if (s->fe->comp || s->be->comp) |
| 4319 | select_compression_request_header(s, req->buf); |
| 4320 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4321 | /* |
| 4322 | * Right now, we know that we have processed the entire headers |
| 4323 | * and that unwanted requests have been filtered out. We can do |
| 4324 | * whatever we want with the remaining request. Also, now we |
| 4325 | * may have separate values for ->fe, ->be. |
| 4326 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4327 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4328 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4329 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4330 | * incoming request. Note that this requires that a connection is |
| 4331 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4332 | */ |
| 4333 | 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] | 4334 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4335 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4336 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4337 | /* Note that for now we don't reuse existing proxy connections */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4338 | if (unlikely((conn = si_alloc_conn(&s->si[1], 0)) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4339 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4340 | txn->status = 500; |
| 4341 | req->analysers = 0; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4342 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4343 | |
| 4344 | if (!(s->flags & SN_ERR_MASK)) |
| 4345 | s->flags |= SN_ERR_RESOURCE; |
| 4346 | if (!(s->flags & SN_FINST_MASK)) |
| 4347 | s->flags |= SN_FINST_R; |
| 4348 | |
| 4349 | return 0; |
| 4350 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4351 | |
| 4352 | path = http_get_path(txn); |
| 4353 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4354 | 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] | 4355 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4356 | /* if the path was found, we have to remove everything between |
| 4357 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4358 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4359 | * u_l characters by a single "/". |
| 4360 | */ |
| 4361 | if (path) { |
| 4362 | char *cur_ptr = req->buf->p; |
| 4363 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4364 | int delta; |
| 4365 | |
| 4366 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4367 | http_msg_move_end(&txn->req, delta); |
| 4368 | cur_end += delta; |
| 4369 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4370 | goto return_bad_req; |
| 4371 | } |
| 4372 | else { |
| 4373 | char *cur_ptr = req->buf->p; |
| 4374 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4375 | int delta; |
| 4376 | |
| 4377 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4378 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4379 | http_msg_move_end(&txn->req, delta); |
| 4380 | cur_end += delta; |
| 4381 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4382 | goto return_bad_req; |
| 4383 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4384 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4385 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4386 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4387 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4388 | * Note that doing so might move headers in the request, but |
| 4389 | * the fields will stay coherent and the URI will not move. |
| 4390 | * This should only be performed in the backend. |
| 4391 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 4392 | 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] | 4393 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 4394 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4395 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4396 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4397 | * 8: the appsession cookie was looked up very early in 1.2, |
| 4398 | * so let's do the same now. |
| 4399 | */ |
| 4400 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4401 | /* It needs to look into the URI unless persistence must be ignored */ |
| 4402 | 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] | 4403 | 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] | 4404 | } |
| 4405 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4406 | /* add unique-id if "header-unique-id" is specified */ |
| 4407 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4408 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 4409 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4410 | goto return_bad_req; |
| 4411 | s->unique_id[0] = '\0'; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4412 | 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] | 4413 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4414 | |
| 4415 | if (s->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4416 | chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 4417 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4418 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4419 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0)) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4420 | goto return_bad_req; |
| 4421 | } |
| 4422 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4423 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4424 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4425 | * asks for it. |
| 4426 | */ |
| 4427 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4428 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4429 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Cyril Bonté | a32d275 | 2012-05-29 23:27:41 +0200 | [diff] [blame] | 4430 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name, |
| 4431 | 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] | 4432 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4433 | /* The header is set to be added only if none is present |
| 4434 | * and we found it, so don't do anything. |
| 4435 | */ |
| 4436 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4437 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4438 | /* Add an X-Forwarded-For header unless the source IP is |
| 4439 | * in the 'except' network range. |
| 4440 | */ |
| 4441 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4442 | (((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] | 4443 | != s->fe->except_net.s_addr) && |
| 4444 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4445 | (((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] | 4446 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4447 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4448 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4449 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4450 | |
| 4451 | /* Note: we rely on the backend to get the header name to be used for |
| 4452 | * x-forwarded-for, because the header is really meant for the backends. |
| 4453 | * However, if the backend did not specify any option, we have to rely |
| 4454 | * on the frontend's header name. |
| 4455 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4456 | if (s->be->fwdfor_hdr_len) { |
| 4457 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4458 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4459 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4460 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4461 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4462 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4463 | 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] | 4464 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4465 | 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] | 4466 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4467 | } |
| 4468 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4469 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4470 | /* FIXME: for the sake of completeness, we should also support |
| 4471 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4472 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4473 | int len; |
| 4474 | char pn[INET6_ADDRSTRLEN]; |
| 4475 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4476 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4477 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4478 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4479 | /* Note: we rely on the backend to get the header name to be used for |
| 4480 | * x-forwarded-for, because the header is really meant for the backends. |
| 4481 | * However, if the backend did not specify any option, we have to rely |
| 4482 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4483 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4484 | if (s->be->fwdfor_hdr_len) { |
| 4485 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4486 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4487 | } else { |
| 4488 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4489 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4490 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4491 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4492 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4493 | 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] | 4494 | goto return_bad_req; |
| 4495 | } |
| 4496 | } |
| 4497 | |
| 4498 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4499 | * 10: add X-Original-To if either the frontend or the backend |
| 4500 | * asks for it. |
| 4501 | */ |
| 4502 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 4503 | |
| 4504 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4505 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4506 | /* Add an X-Original-To header unless the destination IP is |
| 4507 | * in the 'except' network range. |
| 4508 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4509 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4510 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4511 | if (cli_conn->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4512 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4513 | (((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] | 4514 | != s->fe->except_to.s_addr) && |
| 4515 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4516 | (((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] | 4517 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4518 | int len; |
| 4519 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4520 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4521 | |
| 4522 | /* Note: we rely on the backend to get the header name to be used for |
| 4523 | * x-original-to, because the header is really meant for the backends. |
| 4524 | * However, if the backend did not specify any option, we have to rely |
| 4525 | * on the frontend's header name. |
| 4526 | */ |
| 4527 | if (s->be->orgto_hdr_len) { |
| 4528 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4529 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4530 | } else { |
| 4531 | len = s->fe->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4532 | memcpy(trash.str, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4533 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4534 | 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] | 4535 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4536 | 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] | 4537 | goto return_bad_req; |
| 4538 | } |
| 4539 | } |
| 4540 | } |
| 4541 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4542 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4543 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4544 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4545 | * "Upgrade" is present in the Connection header. |
| 4546 | */ |
| 4547 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4548 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4549 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4550 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4551 | unsigned int want_flags = 0; |
| 4552 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4553 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4554 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4555 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4556 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4557 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4558 | want_flags |= TX_CON_CLO_SET; |
| 4559 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4560 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4561 | ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
| 4562 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4563 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4564 | want_flags |= TX_CON_KAL_SET; |
| 4565 | } |
| 4566 | |
| 4567 | 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] | 4568 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4569 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4570 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4571 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4572 | /* If we have no server assigned yet and we're balancing on url_param |
| 4573 | * with a POST request, we may be interested in checking the body for |
| 4574 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4575 | */ |
| 4576 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 4577 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4578 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4579 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4580 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4581 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4582 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4583 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4584 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4585 | #ifdef TCP_QUICKACK |
| 4586 | /* We expect some data from the client. Unless we know for sure |
| 4587 | * we already have a full request, we have to re-enable quick-ack |
| 4588 | * in case we previously disabled it, otherwise we might cause |
| 4589 | * the client to delay further data. |
| 4590 | */ |
| 4591 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4592 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4593 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4594 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4595 | 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] | 4596 | #endif |
| 4597 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4598 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4599 | /************************************************************* |
| 4600 | * OK, that's finished for the headers. We have done what we * |
| 4601 | * could. Let's switch to the DATA state. * |
| 4602 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4603 | req->analyse_exp = TICK_ETERNITY; |
| 4604 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4605 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4606 | /* if the server closes the connection, we want to immediately react |
| 4607 | * and close the socket to save packets and syscalls. |
| 4608 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4609 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4610 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4611 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4612 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4613 | /* OK let's go on with the BODY now */ |
| 4614 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4615 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4616 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4617 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4618 | /* we detected a parsing error. We want to archive this request |
| 4619 | * in the dedicated proxy area for later troubleshooting. |
| 4620 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4621 | 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] | 4622 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4623 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4624 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4625 | txn->status = 400; |
| 4626 | req->analysers = 0; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4627 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4628 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4629 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4630 | if (s->listener->counters) |
| 4631 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4632 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4633 | if (!(s->flags & SN_ERR_MASK)) |
| 4634 | s->flags |= SN_ERR_PRXCOND; |
| 4635 | if (!(s->flags & SN_FINST_MASK)) |
| 4636 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4637 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4638 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4639 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4640 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4641 | * returns zero, at the beginning because it prevents any other processing |
| 4642 | * from occurring, and at the end because it terminates the request. |
| 4643 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4644 | 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] | 4645 | { |
| 4646 | struct http_txn *txn = &s->txn; |
| 4647 | |
| 4648 | /* This connection is being tarpitted. The CLIENT side has |
| 4649 | * already set the connect expiration date to the right |
| 4650 | * timeout. We just have to check that the client is still |
| 4651 | * there and that the timeout has not expired. |
| 4652 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4653 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4654 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4655 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4656 | return 0; |
| 4657 | |
| 4658 | /* We will set the queue timer to the time spent, just for |
| 4659 | * logging purposes. We fake a 500 server error, so that the |
| 4660 | * attacker will not suspect his connection has been tarpitted. |
| 4661 | * It will not cause trouble to the logs because we can exclude |
| 4662 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4663 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4664 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4665 | |
| 4666 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4667 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4668 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4669 | |
| 4670 | req->analysers = 0; |
| 4671 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4672 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4673 | if (!(s->flags & SN_ERR_MASK)) |
| 4674 | s->flags |= SN_ERR_PRXCOND; |
| 4675 | if (!(s->flags & SN_FINST_MASK)) |
| 4676 | s->flags |= SN_FINST_T; |
| 4677 | return 0; |
| 4678 | } |
| 4679 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4680 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4681 | * for either the buffer to be full, or the full advertised contents to have |
| 4682 | * reached the buffer. It must only be called after the standard HTTP request |
| 4683 | * processing has occurred, because it expects the request to be parsed and will |
| 4684 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4685 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4686 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4687 | * 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] | 4688 | */ |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4689 | 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] | 4690 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4691 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4692 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4693 | |
| 4694 | /* We have to parse the HTTP request body to find any required data. |
| 4695 | * "balance url_param check_post" should have been the only way to get |
| 4696 | * into this. We were brought here after HTTP header analysis, so all |
| 4697 | * related structures are ready. |
| 4698 | */ |
| 4699 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4700 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4701 | /* This is the first call */ |
| 4702 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4703 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4704 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4705 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4706 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4707 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4708 | */ |
| 4709 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4710 | struct hdr_ctx ctx; |
| 4711 | ctx.idx = 0; |
| 4712 | /* Expect is allowed in 1.1, look for it */ |
| 4713 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4714 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4715 | bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4716 | } |
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 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4719 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4720 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4721 | /* 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] | 4722 | * req->buf->p still points to the beginning of the message. We |
| 4723 | * must save the body in msg->next because it survives buffer |
| 4724 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4725 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4726 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4727 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4728 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4729 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4730 | else |
| 4731 | msg->msg_state = HTTP_MSG_DATA; |
| 4732 | } |
| 4733 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4734 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4735 | /* We're in content-length mode, we just have to wait for enough data. */ |
| 4736 | if (req->buf->i - msg->sov < msg->body_len) |
| 4737 | goto missing_data; |
| 4738 | |
| 4739 | /* OK we have everything we need now */ |
| 4740 | goto http_end; |
| 4741 | } |
| 4742 | |
| 4743 | /* OK here we're parsing a chunked-encoded message */ |
| 4744 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4745 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4746 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4747 | * 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] | 4748 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4749 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4750 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4751 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4752 | if (!ret) |
| 4753 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4754 | else if (ret < 0) { |
| 4755 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4756 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4757 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4758 | } |
| 4759 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4760 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4761 | * 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] | 4762 | * 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] | 4763 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4764 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4765 | goto http_end; |
| 4766 | |
Willy Tarreau | 226071e | 2014-04-10 11:55:45 +0200 | [diff] [blame] | 4767 | 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] | 4768 | goto http_end; |
| 4769 | |
| 4770 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4771 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4772 | * we have the maximum we can expect. |
| 4773 | */ |
| 4774 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4775 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4776 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4777 | 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] | 4778 | txn->status = 408; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4779 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4780 | |
| 4781 | if (!(s->flags & SN_ERR_MASK)) |
| 4782 | s->flags |= SN_ERR_CLITO; |
| 4783 | if (!(s->flags & SN_FINST_MASK)) |
| 4784 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4785 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4786 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4787 | |
| 4788 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4789 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4790 | /* Not enough data. We'll re-use the http-request |
| 4791 | * timeout here. Ideally, we should set the timeout |
| 4792 | * relative to the accept() date. We just set the |
| 4793 | * request timeout once at the beginning of the |
| 4794 | * request. |
| 4795 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4796 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4797 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4798 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4799 | return 0; |
| 4800 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4801 | |
| 4802 | http_end: |
| 4803 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4804 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4805 | req->analysers &= ~an_bit; |
| 4806 | req->analyse_exp = TICK_ETERNITY; |
| 4807 | return 1; |
| 4808 | |
| 4809 | return_bad_req: /* let's centralize all bad requests */ |
| 4810 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4811 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4812 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4813 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4814 | if (!(s->flags & SN_ERR_MASK)) |
| 4815 | s->flags |= SN_ERR_PRXCOND; |
| 4816 | if (!(s->flags & SN_FINST_MASK)) |
| 4817 | s->flags |= SN_FINST_R; |
| 4818 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4819 | return_err_msg: |
| 4820 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4821 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4822 | if (s->listener->counters) |
| 4823 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4824 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4825 | } |
| 4826 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4827 | /* send a server's name with an outgoing request over an established connection. |
| 4828 | * Note: this function is designed to be called once the request has been scheduled |
| 4829 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4830 | * proceeding. |
| 4831 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4832 | 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] | 4833 | |
| 4834 | struct hdr_ctx ctx; |
| 4835 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4836 | char *hdr_name = be->server_id_hdr_name; |
| 4837 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4838 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4839 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4840 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4841 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4842 | ctx.idx = 0; |
| 4843 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4844 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4845 | if (old_o) { |
| 4846 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4847 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4848 | txn->req.next += old_o; |
| 4849 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4850 | } |
| 4851 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4852 | old_i = chn->buf->i; |
| 4853 | 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] | 4854 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4855 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4859 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4860 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4861 | hdr_val += hdr_name_len; |
| 4862 | *hdr_val++ = ':'; |
| 4863 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4864 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4865 | 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] | 4866 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4867 | if (old_o) { |
| 4868 | /* If this was a forwarded request, we must readjust the amount of |
| 4869 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4870 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4871 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4872 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4873 | old_o += chn->buf->i - old_i; |
| 4874 | b_adv(chn->buf, old_o); |
| 4875 | txn->req.next -= old_o; |
| 4876 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4877 | } |
| 4878 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4879 | return 0; |
| 4880 | } |
| 4881 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4882 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4883 | * right now but it works. |
| 4884 | */ |
| 4885 | void http_end_txn_clean_session(struct session *s) |
| 4886 | { |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4887 | int prev_status = s->txn.status; |
| 4888 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4889 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4890 | * server's connections. We need a safer way to reinitialize buffer |
| 4891 | * flags. We also need a more accurate method for computing per-request |
| 4892 | * data. |
| 4893 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4894 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4895 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4896 | * to the server. |
| 4897 | */ |
| 4898 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4899 | !si_conn_ready(&s->si[1])) { |
| 4900 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4901 | si_shutr(&s->si[1]); |
| 4902 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4903 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4904 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4905 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4906 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4907 | if (unlikely(s->srv_conn)) |
| 4908 | sess_change_server(s, NULL); |
| 4909 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4910 | |
| 4911 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4912 | session_process_counters(s); |
| 4913 | |
| 4914 | if (s->txn.status) { |
| 4915 | int n; |
| 4916 | |
| 4917 | n = s->txn.status / 100; |
| 4918 | if (n < 1 || n > 5) |
| 4919 | n = 0; |
| 4920 | |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4921 | if (s->fe->mode == PR_MODE_HTTP) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4922 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4923 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4924 | s->fe->fe_counters.p.http.comp_rsp++; |
| 4925 | } |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 4926 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4927 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4928 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4929 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4930 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4931 | s->be->be_counters.p.http.comp_rsp++; |
| 4932 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4936 | s->logs.bytes_in -= s->req.buf->i; |
| 4937 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4938 | |
| 4939 | /* let's do a final log if we need it */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 4940 | if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4941 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4942 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4943 | s->do_log(s); |
| 4944 | } |
| 4945 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 4946 | /* stop tracking content-based counters */ |
| 4947 | session_stop_content_counters(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 4948 | session_update_time_stats(s); |
| 4949 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4950 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4951 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4952 | tv_zero(&s->logs.tv_request); |
| 4953 | s->logs.t_queue = -1; |
| 4954 | s->logs.t_connect = -1; |
| 4955 | s->logs.t_data = -1; |
| 4956 | s->logs.t_close = 0; |
| 4957 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4958 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4959 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4960 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 4961 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4962 | |
| 4963 | if (s->pend_pos) |
| 4964 | pendconn_free(s->pend_pos); |
| 4965 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4966 | if (objt_server(s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4967 | if (s->flags & SN_CURR_SESS) { |
| 4968 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4969 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4970 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4971 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 4972 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4973 | } |
| 4974 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4975 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4976 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4977 | /* only release our endpoint if we don't intend to reuse the |
| 4978 | * connection. |
| 4979 | */ |
| 4980 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4981 | !si_conn_ready(&s->si[1])) { |
| 4982 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4983 | } |
| 4984 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4985 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 4986 | s->si[1].err_type = SI_ET_NONE; |
| 4987 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 4988 | s->si[1].exp = TICK_ETERNITY; |
| 4989 | s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_session */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4990 | 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); |
| 4991 | s->res.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] | 4992 | 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] | 4993 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED); |
Cyril Bonté | a83a50b | 2014-10-22 22:30:13 +0200 | [diff] [blame] | 4994 | s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4995 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4996 | s->txn.meth = 0; |
| 4997 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4998 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4999 | |
| 5000 | if (prev_status == 401 || prev_status == 407) { |
| 5001 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 5002 | * a chance of being able to send the visitor again to the same |
| 5003 | * server over the same connection. This is required by some |
| 5004 | * broken protocols such as NTLM, and anyway whenever there is |
| 5005 | * an opportunity for sending the challenge to the proper place, |
| 5006 | * it's better to do it (at least it helps with debugging). |
| 5007 | */ |
| 5008 | s->txn.flags |= TX_PREFER_LAST; |
| 5009 | } |
| 5010 | |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 5011 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5012 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5013 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 5014 | if (s->fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5015 | s->req.flags |= CF_NEVER_WAIT; |
| 5016 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 5017 | } |
| 5018 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5019 | /* if the request buffer is not empty, it means we're |
| 5020 | * about to process another request, so send pending |
| 5021 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5022 | * Just don't do this if the buffer is close to be full, |
| 5023 | * because the request will wait for it to flush a little |
| 5024 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5025 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5026 | if (s->req.buf->i) { |
| 5027 | if (s->res.buf->o && |
| 5028 | !buffer_full(s->res.buf, global.tune.maxrewrite) && |
| 5029 | bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite) |
| 5030 | s->res.flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5031 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5032 | |
| 5033 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5034 | channel_auto_read(&s->req); |
| 5035 | channel_auto_close(&s->req); |
| 5036 | channel_auto_read(&s->res); |
| 5037 | channel_auto_close(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5038 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5039 | /* we're in keep-alive with an idle connection, monitor it */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5040 | si_idle_conn(&s->si[1]); |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5041 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5042 | s->req.analysers = s->listener->analysers; |
| 5043 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5044 | } |
| 5045 | |
| 5046 | |
| 5047 | /* This function updates the request state machine according to the response |
| 5048 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5049 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5050 | * it is only used to find when a request/response couple is complete. Both |
| 5051 | * this function and its equivalent should loop until both return zero. It |
| 5052 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5053 | */ |
| 5054 | int http_sync_req_state(struct session *s) |
| 5055 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5056 | struct channel *chn = &s->req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5057 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5058 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5059 | unsigned int old_state = txn->req.msg_state; |
| 5060 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5061 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 5062 | return 0; |
| 5063 | |
| 5064 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5065 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5066 | * We can shut the read side unless we want to abort_on_close, |
| 5067 | * or we have a POST request. The issue with POST requests is |
| 5068 | * that some browsers still send a CRLF after the request, and |
| 5069 | * this CRLF must be read so that it does not remain in the kernel |
| 5070 | * buffers, otherwise a close could cause an RST on some systems |
| 5071 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5072 | * Note that if we're using keep-alive on the client side, we'd |
| 5073 | * rather poll now and keep the polling enabled for the whole |
| 5074 | * session's life than enabling/disabling it between each |
| 5075 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5076 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5077 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5078 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5079 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 5080 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5081 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5082 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5083 | /* if the server closes the connection, we want to immediately react |
| 5084 | * and close the socket to save packets and syscalls. |
| 5085 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5086 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5087 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5088 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5089 | goto wait_other_side; |
| 5090 | |
| 5091 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5092 | /* The server has not finished to respond, so we |
| 5093 | * don't want to move in order not to upset it. |
| 5094 | */ |
| 5095 | goto wait_other_side; |
| 5096 | } |
| 5097 | |
| 5098 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5099 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5100 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5101 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5102 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5103 | goto wait_other_side; |
| 5104 | } |
| 5105 | |
| 5106 | /* When we get here, it means that both the request and the |
| 5107 | * response have finished receiving. Depending on the connection |
| 5108 | * mode, we'll have to wait for the last bytes to leave in either |
| 5109 | * direction, and sometimes for a close to be effective. |
| 5110 | */ |
| 5111 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5112 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5113 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5114 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5115 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5116 | } |
| 5117 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5118 | /* Option forceclose is set, or either side wants to close, |
| 5119 | * let's enforce it now that we're not expecting any new |
| 5120 | * data to come. The caller knows the session is complete |
| 5121 | * once both states are CLOSED. |
| 5122 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5123 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5124 | channel_shutr_now(chn); |
| 5125 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5126 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5127 | } |
| 5128 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5129 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5130 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5131 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5132 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5133 | channel_auto_read(chn); |
| 5134 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5135 | chn->flags |= CF_NEVER_WAIT; |
| 5136 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5137 | } |
| 5138 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5139 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5140 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5141 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5142 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5143 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5144 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5145 | goto http_msg_closing; |
| 5146 | } |
| 5147 | else { |
| 5148 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5149 | goto http_msg_closed; |
| 5150 | } |
| 5151 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5152 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5153 | } |
| 5154 | |
| 5155 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5156 | http_msg_closing: |
| 5157 | /* nothing else to forward, just waiting for the output buffer |
| 5158 | * to be empty and for the shutw_now to take effect. |
| 5159 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5160 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5161 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5162 | goto http_msg_closed; |
| 5163 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5164 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5165 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5166 | goto wait_other_side; |
| 5167 | } |
| 5168 | } |
| 5169 | |
| 5170 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5171 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5172 | /* see above in MSG_DONE why we only do this in these states */ |
| 5173 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5174 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5175 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5176 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5177 | goto wait_other_side; |
| 5178 | } |
| 5179 | |
| 5180 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5181 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5182 | } |
| 5183 | |
| 5184 | |
| 5185 | /* This function updates the response state machine according to the request |
| 5186 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5187 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5188 | * it is only used to find when a request/response couple is complete. Both |
| 5189 | * this function and its equivalent should loop until both return zero. It |
| 5190 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5191 | */ |
| 5192 | int http_sync_res_state(struct session *s) |
| 5193 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5194 | struct channel *chn = &s->res; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5195 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5196 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5197 | unsigned int old_state = txn->rsp.msg_state; |
| 5198 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5199 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5200 | return 0; |
| 5201 | |
| 5202 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5203 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5204 | * still monitor the server connection for a possible close |
| 5205 | * while the request is being uploaded, so we don't disable |
| 5206 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5207 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5208 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5209 | |
| 5210 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5211 | goto wait_other_side; |
| 5212 | |
| 5213 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5214 | /* The client seems to still be sending data, probably |
| 5215 | * because we got an error response during an upload. |
| 5216 | * We have the choice of either breaking the connection |
| 5217 | * or letting it pass through. Let's do the later. |
| 5218 | */ |
| 5219 | goto wait_other_side; |
| 5220 | } |
| 5221 | |
| 5222 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5223 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5224 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5225 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5226 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5227 | goto wait_other_side; |
| 5228 | } |
| 5229 | |
| 5230 | /* When we get here, it means that both the request and the |
| 5231 | * response have finished receiving. Depending on the connection |
| 5232 | * mode, we'll have to wait for the last bytes to leave in either |
| 5233 | * direction, and sometimes for a close to be effective. |
| 5234 | */ |
| 5235 | |
| 5236 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5237 | /* Server-close mode : shut read and wait for the request |
| 5238 | * side to close its output buffer. The caller will detect |
| 5239 | * when we're in DONE and the other is in CLOSED and will |
| 5240 | * catch that for the final cleanup. |
| 5241 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5242 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5243 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5244 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5245 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5246 | /* Option forceclose is set, or either side wants to close, |
| 5247 | * let's enforce it now that we're not expecting any new |
| 5248 | * data to come. The caller knows the session is complete |
| 5249 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5250 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5251 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5252 | channel_shutr_now(chn); |
| 5253 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5254 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5255 | } |
| 5256 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5257 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5258 | * need to forward remaining data. Keep-alive will need to monitor |
| 5259 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5260 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5261 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5262 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5263 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5264 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5265 | } |
| 5266 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5267 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5268 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5269 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5270 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5271 | goto http_msg_closing; |
| 5272 | } |
| 5273 | else { |
| 5274 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5275 | goto http_msg_closed; |
| 5276 | } |
| 5277 | } |
| 5278 | goto wait_other_side; |
| 5279 | } |
| 5280 | |
| 5281 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5282 | http_msg_closing: |
| 5283 | /* nothing else to forward, just waiting for the output buffer |
| 5284 | * to be empty and for the shutw_now to take effect. |
| 5285 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5286 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5287 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5288 | goto http_msg_closed; |
| 5289 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5290 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5291 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5292 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5293 | if (objt_server(s->target)) |
| 5294 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5295 | goto wait_other_side; |
| 5296 | } |
| 5297 | } |
| 5298 | |
| 5299 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5300 | http_msg_closed: |
| 5301 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5302 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5303 | channel_auto_close(chn); |
| 5304 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5305 | goto wait_other_side; |
| 5306 | } |
| 5307 | |
| 5308 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5309 | /* We force the response to leave immediately if we're waiting for the |
| 5310 | * other side, since there is no pending shutdown to push it out. |
| 5311 | */ |
| 5312 | if (!channel_is_empty(chn)) |
| 5313 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5314 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5315 | } |
| 5316 | |
| 5317 | |
| 5318 | /* Resync the request and response state machines. Return 1 if either state |
| 5319 | * changes. |
| 5320 | */ |
| 5321 | int http_resync_states(struct session *s) |
| 5322 | { |
| 5323 | struct http_txn *txn = &s->txn; |
| 5324 | int old_req_state = txn->req.msg_state; |
| 5325 | int old_res_state = txn->rsp.msg_state; |
| 5326 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5327 | http_sync_req_state(s); |
| 5328 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5329 | if (!http_sync_res_state(s)) |
| 5330 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5331 | if (!http_sync_req_state(s)) |
| 5332 | break; |
| 5333 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5334 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5335 | /* OK, both state machines agree on a compatible state. |
| 5336 | * There are a few cases we're interested in : |
| 5337 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5338 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5339 | * directions, so let's simply disable both analysers. |
| 5340 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5341 | * request. |
| 5342 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5343 | * with server-close mode means we've completed one request and we |
| 5344 | * must re-initialize the server connection. |
| 5345 | */ |
| 5346 | |
| 5347 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5348 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5349 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5350 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5351 | s->req.analysers = 0; |
| 5352 | channel_auto_close(&s->req); |
| 5353 | channel_auto_read(&s->req); |
| 5354 | s->res.analysers = 0; |
| 5355 | channel_auto_close(&s->res); |
| 5356 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5357 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5358 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5359 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5360 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5361 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5362 | s->res.analysers = 0; |
| 5363 | channel_auto_close(&s->res); |
| 5364 | channel_auto_read(&s->res); |
| 5365 | s->req.analysers = 0; |
| 5366 | channel_abort(&s->req); |
| 5367 | channel_auto_close(&s->req); |
| 5368 | channel_auto_read(&s->req); |
| 5369 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5370 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5371 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5372 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5373 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5374 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5375 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5376 | /* server-close/keep-alive: terminate this transaction, |
| 5377 | * possibly killing the server connection and reinitialize |
| 5378 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5379 | */ |
| 5380 | http_end_txn_clean_session(s); |
| 5381 | } |
| 5382 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5383 | return txn->req.msg_state != old_req_state || |
| 5384 | txn->rsp.msg_state != old_res_state; |
| 5385 | } |
| 5386 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5387 | /* This function is an analyser which forwards request body (including chunk |
| 5388 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5389 | * zero byte. The only situation where it must not be called is when we're in |
| 5390 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5391 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5392 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5393 | * 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] | 5394 | * 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] | 5395 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5396 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5397 | 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] | 5398 | { |
| 5399 | struct http_txn *txn = &s->txn; |
| 5400 | struct http_msg *msg = &s->txn.req; |
| 5401 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5402 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5403 | return 0; |
| 5404 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5405 | 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] | 5406 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5407 | /* Output closed while we were sending data. We must abort and |
| 5408 | * wake the other side up. |
| 5409 | */ |
| 5410 | msg->msg_state = HTTP_MSG_ERROR; |
| 5411 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5412 | return 1; |
| 5413 | } |
| 5414 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5415 | /* Note that we don't have to send 100-continue back because we don't |
| 5416 | * need the data to complete our job, and it's up to the server to |
| 5417 | * decide whether to return 100, 417 or anything else in return of |
| 5418 | * an "Expect: 100-continue" header. |
| 5419 | */ |
| 5420 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5421 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5422 | /* we have msg->sov which points to the first byte of message |
| 5423 | * body, and req->buf.p still points to the beginning of the |
| 5424 | * message. We forward the headers now, as we don't need them |
| 5425 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5426 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5427 | b_adv(req->buf, msg->sov); |
| 5428 | msg->next -= msg->sov; |
| 5429 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5430 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5431 | /* The previous analysers guarantee that the state is somewhere |
| 5432 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 5433 | * msg->next are always correct. |
| 5434 | */ |
| 5435 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5436 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 5437 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5438 | else |
| 5439 | msg->msg_state = HTTP_MSG_DATA; |
| 5440 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5441 | } |
| 5442 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5443 | /* Some post-connect processing might want us to refrain from starting to |
| 5444 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5445 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5446 | */ |
| 5447 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5448 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5449 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5450 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5451 | goto missing_data; |
| 5452 | } |
| 5453 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5454 | } |
| 5455 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5456 | /* in most states, we should abort in case of early close */ |
| 5457 | channel_auto_close(req); |
| 5458 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5459 | if (req->to_forward) { |
| 5460 | /* We can't process the buffer's contents yet */ |
| 5461 | req->flags |= CF_WAKE_WRITE; |
| 5462 | goto missing_data; |
| 5463 | } |
| 5464 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5465 | while (1) { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5466 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5467 | /* must still forward */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5468 | /* we may have some pending data starting at req->buf->p */ |
| 5469 | if (msg->chunk_len > req->buf->i - msg->next) { |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5470 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5471 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5472 | } |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5473 | msg->next += msg->chunk_len; |
| 5474 | msg->chunk_len = 0; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5475 | |
| 5476 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5477 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5478 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5479 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5480 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5481 | } |
| 5482 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5483 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 5484 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5485 | * TRAILERS state. |
| 5486 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5487 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5488 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5489 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5490 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5491 | else if (ret < 0) { |
| 5492 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5493 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5494 | 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] | 5495 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5496 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5497 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5498 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5499 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5500 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5501 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5502 | |
| 5503 | if (ret == 0) |
| 5504 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5505 | else if (ret < 0) { |
| 5506 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5507 | if (msg->err_pos >= 0) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5508 | 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] | 5509 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5510 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5511 | /* we're in MSG_CHUNK_SIZE now */ |
| 5512 | } |
| 5513 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5514 | int ret = http_forward_trailers(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 | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5521 | 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] | 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 HTTP_MSG_DONE now */ |
| 5525 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5526 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5527 | int old_state = msg->msg_state; |
| 5528 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5529 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5530 | |
| 5531 | /* we may have some pending data starting at req->buf->p |
| 5532 | * such as last chunk of data or trailers. |
| 5533 | */ |
| 5534 | b_adv(req->buf, msg->next); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5535 | if (unlikely(!(s->req.flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5536 | msg->sov -= msg->next; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5537 | msg->next = 0; |
| 5538 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5539 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5540 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5541 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5542 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5543 | if (http_resync_states(s)) { |
| 5544 | /* some state changes occurred, maybe the analyser |
| 5545 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 5546 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5547 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5548 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5549 | /* request errors are most likely due to |
| 5550 | * the server aborting the transfer. |
| 5551 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5552 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5553 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5554 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5555 | 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] | 5556 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5557 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5558 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5559 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5560 | |
| 5561 | /* If "option abortonclose" is set on the backend, we |
| 5562 | * want to monitor the client's connection and forward |
| 5563 | * any shutdown notification to the server, which will |
| 5564 | * decide whether to close or to go on processing the |
| 5565 | * request. |
| 5566 | */ |
| 5567 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5568 | channel_auto_read(req); |
| 5569 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5570 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5571 | else if (s->txn.meth == HTTP_METH_POST) { |
| 5572 | /* POST requests may require to read extra CRLF |
| 5573 | * sent by broken browsers and which could cause |
| 5574 | * an RST to be sent upon close on some systems |
| 5575 | * (eg: Linux). |
| 5576 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5577 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5578 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5579 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5580 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5581 | } |
| 5582 | } |
| 5583 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5584 | missing_data: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5585 | /* we may have some pending data starting at req->buf->p */ |
| 5586 | b_adv(req->buf, msg->next); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5587 | if (unlikely(!(s->req.flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5588 | msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i); |
| 5589 | |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5590 | msg->next = 0; |
| 5591 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
| 5592 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5593 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5594 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5595 | if (!(s->flags & SN_ERR_MASK)) |
| 5596 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5597 | if (!(s->flags & SN_FINST_MASK)) { |
| 5598 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5599 | s->flags |= SN_FINST_H; |
| 5600 | else |
| 5601 | s->flags |= SN_FINST_D; |
| 5602 | } |
| 5603 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5604 | s->fe->fe_counters.cli_aborts++; |
| 5605 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5606 | if (objt_server(s->target)) |
| 5607 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5608 | |
| 5609 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5610 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5611 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5612 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5613 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5614 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5615 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5616 | /* 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] | 5617 | * 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] | 5618 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5619 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5620 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5621 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5622 | /* 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] | 5623 | * 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] | 5624 | * 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] | 5625 | * modes are already handled by the stream sock layer. We must not do |
| 5626 | * this in content-length mode because it could present the MSG_MORE |
| 5627 | * flag with the last block of forwarded data, which would cause an |
| 5628 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5629 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5630 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5631 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5632 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5633 | return 0; |
| 5634 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5635 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5636 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5637 | if (s->listener->counters) |
| 5638 | s->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5639 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5640 | return_bad_req_stats_ok: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5641 | /* we may have some pending data starting at req->buf->p */ |
| 5642 | b_adv(req->buf, msg->next); |
| 5643 | msg->next = 0; |
| 5644 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5645 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5646 | if (txn->status) { |
| 5647 | /* Note: we don't send any error if some data were already sent */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5648 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5649 | } else { |
| 5650 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5651 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5652 | } |
| 5653 | req->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5654 | s->res.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] | 5655 | |
| 5656 | if (!(s->flags & SN_ERR_MASK)) |
| 5657 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5658 | if (!(s->flags & SN_FINST_MASK)) { |
| 5659 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5660 | s->flags |= SN_FINST_H; |
| 5661 | else |
| 5662 | s->flags |= SN_FINST_D; |
| 5663 | } |
| 5664 | return 0; |
| 5665 | |
| 5666 | aborted_xfer: |
| 5667 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5668 | if (txn->status) { |
| 5669 | /* Note: we don't send any error if some data were already sent */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5670 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5671 | } else { |
| 5672 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5673 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5674 | } |
| 5675 | req->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5676 | s->res.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5677 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5678 | s->fe->fe_counters.srv_aborts++; |
| 5679 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5680 | if (objt_server(s->target)) |
| 5681 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5682 | |
| 5683 | if (!(s->flags & SN_ERR_MASK)) |
| 5684 | s->flags |= SN_ERR_SRVCL; |
| 5685 | if (!(s->flags & SN_FINST_MASK)) { |
| 5686 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5687 | s->flags |= SN_FINST_H; |
| 5688 | else |
| 5689 | s->flags |= SN_FINST_D; |
| 5690 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5691 | return 0; |
| 5692 | } |
| 5693 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5694 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5695 | * processing can continue on next analysers, or zero if it either needs more |
| 5696 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5697 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5698 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5699 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5700 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5701 | 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] | 5702 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5703 | struct http_txn *txn = &s->txn; |
| 5704 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5705 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5706 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5707 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5708 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5709 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5710 | 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] | 5711 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5712 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5713 | rep, |
| 5714 | rep->rex, rep->wex, |
| 5715 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5716 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5717 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5718 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5719 | /* |
| 5720 | * Now parse the partial (or complete) lines. |
| 5721 | * We will check the response syntax, and also join multi-line |
| 5722 | * headers. An index of all the lines will be elaborated while |
| 5723 | * parsing. |
| 5724 | * |
| 5725 | * For the parsing, we use a 28 states FSM. |
| 5726 | * |
| 5727 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5728 | * rep->buf->p = beginning of response |
| 5729 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5730 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5731 | * msg->eol = end of current header or line (LF or CRLF) |
| 5732 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5733 | */ |
| 5734 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5735 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5736 | /* There's a protected area at the end of the buffer for rewriting |
| 5737 | * purposes. We don't want to start to parse the request if the |
| 5738 | * protected area is affected, because we may have to move processed |
| 5739 | * data later, which is much more complicated. |
| 5740 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5741 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5742 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5743 | /* some data has still not left the buffer, wake us once that's done */ |
| 5744 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5745 | goto abort_response; |
| 5746 | channel_dont_close(rep); |
| 5747 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5748 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5749 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5750 | } |
| 5751 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5752 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5753 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5754 | buffer_slow_realign(rep->buf); |
| 5755 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5756 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5757 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5758 | } |
| 5759 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5760 | /* 1: we might have to print this header in debug mode */ |
| 5761 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5762 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5763 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5764 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5765 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5766 | sol = rep->buf->p; |
| 5767 | 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] | 5768 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5769 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5770 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5771 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5772 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5773 | while (cur_idx) { |
| 5774 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5775 | debug_hdr("srvhdr", s, sol, eol); |
| 5776 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5777 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5778 | } |
| 5779 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5780 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5781 | /* |
| 5782 | * Now we quickly check if we have found a full valid response. |
| 5783 | * If not so, we check the FD and buffer states before leaving. |
| 5784 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5785 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5786 | * responses are checked first. |
| 5787 | * |
| 5788 | * Depending on whether the client is still there or not, we |
| 5789 | * may send an error response back or not. Note that normally |
| 5790 | * we should only check for HTTP status there, and check I/O |
| 5791 | * errors somewhere else. |
| 5792 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5793 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5794 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5795 | /* Invalid response */ |
| 5796 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5797 | /* we detected a parsing error. We want to archive this response |
| 5798 | * in the dedicated proxy area for later troubleshooting. |
| 5799 | */ |
| 5800 | hdr_response_bad: |
| 5801 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5802 | 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] | 5803 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5804 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5805 | if (objt_server(s->target)) { |
| 5806 | objt_server(s->target)->counters.failed_resp++; |
| 5807 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5808 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5809 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5810 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5811 | rep->analysers = 0; |
| 5812 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5813 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5814 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5815 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5816 | |
| 5817 | if (!(s->flags & SN_ERR_MASK)) |
| 5818 | s->flags |= SN_ERR_PRXCOND; |
| 5819 | if (!(s->flags & SN_FINST_MASK)) |
| 5820 | s->flags |= SN_FINST_H; |
| 5821 | |
| 5822 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5823 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5824 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5825 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5826 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5827 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5828 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5829 | goto hdr_response_bad; |
| 5830 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5831 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5832 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5833 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5834 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5835 | 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] | 5836 | else if (txn->flags & TX_NOT_FIRST) |
| 5837 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5838 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5839 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5840 | if (objt_server(s->target)) { |
| 5841 | objt_server(s->target)->counters.failed_resp++; |
| 5842 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5843 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5844 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5845 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5846 | rep->analysers = 0; |
| 5847 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5848 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5849 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5850 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5851 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5852 | if (!(s->flags & SN_ERR_MASK)) |
| 5853 | s->flags |= SN_ERR_SRVCL; |
| 5854 | if (!(s->flags & SN_FINST_MASK)) |
| 5855 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5856 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5857 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5858 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 5859 | /* read timeout : return a 504 to the client. */ |
| 5860 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5861 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5862 | 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] | 5863 | else if (txn->flags & TX_NOT_FIRST) |
| 5864 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5865 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5866 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5867 | if (objt_server(s->target)) { |
| 5868 | objt_server(s->target)->counters.failed_resp++; |
| 5869 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5870 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5871 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5872 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5873 | rep->analysers = 0; |
| 5874 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5875 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5876 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5877 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5878 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5879 | if (!(s->flags & SN_ERR_MASK)) |
| 5880 | s->flags |= SN_ERR_SRVTO; |
| 5881 | if (!(s->flags & SN_FINST_MASK)) |
| 5882 | s->flags |= SN_FINST_H; |
| 5883 | return 0; |
| 5884 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5885 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5886 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5887 | else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5888 | s->fe->fe_counters.cli_aborts++; |
| 5889 | s->be->be_counters.cli_aborts++; |
| 5890 | if (objt_server(s->target)) |
| 5891 | objt_server(s->target)->counters.cli_aborts++; |
| 5892 | |
| 5893 | rep->analysers = 0; |
| 5894 | channel_auto_close(rep); |
| 5895 | |
| 5896 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5897 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5898 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5899 | |
| 5900 | if (!(s->flags & SN_ERR_MASK)) |
| 5901 | s->flags |= SN_ERR_CLICL; |
| 5902 | if (!(s->flags & SN_FINST_MASK)) |
| 5903 | s->flags |= SN_FINST_H; |
| 5904 | |
| 5905 | /* process_session() will take care of the error */ |
| 5906 | return 0; |
| 5907 | } |
| 5908 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5909 | /* 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] | 5910 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5911 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5912 | 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] | 5913 | else if (txn->flags & TX_NOT_FIRST) |
| 5914 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5915 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5916 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5917 | if (objt_server(s->target)) { |
| 5918 | objt_server(s->target)->counters.failed_resp++; |
| 5919 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5920 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5921 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5922 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5923 | rep->analysers = 0; |
| 5924 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5925 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5926 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5927 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5928 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5929 | if (!(s->flags & SN_ERR_MASK)) |
| 5930 | s->flags |= SN_ERR_SRVCL; |
| 5931 | if (!(s->flags & SN_FINST_MASK)) |
| 5932 | s->flags |= SN_FINST_H; |
| 5933 | return 0; |
| 5934 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5935 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5936 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5937 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5938 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5939 | 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] | 5940 | else if (txn->flags & TX_NOT_FIRST) |
| 5941 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5942 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5943 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5944 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5945 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5946 | |
| 5947 | if (!(s->flags & SN_ERR_MASK)) |
| 5948 | s->flags |= SN_ERR_CLICL; |
| 5949 | if (!(s->flags & SN_FINST_MASK)) |
| 5950 | s->flags |= SN_FINST_H; |
| 5951 | |
| 5952 | /* process_session() will take care of the error */ |
| 5953 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5954 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5955 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5956 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5957 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5958 | return 0; |
| 5959 | } |
| 5960 | |
| 5961 | /* More interesting part now : we know that we have a complete |
| 5962 | * response which at least looks like HTTP. We have an indicator |
| 5963 | * of each header's length, so we can parse them quickly. |
| 5964 | */ |
| 5965 | |
| 5966 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5967 | 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] | 5968 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5969 | /* |
| 5970 | * 1: get the status code |
| 5971 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5972 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5973 | if (n < 1 || n > 5) |
| 5974 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5975 | /* when the client triggers a 4xx from the server, it's most often due |
| 5976 | * to a missing object or permission. These events should be tracked |
| 5977 | * because if they happen often, it may indicate a brute force or a |
| 5978 | * vulnerability scan. |
| 5979 | */ |
| 5980 | if (n == 4) |
| 5981 | session_inc_http_err_ctr(s); |
| 5982 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5983 | if (objt_server(s->target)) |
| 5984 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5985 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5986 | /* check if the response is HTTP/1.1 or above */ |
| 5987 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5988 | ((rep->buf->p[5] > '1') || |
| 5989 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5990 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5991 | |
| 5992 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5993 | 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] | 5994 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5995 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5996 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5997 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5998 | 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] | 5999 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 6000 | /* Adjust server's health based on status code. Note: status codes 501 |
| 6001 | * and 505 are triggered on demand by client request, so we must not |
| 6002 | * count them as server failures. |
| 6003 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6004 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6005 | 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] | 6006 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6007 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6008 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6009 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6010 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6011 | /* |
| 6012 | * 2: check for cacheability. |
| 6013 | */ |
| 6014 | |
| 6015 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6016 | case 100: |
| 6017 | /* |
| 6018 | * We may be facing a 100-continue response, in which case this |
| 6019 | * is not the right response, and we're waiting for the next one. |
| 6020 | * Let's allow this response to go to the client and wait for the |
| 6021 | * next one. |
| 6022 | */ |
| 6023 | hdr_idx_init(&txn->hdr_idx); |
| 6024 | msg->next -= channel_forward(rep, msg->next); |
| 6025 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 6026 | txn->status = 0; |
| 6027 | s->logs.t_data = -1; /* was not a response yet */ |
| 6028 | goto next_one; |
| 6029 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6030 | case 200: |
| 6031 | case 203: |
| 6032 | case 206: |
| 6033 | case 300: |
| 6034 | case 301: |
| 6035 | case 410: |
| 6036 | /* RFC2616 @13.4: |
| 6037 | * "A response received with a status code of |
| 6038 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 6039 | * by a cache (...) unless a cache-control |
| 6040 | * directive prohibits caching." |
| 6041 | * |
| 6042 | * RFC2616 @9.5: POST method : |
| 6043 | * "Responses to this method are not cacheable, |
| 6044 | * unless the response includes appropriate |
| 6045 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6046 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6047 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6048 | ((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] | 6049 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 6050 | break; |
| 6051 | default: |
| 6052 | break; |
| 6053 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6054 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6055 | /* |
| 6056 | * 3: we may need to capture headers |
| 6057 | */ |
| 6058 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 6059 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6060 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6061 | txn->rsp.cap, s->fe->rsp_cap); |
| 6062 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6063 | /* 4: determine the transfer-length. |
| 6064 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 6065 | * the presence of a message-body in a RESPONSE and its transfer length |
| 6066 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6067 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6068 | * All responses to the HEAD request method MUST NOT include a |
| 6069 | * message-body, even though the presence of entity-header fields |
| 6070 | * might lead one to believe they do. All 1xx (informational), 204 |
| 6071 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 6072 | * message-body. All other responses do include a message-body, |
| 6073 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6074 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6075 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 6076 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 6077 | * always terminated by the first empty line after the header fields, |
| 6078 | * regardless of the entity-header fields present in the message. |
| 6079 | * |
| 6080 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 6081 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 6082 | * transfer-length is defined by the use of this transfer-coding. |
| 6083 | * If a Transfer-Encoding header field is present and the "chunked" |
| 6084 | * transfer-coding is not present, the transfer-length is defined by |
| 6085 | * the sender closing the connection. |
| 6086 | * |
| 6087 | * 3. If a Content-Length header field is present, its decimal value in |
| 6088 | * OCTETs represents both the entity-length and the transfer-length. |
| 6089 | * If a message is received with both a Transfer-Encoding header |
| 6090 | * field and a Content-Length header field, the latter MUST be ignored. |
| 6091 | * |
| 6092 | * 4. If the message uses the media type "multipart/byteranges", and |
| 6093 | * the transfer-length is not otherwise specified, then this self- |
| 6094 | * delimiting media type defines the transfer-length. This media |
| 6095 | * type MUST NOT be used unless the sender knows that the recipient |
| 6096 | * can parse it; the presence in a request of a Range header with |
| 6097 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 6098 | * client can parse multipart/byteranges responses. |
| 6099 | * |
| 6100 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6101 | */ |
| 6102 | |
| 6103 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6104 | * 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] | 6105 | * 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] | 6106 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6107 | */ |
| 6108 | if (txn->meth == HTTP_METH_HEAD || |
| 6109 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6110 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6111 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 6112 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6113 | goto skip_content_length; |
| 6114 | } |
| 6115 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6116 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6117 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6118 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6119 | 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] | 6120 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6121 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6122 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6123 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6124 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6125 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6126 | break; |
| 6127 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6128 | } |
| 6129 | |
| 6130 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 6131 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6132 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6133 | 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] | 6134 | signed long long cl; |
| 6135 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6136 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6137 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6138 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6139 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6140 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6141 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6142 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6143 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6144 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6145 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6146 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6147 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6148 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6149 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6150 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6151 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6152 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6153 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6154 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6155 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6156 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6157 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6158 | } |
| 6159 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6160 | if (s->fe->comp || s->be->comp) |
| 6161 | select_compression_response_header(s, rep->buf); |
| 6162 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6163 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6164 | /* Now we have to check if we need to modify the Connection header. |
| 6165 | * This is more difficult on the response than it is on the request, |
| 6166 | * because we can have two different HTTP versions and we don't know |
| 6167 | * how the client will interprete a response. For instance, let's say |
| 6168 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6169 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6170 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6171 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6172 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6173 | * indicating how a request MAY be understood by the client. In case |
| 6174 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6175 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6176 | * will fall back to explicit close. Note that we won't take risks with |
| 6177 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6178 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6179 | */ |
| 6180 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6181 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6182 | txn->status == 101)) { |
| 6183 | /* Either we've established an explicit tunnel, or we're |
| 6184 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6185 | * to understand the next protocols. We have to switch to tunnel |
| 6186 | * mode, so that we transfer the request and responses then let |
| 6187 | * this protocol pass unmodified. When we later implement specific |
| 6188 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6189 | * header which contains information about that protocol for |
| 6190 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6191 | */ |
| 6192 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6193 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6194 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6195 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6196 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6197 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6198 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6199 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6200 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6201 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6202 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6203 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6204 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6205 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6206 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6207 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6208 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6209 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6210 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6211 | /* now adjust header transformations depending on current state */ |
| 6212 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6213 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6214 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6215 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6216 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6217 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6218 | else { /* SCL / KAL */ |
| 6219 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6220 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6221 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6222 | } |
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 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6225 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6226 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6227 | /* Some keep-alive responses are converted to Server-close if |
| 6228 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6229 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6230 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6231 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6232 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6233 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6234 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6235 | } |
| 6236 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6237 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6238 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6239 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6240 | /* end of job, return OK */ |
| 6241 | rep->analysers &= ~an_bit; |
| 6242 | rep->analyse_exp = TICK_ETERNITY; |
| 6243 | channel_auto_close(rep); |
| 6244 | return 1; |
| 6245 | |
| 6246 | abort_keep_alive: |
| 6247 | /* A keep-alive request to the server failed on a network error. |
| 6248 | * The client is required to retry. We need to close without returning |
| 6249 | * any other information so that the client retries. |
| 6250 | */ |
| 6251 | txn->status = 0; |
| 6252 | rep->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6253 | s->req.analysers = 0; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6254 | channel_auto_close(rep); |
| 6255 | s->logs.logwait = 0; |
| 6256 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6257 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6258 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6259 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6260 | return 0; |
| 6261 | } |
| 6262 | |
| 6263 | /* This function performs all the processing enabled for the current response. |
| 6264 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6265 | * and updates s->res.analysers. It might make sense to explode it into several |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6266 | * other functions. It works like process_request (see indications above). |
| 6267 | */ |
| 6268 | int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px) |
| 6269 | { |
| 6270 | struct http_txn *txn = &s->txn; |
| 6271 | struct http_msg *msg = &txn->rsp; |
| 6272 | struct proxy *cur_proxy; |
| 6273 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 6274 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6275 | |
| 6276 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 6277 | now_ms, __FUNCTION__, |
| 6278 | s, |
| 6279 | rep, |
| 6280 | rep->rex, rep->wex, |
| 6281 | rep->flags, |
| 6282 | rep->buf->i, |
| 6283 | rep->analysers); |
| 6284 | |
| 6285 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6286 | return 0; |
| 6287 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6288 | /* The stats applet needs to adjust the Connection header but we don't |
| 6289 | * apply any filter there. |
| 6290 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6291 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 6292 | rep->analysers &= ~an_bit; |
| 6293 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6294 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6295 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6296 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6297 | /* |
| 6298 | * We will have to evaluate the filters. |
| 6299 | * As opposed to version 1.2, now they will be evaluated in the |
| 6300 | * filters order and not in the header order. This means that |
| 6301 | * each filter has to be validated among all headers. |
| 6302 | * |
| 6303 | * Filters are tried with ->be first, then with ->fe if it is |
| 6304 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6305 | * |
| 6306 | * Maybe we are in resume condiion. In this case I choose the |
| 6307 | * "struct proxy" which contains the rule list matching the resume |
| 6308 | * pointer. If none of theses "struct proxy" match, I initialise |
| 6309 | * the process with the first one. |
| 6310 | * |
| 6311 | * In fact, I check only correspondance betwwen the current list |
| 6312 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 6313 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6314 | */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6315 | if (s->current_rule_list == &s->fe->http_res_rules) |
| 6316 | cur_proxy = s->fe; |
| 6317 | else |
| 6318 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6319 | while (1) { |
| 6320 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6321 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6322 | /* evaluate http-response rules */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 6323 | if (ret == HTTP_RULE_RES_CONT) |
| 6324 | ret = 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] | 6325 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6326 | /* we need to be called again. */ |
| 6327 | if (ret == HTTP_RULE_RES_YIELD) { |
| 6328 | channel_dont_close(rep); |
| 6329 | return 0; |
| 6330 | } |
| 6331 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6332 | /* try headers filters */ |
| 6333 | if (rule_set->rsp_exp != NULL) { |
| 6334 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6335 | return_bad_resp: |
| 6336 | if (objt_server(s->target)) { |
| 6337 | objt_server(s->target)->counters.failed_resp++; |
| 6338 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6339 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6340 | s->be->be_counters.failed_resp++; |
| 6341 | return_srv_prx_502: |
| 6342 | rep->analysers = 0; |
| 6343 | txn->status = 502; |
| 6344 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6345 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6346 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6347 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6348 | if (!(s->flags & SN_ERR_MASK)) |
| 6349 | s->flags |= SN_ERR_PRXCOND; |
| 6350 | if (!(s->flags & SN_FINST_MASK)) |
| 6351 | s->flags |= SN_FINST_H; |
| 6352 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6353 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6354 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6355 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6356 | /* has the response been denied ? */ |
| 6357 | if (txn->flags & TX_SVDENY) { |
| 6358 | if (objt_server(s->target)) |
| 6359 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6360 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6361 | s->be->be_counters.denied_resp++; |
| 6362 | s->fe->fe_counters.denied_resp++; |
| 6363 | if (s->listener->counters) |
| 6364 | s->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6365 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6366 | goto return_srv_prx_502; |
| 6367 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6368 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6369 | /* add response headers from the rule sets in the same order */ |
| 6370 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6371 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6372 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6373 | if (wl->cond) { |
| 6374 | int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 6375 | ret = acl_pass(ret); |
| 6376 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6377 | ret = !ret; |
| 6378 | if (!ret) |
| 6379 | continue; |
| 6380 | } |
| 6381 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6382 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6383 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6384 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6385 | /* check whether we're already working on the frontend */ |
| 6386 | if (cur_proxy == s->fe) |
| 6387 | break; |
| 6388 | cur_proxy = s->fe; |
| 6389 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6390 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6391 | /* After this point, this anayzer can't return yield, so we can |
| 6392 | * remove the bit corresponding to this analyzer from the list. |
| 6393 | * |
| 6394 | * Note that the intermediate returns and goto found previously |
| 6395 | * reset the analyzers. |
| 6396 | */ |
| 6397 | rep->analysers &= ~an_bit; |
| 6398 | rep->analyse_exp = TICK_ETERNITY; |
| 6399 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6400 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6401 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6402 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6403 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6404 | /* |
| 6405 | * Now check for a server cookie. |
| 6406 | */ |
| 6407 | if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name || |
| 6408 | (s->be->options & PR_O_CHK_CACHE)) |
| 6409 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6410 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6411 | /* |
| 6412 | * Check for cache-control or pragma headers if required. |
| 6413 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6414 | 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] | 6415 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6416 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6417 | /* |
| 6418 | * Add server cookie in the response if needed |
| 6419 | */ |
| 6420 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6421 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 6422 | (!(s->flags & SN_DIRECT) || |
| 6423 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6424 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6425 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6426 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6427 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 6428 | !(s->flags & SN_IGNORE_PRST)) { |
| 6429 | /* the server is known, it's not the one the client requested, or the |
| 6430 | * cookie's last seen date needs to be refreshed. We have to |
| 6431 | * insert a set-cookie here, except if we want to insert only on POST |
| 6432 | * requests and this one isn't. Note that servers which don't have cookies |
| 6433 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6434 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6435 | if (!objt_server(s->target)->cookie) { |
| 6436 | chunk_printf(&trash, |
| 6437 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6438 | s->be->cookie_name); |
| 6439 | } |
| 6440 | else { |
| 6441 | 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] | 6442 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6443 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6444 | /* emit last_date, which is mandatory */ |
| 6445 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6446 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6447 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6448 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6449 | if (s->be->cookie_maxlife) { |
| 6450 | /* emit first_date, which is either the original one or |
| 6451 | * the current date. |
| 6452 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6453 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6454 | s30tob64(txn->cookie_first_date ? |
| 6455 | txn->cookie_first_date >> 2 : |
| 6456 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6457 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6458 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6459 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6460 | chunk_appendf(&trash, "; path=/"); |
| 6461 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6462 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6463 | if (s->be->cookie_domain) |
| 6464 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6465 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6466 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6467 | chunk_appendf(&trash, "; HttpOnly"); |
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 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6470 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6471 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6472 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6473 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6474 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6475 | txn->flags &= ~TX_SCK_MASK; |
| 6476 | if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT)) |
| 6477 | /* the server did not change, only the date was updated */ |
| 6478 | txn->flags |= TX_SCK_UPDATED; |
| 6479 | else |
| 6480 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6481 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6482 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6483 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6484 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6485 | * 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] | 6486 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6487 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6488 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6489 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6490 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6491 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6492 | "Cache-control: private", 22) < 0)) |
| 6493 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6494 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6495 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6496 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6497 | /* |
| 6498 | * Check if result will be cacheable with a cookie. |
| 6499 | * We'll block the response if security checks have caught |
| 6500 | * nasty things such as a cacheable cookie. |
| 6501 | */ |
| 6502 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6503 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6504 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6505 | /* we're in presence of a cacheable response containing |
| 6506 | * a set-cookie header. We'll block it as requested by |
| 6507 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6508 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6509 | if (objt_server(s->target)) |
| 6510 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6511 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6512 | s->be->be_counters.denied_resp++; |
| 6513 | s->fe->fe_counters.denied_resp++; |
| 6514 | if (s->listener->counters) |
| 6515 | s->listener->counters->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6516 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6517 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6518 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6519 | send_log(s->be, LOG_ALERT, |
| 6520 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6521 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6522 | goto return_srv_prx_502; |
| 6523 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6524 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6525 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6526 | /* |
| 6527 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6528 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6529 | * 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] | 6530 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6531 | * want to touch any 101 response either since it's switching to another |
| 6532 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6533 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6534 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6535 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 6536 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6537 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6538 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6539 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6540 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6541 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6542 | /* we want a keep-alive response here. Keep-alive header |
| 6543 | * required if either side is not 1.1. |
| 6544 | */ |
| 6545 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6546 | want_flags |= TX_CON_KAL_SET; |
| 6547 | } |
| 6548 | else { |
| 6549 | /* we want a close response here. Close header required if |
| 6550 | * the server is 1.1, regardless of the client. |
| 6551 | */ |
| 6552 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6553 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6554 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6555 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6556 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6557 | http_change_connection_header(txn, msg, want_flags); |
| 6558 | } |
| 6559 | |
| 6560 | skip_header_mangling: |
| 6561 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
| 6562 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 6563 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6564 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6565 | /* if the user wants to log as soon as possible, without counting |
| 6566 | * bytes from the server, then this is the right moment. We have |
| 6567 | * to temporarily assign bytes_out to log what we currently have. |
| 6568 | */ |
| 6569 | if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 6570 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6571 | s->logs.bytes_out = txn->rsp.eoh; |
| 6572 | s->do_log(s); |
| 6573 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6574 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6575 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6576 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6577 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6578 | /* This function is an analyser which forwards response body (including chunk |
| 6579 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6580 | * zero byte. The only situation where it must not be called is when we're in |
| 6581 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6582 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6583 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 6584 | * 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] | 6585 | * |
| 6586 | * It is capable of compressing response data both in content-length mode and |
| 6587 | * in chunked mode. The state machines follows different flows depending on |
| 6588 | * whether content-length and chunked modes are used, since there are no |
| 6589 | * trailers in content-length : |
| 6590 | * |
| 6591 | * chk-mode cl-mode |
| 6592 | * ,----- BODY -----. |
| 6593 | * / \ |
| 6594 | * V size > 0 V chk-mode |
| 6595 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6596 | * | | size == 0 | last byte | |
| 6597 | * | v final crlf v inspected | |
| 6598 | * | TRAILERS -----------> DONE | |
| 6599 | * | | |
| 6600 | * `----------------------------------------------' |
| 6601 | * |
| 6602 | * Compression only happens in the DATA state, and must be flushed in final |
| 6603 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6604 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6605 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6606 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6607 | 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] | 6608 | { |
| 6609 | struct http_txn *txn = &s->txn; |
| 6610 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6611 | static struct buffer *tmpbuf = &buf_empty; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6612 | int compressing = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6613 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6614 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6615 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6616 | return 0; |
| 6617 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6618 | 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] | 6619 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6620 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6621 | /* Output closed while we were sending data. We must abort and |
| 6622 | * wake the other side up. |
| 6623 | */ |
| 6624 | msg->msg_state = HTTP_MSG_ERROR; |
| 6625 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6626 | return 1; |
| 6627 | } |
| 6628 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6629 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6630 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6631 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 6632 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6633 | /* we have msg->sov which points to the first byte of message |
| 6634 | * body, and res->buf.p still points to the beginning of the |
| 6635 | * message. We forward the headers now, as we don't need them |
| 6636 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6637 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6638 | b_adv(res->buf, msg->sov); |
| 6639 | msg->next -= msg->sov; |
| 6640 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6641 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6642 | /* The previous analysers guarantee that the state is somewhere |
| 6643 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 6644 | * msg->next are always correct. |
| 6645 | */ |
| 6646 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 6647 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 6648 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6649 | else |
| 6650 | msg->msg_state = HTTP_MSG_DATA; |
| 6651 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6652 | } |
| 6653 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6654 | if (res->to_forward) { |
| 6655 | /* We can't process the buffer's contents yet */ |
| 6656 | res->flags |= CF_WAKE_WRITE; |
| 6657 | goto missing_data; |
| 6658 | } |
| 6659 | |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6660 | if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) { |
| 6661 | /* We need a compression buffer in the DATA state to put the |
| 6662 | * output of compressed data, and in CRLF state to let the |
| 6663 | * TRAILERS state finish the job of removing the trailing CRLF. |
| 6664 | */ |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6665 | if (unlikely(!tmpbuf->size)) { |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6666 | /* this is the first time we need the compression buffer */ |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 6667 | if (b_alloc(&tmpbuf) == NULL) |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6668 | goto aborted_xfer; /* no memory */ |
| 6669 | } |
| 6670 | |
| 6671 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6672 | if (ret < 0) { |
| 6673 | res->flags |= CF_WAKE_WRITE; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6674 | goto missing_data; /* not enough spaces in buffers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6675 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6676 | compressing = 1; |
| 6677 | } |
| 6678 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6679 | while (1) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6680 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6681 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6682 | /* we may have some pending data starting at res->buf->p */ |
| 6683 | if (unlikely(s->comp_algo)) { |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 6684 | ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6685 | if (ret < 0) |
| 6686 | goto aborted_xfer; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6687 | |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6688 | if (msg->chunk_len) { |
| 6689 | /* input empty or output full */ |
| 6690 | if (res->buf->i > msg->next) |
| 6691 | res->flags |= CF_WAKE_WRITE; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6692 | goto missing_data; |
| 6693 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6694 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6695 | else { |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6696 | if (msg->chunk_len > res->buf->i - msg->next) { |
| 6697 | /* output full */ |
| 6698 | res->flags |= CF_WAKE_WRITE; |
| 6699 | goto missing_data; |
| 6700 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6701 | msg->next += msg->chunk_len; |
| 6702 | msg->chunk_len = 0; |
| 6703 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6704 | |
| 6705 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6706 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6707 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6708 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6709 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6710 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6711 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6712 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6713 | |
| 6714 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 6715 | /* we want the CRLF after the data */ |
| 6716 | |
| 6717 | ret = http_skip_chunk_crlf(msg); |
| 6718 | if (ret == 0) |
| 6719 | goto missing_data; |
| 6720 | else if (ret < 0) { |
| 6721 | if (msg->err_pos >= 0) |
| 6722 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe); |
| 6723 | goto return_bad_res; |
| 6724 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6725 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 6726 | |
| 6727 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6728 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 6729 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6730 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6731 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6732 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6733 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6734 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6735 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6736 | else if (ret < 0) { |
| 6737 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6738 | 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] | 6739 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6740 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 6741 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6742 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6743 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6744 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6745 | if (unlikely(compressing)) { |
| 6746 | /* we need to flush output contents before syncing FSMs */ |
| 6747 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6748 | compressing = 0; |
| 6749 | } |
| 6750 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6751 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6752 | if (ret == 0) |
| 6753 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6754 | else if (ret < 0) { |
| 6755 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6756 | 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] | 6757 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6758 | } |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6759 | /* we're in HTTP_MSG_DONE now, fall through */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6760 | |
| 6761 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6762 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6763 | if (unlikely(compressing)) { |
| 6764 | /* we need to flush output contents before syncing FSMs */ |
| 6765 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6766 | compressing = 0; |
| 6767 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6768 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6769 | /* we may have some pending data starting at res->buf->p |
| 6770 | * such as a last chunk of data or trailers. |
| 6771 | */ |
| 6772 | b_adv(res->buf, msg->next); |
| 6773 | msg->next = 0; |
| 6774 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6775 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6776 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6777 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6778 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6779 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6780 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6781 | if (http_resync_states(s)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6782 | /* some state changes occurred, maybe the analyser |
| 6783 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6784 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6785 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6786 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6787 | /* response errors are most likely due to |
| 6788 | * the client aborting the transfer. |
| 6789 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6790 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6791 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6792 | if (msg->err_pos >= 0) |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6793 | 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] | 6794 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6795 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6796 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6797 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6798 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6799 | } |
| 6800 | } |
| 6801 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6802 | missing_data: |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6803 | /* we may have some pending data starting at res->buf->p */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6804 | if (unlikely(compressing)) { |
| 6805 | 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] | 6806 | compressing = 0; |
| 6807 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6808 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6809 | if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) { |
| 6810 | b_adv(res->buf, msg->next); |
| 6811 | msg->next = 0; |
| 6812 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6813 | } |
| 6814 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6815 | if (res->flags & CF_SHUTW) |
| 6816 | goto aborted_xfer; |
| 6817 | |
| 6818 | /* stop waiting for data if the input is closed before the end. If the |
| 6819 | * client side was already closed, it means that the client has aborted, |
| 6820 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6821 | * server abort. |
| 6822 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6823 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6824 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6825 | goto aborted_xfer; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6826 | if (!(s->flags & SN_ERR_MASK)) |
| 6827 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6828 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6829 | if (objt_server(s->target)) |
| 6830 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6831 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6832 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6833 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6834 | /* we need to obey the req analyser, so if it leaves, we must too */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6835 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6836 | goto return_bad_res; |
| 6837 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6838 | /* 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] | 6839 | * 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] | 6840 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 6841 | * close. |
| 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 | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6844 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6845 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6846 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6847 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6848 | /* 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] | 6849 | * 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] | 6850 | * 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] | 6851 | * modes are already handled by the stream sock layer. We must not do |
| 6852 | * this in content-length mode because it could present the MSG_MORE |
| 6853 | * flag with the last block of forwarded data, which would cause an |
| 6854 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6855 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6856 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6857 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6858 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6859 | /* the session handler will take care of timeouts and errors */ |
| 6860 | return 0; |
| 6861 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6862 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6863 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6864 | if (objt_server(s->target)) |
| 6865 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6866 | |
| 6867 | return_bad_res_stats_ok: |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 6868 | if (unlikely(compressing)) { |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6869 | 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] | 6870 | compressing = 0; |
| 6871 | } |
| 6872 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6873 | /* we may have some pending data starting at res->buf->p */ |
| 6874 | if (s->comp_algo == NULL) { |
| 6875 | b_adv(res->buf, msg->next); |
| 6876 | msg->next = 0; |
| 6877 | } |
| 6878 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6879 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6880 | /* don't send any error message as we're in the body */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6881 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6882 | res->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6883 | 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] | 6884 | if (objt_server(s->target)) |
| 6885 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6886 | |
| 6887 | if (!(s->flags & SN_ERR_MASK)) |
| 6888 | s->flags |= SN_ERR_PRXCOND; |
| 6889 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6890 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6891 | return 0; |
| 6892 | |
| 6893 | aborted_xfer: |
Willy Tarreau | 6fef8ae | 2014-04-22 21:22:06 +0200 | [diff] [blame] | 6894 | if (unlikely(compressing)) { |
| 6895 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
| 6896 | compressing = 0; |
| 6897 | } |
| 6898 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6899 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6900 | /* don't send any error message as we're in the body */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6901 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6902 | res->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6903 | s->req.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6904 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6905 | s->fe->fe_counters.cli_aborts++; |
| 6906 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6907 | if (objt_server(s->target)) |
| 6908 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6909 | |
| 6910 | if (!(s->flags & SN_ERR_MASK)) |
| 6911 | s->flags |= SN_ERR_CLICL; |
| 6912 | if (!(s->flags & SN_FINST_MASK)) |
| 6913 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6914 | return 0; |
| 6915 | } |
| 6916 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6917 | /* Iterate the same filter through all request headers. |
| 6918 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6919 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6920 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6921 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6922 | 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] | 6923 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6924 | char *cur_ptr, *cur_end, *cur_next; |
| 6925 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6926 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6927 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6928 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6929 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6930 | last_hdr = 0; |
| 6931 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6932 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6933 | old_idx = 0; |
| 6934 | |
| 6935 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6936 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6937 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6938 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6939 | (exp->action == ACT_ALLOW || |
| 6940 | exp->action == ACT_DENY || |
| 6941 | exp->action == ACT_TARPIT)) |
| 6942 | return 0; |
| 6943 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6944 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6945 | if (!cur_idx) |
| 6946 | break; |
| 6947 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6948 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6949 | cur_ptr = cur_next; |
| 6950 | cur_end = cur_ptr + cur_hdr->len; |
| 6951 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6952 | |
| 6953 | /* Now we have one header between cur_ptr and cur_end, |
| 6954 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6955 | */ |
| 6956 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6957 | 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] | 6958 | switch (exp->action) { |
| 6959 | case ACT_SETBE: |
| 6960 | /* It is not possible to jump a second time. |
| 6961 | * FIXME: should we return an HTTP/500 here so that |
| 6962 | * the admin knows there's a problem ? |
| 6963 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6964 | if (s->be != s->fe) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6965 | break; |
| 6966 | |
| 6967 | /* Swithing Proxy */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6968 | session_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6969 | last_hdr = 1; |
| 6970 | break; |
| 6971 | |
| 6972 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6973 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6974 | last_hdr = 1; |
| 6975 | break; |
| 6976 | |
| 6977 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6978 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6979 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6980 | break; |
| 6981 | |
| 6982 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6983 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6984 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6985 | break; |
| 6986 | |
| 6987 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6988 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6989 | if (trash.len < 0) |
| 6990 | return -1; |
| 6991 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6992 | 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] | 6993 | /* FIXME: if the user adds a newline in the replacement, the |
| 6994 | * index will not be recalculated for now, and the new line |
| 6995 | * will not be counted as a new header. |
| 6996 | */ |
| 6997 | |
| 6998 | cur_end += delta; |
| 6999 | cur_next += delta; |
| 7000 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7001 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7002 | break; |
| 7003 | |
| 7004 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7005 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7006 | cur_next += delta; |
| 7007 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7008 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7009 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7010 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7011 | cur_hdr->len = 0; |
| 7012 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7013 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7014 | break; |
| 7015 | |
| 7016 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7017 | } |
| 7018 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7019 | /* keep the link from this header to next one in case of later |
| 7020 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7021 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7022 | old_idx = cur_idx; |
| 7023 | } |
| 7024 | return 0; |
| 7025 | } |
| 7026 | |
| 7027 | |
| 7028 | /* Apply the filter to the request line. |
| 7029 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7030 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7031 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7032 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7033 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7034 | 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] | 7035 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7036 | char *cur_ptr, *cur_end; |
| 7037 | int done; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7038 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7039 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7040 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7041 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7042 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7043 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7044 | (exp->action == ACT_ALLOW || |
| 7045 | exp->action == ACT_DENY || |
| 7046 | exp->action == ACT_TARPIT)) |
| 7047 | return 0; |
| 7048 | else if (exp->action == ACT_REMOVE) |
| 7049 | return 0; |
| 7050 | |
| 7051 | done = 0; |
| 7052 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7053 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7054 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7055 | |
| 7056 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7057 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7058 | 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] | 7059 | switch (exp->action) { |
| 7060 | case ACT_SETBE: |
| 7061 | /* It is not possible to jump a second time. |
| 7062 | * FIXME: should we return an HTTP/500 here so that |
| 7063 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7064 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7065 | if (s->be != s->fe) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7066 | break; |
| 7067 | |
| 7068 | /* Swithing Proxy */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7069 | session_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7070 | done = 1; |
| 7071 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7072 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7073 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7074 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7075 | done = 1; |
| 7076 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7077 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7078 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7079 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7080 | done = 1; |
| 7081 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7082 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7083 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7084 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7085 | done = 1; |
| 7086 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7087 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7088 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7089 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7090 | if (trash.len < 0) |
| 7091 | return -1; |
| 7092 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7093 | 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] | 7094 | /* FIXME: if the user adds a newline in the replacement, the |
| 7095 | * index will not be recalculated for now, and the new line |
| 7096 | * will not be counted as a new header. |
| 7097 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7098 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7099 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7100 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7101 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7102 | HTTP_MSG_RQMETH, |
| 7103 | cur_ptr, cur_end + 1, |
| 7104 | NULL, NULL); |
| 7105 | if (unlikely(!cur_end)) |
| 7106 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7107 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7108 | /* we have a full request and we know that we have either a CR |
| 7109 | * or an LF at <ptr>. |
| 7110 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7111 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7112 | 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] | 7113 | /* there is no point trying this regex on headers */ |
| 7114 | return 1; |
| 7115 | } |
| 7116 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7117 | return done; |
| 7118 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7119 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7120 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7121 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7122 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7123 | * 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] | 7124 | * 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] | 7125 | * unparsable request. Since it can manage the switch to another backend, it |
| 7126 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7127 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7128 | 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] | 7129 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7130 | struct http_txn *txn = &s->txn; |
| 7131 | struct hdr_exp *exp; |
| 7132 | |
| 7133 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7134 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7135 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7136 | /* |
| 7137 | * The interleaving of transformations and verdicts |
| 7138 | * makes it difficult to decide to continue or stop |
| 7139 | * the evaluation. |
| 7140 | */ |
| 7141 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7142 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7143 | break; |
| 7144 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7145 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7146 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7147 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7148 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7149 | |
| 7150 | /* if this filter had a condition, evaluate it now and skip to |
| 7151 | * next filter if the condition does not match. |
| 7152 | */ |
| 7153 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7154 | 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] | 7155 | ret = acl_pass(ret); |
| 7156 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7157 | ret = !ret; |
| 7158 | |
| 7159 | if (!ret) |
| 7160 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7161 | } |
| 7162 | |
| 7163 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7164 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7165 | if (unlikely(ret < 0)) |
| 7166 | return -1; |
| 7167 | |
| 7168 | if (likely(ret == 0)) { |
| 7169 | /* The filter did not match the request, it can be |
| 7170 | * iterated through all headers. |
| 7171 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7172 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7173 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7174 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7175 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7176 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7177 | } |
| 7178 | |
| 7179 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7180 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7181 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7182 | * Try to retrieve the server associated to the appsession. |
| 7183 | * If the server is found, it's assigned to the session. |
| 7184 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7185 | void manage_client_side_appsession(struct session *s, const char *buf, int len) { |
| 7186 | struct http_txn *txn = &s->txn; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7187 | appsess *asession = NULL; |
| 7188 | char *sessid_temp = NULL; |
| 7189 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7190 | if (len > s->be->appsession_len) { |
| 7191 | len = s->be->appsession_len; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7192 | } |
| 7193 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7194 | if (s->be->options2 & PR_O2_AS_REQL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7195 | /* 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] | 7196 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7197 | /* 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] | 7198 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7199 | } |
| 7200 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7201 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7202 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7203 | 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] | 7204 | return; |
| 7205 | } |
| 7206 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7207 | memcpy(txn->sessid, buf, len); |
| 7208 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7209 | } |
| 7210 | |
| 7211 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 7212 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7213 | 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] | 7214 | return; |
| 7215 | } |
| 7216 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7217 | memcpy(sessid_temp, buf, len); |
| 7218 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7219 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7220 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7221 | /* free previously allocated memory */ |
| 7222 | pool_free2(apools.sessid, sessid_temp); |
| 7223 | |
| 7224 | if (asession != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7225 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
| 7226 | if (!(s->be->options2 & PR_O2_AS_REQL)) |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7227 | asession->request_count++; |
| 7228 | |
| 7229 | if (asession->serverid != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7230 | struct server *srv = s->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7231 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7232 | while (srv) { |
| 7233 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7234 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7235 | (s->be->options & PR_O_PERSIST) || |
| 7236 | (s->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7237 | /* we found the server and it's usable */ |
| 7238 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7239 | 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] | 7240 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 7241 | s->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 7242 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7243 | break; |
| 7244 | } else { |
| 7245 | txn->flags &= ~TX_CK_MASK; |
| 7246 | txn->flags |= TX_CK_DOWN; |
| 7247 | } |
| 7248 | } |
| 7249 | srv = srv->next; |
| 7250 | } |
| 7251 | } |
| 7252 | } |
| 7253 | } |
| 7254 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7255 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7256 | * same way as with headers above except that the semi-colon also ends a token. |
| 7257 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7258 | * return a valid result. |
| 7259 | */ |
| 7260 | char *find_cookie_value_end(char *s, const char *e) |
| 7261 | { |
| 7262 | int quoted, qdpair; |
| 7263 | |
| 7264 | quoted = qdpair = 0; |
| 7265 | for (; s < e; s++) { |
| 7266 | if (qdpair) qdpair = 0; |
| 7267 | else if (quoted) { |
| 7268 | if (*s == '\\') qdpair = 1; |
| 7269 | else if (*s == '"') quoted = 0; |
| 7270 | } |
| 7271 | else if (*s == '"') quoted = 1; |
| 7272 | else if (*s == ',' || *s == ';') return s; |
| 7273 | } |
| 7274 | return s; |
| 7275 | } |
| 7276 | |
| 7277 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7278 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7279 | * the first delimiter is updated if required. The function tries as much as |
| 7280 | * possible to respect the following principles : |
| 7281 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7282 | * colon, in which case <next> is simply removed |
| 7283 | * - set exactly one space character after the new first delimiter, unless |
| 7284 | * there are not enough characters in the block being moved to do so. |
| 7285 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7286 | * one. |
| 7287 | * |
| 7288 | * It is the caller's responsibility to ensure that : |
| 7289 | * - <from> points to a valid delimiter or the colon ; |
| 7290 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7291 | * - there are non-space chars before <from> ; |
| 7292 | * - there is a CR/LF at or after <next>. |
| 7293 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7294 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7295 | { |
| 7296 | char *prev = *from; |
| 7297 | |
| 7298 | if (*prev == ':') { |
| 7299 | /* We're removing the first value, preserve the colon and add a |
| 7300 | * space if possible. |
| 7301 | */ |
| 7302 | if (!http_is_crlf[(unsigned char)*next]) |
| 7303 | next++; |
| 7304 | prev++; |
| 7305 | if (prev < next) |
| 7306 | *prev++ = ' '; |
| 7307 | |
| 7308 | while (http_is_spht[(unsigned char)*next]) |
| 7309 | next++; |
| 7310 | } else { |
| 7311 | /* Remove useless spaces before the old delimiter. */ |
| 7312 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 7313 | prev--; |
| 7314 | *from = prev; |
| 7315 | |
| 7316 | /* copy the delimiter and if possible a space if we're |
| 7317 | * not at the end of the line. |
| 7318 | */ |
| 7319 | if (!http_is_crlf[(unsigned char)*next]) { |
| 7320 | *prev++ = *next++; |
| 7321 | if (prev + 1 < next) |
| 7322 | *prev++ = ' '; |
| 7323 | while (http_is_spht[(unsigned char)*next]) |
| 7324 | next++; |
| 7325 | } |
| 7326 | } |
| 7327 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7328 | } |
| 7329 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7330 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7331 | * 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] | 7332 | * desirable to call it only when needed. This code is quite complex because |
| 7333 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7334 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7335 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7336 | void manage_client_side_cookies(struct session *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7337 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7338 | struct http_txn *txn = &s->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7339 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7340 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7341 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7342 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
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 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7345 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7346 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7347 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7348 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7349 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7350 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7351 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7352 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7353 | hdr_beg = hdr_next; |
| 7354 | hdr_end = hdr_beg + cur_hdr->len; |
| 7355 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7356 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7357 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7358 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7359 | * "Cookie:" headers. |
| 7360 | */ |
| 7361 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7362 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7363 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7364 | old_idx = cur_idx; |
| 7365 | continue; |
| 7366 | } |
| 7367 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7368 | del_from = NULL; /* nothing to be deleted */ |
| 7369 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7370 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7371 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7372 | * attributes whose name begin with a '$', and associate them with |
| 7373 | * the right cookie, if we want to delete this cookie. |
| 7374 | * So there are 3 cases for each cookie read : |
| 7375 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7376 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7377 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7378 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7379 | * "special" cookie. |
| 7380 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7381 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7382 | * *MUST* delete it. |
| 7383 | * |
| 7384 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7385 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7386 | * the RFC explicitly allows spaces before it, and not within the |
| 7387 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7388 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7389 | * after the equal sign too, resulting in some (rare) buggy |
| 7390 | * implementations trying to do that. So let's do what servers do. |
| 7391 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7392 | * allowed quoted strings in values, with any possible character |
| 7393 | * after a backslash, including control chars and delimitors, which |
| 7394 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7395 | * within values even without quotes. |
| 7396 | * |
| 7397 | * We have to keep multiple pointers in order to support cookie |
| 7398 | * removal at the beginning, middle or end of header without |
| 7399 | * corrupting the header. All of these headers are valid : |
| 7400 | * |
| 7401 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7402 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7403 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7404 | * | | | | | | | | | |
| 7405 | * | | | | | | | | hdr_end <--+ |
| 7406 | * | | | | | | | +--> next |
| 7407 | * | | | | | | +----> val_end |
| 7408 | * | | | | | +-----------> val_beg |
| 7409 | * | | | | +--------------> equal |
| 7410 | * | | | +----------------> att_end |
| 7411 | * | | +---------------------> att_beg |
| 7412 | * | +--------------------------> prev |
| 7413 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7414 | */ |
| 7415 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7416 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7417 | /* Iterate through all cookies on this line */ |
| 7418 | |
| 7419 | /* find att_beg */ |
| 7420 | att_beg = prev + 1; |
| 7421 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7422 | att_beg++; |
| 7423 | |
| 7424 | /* find att_end : this is the first character after the last non |
| 7425 | * space before the equal. It may be equal to hdr_end. |
| 7426 | */ |
| 7427 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7428 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7429 | while (equal < hdr_end) { |
| 7430 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7431 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7432 | if (http_is_spht[(unsigned char)*equal++]) |
| 7433 | continue; |
| 7434 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7435 | } |
| 7436 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7437 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7438 | * is between <att_beg> and <equal>, both may be identical. |
| 7439 | */ |
| 7440 | |
| 7441 | /* look for end of cookie if there is an equal sign */ |
| 7442 | if (equal < hdr_end && *equal == '=') { |
| 7443 | /* look for the beginning of the value */ |
| 7444 | val_beg = equal + 1; |
| 7445 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7446 | val_beg++; |
| 7447 | |
| 7448 | /* find the end of the value, respecting quotes */ |
| 7449 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7450 | |
| 7451 | /* make val_end point to the first white space or delimitor after the value */ |
| 7452 | val_end = next; |
| 7453 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7454 | val_end--; |
| 7455 | } else { |
| 7456 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7457 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7458 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7459 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7460 | * they will automatically be removed if a header before them is removed, |
| 7461 | * since they're supposed to be linked together. |
| 7462 | */ |
| 7463 | if (*att_beg == '$') |
| 7464 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7465 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7466 | /* Ignore cookies with no equal sign */ |
| 7467 | if (equal == next) { |
| 7468 | /* This is not our cookie, so we must preserve it. But if we already |
| 7469 | * scheduled another cookie for removal, we cannot remove the |
| 7470 | * complete header, but we can remove the previous block itself. |
| 7471 | */ |
| 7472 | preserve_hdr = 1; |
| 7473 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7474 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7475 | val_end += delta; |
| 7476 | next += delta; |
| 7477 | hdr_end += delta; |
| 7478 | hdr_next += delta; |
| 7479 | cur_hdr->len += delta; |
| 7480 | http_msg_move_end(&txn->req, delta); |
| 7481 | prev = del_from; |
| 7482 | del_from = NULL; |
| 7483 | } |
| 7484 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7485 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7486 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7487 | /* if there are spaces around the equal sign, we need to |
| 7488 | * strip them otherwise we'll get trouble for cookie captures, |
| 7489 | * or even for rewrites. Since this happens extremely rarely, |
| 7490 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7491 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7492 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7493 | int stripped_before = 0; |
| 7494 | int stripped_after = 0; |
| 7495 | |
| 7496 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7497 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7498 | equal += stripped_before; |
| 7499 | val_beg += stripped_before; |
| 7500 | } |
| 7501 | |
| 7502 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7503 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7504 | val_beg += stripped_after; |
| 7505 | stripped_before += stripped_after; |
| 7506 | } |
| 7507 | |
| 7508 | val_end += stripped_before; |
| 7509 | next += stripped_before; |
| 7510 | hdr_end += stripped_before; |
| 7511 | hdr_next += stripped_before; |
| 7512 | cur_hdr->len += stripped_before; |
| 7513 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7514 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7515 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7516 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7517 | /* First, let's see if we want to capture this cookie. We check |
| 7518 | * that we don't already have a client side cookie, because we |
| 7519 | * can only capture one. Also as an optimisation, we ignore |
| 7520 | * cookies shorter than the declared name. |
| 7521 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7522 | if (s->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7523 | (val_end - att_beg >= s->fe->capture_namelen) && |
| 7524 | memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7525 | int log_len = val_end - att_beg; |
| 7526 | |
| 7527 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7528 | Alert("HTTP logging : out of memory.\n"); |
| 7529 | } else { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7530 | if (log_len > s->fe->capture_len) |
| 7531 | log_len = s->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7532 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7533 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7534 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7535 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7536 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7537 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7538 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7539 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7540 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7541 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7542 | * For cookies in prefix mode, the form is : |
| 7543 | * |
| 7544 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7545 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7546 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7547 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7548 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7549 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7550 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7551 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7552 | * have the server ID between val_beg and delim, and the original cookie between |
| 7553 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7554 | * |
| 7555 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7556 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7557 | * | || || | |+-> next |
| 7558 | * | || || | +--> val_end |
| 7559 | * | || || +---------> delim |
| 7560 | * | || |+------------> val_beg |
| 7561 | * | || +-------------> att_end = equal |
| 7562 | * | |+-----------------> att_beg |
| 7563 | * | +------------------> prev |
| 7564 | * +-------------------------> hdr_beg |
| 7565 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7566 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7567 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7568 | for (delim = val_beg; delim < val_end; delim++) |
| 7569 | if (*delim == COOKIE_DELIM) |
| 7570 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7571 | } else { |
| 7572 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7573 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7574 | /* Now check if the cookie contains a date field, which would |
| 7575 | * appear after a vertical bar ('|') just after the server name |
| 7576 | * and before the delimiter. |
| 7577 | */ |
| 7578 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7579 | if (vbar1) { |
| 7580 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7581 | * right is the last seen date. It is a base64 encoded |
| 7582 | * 30-bit value representing the UNIX date since the |
| 7583 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7584 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7585 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7586 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7587 | if (val_end - vbar1 >= 5) { |
| 7588 | val = b64tos30(vbar1); |
| 7589 | if (val > 0) |
| 7590 | txn->cookie_last_date = val << 2; |
| 7591 | } |
| 7592 | /* look for a second vertical bar */ |
| 7593 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7594 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7595 | val = b64tos30(vbar1 + 1); |
| 7596 | if (val > 0) |
| 7597 | txn->cookie_first_date = val << 2; |
| 7598 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7599 | } |
| 7600 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7601 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7602 | /* if the cookie has an expiration date and the proxy wants to check |
| 7603 | * it, then we do that now. We first check if the cookie is too old, |
| 7604 | * then only if it has expired. We detect strict overflow because the |
| 7605 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7606 | * in the future are ignored if their offset is beyond one day. This |
| 7607 | * allows an admin to fix timezone issues without expiring everyone |
| 7608 | * and at the same time avoids keeping unwanted side effects for too |
| 7609 | * long. |
| 7610 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7611 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7612 | (((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] | 7613 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7614 | txn->flags &= ~TX_CK_MASK; |
| 7615 | txn->flags |= TX_CK_OLD; |
| 7616 | delim = val_beg; // let's pretend we have not found the cookie |
| 7617 | txn->cookie_first_date = 0; |
| 7618 | txn->cookie_last_date = 0; |
| 7619 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7620 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7621 | (((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] | 7622 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7623 | txn->flags &= ~TX_CK_MASK; |
| 7624 | txn->flags |= TX_CK_EXPIRED; |
| 7625 | delim = val_beg; // let's pretend we have not found the cookie |
| 7626 | txn->cookie_first_date = 0; |
| 7627 | txn->cookie_last_date = 0; |
| 7628 | } |
| 7629 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7630 | /* Here, we'll look for the first running server which supports the cookie. |
| 7631 | * This allows to share a same cookie between several servers, for example |
| 7632 | * to dedicate backup servers to specific servers only. |
| 7633 | * However, to prevent clients from sticking to cookie-less backup server |
| 7634 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7635 | * empty cookies and mark them as invalid. |
| 7636 | * The same behaviour is applied when persistence must be ignored. |
| 7637 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7638 | if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7639 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7640 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7641 | while (srv) { |
| 7642 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7643 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7644 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7645 | (s->be->options & PR_O_PERSIST) || |
| 7646 | (s->flags & SN_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7647 | /* we found the server and we can use it */ |
| 7648 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7649 | 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] | 7650 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 7651 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7652 | break; |
| 7653 | } else { |
| 7654 | /* we found a server, but it's down, |
| 7655 | * mark it as such and go on in case |
| 7656 | * another one is available. |
| 7657 | */ |
| 7658 | txn->flags &= ~TX_CK_MASK; |
| 7659 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7660 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7661 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7662 | srv = srv->next; |
| 7663 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7664 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7665 | 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] | 7666 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7667 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7668 | if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7669 | txn->flags |= TX_CK_UNUSED; |
| 7670 | else |
| 7671 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7672 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7673 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7674 | /* depending on the cookie mode, we may have to either : |
| 7675 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7676 | * the server never sees it ; |
| 7677 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7678 | * application cookie so that it does not get accidentely removed later, |
| 7679 | * if we're in cookie prefix mode |
| 7680 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7681 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7682 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7683 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7684 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7685 | val_end += delta; |
| 7686 | next += delta; |
| 7687 | hdr_end += delta; |
| 7688 | hdr_next += delta; |
| 7689 | cur_hdr->len += delta; |
| 7690 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7691 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7692 | del_from = NULL; |
| 7693 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7694 | } |
| 7695 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7696 | (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] | 7697 | del_from = prev; |
| 7698 | } |
| 7699 | } else { |
| 7700 | /* This is not our cookie, so we must preserve it. But if we already |
| 7701 | * scheduled another cookie for removal, we cannot remove the |
| 7702 | * complete header, but we can remove the previous block itself. |
| 7703 | */ |
| 7704 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7705 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7706 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7707 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7708 | if (att_beg >= del_from) |
| 7709 | att_beg += delta; |
| 7710 | if (att_end >= del_from) |
| 7711 | att_end += delta; |
| 7712 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7713 | val_end += delta; |
| 7714 | next += delta; |
| 7715 | hdr_end += delta; |
| 7716 | hdr_next += delta; |
| 7717 | cur_hdr->len += delta; |
| 7718 | http_msg_move_end(&txn->req, delta); |
| 7719 | prev = del_from; |
| 7720 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7721 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7722 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7723 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7724 | /* Look for the appsession cookie unless persistence must be ignored */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7725 | if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7726 | int cmp_len, value_len; |
| 7727 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7728 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7729 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 7730 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 7731 | value_begin = att_beg + s->be->appsession_name_len; |
| 7732 | value_len = val_end - att_beg - s->be->appsession_name_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7733 | } else { |
| 7734 | cmp_len = att_end - att_beg; |
| 7735 | value_begin = val_beg; |
| 7736 | value_len = val_end - val_beg; |
| 7737 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7738 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7739 | /* let's see if the cookie is our appcookie */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7740 | if (cmp_len == s->be->appsession_name_len && |
| 7741 | memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) { |
| 7742 | manage_client_side_appsession(s, value_begin, value_len); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7743 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7744 | } |
| 7745 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7746 | /* continue with next cookie on this header line */ |
| 7747 | att_beg = next; |
| 7748 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7749 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7750 | /* There are no more cookies on this line. |
| 7751 | * We may still have one (or several) marked for deletion at the |
| 7752 | * end of the line. We must do this now in two ways : |
| 7753 | * - if some cookies must be preserved, we only delete from the |
| 7754 | * mark to the end of line ; |
| 7755 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7756 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7757 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7758 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7759 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7760 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7761 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7762 | cur_hdr->len += delta; |
| 7763 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7764 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7765 | |
| 7766 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7767 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7768 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7769 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7770 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7771 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7772 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7773 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7774 | } |
| 7775 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7776 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7777 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7778 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7779 | } |
| 7780 | |
| 7781 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7782 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7783 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7784 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7785 | 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] | 7786 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7787 | char *cur_ptr, *cur_end, *cur_next; |
| 7788 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7789 | struct http_txn *txn = &s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7790 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7791 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7792 | |
| 7793 | last_hdr = 0; |
| 7794 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7795 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7796 | old_idx = 0; |
| 7797 | |
| 7798 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7799 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7800 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7801 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7802 | (exp->action == ACT_ALLOW || |
| 7803 | exp->action == ACT_DENY)) |
| 7804 | return 0; |
| 7805 | |
| 7806 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7807 | if (!cur_idx) |
| 7808 | break; |
| 7809 | |
| 7810 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7811 | cur_ptr = cur_next; |
| 7812 | cur_end = cur_ptr + cur_hdr->len; |
| 7813 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7814 | |
| 7815 | /* Now we have one header between cur_ptr and cur_end, |
| 7816 | * and the next header starts at cur_next. |
| 7817 | */ |
| 7818 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7819 | 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] | 7820 | switch (exp->action) { |
| 7821 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7822 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7823 | last_hdr = 1; |
| 7824 | break; |
| 7825 | |
| 7826 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7827 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7828 | last_hdr = 1; |
| 7829 | break; |
| 7830 | |
| 7831 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7832 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7833 | if (trash.len < 0) |
| 7834 | return -1; |
| 7835 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7836 | 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] | 7837 | /* FIXME: if the user adds a newline in the replacement, the |
| 7838 | * index will not be recalculated for now, and the new line |
| 7839 | * will not be counted as a new header. |
| 7840 | */ |
| 7841 | |
| 7842 | cur_end += delta; |
| 7843 | cur_next += delta; |
| 7844 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7845 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7846 | break; |
| 7847 | |
| 7848 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7849 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7850 | cur_next += delta; |
| 7851 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7852 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7853 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7854 | txn->hdr_idx.used--; |
| 7855 | cur_hdr->len = 0; |
| 7856 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7857 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7858 | break; |
| 7859 | |
| 7860 | } |
| 7861 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7862 | |
| 7863 | /* keep the link from this header to next one in case of later |
| 7864 | * removal of next header. |
| 7865 | */ |
| 7866 | old_idx = cur_idx; |
| 7867 | } |
| 7868 | return 0; |
| 7869 | } |
| 7870 | |
| 7871 | |
| 7872 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7873 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7874 | * or -1 if a replacement resulted in an invalid status line. |
| 7875 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7876 | 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] | 7877 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7878 | char *cur_ptr, *cur_end; |
| 7879 | int done; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7880 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7881 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7882 | |
| 7883 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7884 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7885 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7886 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7887 | (exp->action == ACT_ALLOW || |
| 7888 | exp->action == ACT_DENY)) |
| 7889 | return 0; |
| 7890 | else if (exp->action == ACT_REMOVE) |
| 7891 | return 0; |
| 7892 | |
| 7893 | done = 0; |
| 7894 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7895 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7896 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7897 | |
| 7898 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7899 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7900 | 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] | 7901 | switch (exp->action) { |
| 7902 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7903 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7904 | done = 1; |
| 7905 | break; |
| 7906 | |
| 7907 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7908 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7909 | done = 1; |
| 7910 | break; |
| 7911 | |
| 7912 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7913 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7914 | if (trash.len < 0) |
| 7915 | return -1; |
| 7916 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7917 | 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] | 7918 | /* FIXME: if the user adds a newline in the replacement, the |
| 7919 | * index will not be recalculated for now, and the new line |
| 7920 | * will not be counted as a new header. |
| 7921 | */ |
| 7922 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7923 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7924 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7925 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7926 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7927 | cur_ptr, cur_end + 1, |
| 7928 | NULL, NULL); |
| 7929 | if (unlikely(!cur_end)) |
| 7930 | return -1; |
| 7931 | |
| 7932 | /* we have a full respnse and we know that we have either a CR |
| 7933 | * or an LF at <ptr>. |
| 7934 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7935 | 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] | 7936 | 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] | 7937 | /* there is no point trying this regex on headers */ |
| 7938 | return 1; |
| 7939 | } |
| 7940 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7941 | return done; |
| 7942 | } |
| 7943 | |
| 7944 | |
| 7945 | |
| 7946 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7947 | * 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] | 7948 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7949 | * unparsable response. |
| 7950 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7951 | 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] | 7952 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7953 | struct http_txn *txn = &s->txn; |
| 7954 | struct hdr_exp *exp; |
| 7955 | |
| 7956 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7957 | int ret; |
| 7958 | |
| 7959 | /* |
| 7960 | * The interleaving of transformations and verdicts |
| 7961 | * makes it difficult to decide to continue or stop |
| 7962 | * the evaluation. |
| 7963 | */ |
| 7964 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7965 | if (txn->flags & TX_SVDENY) |
| 7966 | break; |
| 7967 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7968 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7969 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7970 | exp->action == ACT_PASS)) { |
| 7971 | exp = exp->next; |
| 7972 | continue; |
| 7973 | } |
| 7974 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7975 | /* if this filter had a condition, evaluate it now and skip to |
| 7976 | * next filter if the condition does not match. |
| 7977 | */ |
| 7978 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7979 | 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] | 7980 | ret = acl_pass(ret); |
| 7981 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7982 | ret = !ret; |
| 7983 | if (!ret) |
| 7984 | continue; |
| 7985 | } |
| 7986 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7987 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7988 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7989 | if (unlikely(ret < 0)) |
| 7990 | return -1; |
| 7991 | |
| 7992 | if (likely(ret == 0)) { |
| 7993 | /* The filter did not match the response, it can be |
| 7994 | * iterated through all headers. |
| 7995 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7996 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 7997 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7998 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7999 | } |
| 8000 | return 0; |
| 8001 | } |
| 8002 | |
| 8003 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8004 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 8005 | * 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] | 8006 | * desirable to call it only when needed. This function is also used when we |
| 8007 | * 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] | 8008 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8009 | void manage_server_side_cookies(struct session *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8010 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8011 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8012 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8013 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8014 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8015 | char *hdr_beg, *hdr_end, *hdr_next; |
| 8016 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8017 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8018 | /* Iterate through the headers. |
| 8019 | * we start with the start line. |
| 8020 | */ |
| 8021 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8022 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8023 | |
| 8024 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 8025 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8026 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8027 | |
| 8028 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8029 | hdr_beg = hdr_next; |
| 8030 | hdr_end = hdr_beg + cur_hdr->len; |
| 8031 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8032 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8033 | /* We have one full header between hdr_beg and hdr_end, and the |
| 8034 | * next header starts at hdr_next. We're only interested in |
| 8035 | * "Set-Cookie" and "Set-Cookie2" headers. |
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 | is_cookie2 = 0; |
| 8039 | prev = hdr_beg + 10; |
| 8040 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8041 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8042 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 8043 | if (!val) { |
| 8044 | old_idx = cur_idx; |
| 8045 | continue; |
| 8046 | } |
| 8047 | is_cookie2 = 1; |
| 8048 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8049 | } |
| 8050 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8051 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 8052 | * <prev> points to the colon. |
| 8053 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8054 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8055 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8056 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8057 | * check-cache is enabled) and we are not interested in checking |
| 8058 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8059 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8060 | if (s->be->cookie_name == NULL && |
| 8061 | s->be->appsession_name == NULL && |
| 8062 | s->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8063 | return; |
| 8064 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8065 | /* OK so now we know we have to process this response cookie. |
| 8066 | * The format of the Set-Cookie header is slightly different |
| 8067 | * from the format of the Cookie header in that it does not |
| 8068 | * support the comma as a cookie delimiter (thus the header |
| 8069 | * cannot be folded) because the Expires attribute described in |
| 8070 | * the original Netscape's spec may contain an unquoted date |
| 8071 | * with a comma inside. We have to live with this because |
| 8072 | * many browsers don't support Max-Age and some browsers don't |
| 8073 | * support quoted strings. However the Set-Cookie2 header is |
| 8074 | * clean. |
| 8075 | * |
| 8076 | * We have to keep multiple pointers in order to support cookie |
| 8077 | * removal at the beginning, middle or end of header without |
| 8078 | * corrupting the header (in case of set-cookie2). A special |
| 8079 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8080 | * fields after the first semi-colon. The <next> pointer points |
| 8081 | * either to the end of line (set-cookie) or next unquoted comma |
| 8082 | * (set-cookie2). All of these headers are valid : |
| 8083 | * |
| 8084 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8085 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8086 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8087 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8088 | * | | | | | | | | | | |
| 8089 | * | | | | | | | | +-> next hdr_end <--+ |
| 8090 | * | | | | | | | +------------> scav |
| 8091 | * | | | | | | +--------------> val_end |
| 8092 | * | | | | | +--------------------> val_beg |
| 8093 | * | | | | +----------------------> equal |
| 8094 | * | | | +------------------------> att_end |
| 8095 | * | | +----------------------------> att_beg |
| 8096 | * | +------------------------------> prev |
| 8097 | * +-----------------------------------------> hdr_beg |
| 8098 | */ |
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 | for (; prev < hdr_end; prev = next) { |
| 8101 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8102 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8103 | /* find att_beg */ |
| 8104 | att_beg = prev + 1; |
| 8105 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8106 | att_beg++; |
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 | /* find att_end : this is the first character after the last non |
| 8109 | * space before the equal. It may be equal to hdr_end. |
| 8110 | */ |
| 8111 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8112 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8113 | while (equal < hdr_end) { |
| 8114 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8115 | break; |
| 8116 | if (http_is_spht[(unsigned char)*equal++]) |
| 8117 | continue; |
| 8118 | att_end = equal; |
| 8119 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8120 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8121 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8122 | * is between <att_beg> and <equal>, both may be identical. |
| 8123 | */ |
| 8124 | |
| 8125 | /* look for end of cookie if there is an equal sign */ |
| 8126 | if (equal < hdr_end && *equal == '=') { |
| 8127 | /* look for the beginning of the value */ |
| 8128 | val_beg = equal + 1; |
| 8129 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8130 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8131 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8132 | /* find the end of the value, respecting quotes */ |
| 8133 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8134 | |
| 8135 | /* make val_end point to the first white space or delimitor after the value */ |
| 8136 | val_end = next; |
| 8137 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8138 | val_end--; |
| 8139 | } else { |
| 8140 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8141 | val_beg = val_end = next = equal; |
| 8142 | } |
| 8143 | |
| 8144 | if (next < hdr_end) { |
| 8145 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8146 | * a colon or semi-colon before the end. So skip all attr-value |
| 8147 | * pairs and look for the next comma. For Set-Cookie, since |
| 8148 | * commas are permitted in values, skip to the end. |
| 8149 | */ |
| 8150 | if (is_cookie2) |
| 8151 | next = find_hdr_value_end(next, hdr_end); |
| 8152 | else |
| 8153 | next = hdr_end; |
| 8154 | } |
| 8155 | |
| 8156 | /* Now everything is as on the diagram above */ |
| 8157 | |
| 8158 | /* Ignore cookies with no equal sign */ |
| 8159 | if (equal == val_end) |
| 8160 | continue; |
| 8161 | |
| 8162 | /* If there are spaces around the equal sign, we need to |
| 8163 | * strip them otherwise we'll get trouble for cookie captures, |
| 8164 | * or even for rewrites. Since this happens extremely rarely, |
| 8165 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8166 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8167 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8168 | int stripped_before = 0; |
| 8169 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8170 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8171 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8172 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8173 | equal += stripped_before; |
| 8174 | val_beg += stripped_before; |
| 8175 | } |
| 8176 | |
| 8177 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8178 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8179 | val_beg += stripped_after; |
| 8180 | stripped_before += stripped_after; |
| 8181 | } |
| 8182 | |
| 8183 | val_end += stripped_before; |
| 8184 | next += stripped_before; |
| 8185 | hdr_end += stripped_before; |
| 8186 | hdr_next += stripped_before; |
| 8187 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8188 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8189 | } |
| 8190 | |
| 8191 | /* First, let's see if we want to capture this cookie. We check |
| 8192 | * that we don't already have a server side cookie, because we |
| 8193 | * can only capture one. Also as an optimisation, we ignore |
| 8194 | * cookies shorter than the declared name. |
| 8195 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8196 | if (s->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8197 | txn->srv_cookie == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8198 | (val_end - att_beg >= s->fe->capture_namelen) && |
| 8199 | memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8200 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8201 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8202 | Alert("HTTP logging : out of memory.\n"); |
| 8203 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8204 | else { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8205 | if (log_len > s->fe->capture_len) |
| 8206 | log_len = s->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8207 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8208 | txn->srv_cookie[log_len] = 0; |
| 8209 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8210 | } |
| 8211 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8212 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8213 | /* now check if we need to process it for persistence */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8214 | if (!(s->flags & SN_IGNORE_PRST) && |
| 8215 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8216 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8217 | /* assume passive cookie by default */ |
| 8218 | txn->flags &= ~TX_SCK_MASK; |
| 8219 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8220 | |
| 8221 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8222 | * this occurrence because we'll insert another one later. |
| 8223 | * 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] | 8224 | * a direct access. |
| 8225 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8226 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8227 | /* The "preserve" flag was set, we don't want to touch the |
| 8228 | * server's cookie. |
| 8229 | */ |
| 8230 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8231 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 8232 | ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8233 | /* this cookie must be deleted */ |
| 8234 | if (*prev == ':' && next == hdr_end) { |
| 8235 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8236 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8237 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8238 | txn->hdr_idx.used--; |
| 8239 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8240 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8241 | hdr_next += delta; |
| 8242 | http_msg_move_end(&txn->rsp, delta); |
| 8243 | /* note: while both invalid now, <next> and <hdr_end> |
| 8244 | * are still equal, so the for() will stop as expected. |
| 8245 | */ |
| 8246 | } else { |
| 8247 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8248 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8249 | next = prev; |
| 8250 | hdr_end += delta; |
| 8251 | hdr_next += delta; |
| 8252 | cur_hdr->len += delta; |
| 8253 | http_msg_move_end(&txn->rsp, delta); |
| 8254 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8255 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8256 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8257 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8258 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8259 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8260 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8261 | * with this server since we know it. |
| 8262 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8263 | 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] | 8264 | next += delta; |
| 8265 | hdr_end += delta; |
| 8266 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8267 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8268 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8269 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8270 | txn->flags &= ~TX_SCK_MASK; |
| 8271 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8272 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8273 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8274 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8275 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8276 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8277 | 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] | 8278 | next += delta; |
| 8279 | hdr_end += delta; |
| 8280 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8281 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8282 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8283 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8284 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8285 | txn->flags &= ~TX_SCK_MASK; |
| 8286 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8287 | } |
| 8288 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 8289 | /* 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] | 8290 | else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8291 | int cmp_len, value_len; |
| 8292 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8293 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8294 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 8295 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 8296 | value_begin = att_beg + s->be->appsession_name_len; |
| 8297 | 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] | 8298 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8299 | cmp_len = att_end - att_beg; |
| 8300 | value_begin = val_beg; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8301 | value_len = MIN(s->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8302 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8303 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8304 | if ((cmp_len == s->be->appsession_name_len) && |
| 8305 | (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] | 8306 | /* free a possibly previously allocated memory */ |
| 8307 | pool_free2(apools.sessid, txn->sessid); |
| 8308 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8309 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8310 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8311 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8312 | 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] | 8313 | return; |
| 8314 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8315 | memcpy(txn->sessid, value_begin, value_len); |
| 8316 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8317 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8318 | } |
| 8319 | /* that's done for this cookie, check the next one on the same |
| 8320 | * line when next != hdr_end (only if is_cookie2). |
| 8321 | */ |
| 8322 | } |
| 8323 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8324 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8325 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8326 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8327 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8328 | appsess *asession = NULL; |
| 8329 | /* only do insert, if lookup fails */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8330 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8331 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 8332 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8333 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 8334 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8335 | 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] | 8336 | return; |
| 8337 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8338 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 8339 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8340 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 8341 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8342 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8343 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8344 | return; |
| 8345 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8346 | memcpy(asession->sessid, txn->sessid, s->be->appsession_len); |
| 8347 | asession->sessid[s->be->appsession_len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8348 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8349 | server_id_len = strlen(objt_server(s->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8350 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8351 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8352 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8353 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8354 | return; |
| 8355 | } |
| 8356 | asession->serverid[0] = '\0'; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8357 | memcpy(asession->serverid, objt_server(s->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8358 | |
| 8359 | asession->request_count = 0; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8360 | appsession_hash_insert(&(s->be->htbl_proxy), asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8361 | } |
| 8362 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8363 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8364 | asession->request_count++; |
| 8365 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8366 | } |
| 8367 | |
| 8368 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8369 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8370 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8371 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8372 | void check_response_for_cacheability(struct session *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8373 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8374 | struct http_txn *txn = &s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8375 | char *p1, *p2; |
| 8376 | |
| 8377 | char *cur_ptr, *cur_end, *cur_next; |
| 8378 | int cur_idx; |
| 8379 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8380 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8381 | return; |
| 8382 | |
| 8383 | /* Iterate through the headers. |
| 8384 | * we start with the start line. |
| 8385 | */ |
| 8386 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8387 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8388 | |
| 8389 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8390 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8391 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8392 | |
| 8393 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8394 | cur_ptr = cur_next; |
| 8395 | cur_end = cur_ptr + cur_hdr->len; |
| 8396 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8397 | |
| 8398 | /* We have one full header between cur_ptr and cur_end, and the |
| 8399 | * next header starts at cur_next. We're only interested in |
| 8400 | * "Cookie:" headers. |
| 8401 | */ |
| 8402 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8403 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8404 | if (val) { |
| 8405 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8406 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8407 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8408 | return; |
| 8409 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8410 | } |
| 8411 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8412 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8413 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8414 | continue; |
| 8415 | |
| 8416 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8417 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8418 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8419 | |
| 8420 | if (p1 >= cur_end) /* no more info */ |
| 8421 | continue; |
| 8422 | |
| 8423 | /* p1 is at the beginning of the value */ |
| 8424 | p2 = p1; |
| 8425 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8426 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8427 | p2++; |
| 8428 | |
| 8429 | /* we have a complete value between p1 and p2 */ |
| 8430 | if (p2 < cur_end && *p2 == '=') { |
| 8431 | /* we have something of the form no-cache="set-cookie" */ |
| 8432 | if ((cur_end - p1 >= 21) && |
| 8433 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8434 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8435 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8436 | continue; |
| 8437 | } |
| 8438 | |
| 8439 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8440 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8441 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8442 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8443 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8444 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8445 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8446 | return; |
| 8447 | } |
| 8448 | |
| 8449 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8450 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8451 | continue; |
| 8452 | } |
| 8453 | } |
| 8454 | } |
| 8455 | |
| 8456 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8457 | /* |
| 8458 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 8459 | * If the server is found, it's assigned to the session. |
| 8460 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8461 | 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] | 8462 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8463 | char *end_params, *first_param, *cur_param, *next_param; |
| 8464 | char separator; |
| 8465 | int value_len; |
| 8466 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8467 | int mode = s->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8468 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8469 | if (s->be->appsession_name == NULL || |
| 8470 | (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] | 8471 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8472 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8473 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8474 | first_param = NULL; |
| 8475 | switch (mode) { |
| 8476 | case PR_O2_AS_M_PP: |
| 8477 | first_param = memchr(begin, ';', len); |
| 8478 | break; |
| 8479 | case PR_O2_AS_M_QS: |
| 8480 | first_param = memchr(begin, '?', len); |
| 8481 | break; |
| 8482 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8483 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8484 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8485 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8486 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8487 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8488 | switch (mode) { |
| 8489 | case PR_O2_AS_M_PP: |
| 8490 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 8491 | end_params = (char *) begin + len; |
| 8492 | } |
| 8493 | separator = ';'; |
| 8494 | break; |
| 8495 | case PR_O2_AS_M_QS: |
| 8496 | end_params = (char *) begin + len; |
| 8497 | separator = '&'; |
| 8498 | break; |
| 8499 | default: |
| 8500 | /* unknown mode, shouldn't happen */ |
| 8501 | return; |
| 8502 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8503 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8504 | cur_param = next_param = end_params; |
| 8505 | while (cur_param > first_param) { |
| 8506 | cur_param--; |
| 8507 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 8508 | /* let's see if this is the appsession parameter */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8509 | if ((cur_param + s->be->appsession_name_len + 1 < next_param) && |
| 8510 | ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') && |
| 8511 | (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] | 8512 | /* Cool... it's the right one */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8513 | cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 8514 | value_len = MIN(s->be->appsession_len, next_param - cur_param); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8515 | if (value_len > 0) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8516 | manage_client_side_appsession(s, cur_param, value_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8517 | } |
| 8518 | break; |
| 8519 | } |
| 8520 | next_param = cur_param; |
| 8521 | } |
| 8522 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8523 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 8524 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8525 | appsession_hash_dump(&(s->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8526 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8527 | } |
| 8528 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8529 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8530 | * 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] | 8531 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8532 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8533 | * 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] | 8534 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8535 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8536 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8537 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8538 | 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] | 8539 | { |
| 8540 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8541 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8542 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8543 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8544 | if (!uri_auth) |
| 8545 | return 0; |
| 8546 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8547 | 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] | 8548 | return 0; |
| 8549 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8550 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8551 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8552 | return 0; |
| 8553 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8554 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8555 | return 0; |
| 8556 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8557 | return 1; |
| 8558 | } |
| 8559 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8560 | /* |
| 8561 | * 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] | 8562 | * By default it tries to report the error position as msg->err_pos. However if |
| 8563 | * this one is not set, it will then report msg->next, which is the last known |
| 8564 | * 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] | 8565 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8566 | */ |
| 8567 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8568 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8569 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8570 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8571 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8572 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8573 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8574 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8575 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8576 | len1 = MIN(len1, es->len); |
| 8577 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8578 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8579 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8580 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8581 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8582 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8583 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8584 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8585 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8586 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8587 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8588 | es->when = date; // user-visible date |
| 8589 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8590 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8591 | es->oe = other_end; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 8592 | if (objt_conn(s->si[0].end)) |
| 8593 | es->src = __objt_conn(s->si[0].end)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8594 | else |
| 8595 | memset(&es->src, 0, sizeof(es->src)); |
| 8596 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8597 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8598 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8599 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8600 | es->s_flags = s->flags; |
| 8601 | es->t_flags = s->txn.flags; |
| 8602 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8603 | es->b_out = chn->buf->o; |
| 8604 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8605 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8606 | es->m_clen = msg->chunk_len; |
| 8607 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8608 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8609 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8610 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8611 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8612 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8613 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8614 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8615 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8616 | * 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] | 8617 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8618 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8619 | * 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] | 8620 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8621 | 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] | 8622 | struct hdr_idx *idx, int occ, |
| 8623 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8624 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8625 | struct hdr_ctx local_ctx; |
| 8626 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8627 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8628 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8629 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8630 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8631 | if (!ctx) { |
| 8632 | local_ctx.idx = 0; |
| 8633 | ctx = &local_ctx; |
| 8634 | } |
| 8635 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8636 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8637 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8638 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8639 | occ--; |
| 8640 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8641 | *vptr = ctx->line + ctx->val; |
| 8642 | *vlen = ctx->vlen; |
| 8643 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8644 | } |
| 8645 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8646 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8647 | } |
| 8648 | |
| 8649 | /* negative occurrence, we scan all the list then walk back */ |
| 8650 | if (-occ > MAX_HDR_HISTORY) |
| 8651 | return 0; |
| 8652 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8653 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8654 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8655 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8656 | len_hist[hist_ptr] = ctx->vlen; |
| 8657 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8658 | hist_ptr = 0; |
| 8659 | found++; |
| 8660 | } |
| 8661 | if (-occ > found) |
| 8662 | return 0; |
| 8663 | /* 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] | 8664 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8665 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8666 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8667 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8668 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8669 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8670 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8671 | *vptr = ptr_hist[hist_ptr]; |
| 8672 | *vlen = len_hist[hist_ptr]; |
| 8673 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8674 | } |
| 8675 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8676 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8677 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8678 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8679 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8680 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8681 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8682 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8683 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8684 | * line header values and does not stop at commas. |
| 8685 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8686 | */ |
| 8687 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8688 | struct hdr_idx *idx, int occ, |
| 8689 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8690 | { |
| 8691 | struct hdr_ctx local_ctx; |
| 8692 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8693 | int len_hist[MAX_HDR_HISTORY]; |
| 8694 | unsigned int hist_ptr; |
| 8695 | int found; |
| 8696 | |
| 8697 | if (!ctx) { |
| 8698 | local_ctx.idx = 0; |
| 8699 | ctx = &local_ctx; |
| 8700 | } |
| 8701 | |
| 8702 | if (occ >= 0) { |
| 8703 | /* search from the beginning */ |
| 8704 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8705 | occ--; |
| 8706 | if (occ <= 0) { |
| 8707 | *vptr = ctx->line + ctx->val; |
| 8708 | *vlen = ctx->vlen; |
| 8709 | return 1; |
| 8710 | } |
| 8711 | } |
| 8712 | return 0; |
| 8713 | } |
| 8714 | |
| 8715 | /* negative occurrence, we scan all the list then walk back */ |
| 8716 | if (-occ > MAX_HDR_HISTORY) |
| 8717 | return 0; |
| 8718 | |
| 8719 | found = hist_ptr = 0; |
| 8720 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8721 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8722 | len_hist[hist_ptr] = ctx->vlen; |
| 8723 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8724 | hist_ptr = 0; |
| 8725 | found++; |
| 8726 | } |
| 8727 | if (-occ > found) |
| 8728 | return 0; |
| 8729 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8730 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8731 | */ |
| 8732 | hist_ptr += occ; |
| 8733 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8734 | hist_ptr -= MAX_HDR_HISTORY; |
| 8735 | *vptr = ptr_hist[hist_ptr]; |
| 8736 | *vlen = len_hist[hist_ptr]; |
| 8737 | return 1; |
| 8738 | } |
| 8739 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8740 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8741 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8742 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8743 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8744 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8745 | 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] | 8746 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8747 | int max; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8748 | 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] | 8749 | dir, |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 8750 | objt_conn(s->si[0].end) ? (unsigned short)objt_conn(s->si[0].end)->t.sock.fd : -1, |
| 8751 | objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->t.sock.fd : -1); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8752 | |
| 8753 | for (max = 0; start + max < end; max++) |
| 8754 | if (start[max] == '\r' || start[max] == '\n') |
| 8755 | break; |
| 8756 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8757 | UBOUND(max, trash.size - trash.len - 3); |
| 8758 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8759 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8760 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8761 | } |
| 8762 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8763 | /* |
| 8764 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 8765 | * the required fields are properly allocated and that we only need to (re)init |
| 8766 | * them. This should be used before processing any new request. |
| 8767 | */ |
| 8768 | void http_init_txn(struct session *s) |
| 8769 | { |
| 8770 | struct http_txn *txn = &s->txn; |
| 8771 | struct proxy *fe = s->fe; |
| 8772 | |
| 8773 | txn->flags = 0; |
| 8774 | txn->status = -1; |
| 8775 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8776 | txn->cookie_first_date = 0; |
| 8777 | txn->cookie_last_date = 0; |
| 8778 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8779 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8780 | 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] | 8781 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8782 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8783 | 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] | 8784 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 8785 | txn->req.chunk_len = 0LL; |
| 8786 | txn->req.body_len = 0LL; |
| 8787 | txn->rsp.chunk_len = 0LL; |
| 8788 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8789 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8790 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8791 | txn->req.chn = &s->req; |
| 8792 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8793 | |
| 8794 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8795 | |
| 8796 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8797 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8798 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8799 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8800 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8801 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 8802 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8803 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8804 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 8805 | |
| 8806 | if (txn->hdr_idx.v) |
| 8807 | hdr_idx_init(&txn->hdr_idx); |
| 8808 | } |
| 8809 | |
| 8810 | /* to be used at the end of a transaction */ |
| 8811 | void http_end_txn(struct session *s) |
| 8812 | { |
| 8813 | struct http_txn *txn = &s->txn; |
| 8814 | |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8815 | /* release any possible compression context */ |
| 8816 | if (s->flags & SN_COMP_READY) |
| 8817 | s->comp_algo->end(&s->comp_ctx); |
| 8818 | s->comp_algo = NULL; |
| 8819 | s->flags &= ~SN_COMP_READY; |
| 8820 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8821 | /* these ones will have been dynamically allocated */ |
| 8822 | pool_free2(pool2_requri, txn->uri); |
| 8823 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8824 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8825 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8826 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8827 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8828 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8829 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8830 | txn->uri = NULL; |
| 8831 | txn->srv_cookie = NULL; |
| 8832 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8833 | |
| 8834 | if (txn->req.cap) { |
| 8835 | struct cap_hdr *h; |
| 8836 | for (h = s->fe->req_cap; h; h = h->next) |
| 8837 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 8838 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 8839 | } |
| 8840 | |
| 8841 | if (txn->rsp.cap) { |
| 8842 | struct cap_hdr *h; |
| 8843 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 8844 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 8845 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 8846 | } |
| 8847 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8848 | } |
| 8849 | |
| 8850 | /* to be used at the end of a transaction to prepare a new one */ |
| 8851 | void http_reset_txn(struct session *s) |
| 8852 | { |
| 8853 | http_end_txn(s); |
| 8854 | http_init_txn(s); |
| 8855 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 8856 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 8857 | * any rulelist match the NULL pointer. |
| 8858 | */ |
| 8859 | s->current_rule_list = NULL; |
| 8860 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8861 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8862 | s->logs.logwait = s->fe->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8863 | s->logs.level = 0; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 8864 | session_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8865 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8866 | /* re-init store persistence */ |
| 8867 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8868 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8869 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8870 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8871 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8872 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8873 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8874 | /* We must trim any excess data from the response buffer, because we |
| 8875 | * may have blocked an invalid response from a server that we don't |
| 8876 | * want to accidentely forward once we disable the analysers, nor do |
| 8877 | * we want those data to come along with next response. A typical |
| 8878 | * example of such data would be from a buggy server responding to |
| 8879 | * a HEAD with some data, or sending more than the advertised |
| 8880 | * content-length. |
| 8881 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8882 | if (unlikely(s->res.buf->i)) |
| 8883 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8884 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8885 | s->req.rto = s->fe->timeout.client; |
| 8886 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8887 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8888 | s->res.rto = TICK_ETERNITY; |
| 8889 | s->res.wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8890 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8891 | s->req.rex = TICK_ETERNITY; |
| 8892 | s->req.wex = TICK_ETERNITY; |
| 8893 | s->req.analyse_exp = TICK_ETERNITY; |
| 8894 | s->res.rex = TICK_ETERNITY; |
| 8895 | s->res.wex = TICK_ETERNITY; |
| 8896 | s->res.analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8897 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8898 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8899 | void free_http_res_rules(struct list *r) |
| 8900 | { |
| 8901 | struct http_res_rule *tr, *pr; |
| 8902 | |
| 8903 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8904 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8905 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8906 | free(pr); |
| 8907 | } |
| 8908 | } |
| 8909 | |
| 8910 | void free_http_req_rules(struct list *r) |
| 8911 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8912 | struct http_req_rule *tr, *pr; |
| 8913 | |
| 8914 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8915 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8916 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8917 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8918 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8919 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8920 | free(pr); |
| 8921 | } |
| 8922 | } |
| 8923 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8924 | /* parse an "http-request" rule */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8925 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8926 | { |
| 8927 | struct http_req_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8928 | struct http_req_action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8929 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8930 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8931 | |
| 8932 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 8933 | if (!rule) { |
| 8934 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8935 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8936 | } |
| 8937 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8938 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8939 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 8940 | cur_arg = 1; |
Willy Tarreau | 5bd6759 | 2014-04-28 22:00:46 +0200 | [diff] [blame] | 8941 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8942 | rule->action = HTTP_REQ_ACT_DENY; |
| 8943 | cur_arg = 1; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8944 | } else if (!strcmp(args[0], "tarpit")) { |
| 8945 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 8946 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8947 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8948 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8949 | cur_arg = 1; |
| 8950 | |
| 8951 | while(*args[cur_arg]) { |
| 8952 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8953 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8954 | cur_arg+=2; |
| 8955 | continue; |
| 8956 | } else |
| 8957 | break; |
| 8958 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8959 | } else if (!strcmp(args[0], "set-nice")) { |
| 8960 | rule->action = HTTP_REQ_ACT_SET_NICE; |
| 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 value).\n", |
| 8966 | file, linenum, args[0]); |
| 8967 | goto out_err; |
| 8968 | } |
| 8969 | rule->arg.nice = atoi(args[cur_arg]); |
| 8970 | if (rule->arg.nice < -1024) |
| 8971 | rule->arg.nice = -1024; |
| 8972 | else if (rule->arg.nice > 1024) |
| 8973 | rule->arg.nice = 1024; |
| 8974 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8975 | } else if (!strcmp(args[0], "set-tos")) { |
| 8976 | #ifdef IP_TOS |
| 8977 | char *err; |
| 8978 | rule->action = HTTP_REQ_ACT_SET_TOS; |
| 8979 | cur_arg = 1; |
| 8980 | |
| 8981 | if (!*args[cur_arg] || |
| 8982 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8983 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8984 | file, linenum, args[0]); |
| 8985 | goto out_err; |
| 8986 | } |
| 8987 | |
| 8988 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8989 | if (err && *err != '\0') { |
| 8990 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8991 | file, linenum, err, args[0]); |
| 8992 | goto out_err; |
| 8993 | } |
| 8994 | cur_arg++; |
| 8995 | #else |
| 8996 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8997 | goto out_err; |
| 8998 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8999 | } else if (!strcmp(args[0], "set-mark")) { |
| 9000 | #ifdef SO_MARK |
| 9001 | char *err; |
| 9002 | rule->action = HTTP_REQ_ACT_SET_MARK; |
| 9003 | cur_arg = 1; |
| 9004 | |
| 9005 | if (!*args[cur_arg] || |
| 9006 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9007 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9008 | file, linenum, args[0]); |
| 9009 | goto out_err; |
| 9010 | } |
| 9011 | |
| 9012 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9013 | if (err && *err != '\0') { |
| 9014 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9015 | file, linenum, err, args[0]); |
| 9016 | goto out_err; |
| 9017 | } |
| 9018 | cur_arg++; |
| 9019 | global.last_checks |= LSTCHK_NETADM; |
| 9020 | #else |
| 9021 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9022 | goto out_err; |
| 9023 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9024 | } else if (!strcmp(args[0], "set-log-level")) { |
| 9025 | rule->action = HTTP_REQ_ACT_SET_LOGL; |
| 9026 | cur_arg = 1; |
| 9027 | |
| 9028 | if (!*args[cur_arg] || |
| 9029 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9030 | bad_log_level: |
| 9031 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9032 | file, linenum, args[0]); |
| 9033 | goto out_err; |
| 9034 | } |
| 9035 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9036 | rule->arg.loglevel = -1; |
| 9037 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 9038 | goto bad_log_level; |
| 9039 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9040 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9041 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 9042 | cur_arg = 1; |
| 9043 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 9044 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9045 | (*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] | 9046 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9047 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9048 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9049 | } |
| 9050 | |
| 9051 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9052 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9053 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 9054 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9055 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9056 | 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] | 9057 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9058 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9059 | free(proxy->conf.lfs_file); |
| 9060 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9061 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9062 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9063 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
| 9064 | 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] | 9065 | cur_arg = 1; |
| 9066 | |
| 9067 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 9068 | (*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] | 9069 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 9070 | file, linenum, args[0]); |
| 9071 | goto out_err; |
| 9072 | } |
| 9073 | |
| 9074 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9075 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9076 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9077 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9078 | error = NULL; |
| 9079 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9080 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9081 | args[cur_arg + 1], error); |
| 9082 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9083 | goto out_err; |
| 9084 | } |
| 9085 | |
| 9086 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9087 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9088 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9089 | file, linenum); |
| 9090 | |
| 9091 | free(proxy->conf.lfs_file); |
| 9092 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9093 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9094 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9095 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9096 | rule->action = HTTP_REQ_ACT_DEL_HDR; |
| 9097 | cur_arg = 1; |
| 9098 | |
| 9099 | if (!*args[cur_arg] || |
| 9100 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9101 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9102 | file, linenum, args[0]); |
| 9103 | goto out_err; |
| 9104 | } |
| 9105 | |
| 9106 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9107 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9108 | |
| 9109 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9110 | free(proxy->conf.lfs_file); |
| 9111 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9112 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9113 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9114 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9115 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 9116 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9117 | struct sample_expr *expr; |
| 9118 | unsigned int where; |
| 9119 | char *err = NULL; |
| 9120 | |
| 9121 | cur_arg = 1; |
| 9122 | proxy->conf.args.ctx = ARGC_TRK; |
| 9123 | |
| 9124 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9125 | if (!expr) { |
| 9126 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9127 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9128 | free(err); |
| 9129 | goto out_err; |
| 9130 | } |
| 9131 | |
| 9132 | where = 0; |
| 9133 | if (proxy->cap & PR_CAP_FE) |
| 9134 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9135 | if (proxy->cap & PR_CAP_BE) |
| 9136 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9137 | |
| 9138 | if (!(expr->fetch->val & where)) { |
| 9139 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9140 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9141 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9142 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9143 | free(expr); |
| 9144 | goto out_err; |
| 9145 | } |
| 9146 | |
| 9147 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9148 | cur_arg++; |
| 9149 | if (!args[cur_arg]) { |
| 9150 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 9151 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9152 | free(expr); |
| 9153 | goto out_err; |
| 9154 | } |
| 9155 | /* we copy the table name for now, it will be resolved later */ |
| 9156 | rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]); |
| 9157 | cur_arg++; |
| 9158 | } |
| 9159 | rule->act_prm.trk_ctr.expr = expr; |
| 9160 | rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0'; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9161 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9162 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9163 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9164 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9165 | 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] | 9166 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9167 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9168 | goto out_err; |
| 9169 | } |
| 9170 | |
| 9171 | /* this redirect rule might already contain a parsed condition which |
| 9172 | * we'll pass to the http-request rule. |
| 9173 | */ |
| 9174 | rule->action = HTTP_REQ_ACT_REDIR; |
| 9175 | rule->arg.redir = redir; |
| 9176 | rule->cond = redir->cond; |
| 9177 | redir->cond = NULL; |
| 9178 | cur_arg = 2; |
| 9179 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9180 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9181 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9182 | rule->action = HTTP_REQ_ACT_ADD_ACL; |
| 9183 | /* |
| 9184 | * '+ 8' for 'add-acl(' |
| 9185 | * '- 9' for 'add-acl(' + trailing ')' |
| 9186 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9187 | 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] | 9188 | |
| 9189 | cur_arg = 1; |
| 9190 | |
| 9191 | if (!*args[cur_arg] || |
| 9192 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9193 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9194 | file, linenum, args[0]); |
| 9195 | goto out_err; |
| 9196 | } |
| 9197 | |
| 9198 | LIST_INIT(&rule->arg.map.key); |
| 9199 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9200 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9201 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9202 | file, linenum); |
| 9203 | free(proxy->conf.lfs_file); |
| 9204 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9205 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9206 | cur_arg += 1; |
| 9207 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9208 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
| 9209 | rule->action = HTTP_REQ_ACT_DEL_ACL; |
| 9210 | /* |
| 9211 | * '+ 8' for 'del-acl(' |
| 9212 | * '- 9' for 'del-acl(' + trailing ')' |
| 9213 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9214 | 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] | 9215 | |
| 9216 | cur_arg = 1; |
| 9217 | |
| 9218 | if (!*args[cur_arg] || |
| 9219 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9220 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9221 | file, linenum, args[0]); |
| 9222 | goto out_err; |
| 9223 | } |
| 9224 | |
| 9225 | LIST_INIT(&rule->arg.map.key); |
| 9226 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9227 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9228 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9229 | file, linenum); |
| 9230 | free(proxy->conf.lfs_file); |
| 9231 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9232 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9233 | cur_arg += 1; |
| 9234 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9235 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
| 9236 | rule->action = HTTP_REQ_ACT_DEL_MAP; |
| 9237 | /* |
| 9238 | * '+ 8' for 'del-map(' |
| 9239 | * '- 9' for 'del-map(' + trailing ')' |
| 9240 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9241 | 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] | 9242 | |
| 9243 | cur_arg = 1; |
| 9244 | |
| 9245 | if (!*args[cur_arg] || |
| 9246 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9247 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9248 | file, linenum, args[0]); |
| 9249 | goto out_err; |
| 9250 | } |
| 9251 | |
| 9252 | LIST_INIT(&rule->arg.map.key); |
| 9253 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9254 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9255 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9256 | file, linenum); |
| 9257 | free(proxy->conf.lfs_file); |
| 9258 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9259 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9260 | cur_arg += 1; |
| 9261 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9262 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9263 | rule->action = HTTP_REQ_ACT_SET_MAP; |
| 9264 | /* |
| 9265 | * '+ 8' for 'set-map(' |
| 9266 | * '- 9' for 'set-map(' + trailing ')' |
| 9267 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9268 | 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] | 9269 | |
| 9270 | cur_arg = 1; |
| 9271 | |
| 9272 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9273 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9274 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9275 | file, linenum, args[0]); |
| 9276 | goto out_err; |
| 9277 | } |
| 9278 | |
| 9279 | LIST_INIT(&rule->arg.map.key); |
| 9280 | LIST_INIT(&rule->arg.map.value); |
| 9281 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9282 | |
| 9283 | /* key pattern */ |
| 9284 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9285 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9286 | file, linenum); |
| 9287 | |
| 9288 | /* value pattern */ |
| 9289 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9290 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9291 | file, linenum); |
| 9292 | free(proxy->conf.lfs_file); |
| 9293 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9294 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9295 | |
| 9296 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9297 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9298 | char *errmsg = NULL; |
| 9299 | cur_arg = 1; |
| 9300 | /* try in the module list */ |
| 9301 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9302 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9303 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9304 | free(errmsg); |
| 9305 | goto out_err; |
| 9306 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9307 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9308 | 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] | 9309 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9310 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9311 | } |
| 9312 | |
| 9313 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9314 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9315 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9316 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9317 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9318 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9319 | file, linenum, args[0], errmsg); |
| 9320 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9321 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9322 | } |
| 9323 | rule->cond = cond; |
| 9324 | } |
| 9325 | else if (*args[cur_arg]) { |
| 9326 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9327 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9328 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9329 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9330 | } |
| 9331 | |
| 9332 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9333 | out_err: |
| 9334 | free(rule); |
| 9335 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9336 | } |
| 9337 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9338 | /* parse an "http-respose" rule */ |
| 9339 | struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 9340 | { |
| 9341 | struct http_res_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9342 | struct http_res_action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9343 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9344 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9345 | |
| 9346 | rule = calloc(1, sizeof(*rule)); |
| 9347 | if (!rule) { |
| 9348 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9349 | goto out_err; |
| 9350 | } |
| 9351 | |
| 9352 | if (!strcmp(args[0], "allow")) { |
| 9353 | rule->action = HTTP_RES_ACT_ALLOW; |
| 9354 | cur_arg = 1; |
| 9355 | } else if (!strcmp(args[0], "deny")) { |
| 9356 | rule->action = HTTP_RES_ACT_DENY; |
| 9357 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9358 | } else if (!strcmp(args[0], "set-nice")) { |
| 9359 | rule->action = HTTP_RES_ACT_SET_NICE; |
| 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 value).\n", |
| 9365 | file, linenum, args[0]); |
| 9366 | goto out_err; |
| 9367 | } |
| 9368 | rule->arg.nice = atoi(args[cur_arg]); |
| 9369 | if (rule->arg.nice < -1024) |
| 9370 | rule->arg.nice = -1024; |
| 9371 | else if (rule->arg.nice > 1024) |
| 9372 | rule->arg.nice = 1024; |
| 9373 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9374 | } else if (!strcmp(args[0], "set-tos")) { |
| 9375 | #ifdef IP_TOS |
| 9376 | char *err; |
| 9377 | rule->action = HTTP_RES_ACT_SET_TOS; |
| 9378 | cur_arg = 1; |
| 9379 | |
| 9380 | if (!*args[cur_arg] || |
| 9381 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9382 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9383 | file, linenum, args[0]); |
| 9384 | goto out_err; |
| 9385 | } |
| 9386 | |
| 9387 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9388 | if (err && *err != '\0') { |
| 9389 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9390 | file, linenum, err, args[0]); |
| 9391 | goto out_err; |
| 9392 | } |
| 9393 | cur_arg++; |
| 9394 | #else |
| 9395 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9396 | goto out_err; |
| 9397 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9398 | } else if (!strcmp(args[0], "set-mark")) { |
| 9399 | #ifdef SO_MARK |
| 9400 | char *err; |
| 9401 | rule->action = HTTP_RES_ACT_SET_MARK; |
| 9402 | cur_arg = 1; |
| 9403 | |
| 9404 | if (!*args[cur_arg] || |
| 9405 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9406 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9407 | file, linenum, args[0]); |
| 9408 | goto out_err; |
| 9409 | } |
| 9410 | |
| 9411 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9412 | if (err && *err != '\0') { |
| 9413 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9414 | file, linenum, err, args[0]); |
| 9415 | goto out_err; |
| 9416 | } |
| 9417 | cur_arg++; |
| 9418 | global.last_checks |= LSTCHK_NETADM; |
| 9419 | #else |
| 9420 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9421 | goto out_err; |
| 9422 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9423 | } else if (!strcmp(args[0], "set-log-level")) { |
| 9424 | rule->action = HTTP_RES_ACT_SET_LOGL; |
| 9425 | cur_arg = 1; |
| 9426 | |
| 9427 | if (!*args[cur_arg] || |
| 9428 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9429 | bad_log_level: |
| 9430 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9431 | file, linenum, args[0]); |
| 9432 | goto out_err; |
| 9433 | } |
| 9434 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9435 | rule->arg.loglevel = -1; |
| 9436 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 9437 | goto bad_log_level; |
| 9438 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9439 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9440 | rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR; |
| 9441 | cur_arg = 1; |
| 9442 | |
| 9443 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9444 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9445 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9446 | file, linenum, args[0]); |
| 9447 | goto out_err; |
| 9448 | } |
| 9449 | |
| 9450 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9451 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9452 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9453 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9454 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9455 | 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] | 9456 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9457 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9458 | free(proxy->conf.lfs_file); |
| 9459 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9460 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9461 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9462 | } 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] | 9463 | 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] | 9464 | cur_arg = 1; |
| 9465 | |
| 9466 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9467 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9468 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9469 | file, linenum, args[0]); |
| 9470 | goto out_err; |
| 9471 | } |
| 9472 | |
| 9473 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9474 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9475 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9476 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9477 | error = NULL; |
| 9478 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9479 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9480 | args[cur_arg + 1], error); |
| 9481 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9482 | goto out_err; |
| 9483 | } |
| 9484 | |
| 9485 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9486 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9487 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9488 | file, linenum); |
| 9489 | |
| 9490 | free(proxy->conf.lfs_file); |
| 9491 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9492 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9493 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9494 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9495 | rule->action = HTTP_RES_ACT_DEL_HDR; |
| 9496 | cur_arg = 1; |
| 9497 | |
| 9498 | if (!*args[cur_arg] || |
| 9499 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9500 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9501 | file, linenum, args[0]); |
| 9502 | goto out_err; |
| 9503 | } |
| 9504 | |
| 9505 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9506 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9507 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9508 | proxy->conf.args.ctx = ARGC_HRS; |
| 9509 | free(proxy->conf.lfs_file); |
| 9510 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9511 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9512 | cur_arg += 1; |
| 9513 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9514 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9515 | rule->action = HTTP_RES_ACT_ADD_ACL; |
| 9516 | /* |
| 9517 | * '+ 8' for 'add-acl(' |
| 9518 | * '- 9' for 'add-acl(' + trailing ')' |
| 9519 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9520 | 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] | 9521 | |
| 9522 | cur_arg = 1; |
| 9523 | |
| 9524 | if (!*args[cur_arg] || |
| 9525 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9526 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9527 | file, linenum, args[0]); |
| 9528 | goto out_err; |
| 9529 | } |
| 9530 | |
| 9531 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9532 | proxy->conf.args.ctx = ARGC_HRS; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9533 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9534 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9535 | file, linenum); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9536 | free(proxy->conf.lfs_file); |
| 9537 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9538 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9539 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9540 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9541 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9542 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
| 9543 | rule->action = HTTP_RES_ACT_DEL_ACL; |
| 9544 | /* |
| 9545 | * '+ 8' for 'del-acl(' |
| 9546 | * '- 9' for 'del-acl(' + trailing ')' |
| 9547 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9548 | 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] | 9549 | |
| 9550 | cur_arg = 1; |
| 9551 | |
| 9552 | if (!*args[cur_arg] || |
| 9553 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9554 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9555 | file, linenum, args[0]); |
| 9556 | goto out_err; |
| 9557 | } |
| 9558 | |
| 9559 | LIST_INIT(&rule->arg.map.key); |
| 9560 | proxy->conf.args.ctx = ARGC_HRS; |
| 9561 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9562 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9563 | file, linenum); |
| 9564 | free(proxy->conf.lfs_file); |
| 9565 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9566 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9567 | cur_arg += 1; |
| 9568 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9569 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
| 9570 | rule->action = HTTP_RES_ACT_DEL_MAP; |
| 9571 | /* |
| 9572 | * '+ 8' for 'del-map(' |
| 9573 | * '- 9' for 'del-map(' + trailing ')' |
| 9574 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9575 | 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] | 9576 | |
| 9577 | cur_arg = 1; |
| 9578 | |
| 9579 | if (!*args[cur_arg] || |
| 9580 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9581 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9582 | file, linenum, args[0]); |
| 9583 | goto out_err; |
| 9584 | } |
| 9585 | |
| 9586 | LIST_INIT(&rule->arg.map.key); |
| 9587 | proxy->conf.args.ctx = ARGC_HRS; |
| 9588 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9589 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9590 | file, linenum); |
| 9591 | free(proxy->conf.lfs_file); |
| 9592 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9593 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9594 | cur_arg += 1; |
| 9595 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9596 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9597 | rule->action = HTTP_RES_ACT_SET_MAP; |
| 9598 | /* |
| 9599 | * '+ 8' for 'set-map(' |
| 9600 | * '- 9' for 'set-map(' + trailing ')' |
| 9601 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9602 | 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] | 9603 | |
| 9604 | cur_arg = 1; |
| 9605 | |
| 9606 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9607 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9608 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9609 | file, linenum, args[0]); |
| 9610 | goto out_err; |
| 9611 | } |
| 9612 | |
| 9613 | LIST_INIT(&rule->arg.map.key); |
| 9614 | LIST_INIT(&rule->arg.map.value); |
| 9615 | |
| 9616 | proxy->conf.args.ctx = ARGC_HRS; |
| 9617 | |
| 9618 | /* key pattern */ |
| 9619 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9620 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9621 | file, linenum); |
| 9622 | |
| 9623 | /* value pattern */ |
| 9624 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9625 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9626 | file, linenum); |
| 9627 | |
| 9628 | free(proxy->conf.lfs_file); |
| 9629 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9630 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9631 | |
| 9632 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9633 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9634 | char *errmsg = NULL; |
| 9635 | cur_arg = 1; |
| 9636 | /* try in the module list */ |
| 9637 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9638 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9639 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9640 | free(errmsg); |
| 9641 | goto out_err; |
| 9642 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9643 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9644 | 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] | 9645 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 9646 | goto out_err; |
| 9647 | } |
| 9648 | |
| 9649 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9650 | struct acl_cond *cond; |
| 9651 | char *errmsg = NULL; |
| 9652 | |
| 9653 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9654 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9655 | file, linenum, args[0], errmsg); |
| 9656 | free(errmsg); |
| 9657 | goto out_err; |
| 9658 | } |
| 9659 | rule->cond = cond; |
| 9660 | } |
| 9661 | else if (*args[cur_arg]) { |
| 9662 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9663 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9664 | file, linenum, args[0], args[cur_arg]); |
| 9665 | goto out_err; |
| 9666 | } |
| 9667 | |
| 9668 | return rule; |
| 9669 | out_err: |
| 9670 | free(rule); |
| 9671 | return NULL; |
| 9672 | } |
| 9673 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9674 | /* 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] | 9675 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
| 9676 | * dynamic log-format rule instead of a static string. |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9677 | */ |
| 9678 | 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] | 9679 | const char **args, char **errmsg, int use_fmt) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9680 | { |
| 9681 | struct redirect_rule *rule; |
| 9682 | int cur_arg; |
| 9683 | int type = REDIRECT_TYPE_NONE; |
| 9684 | int code = 302; |
| 9685 | const char *destination = NULL; |
| 9686 | const char *cookie = NULL; |
| 9687 | int cookie_set = 0; |
| 9688 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9689 | struct acl_cond *cond = NULL; |
| 9690 | |
| 9691 | cur_arg = 0; |
| 9692 | while (*(args[cur_arg])) { |
| 9693 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9694 | if (!*args[cur_arg + 1]) |
| 9695 | goto missing_arg; |
| 9696 | |
| 9697 | type = REDIRECT_TYPE_LOCATION; |
| 9698 | cur_arg++; |
| 9699 | destination = args[cur_arg]; |
| 9700 | } |
| 9701 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9702 | if (!*args[cur_arg + 1]) |
| 9703 | goto missing_arg; |
| 9704 | |
| 9705 | type = REDIRECT_TYPE_PREFIX; |
| 9706 | cur_arg++; |
| 9707 | destination = args[cur_arg]; |
| 9708 | } |
| 9709 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9710 | if (!*args[cur_arg + 1]) |
| 9711 | goto missing_arg; |
| 9712 | |
| 9713 | type = REDIRECT_TYPE_SCHEME; |
| 9714 | cur_arg++; |
| 9715 | destination = args[cur_arg]; |
| 9716 | } |
| 9717 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9718 | if (!*args[cur_arg + 1]) |
| 9719 | goto missing_arg; |
| 9720 | |
| 9721 | cur_arg++; |
| 9722 | cookie = args[cur_arg]; |
| 9723 | cookie_set = 1; |
| 9724 | } |
| 9725 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9726 | if (!*args[cur_arg + 1]) |
| 9727 | goto missing_arg; |
| 9728 | |
| 9729 | cur_arg++; |
| 9730 | cookie = args[cur_arg]; |
| 9731 | cookie_set = 0; |
| 9732 | } |
| 9733 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9734 | if (!*args[cur_arg + 1]) |
| 9735 | goto missing_arg; |
| 9736 | |
| 9737 | cur_arg++; |
| 9738 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9739 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9740 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9741 | "'%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] | 9742 | args[cur_arg - 1], args[cur_arg]); |
| 9743 | return NULL; |
| 9744 | } |
| 9745 | } |
| 9746 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9747 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9748 | } |
| 9749 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9750 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9751 | } |
| 9752 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9753 | strcmp(args[cur_arg], "unless") == 0) { |
| 9754 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 9755 | if (!cond) { |
| 9756 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9757 | return NULL; |
| 9758 | } |
| 9759 | break; |
| 9760 | } |
| 9761 | else { |
| 9762 | memprintf(errmsg, |
| 9763 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9764 | args[cur_arg]); |
| 9765 | return NULL; |
| 9766 | } |
| 9767 | cur_arg++; |
| 9768 | } |
| 9769 | |
| 9770 | if (type == REDIRECT_TYPE_NONE) { |
| 9771 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9772 | return NULL; |
| 9773 | } |
| 9774 | |
| 9775 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 9776 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9777 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9778 | |
| 9779 | if (!use_fmt) { |
| 9780 | /* old-style static redirect rule */ |
| 9781 | rule->rdr_str = strdup(destination); |
| 9782 | rule->rdr_len = strlen(destination); |
| 9783 | } |
| 9784 | else { |
| 9785 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9786 | |
| 9787 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9788 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9789 | * makes it hard for the user to configure a self-redirection. |
| 9790 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9791 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9792 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9793 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9794 | (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9795 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9796 | free(curproxy->conf.lfs_file); |
| 9797 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9798 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9799 | } |
| 9800 | } |
| 9801 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9802 | if (cookie) { |
| 9803 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9804 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9805 | */ |
| 9806 | rule->cookie_len = strlen(cookie); |
| 9807 | if (cookie_set) { |
| 9808 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9809 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9810 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9811 | rule->cookie_len += 9; |
| 9812 | } else { |
| 9813 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9814 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9815 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9816 | rule->cookie_len += 20; |
| 9817 | } |
| 9818 | } |
| 9819 | rule->type = type; |
| 9820 | rule->code = code; |
| 9821 | rule->flags = flags; |
| 9822 | LIST_INIT(&rule->list); |
| 9823 | return rule; |
| 9824 | |
| 9825 | missing_arg: |
| 9826 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9827 | return NULL; |
| 9828 | } |
| 9829 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9830 | /************************************************************************/ |
| 9831 | /* The code below is dedicated to ACL parsing and matching */ |
| 9832 | /************************************************************************/ |
| 9833 | |
| 9834 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9835 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9836 | * which means that a request or response is ready. If some data is missing, |
| 9837 | * 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] | 9838 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9839 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9840 | * |
| 9841 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9842 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9843 | * decide whether or not an HTTP message is present ; |
| 9844 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9845 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9846 | * 1 if an HTTP message is ready |
| 9847 | */ |
| 9848 | static int |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9849 | 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] | 9850 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9851 | { |
| 9852 | struct http_txn *txn = l7; |
| 9853 | struct http_msg *msg = &txn->req; |
| 9854 | |
| 9855 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 9856 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 9857 | */ |
| 9858 | |
| 9859 | if (unlikely(!s || !txn)) |
| 9860 | return 0; |
| 9861 | |
| 9862 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9863 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9864 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9865 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9866 | /* If the buffer does not leave enough free space at the end, |
| 9867 | * we must first realign it. |
| 9868 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9869 | if (s->req.buf->p > s->req.buf->data && |
| 9870 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 9871 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9872 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9873 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9874 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9875 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9876 | |
| 9877 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9878 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9879 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9880 | |
| 9881 | /* Still no valid request ? */ |
| 9882 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9883 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9884 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9885 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9886 | } |
| 9887 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9888 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9889 | return 0; |
| 9890 | } |
| 9891 | |
| 9892 | /* OK we just got a valid HTTP request. We have some minor |
| 9893 | * preparation to perform so that further checks can rely |
| 9894 | * on HTTP tests. |
| 9895 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9896 | |
| 9897 | /* If the request was parsed but was too large, we must absolutely |
| 9898 | * return an error so that it is not processed. At the moment this |
| 9899 | * cannot happen, but if the parsers are to change in the future, |
| 9900 | * we want this check to be maintained. |
| 9901 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9902 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 9903 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9904 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9905 | smp->data.uint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9906 | return 1; |
| 9907 | } |
| 9908 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9909 | 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] | 9910 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 9911 | s->flags |= SN_REDIRECTABLE; |
| 9912 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9913 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9914 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9915 | } |
| 9916 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9917 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9918 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9919 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9920 | |
| 9921 | /* otherwise everything's ready for the request */ |
| 9922 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9923 | else { |
| 9924 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9925 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9926 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9927 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9928 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9929 | } |
| 9930 | |
| 9931 | /* everything's OK */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9932 | smp->data.uint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9933 | return 1; |
| 9934 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9935 | |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 9936 | /* Note: these functinos *do* modify the sample. Even in case of success, at |
| 9937 | * least the type and uint value are modified. |
| 9938 | */ |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9939 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9940 | 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] | 9941 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9942 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9943 | 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] | 9944 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9945 | |
| 9946 | /* 1. Check on METHOD |
| 9947 | * We use the pre-parsed method if it is known, and store its number as an |
| 9948 | * integer. If it is unknown, we use the pointer and the length. |
| 9949 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9950 | 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] | 9951 | { |
| 9952 | int len, meth; |
| 9953 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9954 | len = strlen(text); |
| 9955 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9956 | |
| 9957 | pattern->val.i = meth; |
| 9958 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 9959 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9960 | pattern->len = len; |
| 9961 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9962 | else { |
| 9963 | pattern->ptr.str = NULL; |
| 9964 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9965 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9966 | return 1; |
| 9967 | } |
| 9968 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9969 | /* This function fetches the method of current HTTP request and stores |
| 9970 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 9971 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 9972 | * in <len> and <ptr> is NULL ; |
| 9973 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 9974 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9975 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9976 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9977 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9978 | 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] | 9979 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9980 | { |
| 9981 | int meth; |
| 9982 | struct http_txn *txn = l7; |
| 9983 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9984 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9985 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9986 | meth = txn->meth; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9987 | smp->type = SMP_T_METH; |
| 9988 | smp->data.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9989 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9990 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9991 | /* ensure the indexes are not affected */ |
| 9992 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9993 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9994 | smp->data.meth.str.len = txn->req.sl.rq.m_l; |
| 9995 | smp->data.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9996 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9997 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9998 | return 1; |
| 9999 | } |
| 10000 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10001 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10002 | 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] | 10003 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10004 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10005 | struct pattern_list *lst; |
| 10006 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10007 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10008 | list_for_each_entry(lst, &expr->patterns, list) { |
| 10009 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10010 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10011 | /* well-known method */ |
| 10012 | if (pattern->val.i != HTTP_METH_OTHER) { |
| 10013 | if (smp->data.meth.meth == pattern->val.i) |
| 10014 | return pattern; |
| 10015 | else |
| 10016 | continue; |
| 10017 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10018 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10019 | /* Other method, we must compare the strings */ |
| 10020 | if (pattern->len != smp->data.meth.str.len) |
| 10021 | continue; |
| 10022 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10023 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | 4de2a94 | 2014-08-28 20:42:57 +0200 | [diff] [blame] | 10024 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) || |
| 10025 | (!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] | 10026 | return pattern; |
| 10027 | } |
| 10028 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10029 | } |
| 10030 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10031 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10032 | 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] | 10033 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10034 | { |
| 10035 | struct http_txn *txn = l7; |
| 10036 | char *ptr; |
| 10037 | int len; |
| 10038 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10039 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10040 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10041 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10042 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10043 | |
| 10044 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10045 | if (len <= 0) |
| 10046 | return 0; |
| 10047 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10048 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10049 | smp->data.str.str = ptr; |
| 10050 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10051 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10052 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10053 | return 1; |
| 10054 | } |
| 10055 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10056 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10057 | 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] | 10058 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10059 | { |
| 10060 | struct http_txn *txn = l7; |
| 10061 | char *ptr; |
| 10062 | int len; |
| 10063 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10064 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10065 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10066 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10067 | return 0; |
| 10068 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10069 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10070 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10071 | |
| 10072 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10073 | if (len <= 0) |
| 10074 | return 0; |
| 10075 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10076 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10077 | smp->data.str.str = ptr; |
| 10078 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10079 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10080 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10081 | return 1; |
| 10082 | } |
| 10083 | |
| 10084 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10085 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10086 | 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] | 10087 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10088 | { |
| 10089 | struct http_txn *txn = l7; |
| 10090 | char *ptr; |
| 10091 | int len; |
| 10092 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10093 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10094 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10095 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10096 | return 0; |
| 10097 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10098 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10099 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10100 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10101 | smp->type = SMP_T_UINT; |
| 10102 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10103 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10104 | return 1; |
| 10105 | } |
| 10106 | |
| 10107 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10108 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10109 | 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] | 10110 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10111 | { |
| 10112 | struct http_txn *txn = l7; |
| 10113 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10114 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10115 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10116 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10117 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10118 | 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] | 10119 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10120 | return 1; |
| 10121 | } |
| 10122 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10123 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10124 | 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] | 10125 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10126 | { |
| 10127 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10128 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10129 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10130 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10131 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10132 | 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] | 10133 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10134 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10135 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10136 | smp->type = SMP_T_IPV4; |
| 10137 | smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10138 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10139 | return 1; |
| 10140 | } |
| 10141 | |
| 10142 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10143 | 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] | 10144 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10145 | { |
| 10146 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10147 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10148 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10149 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10150 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10151 | 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] | 10152 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10153 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10154 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10155 | smp->type = SMP_T_UINT; |
| 10156 | smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10157 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10158 | return 1; |
| 10159 | } |
| 10160 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10161 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10162 | * Accepts an optional argument of type string containing the header field name, |
| 10163 | * and an optional argument of type signed or unsigned integer to request an |
| 10164 | * 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] | 10165 | * headers are considered from the first one. It does not stop on commas and |
| 10166 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10167 | */ |
| 10168 | static int |
| 10169 | 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] | 10170 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10171 | { |
| 10172 | struct http_txn *txn = l7; |
| 10173 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10174 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
| 10175 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 10176 | int occ = 0; |
| 10177 | const char *name_str = NULL; |
| 10178 | int name_len = 0; |
| 10179 | |
| 10180 | if (!ctx) { |
| 10181 | /* first call */ |
| 10182 | ctx = &static_hdr_ctx; |
| 10183 | ctx->idx = 0; |
| 10184 | smp->ctx.a[0] = ctx; |
| 10185 | } |
| 10186 | |
| 10187 | if (args) { |
| 10188 | if (args[0].type != ARGT_STR) |
| 10189 | return 0; |
| 10190 | name_str = args[0].data.str.str; |
| 10191 | name_len = args[0].data.str.len; |
| 10192 | |
| 10193 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10194 | occ = args[1].data.uint; |
| 10195 | } |
| 10196 | |
| 10197 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10198 | |
| 10199 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10200 | /* search for header from the beginning */ |
| 10201 | ctx->idx = 0; |
| 10202 | |
| 10203 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10204 | /* no explicit occurrence and single fetch => last header by default */ |
| 10205 | occ = -1; |
| 10206 | |
| 10207 | if (!occ) |
| 10208 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10209 | smp->flags |= SMP_F_NOT_LAST; |
| 10210 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10211 | smp->type = SMP_T_STR; |
| 10212 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10213 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
| 10214 | return 1; |
| 10215 | |
| 10216 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10217 | return 0; |
| 10218 | } |
| 10219 | |
| 10220 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10221 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10222 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10223 | */ |
| 10224 | static int |
| 10225 | 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] | 10226 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10227 | { |
| 10228 | struct http_txn *txn = l7; |
| 10229 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10230 | struct hdr_ctx ctx; |
| 10231 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 10232 | int cnt; |
| 10233 | |
| 10234 | if (!args || args->type != ARGT_STR) |
| 10235 | return 0; |
| 10236 | |
| 10237 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10238 | |
| 10239 | ctx.idx = 0; |
| 10240 | cnt = 0; |
| 10241 | while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx)) |
| 10242 | cnt++; |
| 10243 | |
| 10244 | smp->type = SMP_T_UINT; |
| 10245 | smp->data.uint = cnt; |
| 10246 | smp->flags = SMP_F_VOL_HDR; |
| 10247 | return 1; |
| 10248 | } |
| 10249 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10250 | static int |
| 10251 | 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] | 10252 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10253 | { |
| 10254 | struct http_txn *txn = l7; |
| 10255 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10256 | struct hdr_ctx ctx; |
| 10257 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 10258 | struct chunk *temp; |
| 10259 | char del = ','; |
| 10260 | |
| 10261 | if (args && args->type == ARGT_STR) |
| 10262 | del = *args[0].data.str.str; |
| 10263 | |
| 10264 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10265 | |
| 10266 | temp = get_trash_chunk(); |
| 10267 | |
| 10268 | ctx.idx = 0; |
| 10269 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 10270 | if (temp->len) |
| 10271 | temp->str[temp->len++] = del; |
| 10272 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 10273 | temp->len += ctx.del; |
| 10274 | } |
| 10275 | |
| 10276 | smp->type = SMP_T_STR; |
| 10277 | smp->data.str.str = temp->str; |
| 10278 | smp->data.str.len = temp->len; |
| 10279 | smp->flags = SMP_F_VOL_HDR; |
| 10280 | return 1; |
| 10281 | } |
| 10282 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10283 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10284 | * Accepts an optional argument of type string containing the header field name, |
| 10285 | * and an optional argument of type signed or unsigned integer to request an |
| 10286 | * 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] | 10287 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10288 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10289 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10290 | 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] | 10291 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10292 | { |
| 10293 | struct http_txn *txn = l7; |
| 10294 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10295 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10296 | 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] | 10297 | int occ = 0; |
| 10298 | const char *name_str = NULL; |
| 10299 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10300 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10301 | if (!ctx) { |
| 10302 | /* first call */ |
| 10303 | ctx = &static_hdr_ctx; |
| 10304 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10305 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10306 | } |
| 10307 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10308 | if (args) { |
| 10309 | if (args[0].type != ARGT_STR) |
| 10310 | return 0; |
| 10311 | name_str = args[0].data.str.str; |
| 10312 | name_len = args[0].data.str.len; |
| 10313 | |
| 10314 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10315 | occ = args[1].data.uint; |
| 10316 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10317 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10318 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10319 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10320 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10321 | /* search for header from the beginning */ |
| 10322 | ctx->idx = 0; |
| 10323 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10324 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10325 | /* no explicit occurrence and single fetch => last header by default */ |
| 10326 | occ = -1; |
| 10327 | |
| 10328 | if (!occ) |
| 10329 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10330 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10331 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10332 | smp->type = SMP_T_STR; |
| 10333 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10334 | 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] | 10335 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10336 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10337 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10338 | return 0; |
| 10339 | } |
| 10340 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10341 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10342 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10343 | */ |
| 10344 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10345 | 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] | 10346 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10347 | { |
| 10348 | struct http_txn *txn = l7; |
| 10349 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10350 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10351 | 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] | 10352 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10353 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10354 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10355 | return 0; |
| 10356 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10357 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10358 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10359 | ctx.idx = 0; |
| 10360 | cnt = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10361 | 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] | 10362 | cnt++; |
| 10363 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10364 | smp->type = SMP_T_UINT; |
| 10365 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10366 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10367 | return 1; |
| 10368 | } |
| 10369 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10370 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10371 | * takes a mandatory argument of type string and an optional one of type int |
| 10372 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10373 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10374 | */ |
| 10375 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10376 | 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] | 10377 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10378 | { |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10379 | 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] | 10380 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10381 | if (ret > 0) { |
| 10382 | smp->type = SMP_T_UINT; |
| 10383 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10384 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10385 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10386 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10387 | } |
| 10388 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10389 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10390 | * and an optional one of type int to designate a specific occurrence. |
| 10391 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10392 | */ |
| 10393 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10394 | 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] | 10395 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10396 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10397 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10398 | |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10399 | 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] | 10400 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 10401 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10402 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10403 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10404 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10405 | if (smp->data.str.len < temp->size - 1) { |
| 10406 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 10407 | temp->str[smp->data.str.len] = '\0'; |
| 10408 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 10409 | smp->type = SMP_T_IPV6; |
| 10410 | break; |
| 10411 | } |
| 10412 | } |
| 10413 | } |
| 10414 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10415 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10416 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10417 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10418 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10419 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10420 | } |
| 10421 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10422 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10423 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10424 | */ |
| 10425 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10426 | 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] | 10427 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10428 | { |
| 10429 | struct http_txn *txn = l7; |
| 10430 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10431 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10432 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10433 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10434 | 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] | 10435 | ptr = http_get_path(txn); |
| 10436 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10437 | return 0; |
| 10438 | |
| 10439 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10440 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10441 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10442 | |
| 10443 | while (ptr < end && *ptr != '?') |
| 10444 | ptr++; |
| 10445 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10446 | smp->data.str.len = ptr - smp->data.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10447 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10448 | return 1; |
| 10449 | } |
| 10450 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10451 | /* This produces a concatenation of the first occurrence of the Host header |
| 10452 | * followed by the path component if it begins with a slash ('/'). This means |
| 10453 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10454 | * If no Host header is found, then the path is returned as-is. The returned |
| 10455 | * 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] | 10456 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10457 | */ |
| 10458 | static int |
| 10459 | 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] | 10460 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10461 | { |
| 10462 | struct http_txn *txn = l7; |
| 10463 | char *ptr, *end, *beg; |
| 10464 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10465 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10466 | |
| 10467 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10468 | |
| 10469 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10470 | 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] | 10471 | return smp_fetch_path(px, l4, l7, opt, args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10472 | |
| 10473 | /* 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] | 10474 | temp = get_trash_chunk(); |
| 10475 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10476 | smp->type = SMP_T_STR; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10477 | smp->data.str.str = temp->str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10478 | smp->data.str.len = ctx.vlen; |
| 10479 | |
| 10480 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10481 | 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] | 10482 | beg = http_get_path(txn); |
| 10483 | if (!beg) |
| 10484 | beg = end; |
| 10485 | |
| 10486 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10487 | |
| 10488 | if (beg < ptr && *beg == '/') { |
| 10489 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 10490 | smp->data.str.len += ptr - beg; |
| 10491 | } |
| 10492 | |
| 10493 | smp->flags = SMP_F_VOL_1ST; |
| 10494 | return 1; |
| 10495 | } |
| 10496 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10497 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10498 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10499 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10500 | * 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] | 10501 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10502 | * 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] | 10503 | * high-traffic sites without having to store whole paths. |
| 10504 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10505 | int |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10506 | 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] | 10507 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10508 | { |
| 10509 | struct http_txn *txn = l7; |
| 10510 | struct hdr_ctx ctx; |
| 10511 | unsigned int hash = 0; |
| 10512 | char *ptr, *beg, *end; |
| 10513 | int len; |
| 10514 | |
| 10515 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10516 | |
| 10517 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10518 | 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] | 10519 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10520 | ptr = ctx.line + ctx.val; |
| 10521 | len = ctx.vlen; |
| 10522 | while (len--) |
| 10523 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10524 | } |
| 10525 | |
| 10526 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10527 | 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] | 10528 | beg = http_get_path(txn); |
| 10529 | if (!beg) |
| 10530 | beg = end; |
| 10531 | |
| 10532 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10533 | |
| 10534 | if (beg < ptr && *beg == '/') { |
| 10535 | while (beg < ptr) |
| 10536 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10537 | } |
| 10538 | hash = full_hash(hash); |
| 10539 | |
| 10540 | smp->type = SMP_T_UINT; |
| 10541 | smp->data.uint = hash; |
| 10542 | smp->flags = SMP_F_VOL_1ST; |
| 10543 | return 1; |
| 10544 | } |
| 10545 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10546 | /* 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] | 10547 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10548 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10549 | * 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] | 10550 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10551 | * 8 bytes would still work. |
| 10552 | */ |
| 10553 | static int |
| 10554 | 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] | 10555 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10556 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10557 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10558 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
| 10559 | |
| 10560 | if (!cli_conn) |
| 10561 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10562 | |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10563 | if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10564 | return 0; |
| 10565 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10566 | temp = get_trash_chunk(); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10567 | *(unsigned int *)temp->str = htonl(smp->data.uint); |
| 10568 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10569 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10570 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10571 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10572 | 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] | 10573 | temp->len += 4; |
| 10574 | break; |
| 10575 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10576 | 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] | 10577 | temp->len += 16; |
| 10578 | break; |
| 10579 | default: |
| 10580 | return 0; |
| 10581 | } |
| 10582 | |
| 10583 | smp->data.str = *temp; |
| 10584 | smp->type = SMP_T_BIN; |
| 10585 | return 1; |
| 10586 | } |
| 10587 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10588 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10589 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10590 | * of type string carrying the whole query string. |
| 10591 | */ |
| 10592 | static int |
| 10593 | 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] | 10594 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10595 | { |
| 10596 | struct http_txn *txn = l7; |
| 10597 | char *ptr, *end; |
| 10598 | |
| 10599 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10600 | |
| 10601 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 10602 | end = ptr + txn->req.sl.rq.u_l; |
| 10603 | |
| 10604 | /* look up the '?' */ |
| 10605 | do { |
| 10606 | if (ptr == end) |
| 10607 | return 0; |
| 10608 | } while (*ptr++ != '?'); |
| 10609 | |
| 10610 | smp->type = SMP_T_STR; |
| 10611 | smp->data.str.str = ptr; |
| 10612 | smp->data.str.len = end - ptr; |
| 10613 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10614 | return 1; |
| 10615 | } |
| 10616 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10617 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10618 | 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] | 10619 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10620 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10621 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10622 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10623 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10624 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10625 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10626 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10627 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 10628 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10629 | return 1; |
| 10630 | } |
| 10631 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10632 | /* return a valid test if the current request is the first one on the connection */ |
| 10633 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10634 | 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] | 10635 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10636 | { |
| 10637 | if (!s) |
| 10638 | return 0; |
| 10639 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10640 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 10641 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10642 | return 1; |
| 10643 | } |
| 10644 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10645 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10646 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10647 | 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] | 10648 | 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] | 10649 | { |
| 10650 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10651 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10652 | return 0; |
| 10653 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10654 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10655 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10656 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10657 | return 0; |
| 10658 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10659 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10660 | 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] | 10661 | return 1; |
| 10662 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10663 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10664 | /* Accepts exactly 1 argument of type userlist */ |
| 10665 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10666 | 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] | 10667 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10668 | { |
| 10669 | |
| 10670 | if (!args || args->type != ARGT_USR) |
| 10671 | return 0; |
| 10672 | |
| 10673 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10674 | |
| 10675 | if (!get_http_auth(l4)) |
| 10676 | return 0; |
| 10677 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10678 | /* if the user does not belong to the userlist or has a wrong password, |
| 10679 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10680 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10681 | */ |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10682 | if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass)) |
| 10683 | return 0; |
| 10684 | |
| 10685 | /* pat_match_auth() will need the user list */ |
| 10686 | smp->ctx.a[0] = args->data.usr; |
| 10687 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10688 | smp->type = SMP_T_STR; |
| 10689 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10690 | smp->data.str.str = l4->txn.auth.user; |
| 10691 | smp->data.str.len = strlen(l4->txn.auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10692 | |
| 10693 | return 1; |
| 10694 | } |
| 10695 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10696 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10697 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10698 | * the cookie value is returned into *value and *value_l, and the function |
| 10699 | * returns a pointer to the next pointer to search from if the value was found. |
| 10700 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10701 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10702 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10703 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10704 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10705 | * list is expected. |
| 10706 | */ |
| 10707 | static char * |
| 10708 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10709 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10710 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10711 | { |
| 10712 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10713 | char *next; |
| 10714 | |
| 10715 | /* we search at least a cookie name followed by an equal, and more |
| 10716 | * generally something like this : |
| 10717 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10718 | */ |
| 10719 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10720 | /* Iterate through all cookies on this line */ |
| 10721 | |
| 10722 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 10723 | att_beg++; |
| 10724 | |
| 10725 | /* find att_end : this is the first character after the last non |
| 10726 | * space before the equal. It may be equal to hdr_end. |
| 10727 | */ |
| 10728 | equal = att_end = att_beg; |
| 10729 | |
| 10730 | while (equal < hdr_end) { |
| 10731 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10732 | break; |
| 10733 | if (http_is_spht[(unsigned char)*equal++]) |
| 10734 | continue; |
| 10735 | att_end = equal; |
| 10736 | } |
| 10737 | |
| 10738 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10739 | * is between <att_beg> and <equal>, both may be identical. |
| 10740 | */ |
| 10741 | |
| 10742 | /* look for end of cookie if there is an equal sign */ |
| 10743 | if (equal < hdr_end && *equal == '=') { |
| 10744 | /* look for the beginning of the value */ |
| 10745 | val_beg = equal + 1; |
| 10746 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 10747 | val_beg++; |
| 10748 | |
| 10749 | /* find the end of the value, respecting quotes */ |
| 10750 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10751 | |
| 10752 | /* make val_end point to the first white space or delimitor after the value */ |
| 10753 | val_end = next; |
| 10754 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 10755 | val_end--; |
| 10756 | } else { |
| 10757 | val_beg = val_end = next = equal; |
| 10758 | } |
| 10759 | |
| 10760 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10761 | * they will automatically be removed if a header before them is removed, |
| 10762 | * since they're supposed to be linked together. |
| 10763 | */ |
| 10764 | if (*att_beg == '$') |
| 10765 | continue; |
| 10766 | |
| 10767 | /* Ignore cookies with no equal sign */ |
| 10768 | if (equal == next) |
| 10769 | continue; |
| 10770 | |
| 10771 | /* Now we have the cookie name between att_beg and att_end, and |
| 10772 | * its value between val_beg and val_end. |
| 10773 | */ |
| 10774 | |
| 10775 | if (att_end - att_beg == cookie_name_l && |
| 10776 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10777 | /* let's return this value and indicate where to go on from */ |
| 10778 | *value = val_beg; |
| 10779 | *value_l = val_end - val_beg; |
| 10780 | return next + 1; |
| 10781 | } |
| 10782 | |
| 10783 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10784 | if (!list) |
| 10785 | break; |
| 10786 | } |
| 10787 | |
| 10788 | return NULL; |
| 10789 | } |
| 10790 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10791 | /* Fetch a captured HTTP request header. The index is the position of |
| 10792 | * the "capture" option in the configuration file |
| 10793 | */ |
| 10794 | static int |
| 10795 | 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] | 10796 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10797 | { |
| 10798 | struct proxy *fe = l4->fe; |
| 10799 | struct http_txn *txn = l7; |
| 10800 | int idx; |
| 10801 | |
| 10802 | if (!args || args->type != ARGT_UINT) |
| 10803 | return 0; |
| 10804 | |
| 10805 | idx = args->data.uint; |
| 10806 | |
| 10807 | if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL) |
| 10808 | return 0; |
| 10809 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10810 | smp->type = SMP_T_STR; |
| 10811 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10812 | smp->data.str.str = txn->req.cap[idx]; |
| 10813 | smp->data.str.len = strlen(txn->req.cap[idx]); |
| 10814 | |
| 10815 | return 1; |
| 10816 | } |
| 10817 | |
| 10818 | /* Fetch a captured HTTP response header. The index is the position of |
| 10819 | * the "capture" option in the configuration file |
| 10820 | */ |
| 10821 | static int |
| 10822 | 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] | 10823 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10824 | { |
| 10825 | struct proxy *fe = l4->fe; |
| 10826 | struct http_txn *txn = l7; |
| 10827 | int idx; |
| 10828 | |
| 10829 | if (!args || args->type != ARGT_UINT) |
| 10830 | return 0; |
| 10831 | |
| 10832 | idx = args->data.uint; |
| 10833 | |
| 10834 | if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL) |
| 10835 | return 0; |
| 10836 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10837 | smp->type = SMP_T_STR; |
| 10838 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10839 | smp->data.str.str = txn->rsp.cap[idx]; |
| 10840 | smp->data.str.len = strlen(txn->rsp.cap[idx]); |
| 10841 | |
| 10842 | return 1; |
| 10843 | } |
| 10844 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10845 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 10846 | static int |
| 10847 | 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] | 10848 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10849 | { |
| 10850 | struct chunk *temp; |
| 10851 | struct http_txn *txn = l7; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10852 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10853 | |
| 10854 | if (!txn->uri) |
| 10855 | return 0; |
| 10856 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10857 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10858 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10859 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10860 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10861 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10862 | temp = get_trash_chunk(); |
| 10863 | temp->str = txn->uri; |
| 10864 | temp->len = ptr - txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10865 | smp->data.str = *temp; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10866 | smp->type = SMP_T_STR; |
| 10867 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10868 | |
| 10869 | return 1; |
| 10870 | |
| 10871 | } |
| 10872 | |
| 10873 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 10874 | static int |
| 10875 | 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] | 10876 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10877 | { |
| 10878 | struct chunk *temp; |
| 10879 | struct http_txn *txn = l7; |
| 10880 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10881 | |
| 10882 | if (!txn->uri) |
| 10883 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10884 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10885 | ptr = txn->uri; |
| 10886 | |
| 10887 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10888 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10889 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10890 | if (!*ptr) |
| 10891 | return 0; |
| 10892 | |
| 10893 | ptr++; /* skip the space */ |
| 10894 | |
| 10895 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10896 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10897 | if (!ptr) |
| 10898 | return 0; |
| 10899 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 10900 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10901 | |
| 10902 | smp->data.str = *temp; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10903 | smp->data.str.len = ptr - temp->str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10904 | smp->type = SMP_T_STR; |
| 10905 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10906 | |
| 10907 | return 1; |
| 10908 | } |
| 10909 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10910 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 10911 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10912 | */ |
| 10913 | static int |
| 10914 | 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] | 10915 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10916 | { |
| 10917 | struct http_txn *txn = l7; |
| 10918 | |
| 10919 | if (txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
| 10920 | return 0; |
| 10921 | |
| 10922 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 10923 | smp->data.str.str = "HTTP/1.1"; |
| 10924 | else |
| 10925 | smp->data.str.str = "HTTP/1.0"; |
| 10926 | |
| 10927 | smp->data.str.len = 8; |
| 10928 | smp->type = SMP_T_STR; |
| 10929 | smp->flags = SMP_F_CONST; |
| 10930 | return 1; |
| 10931 | |
| 10932 | } |
| 10933 | |
| 10934 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 10935 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10936 | */ |
| 10937 | static int |
| 10938 | 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] | 10939 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10940 | { |
| 10941 | struct http_txn *txn = l7; |
| 10942 | |
| 10943 | if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
| 10944 | return 0; |
| 10945 | |
| 10946 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
| 10947 | smp->data.str.str = "HTTP/1.1"; |
| 10948 | else |
| 10949 | smp->data.str.str = "HTTP/1.0"; |
| 10950 | |
| 10951 | smp->data.str.len = 8; |
| 10952 | smp->type = SMP_T_STR; |
| 10953 | smp->flags = SMP_F_CONST; |
| 10954 | return 1; |
| 10955 | |
| 10956 | } |
| 10957 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10958 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10959 | /* 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] | 10960 | * 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] | 10961 | * 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] | 10962 | * 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] | 10963 | * 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] | 10964 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 10965 | * 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] | 10966 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 10967 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10968 | */ |
William Lallemand | 07c8b24 | 2014-05-02 17:11:07 +0200 | [diff] [blame] | 10969 | 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] | 10970 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10971 | { |
| 10972 | struct http_txn *txn = l7; |
| 10973 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10974 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10975 | const struct http_msg *msg; |
| 10976 | const char *hdr_name; |
| 10977 | int hdr_name_len; |
| 10978 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10979 | int occ = 0; |
| 10980 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10981 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10982 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10983 | return 0; |
| 10984 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10985 | if (!ctx) { |
| 10986 | /* first call */ |
| 10987 | ctx = &static_hdr_ctx; |
| 10988 | ctx->idx = 0; |
| 10989 | smp->ctx.a[2] = ctx; |
| 10990 | } |
| 10991 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10992 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10993 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10994 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10995 | msg = &txn->req; |
| 10996 | hdr_name = "Cookie"; |
| 10997 | hdr_name_len = 6; |
| 10998 | } else { |
| 10999 | msg = &txn->rsp; |
| 11000 | hdr_name = "Set-Cookie"; |
| 11001 | hdr_name_len = 10; |
| 11002 | } |
| 11003 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11004 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 11005 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 11006 | occ = -1; |
| 11007 | |
| 11008 | /* OK so basically here, either we want only one value and it's the |
| 11009 | * last one, or we want to iterate over all of them and we fetch the |
| 11010 | * next one. |
| 11011 | */ |
| 11012 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11013 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11014 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11015 | /* search for the header from the beginning, we must first initialize |
| 11016 | * the search parameters. |
| 11017 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11018 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11019 | ctx->idx = 0; |
| 11020 | } |
| 11021 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11022 | smp->flags |= SMP_F_VOL_HDR; |
| 11023 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11024 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11025 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 11026 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11027 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 11028 | goto out; |
| 11029 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11030 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11031 | continue; |
| 11032 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11033 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 11034 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11035 | } |
| 11036 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11037 | smp->type = SMP_T_STR; |
| 11038 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11039 | 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] | 11040 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 11041 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11042 | &smp->data.str.str, |
| 11043 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11044 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11045 | found = 1; |
| 11046 | if (occ >= 0) { |
| 11047 | /* one value was returned into smp->data.str.{str,len} */ |
| 11048 | smp->flags |= SMP_F_NOT_LAST; |
| 11049 | return 1; |
| 11050 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11051 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11052 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11053 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11054 | /* all cookie headers and values were scanned. If we're looking for the |
| 11055 | * last occurrence, we may return it now. |
| 11056 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11057 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11058 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11059 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11060 | } |
| 11061 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11062 | /* 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] | 11063 | * 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] | 11064 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 11065 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11066 | */ |
| 11067 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11068 | 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] | 11069 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11070 | { |
| 11071 | struct http_txn *txn = l7; |
| 11072 | struct hdr_idx *idx = &txn->hdr_idx; |
| 11073 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11074 | const struct http_msg *msg; |
| 11075 | const char *hdr_name; |
| 11076 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11077 | int cnt; |
| 11078 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11079 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11080 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11081 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11082 | return 0; |
| 11083 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11084 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11085 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 11086 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11087 | msg = &txn->req; |
| 11088 | hdr_name = "Cookie"; |
| 11089 | hdr_name_len = 6; |
| 11090 | } else { |
| 11091 | msg = &txn->rsp; |
| 11092 | hdr_name = "Set-Cookie"; |
| 11093 | hdr_name_len = 10; |
| 11094 | } |
| 11095 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11096 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 11097 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11098 | ctx.idx = 0; |
| 11099 | cnt = 0; |
| 11100 | |
| 11101 | while (1) { |
| 11102 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 11103 | if (!val_beg) { |
| 11104 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 11105 | break; |
| 11106 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11107 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11108 | continue; |
| 11109 | |
| 11110 | val_beg = ctx.line + ctx.val; |
| 11111 | val_end = val_beg + ctx.vlen; |
| 11112 | } |
| 11113 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11114 | smp->type = SMP_T_STR; |
| 11115 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11116 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11117 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 11118 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11119 | &smp->data.str.str, |
| 11120 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11121 | cnt++; |
| 11122 | } |
| 11123 | } |
| 11124 | |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 11125 | smp->type = SMP_T_UINT; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11126 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11127 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11128 | return 1; |
| 11129 | } |
| 11130 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11131 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 11132 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 11133 | */ |
| 11134 | static int |
| 11135 | 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] | 11136 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11137 | { |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 11138 | 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] | 11139 | |
| 11140 | if (ret > 0) { |
| 11141 | smp->type = SMP_T_UINT; |
| 11142 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 11143 | } |
| 11144 | |
| 11145 | return ret; |
| 11146 | } |
| 11147 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11148 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11149 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11150 | /************************************************************************/ |
| 11151 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11152 | /* |
| 11153 | * Given a path string and its length, find the position of beginning of the |
| 11154 | * query string. Returns NULL if no query string is found in the path. |
| 11155 | * |
| 11156 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11157 | * |
| 11158 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 11159 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11160 | 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] | 11161 | { |
| 11162 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11163 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11164 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11165 | return p ? p + 1 : NULL; |
| 11166 | } |
| 11167 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11168 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11169 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11170 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11171 | } |
| 11172 | |
| 11173 | /* |
| 11174 | * Given a url parameter, find the starting position of the first occurence, |
| 11175 | * or NULL if the parameter is not found. |
| 11176 | * |
| 11177 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11178 | * the function will return query_string+8. |
| 11179 | */ |
| 11180 | static char* |
| 11181 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11182 | char* url_param_name, size_t url_param_name_l, |
| 11183 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11184 | { |
| 11185 | char *pos, *last; |
| 11186 | |
| 11187 | pos = query_string; |
| 11188 | last = query_string + query_string_l - url_param_name_l - 1; |
| 11189 | |
| 11190 | while (pos <= last) { |
| 11191 | if (pos[url_param_name_l] == '=') { |
| 11192 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 11193 | return pos; |
| 11194 | pos += url_param_name_l + 1; |
| 11195 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11196 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11197 | pos++; |
| 11198 | pos++; |
| 11199 | } |
| 11200 | return NULL; |
| 11201 | } |
| 11202 | |
| 11203 | /* |
| 11204 | * Given a url parameter name, returns its value and size into *value and |
| 11205 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 11206 | * zero is returned and value/value_l are not touched. |
| 11207 | */ |
| 11208 | static int |
| 11209 | find_url_param_value(char* path, size_t path_l, |
| 11210 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11211 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11212 | { |
| 11213 | char *query_string, *qs_end; |
| 11214 | char *arg_start; |
| 11215 | char *value_start, *value_end; |
| 11216 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11217 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11218 | if (!query_string) |
| 11219 | return 0; |
| 11220 | |
| 11221 | qs_end = path + path_l; |
| 11222 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11223 | url_param_name, url_param_name_l, |
| 11224 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11225 | if (!arg_start) |
| 11226 | return 0; |
| 11227 | |
| 11228 | value_start = arg_start + url_param_name_l + 1; |
| 11229 | value_end = value_start; |
| 11230 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11231 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11232 | value_end++; |
| 11233 | |
| 11234 | *value = value_start; |
| 11235 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 11236 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11237 | } |
| 11238 | |
| 11239 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11240 | 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] | 11241 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11242 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11243 | char delim = '?'; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11244 | struct http_txn *txn = l7; |
| 11245 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11246 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11247 | if (!args || args[0].type != ARGT_STR || |
| 11248 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11249 | return 0; |
| 11250 | |
| 11251 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11252 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11253 | if (args[1].type) |
| 11254 | delim = *args[1].data.str.str; |
| 11255 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11256 | 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] | 11257 | args->data.str.str, args->data.str.len, |
| 11258 | &smp->data.str.str, &smp->data.str.len, |
| 11259 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11260 | return 0; |
| 11261 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11262 | smp->type = SMP_T_STR; |
| 11263 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11264 | return 1; |
| 11265 | } |
| 11266 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11267 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11268 | * above). |
| 11269 | */ |
| 11270 | static int |
| 11271 | 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] | 11272 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11273 | { |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 11274 | 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] | 11275 | |
| 11276 | if (ret > 0) { |
| 11277 | smp->type = SMP_T_UINT; |
| 11278 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 11279 | } |
| 11280 | |
| 11281 | return ret; |
| 11282 | } |
| 11283 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11284 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11285 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11286 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11287 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11288 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11289 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11290 | * high-traffic sites without having to store whole paths. |
| 11291 | * this differs from the base32 functions in that it includes the url parameters |
| 11292 | * as well as the path |
| 11293 | */ |
| 11294 | static int |
| 11295 | 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] | 11296 | 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] | 11297 | { |
| 11298 | struct http_txn *txn = l7; |
| 11299 | struct hdr_ctx ctx; |
| 11300 | unsigned int hash = 0; |
| 11301 | char *ptr, *beg, *end; |
| 11302 | int len; |
| 11303 | |
| 11304 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11305 | |
| 11306 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11307 | 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] | 11308 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11309 | ptr = ctx.line + ctx.val; |
| 11310 | len = ctx.vlen; |
| 11311 | while (len--) |
| 11312 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11313 | } |
| 11314 | |
| 11315 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11316 | 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] | 11317 | beg = http_get_path(txn); |
| 11318 | if (!beg) |
| 11319 | beg = end; |
| 11320 | |
| 11321 | for (ptr = beg; ptr < end ; ptr++); |
| 11322 | |
| 11323 | if (beg < ptr && *beg == '/') { |
| 11324 | while (beg < ptr) |
| 11325 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11326 | } |
| 11327 | hash = full_hash(hash); |
| 11328 | |
| 11329 | smp->type = SMP_T_UINT; |
| 11330 | smp->data.uint = hash; |
| 11331 | smp->flags = SMP_F_VOL_1ST; |
| 11332 | return 1; |
| 11333 | } |
| 11334 | |
| 11335 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11336 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11337 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11338 | * on the source address length. The URL hash is stored before the address so |
| 11339 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11340 | * 8 bytes would still work. |
| 11341 | */ |
| 11342 | static int |
| 11343 | 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] | 11344 | 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] | 11345 | { |
| 11346 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11347 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11348 | |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 11349 | 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] | 11350 | return 0; |
| 11351 | |
| 11352 | temp = get_trash_chunk(); |
| 11353 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 11354 | temp->len += sizeof(smp->data.uint); |
| 11355 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11356 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11357 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11358 | 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] | 11359 | temp->len += 4; |
| 11360 | break; |
| 11361 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11362 | 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] | 11363 | temp->len += 16; |
| 11364 | break; |
| 11365 | default: |
| 11366 | return 0; |
| 11367 | } |
| 11368 | |
| 11369 | smp->data.str = *temp; |
| 11370 | smp->type = SMP_T_BIN; |
| 11371 | return 1; |
| 11372 | } |
| 11373 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11374 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11375 | * keyword. These keywords support an optional positive or negative occurrence |
| 11376 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11377 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11378 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11379 | * error message in case of error, that the caller is responsible for freeing. |
| 11380 | * The initial location must either be freeable or NULL. |
| 11381 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 11382 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11383 | { |
| 11384 | 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] | 11385 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11386 | return 0; |
| 11387 | } |
| 11388 | return 1; |
| 11389 | } |
| 11390 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11391 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11392 | * adds an optional offset found in args[0] and emits a string representing |
| 11393 | * the date in RFC-1123/5322 format. |
| 11394 | */ |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 11395 | static int sample_conv_http_date(struct session *session, const struct arg *args, |
| 11396 | struct sample *smp, void *private) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11397 | { |
| 11398 | const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; |
| 11399 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11400 | struct chunk *temp; |
| 11401 | struct tm *tm; |
| 11402 | time_t curr_date = smp->data.uint; |
| 11403 | |
| 11404 | /* add offset */ |
| 11405 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 11406 | curr_date += args[0].data.sint; |
| 11407 | |
| 11408 | tm = gmtime(&curr_date); |
| 11409 | |
| 11410 | temp = get_trash_chunk(); |
| 11411 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11412 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11413 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11414 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11415 | |
| 11416 | smp->data.str = *temp; |
| 11417 | smp->type = SMP_T_STR; |
| 11418 | return 1; |
| 11419 | } |
| 11420 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11421 | /* Match language range with language tag. RFC2616 14.4: |
| 11422 | * |
| 11423 | * A language-range matches a language-tag if it exactly equals |
| 11424 | * the tag, or if it exactly equals a prefix of the tag such |
| 11425 | * that the first tag character following the prefix is "-". |
| 11426 | * |
| 11427 | * Return 1 if the strings match, else return 0. |
| 11428 | */ |
| 11429 | static inline int language_range_match(const char *range, int range_len, |
| 11430 | const char *tag, int tag_len) |
| 11431 | { |
| 11432 | const char *end = range + range_len; |
| 11433 | const char *tend = tag + tag_len; |
| 11434 | while (range < end) { |
| 11435 | if (*range == '-' && tag == tend) |
| 11436 | return 1; |
| 11437 | if (*range != *tag || tag == tend) |
| 11438 | return 0; |
| 11439 | range++; |
| 11440 | tag++; |
| 11441 | } |
| 11442 | /* Return true only if the last char of the tag is matched. */ |
| 11443 | return tag == tend; |
| 11444 | } |
| 11445 | |
| 11446 | /* 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] | 11447 | static int sample_conv_q_prefered(struct session *session, const struct arg *args, |
| 11448 | struct sample *smp, void *private) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11449 | { |
| 11450 | const char *al = smp->data.str.str; |
| 11451 | const char *end = al + smp->data.str.len; |
| 11452 | const char *token; |
| 11453 | int toklen; |
| 11454 | int qvalue; |
| 11455 | const char *str; |
| 11456 | const char *w; |
| 11457 | int best_q = 0; |
| 11458 | |
| 11459 | /* Set the constant to the sample, because the output of the |
| 11460 | * function will be peek in the constant configuration string. |
| 11461 | */ |
| 11462 | smp->flags |= SMP_F_CONST; |
| 11463 | smp->data.str.size = 0; |
| 11464 | smp->data.str.str = ""; |
| 11465 | smp->data.str.len = 0; |
| 11466 | |
| 11467 | /* Parse the accept language */ |
| 11468 | while (1) { |
| 11469 | |
| 11470 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11471 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11472 | al++; |
| 11473 | if (al >= end) |
| 11474 | break; |
| 11475 | |
| 11476 | /* Start of the fisrt word. */ |
| 11477 | token = al; |
| 11478 | |
| 11479 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11480 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11481 | al++; |
| 11482 | if (al == token) |
| 11483 | goto expect_comma; |
| 11484 | |
| 11485 | /* Length of the token. */ |
| 11486 | toklen = al - token; |
| 11487 | qvalue = 1000; |
| 11488 | |
| 11489 | /* Check if the token exists in the list. If the token not exists, |
| 11490 | * jump to the next token. |
| 11491 | */ |
| 11492 | str = args[0].data.str.str; |
| 11493 | w = str; |
| 11494 | while (1) { |
| 11495 | if (*str == ';' || *str == '\0') { |
| 11496 | if (language_range_match(token, toklen, w, str-w)) |
| 11497 | goto look_for_q; |
| 11498 | if (*str == '\0') |
| 11499 | goto expect_comma; |
| 11500 | w = str + 1; |
| 11501 | } |
| 11502 | str++; |
| 11503 | } |
| 11504 | goto expect_comma; |
| 11505 | |
| 11506 | look_for_q: |
| 11507 | |
| 11508 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11509 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11510 | al++; |
| 11511 | if (al >= end) |
| 11512 | goto process_value; |
| 11513 | |
| 11514 | /* If ',' is found, process the result */ |
| 11515 | if (*al == ',') |
| 11516 | goto process_value; |
| 11517 | |
| 11518 | /* If the character is different from ';', look |
| 11519 | * for the end of the header part in best effort. |
| 11520 | */ |
| 11521 | if (*al != ';') |
| 11522 | goto expect_comma; |
| 11523 | |
| 11524 | /* Assumes that the char is ';', now expect "q=". */ |
| 11525 | al++; |
| 11526 | |
| 11527 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11528 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11529 | al++; |
| 11530 | if (al >= end) |
| 11531 | goto process_value; |
| 11532 | |
| 11533 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11534 | if (*al != 'q') |
| 11535 | goto process_value; |
| 11536 | al++; |
| 11537 | |
| 11538 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11539 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11540 | al++; |
| 11541 | if (al >= end) |
| 11542 | goto process_value; |
| 11543 | |
| 11544 | /* Expect '='. If no '=', continue in best effort */ |
| 11545 | if (*al != '=') |
| 11546 | goto process_value; |
| 11547 | al++; |
| 11548 | |
| 11549 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11550 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11551 | al++; |
| 11552 | if (al >= end) |
| 11553 | goto process_value; |
| 11554 | |
| 11555 | /* Parse the q value. */ |
| 11556 | qvalue = parse_qvalue(al, &al); |
| 11557 | |
| 11558 | process_value: |
| 11559 | |
| 11560 | /* If the new q value is the best q value, then store the associated |
| 11561 | * language in the response. If qvalue is the biggest value (1000), |
| 11562 | * break the process. |
| 11563 | */ |
| 11564 | if (qvalue > best_q) { |
| 11565 | smp->data.str.str = (char *)w; |
| 11566 | smp->data.str.len = str - w; |
| 11567 | if (qvalue >= 1000) |
| 11568 | break; |
| 11569 | best_q = qvalue; |
| 11570 | } |
| 11571 | |
| 11572 | expect_comma: |
| 11573 | |
| 11574 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11575 | while (al < end && *al != ',') |
| 11576 | al++; |
| 11577 | if (al >= end) |
| 11578 | break; |
| 11579 | |
| 11580 | /* Comma is found, jump it and restart the analyzer. */ |
| 11581 | al++; |
| 11582 | } |
| 11583 | |
| 11584 | /* Set default value if required. */ |
| 11585 | if (smp->data.str.len == 0 && args[1].type == ARGT_STR) { |
| 11586 | smp->data.str.str = args[1].data.str.str; |
| 11587 | smp->data.str.len = args[1].data.str.len; |
| 11588 | } |
| 11589 | |
| 11590 | /* Return true only if a matching language was found. */ |
| 11591 | return smp->data.str.len != 0; |
| 11592 | } |
| 11593 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11594 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 11595 | * builds a string in the trash from the specified format string, then modifies |
| 11596 | * the relevant part of the request line accordingly. Then it updates various |
| 11597 | * pointers to the next elements which were moved, and the total buffer length. |
| 11598 | * It finds the action to be performed in p[2], previously filled by function |
Thierry FOURNIER | 01c3012 | 2015-03-14 14:14:47 +0100 | [diff] [blame] | 11599 | * parse_set_req_line(). It always returns 1. If an error occurs the action |
| 11600 | * is canceled, but the rule processing continue. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11601 | */ |
| 11602 | int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct session *s, struct http_txn *txn) |
| 11603 | { |
| 11604 | char *cur_ptr, *cur_end; |
| 11605 | int offset; |
| 11606 | int delta; |
| 11607 | |
| 11608 | chunk_reset(&trash); |
| 11609 | |
| 11610 | /* prepare a '?' just in case we have to create a query string */ |
| 11611 | trash.str[trash.len++] = '?'; |
| 11612 | offset = 1; |
| 11613 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]); |
| 11614 | |
| 11615 | switch (*(int *)&rule->arg.act.p[2]) { |
| 11616 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11617 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11618 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 11619 | |
| 11620 | /* adjust req line offsets and lengths */ |
| 11621 | delta = trash.len - offset - (cur_end - cur_ptr); |
| 11622 | txn->req.sl.rq.m_l += delta; |
| 11623 | txn->req.sl.rq.u += delta; |
| 11624 | txn->req.sl.rq.v += delta; |
| 11625 | break; |
| 11626 | |
| 11627 | case 1: // path |
| 11628 | cur_ptr = http_get_path(txn); |
| 11629 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11630 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11631 | |
| 11632 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11633 | while (cur_end < s->req.buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?') |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11634 | cur_end++; |
| 11635 | |
| 11636 | /* adjust req line offsets and lengths */ |
| 11637 | delta = trash.len - offset - (cur_end - cur_ptr); |
| 11638 | txn->req.sl.rq.u_l += delta; |
| 11639 | txn->req.sl.rq.v += delta; |
| 11640 | break; |
| 11641 | |
| 11642 | case 2: // query |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11643 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11644 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11645 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 11646 | cur_ptr++; |
| 11647 | |
| 11648 | /* skip the question mark or indicate that we must insert it |
| 11649 | * (but only if the format string is not empty then). |
| 11650 | */ |
| 11651 | if (cur_ptr < cur_end) |
| 11652 | cur_ptr++; |
| 11653 | else if (trash.len > 1) |
| 11654 | offset = 0; |
| 11655 | |
| 11656 | /* adjust req line offsets and lengths */ |
| 11657 | delta = trash.len - offset - (cur_end - cur_ptr); |
| 11658 | txn->req.sl.rq.u_l += delta; |
| 11659 | txn->req.sl.rq.v += delta; |
| 11660 | break; |
| 11661 | |
| 11662 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11663 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11664 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11665 | |
| 11666 | /* adjust req line offsets and lengths */ |
| 11667 | delta = trash.len - offset - (cur_end - cur_ptr); |
| 11668 | txn->req.sl.rq.u_l += delta; |
| 11669 | txn->req.sl.rq.v += delta; |
| 11670 | break; |
| 11671 | |
| 11672 | default: |
Thierry FOURNIER | 01c3012 | 2015-03-14 14:14:47 +0100 | [diff] [blame] | 11673 | return 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11674 | } |
| 11675 | |
| 11676 | /* commit changes and adjust end of message */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11677 | delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, trash.str + offset, trash.len - offset); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11678 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | 01c3012 | 2015-03-14 14:14:47 +0100 | [diff] [blame] | 11679 | return 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11680 | } |
| 11681 | |
| 11682 | /* parse an http-request action among : |
| 11683 | * set-method |
| 11684 | * set-path |
| 11685 | * set-query |
| 11686 | * set-uri |
| 11687 | * |
| 11688 | * All of them accept a single argument of type string representing a log-format. |
| 11689 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 11690 | * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri). |
| 11691 | * It returns 0 on success, < 0 on error. |
| 11692 | */ |
| 11693 | int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err) |
| 11694 | { |
| 11695 | int cur_arg = *orig_arg; |
| 11696 | |
| 11697 | rule->action = HTTP_REQ_ACT_CUSTOM_CONT; |
| 11698 | |
| 11699 | switch (args[0][4]) { |
| 11700 | case 'm' : |
| 11701 | *(int *)&rule->arg.act.p[2] = 0; |
| 11702 | rule->action_ptr = http_action_set_req_line; |
| 11703 | break; |
| 11704 | case 'p' : |
| 11705 | *(int *)&rule->arg.act.p[2] = 1; |
| 11706 | rule->action_ptr = http_action_set_req_line; |
| 11707 | break; |
| 11708 | case 'q' : |
| 11709 | *(int *)&rule->arg.act.p[2] = 2; |
| 11710 | rule->action_ptr = http_action_set_req_line; |
| 11711 | break; |
| 11712 | case 'u' : |
| 11713 | *(int *)&rule->arg.act.p[2] = 3; |
| 11714 | rule->action_ptr = http_action_set_req_line; |
| 11715 | break; |
| 11716 | default: |
| 11717 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
| 11718 | return -1; |
| 11719 | } |
| 11720 | |
| 11721 | if (!*args[cur_arg] || |
| 11722 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 11723 | memprintf(err, "expects exactly 1 argument <format>"); |
| 11724 | return -1; |
| 11725 | } |
| 11726 | |
| 11727 | LIST_INIT((struct list *)&rule->arg.act.p[0]); |
| 11728 | proxy->conf.args.ctx = ARGC_HRQ; |
| 11729 | parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP, |
| 11730 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 11731 | proxy->conf.args.file, proxy->conf.args.line); |
| 11732 | |
| 11733 | (*orig_arg)++; |
| 11734 | return 0; |
| 11735 | } |
| 11736 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 11737 | /* |
| 11738 | * Return the struct http_req_action_kw associated to a keyword. |
| 11739 | */ |
| 11740 | struct http_req_action_kw *action_http_req_custom(const char *kw) |
| 11741 | { |
| 11742 | if (!LIST_ISEMPTY(&http_req_keywords.list)) { |
| 11743 | struct http_req_action_kw_list *kw_list; |
| 11744 | int i; |
| 11745 | |
| 11746 | list_for_each_entry(kw_list, &http_req_keywords.list, list) { |
| 11747 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11748 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11749 | return &kw_list->kw[i]; |
| 11750 | } |
| 11751 | } |
| 11752 | } |
| 11753 | return NULL; |
| 11754 | } |
| 11755 | |
| 11756 | /* |
| 11757 | * Return the struct http_res_action_kw associated to a keyword. |
| 11758 | */ |
| 11759 | struct http_res_action_kw *action_http_res_custom(const char *kw) |
| 11760 | { |
| 11761 | if (!LIST_ISEMPTY(&http_res_keywords.list)) { |
| 11762 | struct http_res_action_kw_list *kw_list; |
| 11763 | int i; |
| 11764 | |
| 11765 | list_for_each_entry(kw_list, &http_res_keywords.list, list) { |
| 11766 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11767 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11768 | return &kw_list->kw[i]; |
| 11769 | } |
| 11770 | } |
| 11771 | } |
| 11772 | return NULL; |
| 11773 | } |
| 11774 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11775 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11776 | /* All supported ACL keywords must be declared here. */ |
| 11777 | /************************************************************************/ |
| 11778 | |
| 11779 | /* Note: must not be declared <const> as its list will be overwritten. |
| 11780 | * Please take care of keeping this list alphabetically sorted. |
| 11781 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11782 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11783 | { "base", "base", PAT_MATCH_STR }, |
| 11784 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 11785 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 11786 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 11787 | { "base_end", "base", PAT_MATCH_END }, |
| 11788 | { "base_len", "base", PAT_MATCH_LEN }, |
| 11789 | { "base_reg", "base", PAT_MATCH_REG }, |
| 11790 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11791 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11792 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 11793 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 11794 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 11795 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 11796 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 11797 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 11798 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 11799 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11800 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11801 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 11802 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 11803 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 11804 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 11805 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 11806 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 11807 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 11808 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11809 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11810 | /* these two declarations uses strings with list storage (in place |
| 11811 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 11812 | * and delete functions are relative to the list management. The parse |
| 11813 | * and match method are related to the corresponding fetch methods. This |
| 11814 | * is very particular ACL declaration mode. |
| 11815 | */ |
| 11816 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 11817 | { "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] | 11818 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11819 | { "path", "path", PAT_MATCH_STR }, |
| 11820 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 11821 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 11822 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 11823 | { "path_end", "path", PAT_MATCH_END }, |
| 11824 | { "path_len", "path", PAT_MATCH_LEN }, |
| 11825 | { "path_reg", "path", PAT_MATCH_REG }, |
| 11826 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11827 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11828 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 11829 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11830 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11831 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 11832 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 11833 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 11834 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 11835 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 11836 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 11837 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 11838 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11839 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11840 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 11841 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 11842 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 11843 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 11844 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 11845 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 11846 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 11847 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11848 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11849 | { "url", "url", PAT_MATCH_STR }, |
| 11850 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 11851 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 11852 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 11853 | { "url_end", "url", PAT_MATCH_END }, |
| 11854 | { "url_len", "url", PAT_MATCH_LEN }, |
| 11855 | { "url_reg", "url", PAT_MATCH_REG }, |
| 11856 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11857 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11858 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 11859 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 11860 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 11861 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 11862 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 11863 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 11864 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 11865 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11866 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 11867 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11868 | }}; |
| 11869 | |
| 11870 | /************************************************************************/ |
| 11871 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11872 | /************************************************************************/ |
| 11873 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11874 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11875 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11876 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11877 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 11878 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11879 | /* capture are allocated and are permanent in the session */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11880 | { "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] | 11881 | |
| 11882 | /* retrieve these captures from the HTTP logs */ |
| 11883 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11884 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11885 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11886 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11887 | { "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] | 11888 | { "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] | 11889 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11890 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 11891 | * are only here to match the ACL's name, are request-only and are used |
| 11892 | * for ACL compatibility only. |
| 11893 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11894 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11895 | { "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] | 11896 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11897 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11898 | |
| 11899 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 11900 | * only here to match the ACL's name, are request-only and are used for |
| 11901 | * ACL compatibility only. |
| 11902 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11903 | { "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] | 11904 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11905 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11906 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11907 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 11908 | { "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] | 11909 | { "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] | 11910 | { "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] | 11911 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11912 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11913 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11914 | |
| 11915 | /* HTTP protocol on the request path */ |
| 11916 | { "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] | 11917 | { "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] | 11918 | |
| 11919 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11920 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11921 | { "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] | 11922 | |
| 11923 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11924 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 11925 | { "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] | 11926 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11927 | /* 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] | 11928 | { "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] | 11929 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11930 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11931 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11932 | { "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] | 11933 | { "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] | 11934 | { "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] | 11935 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11936 | { "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] | 11937 | { "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] | 11938 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11939 | |
| 11940 | /* 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] | 11941 | { "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] | 11942 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11943 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11944 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11945 | { "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] | 11946 | { "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] | 11947 | { "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] | 11948 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11949 | { "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] | 11950 | { "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] | 11951 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11952 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11953 | /* 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] | 11954 | { "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] | 11955 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11956 | { "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] | 11957 | { "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] | 11958 | |
| 11959 | /* 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] | 11960 | { "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] | 11961 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11962 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 11963 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11964 | |
| 11965 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11966 | { "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] | 11967 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11968 | { "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] | 11969 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11970 | { "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] | 11971 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11972 | { "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] | 11973 | { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11974 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11975 | }}; |
| 11976 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11977 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11978 | /************************************************************************/ |
| 11979 | /* All supported converter keywords must be declared here. */ |
| 11980 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11981 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 11982 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11983 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR}, |
| 11984 | { "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] | 11985 | { NULL, NULL, 0, 0, 0 }, |
| 11986 | }}; |
| 11987 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11988 | /************************************************************************/ |
| 11989 | /* All supported http-request action keywords must be declared here. */ |
| 11990 | /************************************************************************/ |
| 11991 | struct http_req_action_kw_list http_req_actions = { |
| 11992 | .scope = "http", |
| 11993 | .kw = { |
| 11994 | { "set-method", parse_set_req_line }, |
| 11995 | { "set-path", parse_set_req_line }, |
| 11996 | { "set-query", parse_set_req_line }, |
| 11997 | { "set-uri", parse_set_req_line }, |
| 11998 | } |
| 11999 | }; |
| 12000 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12001 | __attribute__((constructor)) |
| 12002 | static void __http_protocol_init(void) |
| 12003 | { |
| 12004 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 12005 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12006 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12007 | http_req_keywords_register(&http_req_actions); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12008 | } |
| 12009 | |
| 12010 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 12011 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 12012 | * Local variables: |
| 12013 | * c-indent-level: 8 |
| 12014 | * c-basic-offset: 8 |
| 12015 | * End: |
| 12016 | */ |