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" |
| 127 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 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 | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 255 | void init_proto_http() |
| 256 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 257 | int i; |
| 258 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 259 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 260 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 261 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 262 | if (!http_err_msgs[msg]) { |
| 263 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 264 | abort(); |
| 265 | } |
| 266 | |
| 267 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 268 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 269 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 270 | |
| 271 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 272 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 273 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 274 | * printable characters above. |
| 275 | */ |
| 276 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 277 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 278 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 279 | for (i = 0; i < 32; i++) { |
| 280 | FD_SET(i, hdr_encode_map); |
| 281 | FD_SET(i, url_encode_map); |
| 282 | } |
| 283 | for (i = 127; i < 256; i++) { |
| 284 | FD_SET(i, hdr_encode_map); |
| 285 | FD_SET(i, url_encode_map); |
| 286 | } |
| 287 | |
| 288 | tmp = "\"#{|}"; |
| 289 | while (*tmp) { |
| 290 | FD_SET(*tmp, hdr_encode_map); |
| 291 | tmp++; |
| 292 | } |
| 293 | |
| 294 | tmp = "\"#"; |
| 295 | while (*tmp) { |
| 296 | FD_SET(*tmp, url_encode_map); |
| 297 | tmp++; |
| 298 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 299 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 300 | /* initialize the http header encoding map. The draft httpbis define the |
| 301 | * header content as: |
| 302 | * |
| 303 | * HTTP-message = start-line |
| 304 | * *( header-field CRLF ) |
| 305 | * CRLF |
| 306 | * [ message-body ] |
| 307 | * header-field = field-name ":" OWS field-value OWS |
| 308 | * field-value = *( field-content / obs-fold ) |
| 309 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 310 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 311 | * field-vchar = VCHAR / obs-text |
| 312 | * VCHAR = %x21-7E |
| 313 | * obs-text = %x80-FF |
| 314 | * |
| 315 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 316 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 317 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 318 | */ |
| 319 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 320 | for (i = 0x00; i <= 0x08; i++) |
| 321 | FD_SET(i, http_encode_map); |
| 322 | for (i = 0x0a; i <= 0x1f; i++) |
| 323 | FD_SET(i, http_encode_map); |
| 324 | FD_SET(0x7f, http_encode_map); |
| 325 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 326 | /* memory allocations */ |
| 327 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 328 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 329 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 330 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 331 | /* |
| 332 | * We have 26 list of methods (1 per first letter), each of which can have |
| 333 | * up to 3 entries (2 valid, 1 null). |
| 334 | */ |
| 335 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 336 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 337 | int len; |
| 338 | const char text[8]; |
| 339 | }; |
| 340 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 341 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 342 | ['C' - 'A'] = { |
| 343 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 344 | }, |
| 345 | ['D' - 'A'] = { |
| 346 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 347 | }, |
| 348 | ['G' - 'A'] = { |
| 349 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 350 | }, |
| 351 | ['H' - 'A'] = { |
| 352 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 353 | }, |
| 354 | ['P' - 'A'] = { |
| 355 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 356 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 357 | }, |
| 358 | ['T' - 'A'] = { |
| 359 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 360 | }, |
| 361 | /* rest is empty like this : |
| 362 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 363 | */ |
| 364 | }; |
| 365 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 366 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
| 367 | [HTTP_METH_NONE] = { "", 0 }, |
| 368 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 369 | [HTTP_METH_GET] = { "GET", 3 }, |
| 370 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 371 | [HTTP_METH_POST] = { "POST", 4 }, |
| 372 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 373 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 374 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 375 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 376 | }; |
| 377 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 378 | /* 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] | 379 | * 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] | 380 | * RFC2616 for those chars. |
| 381 | */ |
| 382 | |
| 383 | const char http_is_spht[256] = { |
| 384 | [' '] = 1, ['\t'] = 1, |
| 385 | }; |
| 386 | |
| 387 | const char http_is_crlf[256] = { |
| 388 | ['\r'] = 1, ['\n'] = 1, |
| 389 | }; |
| 390 | |
| 391 | const char http_is_lws[256] = { |
| 392 | [' '] = 1, ['\t'] = 1, |
| 393 | ['\r'] = 1, ['\n'] = 1, |
| 394 | }; |
| 395 | |
| 396 | const char http_is_sep[256] = { |
| 397 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 398 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 399 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 400 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 401 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 402 | }; |
| 403 | |
| 404 | const char http_is_ctl[256] = { |
| 405 | [0 ... 31] = 1, |
| 406 | [127] = 1, |
| 407 | }; |
| 408 | |
| 409 | /* |
| 410 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 411 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 412 | * them correctly. Instead, define every non-CTL char's status. |
| 413 | */ |
| 414 | const char http_is_token[256] = { |
| 415 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 416 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 417 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 418 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 419 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 420 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 421 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 422 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 423 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 424 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 425 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 426 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 427 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 428 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 429 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 430 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 431 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 432 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 433 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 434 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 435 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 436 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 437 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 438 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 439 | }; |
| 440 | |
| 441 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 442 | /* |
| 443 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 444 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 445 | */ |
| 446 | const char http_is_ver_token[256] = { |
| 447 | ['.'] = 1, ['/'] = 1, |
| 448 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 449 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 450 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 451 | }; |
| 452 | |
| 453 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 454 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 455 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 456 | * 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] | 457 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 458 | * of headers is automatically adjusted. The number of bytes added is returned |
| 459 | * on success, otherwise <0 is returned indicating an error. |
| 460 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 461 | 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] | 462 | { |
| 463 | int bytes, len; |
| 464 | |
| 465 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 466 | 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] | 467 | if (!bytes) |
| 468 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 469 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 470 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 471 | } |
| 472 | |
| 473 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 474 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 475 | * 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] | 476 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 477 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 478 | * of headers is automatically adjusted. The number of bytes added is returned |
| 479 | * on success, otherwise <0 is returned indicating an error. |
| 480 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 481 | int http_header_add_tail2(struct http_msg *msg, |
| 482 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 483 | { |
| 484 | int bytes; |
| 485 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 486 | 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] | 487 | if (!bytes) |
| 488 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 489 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 490 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 491 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 492 | |
| 493 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 494 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 495 | * If so, returns the position of the first non-space character relative to |
| 496 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 497 | * to return a pointer to the place after the first space. Returns 0 if the |
| 498 | * header name does not match. Checks are case-insensitive. |
| 499 | */ |
| 500 | int http_header_match2(const char *hdr, const char *end, |
| 501 | const char *name, int len) |
| 502 | { |
| 503 | const char *val; |
| 504 | |
| 505 | if (hdr + len >= end) |
| 506 | return 0; |
| 507 | if (hdr[len] != ':') |
| 508 | return 0; |
| 509 | if (strncasecmp(hdr, name, len) != 0) |
| 510 | return 0; |
| 511 | val = hdr + len + 1; |
| 512 | while (val < end && HTTP_IS_SPHT(*val)) |
| 513 | val++; |
| 514 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 515 | return len + 2; /* we may replace starting from second space */ |
| 516 | return val - hdr; |
| 517 | } |
| 518 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 519 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 520 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 521 | * structure holds everything necessary to use the header and find next |
| 522 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 523 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 524 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 525 | * http_find_header2() except that it is designed to work with full-line headers |
| 526 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 527 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 528 | * header is rescanned. This allows rescanning after a header deletion. |
| 529 | */ |
| 530 | int http_find_full_header2(const char *name, int len, |
| 531 | char *sol, struct hdr_idx *idx, |
| 532 | struct hdr_ctx *ctx) |
| 533 | { |
| 534 | char *eol, *sov; |
| 535 | int cur_idx, old_idx; |
| 536 | |
| 537 | cur_idx = ctx->idx; |
| 538 | if (cur_idx) { |
| 539 | /* We have previously returned a header, let's search another one */ |
| 540 | sol = ctx->line; |
| 541 | eol = sol + idx->v[cur_idx].len; |
| 542 | goto next_hdr; |
| 543 | } |
| 544 | |
| 545 | /* first request for this header */ |
| 546 | sol += hdr_idx_first_pos(idx); |
| 547 | old_idx = 0; |
| 548 | cur_idx = hdr_idx_first_idx(idx); |
| 549 | while (cur_idx) { |
| 550 | eol = sol + idx->v[cur_idx].len; |
| 551 | |
| 552 | if (len == 0) { |
| 553 | /* No argument was passed, we want any header. |
| 554 | * To achieve this, we simply build a fake request. */ |
| 555 | while (sol + len < eol && sol[len] != ':') |
| 556 | len++; |
| 557 | name = sol; |
| 558 | } |
| 559 | |
| 560 | if ((len < eol - sol) && |
| 561 | (sol[len] == ':') && |
| 562 | (strncasecmp(sol, name, len) == 0)) { |
| 563 | ctx->del = len; |
| 564 | sov = sol + len + 1; |
| 565 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 566 | sov++; |
| 567 | |
| 568 | ctx->line = sol; |
| 569 | ctx->prev = old_idx; |
| 570 | ctx->idx = cur_idx; |
| 571 | ctx->val = sov - sol; |
| 572 | ctx->tws = 0; |
| 573 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 574 | eol--; |
| 575 | ctx->tws++; |
| 576 | } |
| 577 | ctx->vlen = eol - sov; |
| 578 | return 1; |
| 579 | } |
| 580 | next_hdr: |
| 581 | sol = eol + idx->v[cur_idx].cr + 1; |
| 582 | old_idx = cur_idx; |
| 583 | cur_idx = idx->v[cur_idx].next; |
| 584 | } |
| 585 | return 0; |
| 586 | } |
| 587 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 588 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 589 | * par 2.2 for more information. Note that it requires a valid header to return |
| 590 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 591 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 592 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 593 | { |
| 594 | int quoted, qdpair; |
| 595 | |
| 596 | quoted = qdpair = 0; |
| 597 | for (; s < e; s++) { |
| 598 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 599 | else if (quoted) { |
| 600 | if (*s == '\\') qdpair = 1; |
| 601 | else if (*s == '"') quoted = 0; |
| 602 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 603 | else if (*s == '"') quoted = 1; |
| 604 | else if (*s == ',') return s; |
| 605 | } |
| 606 | return s; |
| 607 | } |
| 608 | |
| 609 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 610 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 611 | * structure holds everything necessary to use the header and find next |
| 612 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 613 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 614 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 615 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 616 | * is NULL when searching for a new values of a header, the current header is |
| 617 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 618 | */ |
| 619 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 620 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 621 | struct hdr_ctx *ctx) |
| 622 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 623 | char *eol, *sov; |
| 624 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 625 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 626 | cur_idx = ctx->idx; |
| 627 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 628 | /* We have previously returned a value, let's search |
| 629 | * another one on the same line. |
| 630 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 631 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 632 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 633 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 634 | eol = sol + idx->v[cur_idx].len; |
| 635 | |
| 636 | if (sov >= eol) |
| 637 | /* no more values in this header */ |
| 638 | goto next_hdr; |
| 639 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 640 | /* values remaining for this header, skip the comma but save it |
| 641 | * for later use (eg: for header deletion). |
| 642 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 643 | sov++; |
| 644 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 645 | sov++; |
| 646 | |
| 647 | goto return_hdr; |
| 648 | } |
| 649 | |
| 650 | /* first request for this header */ |
| 651 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 652 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 653 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 654 | while (cur_idx) { |
| 655 | eol = sol + idx->v[cur_idx].len; |
| 656 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 657 | if (len == 0) { |
| 658 | /* No argument was passed, we want any header. |
| 659 | * To achieve this, we simply build a fake request. */ |
| 660 | while (sol + len < eol && sol[len] != ':') |
| 661 | len++; |
| 662 | name = sol; |
| 663 | } |
| 664 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 665 | if ((len < eol - sol) && |
| 666 | (sol[len] == ':') && |
| 667 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 668 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 669 | sov = sol + len + 1; |
| 670 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 671 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 672 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 673 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 674 | ctx->prev = old_idx; |
| 675 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 676 | ctx->idx = cur_idx; |
| 677 | ctx->val = sov - sol; |
| 678 | |
| 679 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 680 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 681 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 682 | eol--; |
| 683 | ctx->tws++; |
| 684 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 685 | ctx->vlen = eol - sov; |
| 686 | return 1; |
| 687 | } |
| 688 | next_hdr: |
| 689 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 690 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 691 | cur_idx = idx->v[cur_idx].next; |
| 692 | } |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 697 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 698 | struct hdr_ctx *ctx) |
| 699 | { |
| 700 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 701 | } |
| 702 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 703 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 704 | * the http_find_header functions. The value is removed, as well as surrounding |
| 705 | * 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] | 706 | * 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] | 707 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 708 | * no more header, so any processing may stop. The ctx is always left in a form |
| 709 | * that can be handled by http_find_header2() to find next occurrence. |
| 710 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 711 | 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] | 712 | { |
| 713 | int cur_idx = ctx->idx; |
| 714 | char *sol = ctx->line; |
| 715 | struct hdr_idx_elem *hdr; |
| 716 | int delta, skip_comma; |
| 717 | |
| 718 | if (!cur_idx) |
| 719 | return 0; |
| 720 | |
| 721 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 722 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 723 | /* 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] | 724 | 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] | 725 | http_msg_move_end(msg, delta); |
| 726 | idx->used--; |
| 727 | hdr->len = 0; /* unused entry */ |
| 728 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 729 | if (idx->tail == ctx->idx) |
| 730 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 731 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 732 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 733 | 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] | 734 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 735 | return ctx->idx; |
| 736 | } |
| 737 | |
| 738 | /* 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] | 739 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 740 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 741 | */ |
| 742 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 743 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 744 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 745 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 746 | NULL, 0); |
| 747 | hdr->len += delta; |
| 748 | http_msg_move_end(msg, delta); |
| 749 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 750 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 751 | return ctx->idx; |
| 752 | } |
| 753 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 754 | /* This function handles a server error at the stream interface level. The |
| 755 | * stream interface is assumed to be already in a closed state. An optional |
| 756 | * message is copied into the input buffer, and an HTTP status code stored. |
| 757 | * 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] | 758 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 759 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 760 | static void http_server_error(struct session *s, struct stream_interface *si, |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 761 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 762 | { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 763 | channel_auto_read(si->ob); |
| 764 | channel_abort(si->ob); |
| 765 | channel_auto_close(si->ob); |
| 766 | channel_erase(si->ob); |
| 767 | channel_auto_close(si->ib); |
| 768 | channel_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 769 | if (status > 0 && msg) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 770 | s->txn.status = status; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 771 | bo_inject(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 772 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 773 | if (!(s->flags & SN_ERR_MASK)) |
| 774 | s->flags |= err; |
| 775 | if (!(s->flags & SN_FINST_MASK)) |
| 776 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 777 | } |
| 778 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 779 | /* This function returns the appropriate error location for the given session |
| 780 | * and message. |
| 781 | */ |
| 782 | |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 783 | struct chunk *http_error_message(struct session *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 784 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 785 | if (s->be->errmsg[msgnum].str) |
| 786 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 787 | else if (s->fe->errmsg[msgnum].str) |
| 788 | return &s->fe->errmsg[msgnum]; |
| 789 | else |
| 790 | return &http_err_chunks[msgnum]; |
| 791 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 792 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 793 | /* |
| 794 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 795 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 796 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 797 | 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] | 798 | { |
| 799 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 800 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 801 | |
| 802 | m = ((unsigned)*str - 'A'); |
| 803 | |
| 804 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 805 | for (h = http_methods[m]; h->len > 0; h++) { |
| 806 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 807 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 808 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 809 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 810 | }; |
| 811 | return HTTP_METH_OTHER; |
| 812 | } |
| 813 | return HTTP_METH_NONE; |
| 814 | |
| 815 | } |
| 816 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 817 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 818 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 819 | * It is returned otherwise. |
| 820 | */ |
| 821 | static char * |
| 822 | http_get_path(struct http_txn *txn) |
| 823 | { |
| 824 | char *ptr, *end; |
| 825 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 826 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 827 | end = ptr + txn->req.sl.rq.u_l; |
| 828 | |
| 829 | if (ptr >= end) |
| 830 | return NULL; |
| 831 | |
| 832 | /* RFC2616, par. 5.1.2 : |
| 833 | * Request-URI = "*" | absuri | abspath | authority |
| 834 | */ |
| 835 | |
| 836 | if (*ptr == '*') |
| 837 | return NULL; |
| 838 | |
| 839 | if (isalpha((unsigned char)*ptr)) { |
| 840 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 841 | ptr++; |
| 842 | while (ptr < end && |
| 843 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 844 | ptr++; |
| 845 | /* skip '://' */ |
| 846 | if (ptr == end || *ptr++ != ':') |
| 847 | return NULL; |
| 848 | if (ptr == end || *ptr++ != '/') |
| 849 | return NULL; |
| 850 | if (ptr == end || *ptr++ != '/') |
| 851 | return NULL; |
| 852 | } |
| 853 | /* skip [user[:passwd]@]host[:[port]] */ |
| 854 | |
| 855 | while (ptr < end && *ptr != '/') |
| 856 | ptr++; |
| 857 | |
| 858 | if (ptr == end) |
| 859 | return NULL; |
| 860 | |
| 861 | /* OK, we got the '/' ! */ |
| 862 | return ptr; |
| 863 | } |
| 864 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 865 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 866 | * If not found, return NULL. It is returned otherwise. |
| 867 | */ |
| 868 | static char * |
| 869 | http_get_path_from_string(char *str) |
| 870 | { |
| 871 | char *ptr = str; |
| 872 | |
| 873 | /* RFC2616, par. 5.1.2 : |
| 874 | * Request-URI = "*" | absuri | abspath | authority |
| 875 | */ |
| 876 | |
| 877 | if (*ptr == '*') |
| 878 | return NULL; |
| 879 | |
| 880 | if (isalpha((unsigned char)*ptr)) { |
| 881 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 882 | ptr++; |
| 883 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 884 | ptr++; |
| 885 | /* skip '://' */ |
| 886 | if (*ptr == '\0' || *ptr++ != ':') |
| 887 | return NULL; |
| 888 | if (*ptr == '\0' || *ptr++ != '/') |
| 889 | return NULL; |
| 890 | if (*ptr == '\0' || *ptr++ != '/') |
| 891 | return NULL; |
| 892 | } |
| 893 | /* skip [user[:passwd]@]host[:[port]] */ |
| 894 | |
| 895 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 896 | ptr++; |
| 897 | |
| 898 | if (*ptr == '\0' || *ptr == ' ') |
| 899 | return NULL; |
| 900 | |
| 901 | /* OK, we got the '/' ! */ |
| 902 | return ptr; |
| 903 | } |
| 904 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 905 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 906 | * in redirect mode. This may only be called just after the stream interface |
| 907 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 908 | * follow normal proxy processing. NOTE: this function is designed to support |
| 909 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 910 | */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 911 | void http_perform_server_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 912 | { |
| 913 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 914 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 915 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 916 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 917 | |
| 918 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 919 | trash.len = strlen(HTTP_302); |
| 920 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 921 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 922 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 923 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 924 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 925 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 926 | return; |
| 927 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 928 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 929 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 930 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 931 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 932 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 933 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 934 | /* 3: add the request URI. Since it was already forwarded, we need |
| 935 | * to temporarily rewind the buffer. |
| 936 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 937 | txn = &s->txn; |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 938 | b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 939 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 940 | path = http_get_path(txn); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 941 | 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] | 942 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 943 | b_adv(s->req->buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 944 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 945 | if (!path) |
| 946 | return; |
| 947 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 948 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 949 | return; |
| 950 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 951 | memcpy(trash.str + trash.len, path, len); |
| 952 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 953 | |
| 954 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 955 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 956 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 957 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 958 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 959 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 960 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 961 | |
| 962 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 963 | si_shutr(si); |
| 964 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 965 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 966 | si->state = SI_ST_CLO; |
| 967 | |
| 968 | /* send the message */ |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 969 | 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] | 970 | |
| 971 | /* 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] | 972 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 973 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 974 | } |
| 975 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 976 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 977 | * that the server side is closed. Note that err_type is actually a |
| 978 | * bitmask, where almost only aborts may be cumulated with other |
| 979 | * values. We consider that aborted operations are more important |
| 980 | * than timeouts or errors due to the fact that nobody else in the |
| 981 | * logs might explain incomplete retries. All others should avoid |
| 982 | * being cumulated. It should normally not be possible to have multiple |
| 983 | * 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] | 984 | * Note that connection errors appearing on the second request of a keep-alive |
| 985 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 986 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 987 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 988 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 989 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 990 | |
| 991 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 992 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 993 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 994 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 995 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 996 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 997 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 998 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 999 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1000 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1001 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1002 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1003 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1004 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1005 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1006 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 1007 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1008 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1009 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
Willy Tarreau | 3634624 | 2014-02-24 18:26:30 +0100 | [diff] [blame] | 1010 | 503, (s->flags & SN_SRV_REUSED) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1011 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1012 | else if (err_type & SI_ET_CONN_RES) |
| 1013 | http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1014 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 1015 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1016 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1017 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1018 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1019 | } |
| 1020 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1021 | extern const char sess_term_cond[8]; |
| 1022 | extern const char sess_fin_state[8]; |
| 1023 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1024 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1025 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1026 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1027 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1028 | /* |
| 1029 | * Capture headers from message starting at <som> according to header list |
| 1030 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1031 | */ |
| 1032 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1033 | char **cap, struct cap_hdr *cap_hdr) |
| 1034 | { |
| 1035 | char *eol, *sol, *col, *sov; |
| 1036 | int cur_idx; |
| 1037 | struct cap_hdr *h; |
| 1038 | int len; |
| 1039 | |
| 1040 | sol = som + hdr_idx_first_pos(idx); |
| 1041 | cur_idx = hdr_idx_first_idx(idx); |
| 1042 | |
| 1043 | while (cur_idx) { |
| 1044 | eol = sol + idx->v[cur_idx].len; |
| 1045 | |
| 1046 | col = sol; |
| 1047 | while (col < eol && *col != ':') |
| 1048 | col++; |
| 1049 | |
| 1050 | sov = col + 1; |
| 1051 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1052 | sov++; |
| 1053 | |
| 1054 | for (h = cap_hdr; h; h = h->next) { |
| 1055 | if ((h->namelen == col - sol) && |
| 1056 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1057 | if (cap[h->index] == NULL) |
| 1058 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1059 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1060 | |
| 1061 | if (cap[h->index] == NULL) { |
| 1062 | Alert("HTTP capture : out of memory.\n"); |
| 1063 | continue; |
| 1064 | } |
| 1065 | |
| 1066 | len = eol - sov; |
| 1067 | if (len > h->len) |
| 1068 | len = h->len; |
| 1069 | |
| 1070 | memcpy(cap[h->index], sov, len); |
| 1071 | cap[h->index][len]=0; |
| 1072 | } |
| 1073 | } |
| 1074 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1075 | cur_idx = idx->v[cur_idx].next; |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1080 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1081 | */ |
| 1082 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1083 | |
| 1084 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1085 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1086 | */ |
| 1087 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1088 | ptr++; \ |
| 1089 | if (likely(ptr < end)) \ |
| 1090 | goto good; \ |
| 1091 | else { \ |
| 1092 | state = (st); \ |
| 1093 | goto http_msg_ood; \ |
| 1094 | } \ |
| 1095 | } while (0) |
| 1096 | |
| 1097 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1098 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1099 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1100 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1101 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1102 | * will give undefined results. |
| 1103 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1104 | * and that msg->sol points to the beginning of the response. |
| 1105 | * If a complete line is found (which implies that at least one CR or LF is |
| 1106 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1107 | * returned indicating an incomplete line (which does not mean that parts have |
| 1108 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1109 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1110 | * upon next call. |
| 1111 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1112 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1113 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1114 | * 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] | 1115 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1116 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1117 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1118 | enum ht_state state, const char *ptr, const char *end, |
| 1119 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1120 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1121 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1122 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1123 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1124 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1125 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1126 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1127 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1128 | |
| 1129 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1130 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1131 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1132 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1133 | state = HTTP_MSG_ERROR; |
| 1134 | break; |
| 1135 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1136 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1137 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1138 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1139 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1140 | goto http_msg_rpcode; |
| 1141 | } |
| 1142 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1143 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1144 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1145 | state = HTTP_MSG_ERROR; |
| 1146 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1147 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1148 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1149 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1150 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1151 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1152 | |
| 1153 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1154 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1155 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1156 | } |
| 1157 | |
| 1158 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1159 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1160 | http_msg_rsp_reason: |
| 1161 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1162 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1163 | msg->sl.st.r_l = 0; |
| 1164 | goto http_msg_rpline_eol; |
| 1165 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1166 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1167 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1168 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1169 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1170 | goto http_msg_rpreason; |
| 1171 | } |
| 1172 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1173 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1174 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1175 | goto http_msg_rsp_reason; |
| 1176 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1177 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1178 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1179 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1180 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1181 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1182 | http_msg_rpline_eol: |
| 1183 | /* We have seen the end of line. Note that we do not |
| 1184 | * necessarily have the \n yet, but at least we know that we |
| 1185 | * have EITHER \r OR \n, otherwise the response would not be |
| 1186 | * complete. We can then record the response length and return |
| 1187 | * to the caller which will be able to register it. |
| 1188 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1189 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1190 | return ptr; |
| 1191 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1192 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1193 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1194 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1195 | exit(1); |
| 1196 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1197 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1201 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1202 | if (ret_state) |
| 1203 | *ret_state = state; |
| 1204 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1205 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1206 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1207 | } |
| 1208 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1209 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1210 | * This function parses a request line between <ptr> and <end>, starting with |
| 1211 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1212 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1213 | * will give undefined results. |
| 1214 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1215 | * and that msg->sol points to the beginning of the request. |
| 1216 | * If a complete line is found (which implies that at least one CR or LF is |
| 1217 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1218 | * returned indicating an incomplete line (which does not mean that parts have |
| 1219 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1220 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1221 | * upon next call. |
| 1222 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1223 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1224 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1225 | * 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] | 1226 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1227 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1228 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1229 | enum ht_state state, const char *ptr, const char *end, |
| 1230 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1231 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1232 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1233 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1234 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1235 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1236 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1237 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1238 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1239 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1240 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1241 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1242 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1243 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1244 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1245 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1246 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1247 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1248 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1249 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1250 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1251 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1252 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1253 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1254 | msg->sl.rq.v_l = 0; |
| 1255 | goto http_msg_rqline_eol; |
| 1256 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1257 | state = HTTP_MSG_ERROR; |
| 1258 | break; |
| 1259 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1260 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1261 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1262 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1263 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1264 | goto http_msg_rquri; |
| 1265 | } |
| 1266 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1267 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1268 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1269 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1270 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1271 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1272 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1273 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1274 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1275 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1276 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1277 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1278 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1279 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1280 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1281 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1282 | /* non-ASCII chars are forbidden unless option |
| 1283 | * accept-invalid-http-request is enabled in the frontend. |
| 1284 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1285 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1286 | if (msg->err_pos < -1) |
| 1287 | goto invalid_char; |
| 1288 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1289 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1290 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1291 | } |
| 1292 | |
| 1293 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1294 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1295 | goto http_msg_req09_uri_e; |
| 1296 | } |
| 1297 | |
| 1298 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1299 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1300 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1301 | state = HTTP_MSG_ERROR; |
| 1302 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1303 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1304 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1305 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1306 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1307 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1308 | goto http_msg_rqver; |
| 1309 | } |
| 1310 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1311 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1312 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1313 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1314 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1315 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1316 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1317 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1318 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1319 | |
| 1320 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1321 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1322 | http_msg_rqline_eol: |
| 1323 | /* We have seen the end of line. Note that we do not |
| 1324 | * necessarily have the \n yet, but at least we know that we |
| 1325 | * have EITHER \r OR \n, otherwise the request would not be |
| 1326 | * complete. We can then record the request length and return |
| 1327 | * to the caller which will be able to register it. |
| 1328 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1329 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1330 | return ptr; |
| 1331 | } |
| 1332 | |
| 1333 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1334 | state = HTTP_MSG_ERROR; |
| 1335 | break; |
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 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1338 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1339 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1340 | exit(1); |
| 1341 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1342 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1343 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1344 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1345 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1346 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1347 | if (ret_state) |
| 1348 | *ret_state = state; |
| 1349 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1350 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1351 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1352 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1353 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1354 | /* |
| 1355 | * Returns the data from Authorization header. Function may be called more |
| 1356 | * than once so data is stored in txn->auth_data. When no header is found |
| 1357 | * 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] | 1358 | * searching again for something we are unable to find anyway. However, if |
| 1359 | * the result if valid, the cache is not reused because we would risk to |
| 1360 | * have the credentials overwritten by another session in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1361 | */ |
| 1362 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 1363 | /* This bufffer is initialized in the file 'src/haproxy.c'. This length is |
| 1364 | * set according to global.tune.bufsize. |
| 1365 | */ |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1366 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1367 | |
| 1368 | int |
| 1369 | get_http_auth(struct session *s) |
| 1370 | { |
| 1371 | |
| 1372 | struct http_txn *txn = &s->txn; |
| 1373 | struct chunk auth_method; |
| 1374 | struct hdr_ctx ctx; |
| 1375 | char *h, *p; |
| 1376 | int len; |
| 1377 | |
| 1378 | #ifdef DEBUG_AUTH |
| 1379 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1380 | #endif |
| 1381 | |
| 1382 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1383 | return 0; |
| 1384 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1385 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1386 | |
| 1387 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1388 | |
| 1389 | if (txn->flags & TX_USE_PX_CONN) { |
| 1390 | h = "Proxy-Authorization"; |
| 1391 | len = strlen(h); |
| 1392 | } else { |
| 1393 | h = "Authorization"; |
| 1394 | len = strlen(h); |
| 1395 | } |
| 1396 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1397 | 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] | 1398 | return 0; |
| 1399 | |
| 1400 | h = ctx.line + ctx.val; |
| 1401 | |
| 1402 | p = memchr(h, ' ', ctx.vlen); |
| 1403 | if (!p || p == h) |
| 1404 | return 0; |
| 1405 | |
| 1406 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1407 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1408 | |
| 1409 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1410 | |
| 1411 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1412 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1413 | |
| 1414 | if (len < 0) |
| 1415 | return 0; |
| 1416 | |
| 1417 | |
| 1418 | get_http_auth_buff[len] = '\0'; |
| 1419 | |
| 1420 | p = strchr(get_http_auth_buff, ':'); |
| 1421 | |
| 1422 | if (!p) |
| 1423 | return 0; |
| 1424 | |
| 1425 | txn->auth.user = get_http_auth_buff; |
| 1426 | *p = '\0'; |
| 1427 | txn->auth.pass = p+1; |
| 1428 | |
| 1429 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1430 | return 1; |
| 1431 | } |
| 1432 | |
| 1433 | return 0; |
| 1434 | } |
| 1435 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1436 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1437 | /* |
| 1438 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1439 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1440 | * ensuring that the message does not wrap. The function can be preempted |
| 1441 | * everywhere when data are missing and recalled at the exact same location |
| 1442 | * with no information loss. The message may even be realigned between two |
| 1443 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1444 | * 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] | 1445 | * fields. Note that msg->sol will be initialized after completing the first |
| 1446 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1447 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1448 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1449 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1450 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1451 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1452 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1453 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1454 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1455 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1456 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1457 | ptr = buf->p + msg->next; |
| 1458 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1459 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1460 | if (unlikely(ptr >= end)) |
| 1461 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1462 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1463 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1464 | /* |
| 1465 | * First, states that are specific to the response only. |
| 1466 | * We check them first so that request and headers are |
| 1467 | * closer to each other (accessed more often). |
| 1468 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1469 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1470 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1471 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1472 | /* we have a start of message, but we have to check |
| 1473 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1474 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1475 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1476 | if (unlikely(ptr != buf->p)) { |
| 1477 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1478 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1479 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1480 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1481 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1482 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1483 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1484 | hdr_idx_init(idx); |
| 1485 | state = HTTP_MSG_RPVER; |
| 1486 | goto http_msg_rpver; |
| 1487 | } |
| 1488 | |
| 1489 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1490 | goto http_msg_invalid; |
| 1491 | |
| 1492 | if (unlikely(*ptr == '\n')) |
| 1493 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1494 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1495 | /* stop here */ |
| 1496 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1497 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1498 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1499 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1500 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1501 | /* stop here */ |
| 1502 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1503 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1504 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1505 | case HTTP_MSG_RPVER_SP: |
| 1506 | case HTTP_MSG_RPCODE: |
| 1507 | case HTTP_MSG_RPCODE_SP: |
| 1508 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1509 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1510 | state, ptr, end, |
| 1511 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1512 | if (unlikely(!ptr)) |
| 1513 | return; |
| 1514 | |
| 1515 | /* we have a full response and we know that we have either a CR |
| 1516 | * or an LF at <ptr>. |
| 1517 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1518 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1519 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1520 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1521 | if (likely(*ptr == '\r')) |
| 1522 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1523 | goto http_msg_rpline_end; |
| 1524 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1525 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1526 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1527 | /* msg->sol must point to the first of CR or LF. */ |
| 1528 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1529 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1530 | /* stop here */ |
| 1531 | |
| 1532 | /* |
| 1533 | * Second, states that are specific to the request only |
| 1534 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1535 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1536 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +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 (likely(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 | 8d5d7f2 | 2007-01-21 19:16:41 +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.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1550 | state = HTTP_MSG_RQMETH; |
| 1551 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1552 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1553 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1554 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1555 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1556 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1557 | if (unlikely(*ptr == '\n')) |
| 1558 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1559 | 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] | 1560 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1561 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1562 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1563 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1564 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1565 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1566 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1567 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1568 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1569 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1570 | case HTTP_MSG_RQMETH_SP: |
| 1571 | case HTTP_MSG_RQURI: |
| 1572 | case HTTP_MSG_RQURI_SP: |
| 1573 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1574 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1575 | state, ptr, end, |
| 1576 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1577 | if (unlikely(!ptr)) |
| 1578 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1579 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1580 | /* we have a full request and we know that we have either a CR |
| 1581 | * or an LF at <ptr>. |
| 1582 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1583 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1584 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1585 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1586 | if (likely(*ptr == '\r')) |
| 1587 | 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] | 1588 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1589 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1590 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1591 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1592 | /* check for HTTP/0.9 request : no version information available. |
| 1593 | * msg->sol must point to the first of CR or LF. |
| 1594 | */ |
| 1595 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1596 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1597 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1598 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1599 | 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] | 1600 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1601 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1602 | /* |
| 1603 | * Common states below |
| 1604 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1605 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1606 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1607 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1608 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1609 | goto http_msg_hdr_name; |
| 1610 | } |
| 1611 | |
| 1612 | if (likely(*ptr == '\r')) |
| 1613 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1614 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1615 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1616 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1617 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1618 | /* assumes msg->sol points to the first char */ |
| 1619 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1620 | 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] | 1621 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1622 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1623 | 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] | 1624 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1625 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1626 | goto http_msg_invalid; |
| 1627 | |
| 1628 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1629 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1630 | |
| 1631 | /* and we still accept this non-token character */ |
| 1632 | 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] | 1633 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1634 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1635 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1636 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1637 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1638 | 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] | 1639 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1640 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1641 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1642 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1643 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1644 | goto http_msg_hdr_val; |
| 1645 | } |
| 1646 | |
| 1647 | if (likely(*ptr == '\r')) |
| 1648 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1649 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1650 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1651 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1652 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1653 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1654 | 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] | 1655 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1656 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1657 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1658 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1659 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1660 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1661 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1662 | goto http_msg_hdr_l1_sp; |
| 1663 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1664 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1665 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1666 | goto http_msg_complete_header; |
| 1667 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1668 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1669 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1670 | /* assumes msg->sol points to the first char, and msg->sov |
| 1671 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1672 | */ |
| 1673 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1674 | 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] | 1675 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1676 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1677 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1678 | * real header end in case it ends with lots of LWS, but is this |
| 1679 | * really needed ? |
| 1680 | */ |
| 1681 | if (likely(*ptr == '\r')) |
| 1682 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1683 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1684 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1685 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1686 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1687 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1688 | 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] | 1689 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1690 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1691 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1692 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1693 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1694 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1695 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1696 | goto http_msg_hdr_val; |
| 1697 | } |
| 1698 | http_msg_complete_header: |
| 1699 | /* |
| 1700 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1701 | * Assumes msg->sol points to the first char, msg->sov points |
| 1702 | * to the first character of the value and msg->eol to the |
| 1703 | * first CR or LF so we know how the line ends. We insert last |
| 1704 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1705 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1706 | 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] | 1707 | idx, idx->tail) < 0)) |
| 1708 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1709 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1710 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1711 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1712 | goto http_msg_hdr_name; |
| 1713 | } |
| 1714 | |
| 1715 | if (likely(*ptr == '\r')) |
| 1716 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1717 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1718 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1719 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1720 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1721 | /* Assumes msg->sol points to the first of either CR or LF. |
| 1722 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 1723 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 1724 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1725 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1726 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1727 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1728 | msg->eoh = msg->sol; |
| 1729 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1730 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1731 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1732 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1733 | |
| 1734 | case HTTP_MSG_ERROR: |
| 1735 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1736 | break; |
| 1737 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1738 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1739 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1740 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1741 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1742 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1743 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1744 | } |
| 1745 | http_msg_ood: |
| 1746 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1747 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1748 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1749 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1750 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1751 | http_msg_invalid: |
| 1752 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1753 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1754 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1755 | return; |
| 1756 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1757 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1758 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1759 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1760 | * nothing is done and 1 is returned. |
| 1761 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1762 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1763 | { |
| 1764 | int delta; |
| 1765 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1766 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1767 | |
| 1768 | if (msg->sl.rq.v_l != 0) |
| 1769 | return 1; |
| 1770 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1771 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1772 | if (txn->meth != HTTP_METH_GET) |
| 1773 | return 0; |
| 1774 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1775 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1776 | delta = 0; |
| 1777 | |
| 1778 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1779 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1780 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1781 | } |
| 1782 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1783 | 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] | 1784 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1785 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1786 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1787 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1788 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1789 | NULL, NULL); |
| 1790 | if (unlikely(!cur_end)) |
| 1791 | return 0; |
| 1792 | |
| 1793 | /* we have a full HTTP/1.0 request now and we know that |
| 1794 | * we have either a CR or an LF at <ptr>. |
| 1795 | */ |
| 1796 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1797 | return 1; |
| 1798 | } |
| 1799 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1800 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1801 | * and "keep-alive" values. If we already know that some headers may safely |
| 1802 | * 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] | 1803 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1804 | * - 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] | 1805 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1806 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1807 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1808 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1809 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1810 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1811 | 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] | 1812 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1813 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1814 | const char *hdr_val = "Connection"; |
| 1815 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1816 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1817 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1818 | return; |
| 1819 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1820 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1821 | hdr_val = "Proxy-Connection"; |
| 1822 | hdr_len = 16; |
| 1823 | } |
| 1824 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1825 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1826 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1827 | 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] | 1828 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1829 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1830 | if (to_del & 2) |
| 1831 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1832 | else |
| 1833 | txn->flags |= TX_CON_KAL_SET; |
| 1834 | } |
| 1835 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1836 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1837 | if (to_del & 1) |
| 1838 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1839 | else |
| 1840 | txn->flags |= TX_CON_CLO_SET; |
| 1841 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1842 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1843 | txn->flags |= TX_HDR_CONN_UPG; |
| 1844 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1845 | } |
| 1846 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1847 | txn->flags |= TX_HDR_CONN_PRS; |
| 1848 | return; |
| 1849 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1850 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1851 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1852 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1853 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1854 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1855 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1856 | 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] | 1857 | { |
| 1858 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1859 | const char *hdr_val = "Connection"; |
| 1860 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1861 | |
| 1862 | ctx.idx = 0; |
| 1863 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1864 | |
| 1865 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1866 | hdr_val = "Proxy-Connection"; |
| 1867 | hdr_len = 16; |
| 1868 | } |
| 1869 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1870 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1871 | 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] | 1872 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1873 | if (wanted & TX_CON_KAL_SET) |
| 1874 | txn->flags |= TX_CON_KAL_SET; |
| 1875 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1876 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1877 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1878 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1879 | if (wanted & TX_CON_CLO_SET) |
| 1880 | txn->flags |= TX_CON_CLO_SET; |
| 1881 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1882 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1883 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1884 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1885 | |
| 1886 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1887 | return; |
| 1888 | |
| 1889 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1890 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1891 | hdr_val = "Connection: close"; |
| 1892 | hdr_len = 17; |
| 1893 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1894 | hdr_val = "Proxy-Connection: close"; |
| 1895 | hdr_len = 23; |
| 1896 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1897 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1901 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1902 | hdr_val = "Connection: keep-alive"; |
| 1903 | hdr_len = 22; |
| 1904 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1905 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1906 | hdr_len = 28; |
| 1907 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1908 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1909 | } |
| 1910 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1911 | } |
| 1912 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 1913 | /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to |
| 1914 | * the first byte of data after the chunk size, so that we know we can forward |
| 1915 | * exactly msg->next bytes. msg->sol contains the exact number of bytes forming |
| 1916 | * the chunk size. That way it is always possible to differentiate between the |
| 1917 | * start of the body and the start of the data. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1918 | * 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] | 1919 | * 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] | 1920 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1921 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1922 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1923 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1924 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1925 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1926 | const char *end = buf->data + buf->size; |
| 1927 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1928 | unsigned int chunk = 0; |
| 1929 | |
| 1930 | /* The chunk size is in the following form, though we are only |
| 1931 | * interested in the size and CRLF : |
| 1932 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1933 | */ |
| 1934 | while (1) { |
| 1935 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1936 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1937 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1938 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1939 | if (c < 0) /* not a hex digit anymore */ |
| 1940 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1941 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1942 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1943 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1944 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1945 | chunk = (chunk << 4) + c; |
| 1946 | } |
| 1947 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1948 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1949 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1950 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1951 | |
| 1952 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1953 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1954 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1955 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1956 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1957 | } |
| 1958 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1959 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1960 | * for the end of chunk size. |
| 1961 | */ |
| 1962 | while (1) { |
| 1963 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1964 | /* we now have a CR or an LF at ptr */ |
| 1965 | if (likely(*ptr == '\r')) { |
| 1966 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1967 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1968 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1969 | return 0; |
| 1970 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1971 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1972 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1973 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1974 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1975 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1976 | /* done */ |
| 1977 | break; |
| 1978 | } |
| 1979 | else if (*ptr == ';') { |
| 1980 | /* chunk extension, ends at next CRLF */ |
| 1981 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1982 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1983 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1984 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1985 | |
| 1986 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1987 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1988 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1989 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1990 | return 0; |
| 1991 | } |
| 1992 | /* we have a CRLF now, loop above */ |
| 1993 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1994 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1995 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1996 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1997 | } |
| 1998 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1999 | /* 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] | 2000 | * which may or may not be present. We save that into ->next, |
| 2001 | * and the number of bytes parsed into msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2002 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2003 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2004 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2005 | msg->sol += buf->size; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2006 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2007 | msg->chunk_len = chunk; |
| 2008 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2009 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2010 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2011 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2012 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2013 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2014 | } |
| 2015 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2016 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2017 | * 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] | 2018 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2019 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2020 | * If not enough data are available, the function does not change anything |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2021 | * except maybe msg->next if it could parse some lines, and returns zero. |
| 2022 | * If a parse error is encountered, the function returns < 0 and does not |
| 2023 | * change anything except maybe msg->next. Note that the message must |
| 2024 | * already be in HTTP_MSG_TRAILERS state before calling this function, |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2025 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2026 | * forwarding, and that msg->next exactly matches the length of trailers |
| 2027 | * already parsed and not forwarded. It is also important to note that this |
| 2028 | * 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] | 2029 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2030 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2031 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2032 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2033 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2034 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2035 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2036 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2037 | const char *ptr = b_ptr(buf, msg->next); |
| 2038 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2039 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2040 | |
| 2041 | /* scan current line and stop at LF or CRLF */ |
| 2042 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2043 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2044 | return 0; |
| 2045 | |
| 2046 | if (*ptr == '\n') { |
| 2047 | if (!p1) |
| 2048 | p1 = ptr; |
| 2049 | p2 = ptr; |
| 2050 | break; |
| 2051 | } |
| 2052 | |
| 2053 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2054 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2055 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2056 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2057 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2058 | p1 = ptr; |
| 2059 | } |
| 2060 | |
| 2061 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2062 | if (ptr >= buf->data + buf->size) |
| 2063 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | /* after LF; point to beginning of next line */ |
| 2067 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2068 | if (p2 >= buf->data + buf->size) |
| 2069 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2070 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2071 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2072 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2073 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2074 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2075 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2076 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2077 | * Everything was scheduled for forwarding, there's nothing |
| 2078 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2079 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2080 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2081 | msg->msg_state = HTTP_MSG_DONE; |
| 2082 | return 1; |
| 2083 | } |
| 2084 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2085 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2086 | } |
| 2087 | } |
| 2088 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2089 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF |
| 2090 | * or a possible LF alone at the end of a chunk. It automatically adjusts |
| 2091 | * 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] | 2092 | * 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] | 2093 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2094 | * not enough data are available, the function does not change anything and |
| 2095 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2096 | * does not change anything. Note: this function is designed to parse wrapped |
| 2097 | * CRLF at the end of the buffer. |
| 2098 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2099 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2100 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2101 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2102 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2103 | int bytes; |
| 2104 | |
| 2105 | /* NB: we'll check data availabilty at the end. It's not a |
| 2106 | * problem because whatever we match first will be checked |
| 2107 | * against the correct length. |
| 2108 | */ |
| 2109 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2110 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2111 | if (*ptr == '\r') { |
| 2112 | bytes++; |
| 2113 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2114 | if (ptr >= buf->data + buf->size) |
| 2115 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2116 | } |
| 2117 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2118 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2119 | return 0; |
| 2120 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2121 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2122 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2123 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2124 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2125 | |
| 2126 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2127 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2128 | ptr = buf->data; |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2129 | /* Advance ->next to allow the CRLF to be forwarded */ |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2130 | msg->next += bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2131 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2132 | return 1; |
| 2133 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2134 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2135 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2136 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2137 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2138 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2139 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2140 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2141 | { |
| 2142 | int q = 1000; |
| 2143 | |
| 2144 | if (!isdigit(*qvalue)) |
| 2145 | goto out; |
| 2146 | q = (*qvalue++ - '0') * 1000; |
| 2147 | |
| 2148 | if (*qvalue++ != '.') |
| 2149 | goto out; |
| 2150 | |
| 2151 | if (!isdigit(*qvalue)) |
| 2152 | goto out; |
| 2153 | q += (*qvalue++ - '0') * 100; |
| 2154 | |
| 2155 | if (!isdigit(*qvalue)) |
| 2156 | goto out; |
| 2157 | q += (*qvalue++ - '0') * 10; |
| 2158 | |
| 2159 | if (!isdigit(*qvalue)) |
| 2160 | goto out; |
| 2161 | q += (*qvalue++ - '0') * 1; |
| 2162 | out: |
| 2163 | if (q > 1000) |
| 2164 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2165 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2166 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2167 | return q; |
| 2168 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2169 | |
| 2170 | /* |
| 2171 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2172 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2173 | int select_compression_request_header(struct session *s, struct buffer *req) |
| 2174 | { |
| 2175 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2176 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2177 | struct hdr_ctx ctx; |
| 2178 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2179 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2180 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2181 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 2182 | * 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] | 2183 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 2184 | */ |
| 2185 | ctx.idx = 0; |
| 2186 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2187 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2188 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2189 | (ctx.vlen < 31 || |
| 2190 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2191 | ctx.line[ctx.val + 30] < '6' || |
| 2192 | (ctx.line[ctx.val + 30] == '6' && |
| 2193 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2194 | s->comp_algo = NULL; |
| 2195 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2196 | } |
| 2197 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2198 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2199 | 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] | 2200 | int best_q = 0; |
| 2201 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2202 | ctx.idx = 0; |
| 2203 | 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] | 2204 | const char *qval; |
| 2205 | int q; |
| 2206 | int toklen; |
| 2207 | |
| 2208 | /* try to isolate the token from the optional q-value */ |
| 2209 | toklen = 0; |
| 2210 | while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)]) |
| 2211 | toklen++; |
| 2212 | |
| 2213 | qval = ctx.line + ctx.val + toklen; |
| 2214 | while (1) { |
| 2215 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2216 | qval++; |
| 2217 | |
| 2218 | if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') { |
| 2219 | qval = NULL; |
| 2220 | break; |
| 2221 | } |
| 2222 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2223 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2224 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2225 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2226 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2227 | if (qval >= ctx.line + ctx.val + ctx.vlen) { |
| 2228 | qval = NULL; |
| 2229 | break; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2230 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2231 | if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0) |
| 2232 | break; |
| 2233 | |
| 2234 | while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';') |
| 2235 | qval++; |
| 2236 | } |
| 2237 | |
| 2238 | /* 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] | 2239 | q = qval ? parse_qvalue(qval + 2, NULL) : 1000; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2240 | |
| 2241 | if (q <= best_q) |
| 2242 | continue; |
| 2243 | |
| 2244 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 2245 | if (*(ctx.line + ctx.val) == '*' || |
| 2246 | word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) { |
| 2247 | s->comp_algo = comp_algo; |
| 2248 | best_q = q; |
| 2249 | break; |
| 2250 | } |
| 2251 | } |
| 2252 | } |
| 2253 | } |
| 2254 | |
| 2255 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2256 | if (s->comp_algo) { |
| 2257 | if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) { |
| 2258 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2259 | ctx.idx = 0; |
| 2260 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2261 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2262 | } |
| 2263 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2264 | return 1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | /* identity is implicit does not require headers */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2268 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
| 2269 | 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] | 2270 | if (comp_algo->add_data == identity_add_data) { |
| 2271 | s->comp_algo = comp_algo; |
| 2272 | return 1; |
| 2273 | } |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2278 | return 0; |
| 2279 | } |
| 2280 | |
| 2281 | /* |
| 2282 | * Selects a comression algorithm depending of the server response. |
| 2283 | */ |
| 2284 | int select_compression_response_header(struct session *s, struct buffer *res) |
| 2285 | { |
| 2286 | struct http_txn *txn = &s->txn; |
| 2287 | struct http_msg *msg = &txn->rsp; |
| 2288 | struct hdr_ctx ctx; |
| 2289 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2290 | |
| 2291 | /* no common compression algorithm was found in request header */ |
| 2292 | if (s->comp_algo == NULL) |
| 2293 | goto fail; |
| 2294 | |
| 2295 | /* HTTP < 1.1 should not be compressed */ |
Willy Tarreau | 7257550 | 2013-12-24 14:41:35 +0100 | [diff] [blame] | 2296 | 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] | 2297 | goto fail; |
| 2298 | |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2299 | /* 200 only */ |
| 2300 | if (txn->status != 200) |
| 2301 | goto fail; |
| 2302 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2303 | /* Content-Length is null */ |
| 2304 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2305 | goto fail; |
| 2306 | |
| 2307 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2308 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2309 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2310 | goto fail; |
| 2311 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2312 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2313 | ctx.idx = 0; |
| 2314 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2315 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2316 | goto fail; |
| 2317 | } |
| 2318 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2319 | comp_type = NULL; |
| 2320 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2321 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2322 | * not listed in the "compression type" directive if any. If no content-type was |
| 2323 | * found but configuration requires one, we don't compress either. Backend has |
| 2324 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2325 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2326 | ctx.idx = 0; |
| 2327 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2328 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2329 | goto fail; |
| 2330 | |
| 2331 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 2332 | (s->fe->comp && (comp_type = s->fe->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2333 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2334 | if (ctx.vlen >= comp_type->name_len && |
| 2335 | 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] | 2336 | /* this Content-Type should be compressed */ |
| 2337 | break; |
| 2338 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2339 | /* this Content-Type should not be compressed */ |
| 2340 | if (comp_type == NULL) |
| 2341 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2342 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2343 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2344 | else { /* no content-type header */ |
| 2345 | if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types)) |
| 2346 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2347 | } |
| 2348 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2349 | /* limit compression rate */ |
| 2350 | if (global.comp_rate_lim > 0) |
| 2351 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2352 | goto fail; |
| 2353 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2354 | /* limit cpu usage */ |
| 2355 | if (idle_pct < compress_min_idle) |
| 2356 | goto fail; |
| 2357 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2358 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2359 | 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] | 2360 | goto fail; |
| 2361 | |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2362 | s->flags |= SN_COMP_READY; |
| 2363 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2364 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2365 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2366 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2367 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2368 | |
| 2369 | /* add Transfer-Encoding header */ |
| 2370 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2371 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2372 | |
| 2373 | /* |
| 2374 | * Add Content-Encoding header when it's not identity encoding. |
| 2375 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2376 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2377 | * header. |
| 2378 | */ |
| 2379 | if (s->comp_algo->add_data != identity_add_data) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2380 | trash.len = 18; |
| 2381 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
| 2382 | memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len); |
| 2383 | trash.len += s->comp_algo->name_len; |
| 2384 | trash.str[trash.len] = '\0'; |
| 2385 | 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] | 2386 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2387 | return 1; |
| 2388 | |
| 2389 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2390 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2391 | return 0; |
| 2392 | } |
| 2393 | |
| 2394 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2395 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2396 | * processing can continue on next analysers, or zero if it either needs more |
| 2397 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2398 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2399 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2400 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2401 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2402 | 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] | 2403 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2404 | /* |
| 2405 | * We will parse the partial (or complete) lines. |
| 2406 | * We will check the request syntax, and also join multi-line |
| 2407 | * headers. An index of all the lines will be elaborated while |
| 2408 | * parsing. |
| 2409 | * |
| 2410 | * For the parsing, we use a 28 states FSM. |
| 2411 | * |
| 2412 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2413 | * req->buf->p = beginning of request |
| 2414 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2415 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2416 | * msg->eol = end of current header or line (LF or CRLF) |
| 2417 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2418 | * |
| 2419 | * 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] | 2420 | * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2421 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2422 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2423 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2424 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2425 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2426 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2427 | struct http_txn *txn = &s->txn; |
| 2428 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2429 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2430 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2431 | 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] | 2432 | now_ms, __FUNCTION__, |
| 2433 | s, |
| 2434 | req, |
| 2435 | req->rex, req->wex, |
| 2436 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2437 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2438 | req->analysers); |
| 2439 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2440 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2441 | s->srv_error = http_return_srv_error; |
| 2442 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2443 | /* There's a protected area at the end of the buffer for rewriting |
| 2444 | * purposes. We don't want to start to parse the request if the |
| 2445 | * protected area is affected, because we may have to move processed |
| 2446 | * data later, which is much more complicated. |
| 2447 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2448 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2449 | if (txn->flags & TX_NOT_FIRST) { |
| 2450 | if (unlikely(!channel_reserved(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2451 | 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] | 2452 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2453 | /* 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] | 2454 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2455 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2456 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2457 | return 0; |
| 2458 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2459 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2460 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2461 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2462 | } |
| 2463 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2464 | /* Note that we have the same problem with the response ; we |
| 2465 | * may want to send a redirect, error or anything which requires |
| 2466 | * some spare space. So we'll ensure that we have at least |
| 2467 | * maxrewrite bytes available in the response buffer before |
| 2468 | * processing that one. This will only affect pipelined |
| 2469 | * keep-alive requests. |
| 2470 | */ |
| 2471 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2472 | unlikely(!channel_reserved(s->rep) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2473 | bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) || |
| 2474 | bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) { |
| 2475 | if (s->rep->buf->o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2476 | if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2477 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2478 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2479 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2480 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2481 | s->rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2482 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2483 | return 0; |
| 2484 | } |
| 2485 | } |
| 2486 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2487 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2488 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2489 | } |
| 2490 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2491 | /* 1: we might have to print this header in debug mode */ |
| 2492 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2493 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2494 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2495 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2496 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2497 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2498 | /* this is a bit complex : in case of error on the request line, |
| 2499 | * we know that rq.l is still zero, so we display only the part |
| 2500 | * up to the end of the line (truncated by debug_hdr). |
| 2501 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2502 | 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] | 2503 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2504 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2505 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2506 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2507 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2508 | while (cur_idx) { |
| 2509 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2510 | debug_hdr("clihdr", s, sol, eol); |
| 2511 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2512 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2513 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2514 | } |
| 2515 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2516 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2517 | /* |
| 2518 | * Now we quickly check if we have found a full valid request. |
| 2519 | * If not so, we check the FD and buffer states before leaving. |
| 2520 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2521 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2522 | * requests are checked first. When waiting for a second request |
| 2523 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2524 | * we note the error in the session flags but don't set any state. |
| 2525 | * Since the error will be noted there, it will not be counted by |
| 2526 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2527 | * Last, we may increase some tracked counters' http request errors on |
| 2528 | * the cases that are deliberately the client's fault. For instance, |
| 2529 | * a timeout or connection reset is not counted as an error. However |
| 2530 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2531 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2532 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2533 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2534 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2535 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2536 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2537 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2538 | session_inc_http_req_ctr(s); |
| 2539 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2540 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2541 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2542 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2543 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2544 | /* 1: Since we are in header mode, if there's no space |
| 2545 | * left for headers, we won't be able to free more |
| 2546 | * later, so the session will never terminate. We |
| 2547 | * must terminate it now. |
| 2548 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2549 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2550 | /* FIXME: check if URI is set and return Status |
| 2551 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2552 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2553 | session_inc_http_req_ctr(s); |
| 2554 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2555 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2556 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2557 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2558 | goto return_bad_req; |
| 2559 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2560 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2561 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2562 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2563 | if (!(s->flags & SN_ERR_MASK)) |
| 2564 | s->flags |= SN_ERR_CLICL; |
| 2565 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2566 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2567 | goto failed_keep_alive; |
| 2568 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2569 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2570 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2571 | 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] | 2572 | session_inc_http_err_ctr(s); |
| 2573 | } |
| 2574 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2575 | txn->status = 400; |
| 2576 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2577 | msg->msg_state = HTTP_MSG_ERROR; |
| 2578 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2579 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2580 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2581 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2582 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2583 | if (s->listener->counters) |
| 2584 | s->listener->counters->failed_req++; |
| 2585 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2586 | if (!(s->flags & SN_FINST_MASK)) |
| 2587 | s->flags |= SN_FINST_R; |
| 2588 | return 0; |
| 2589 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2590 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2591 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2592 | 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] | 2593 | if (!(s->flags & SN_ERR_MASK)) |
| 2594 | s->flags |= SN_ERR_CLITO; |
| 2595 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2596 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2597 | goto failed_keep_alive; |
| 2598 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2599 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2600 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2601 | 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] | 2602 | session_inc_http_err_ctr(s); |
| 2603 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2604 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2605 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2606 | msg->msg_state = HTTP_MSG_ERROR; |
| 2607 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2608 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2609 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2610 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2611 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2612 | if (s->listener->counters) |
| 2613 | s->listener->counters->failed_req++; |
| 2614 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2615 | if (!(s->flags & SN_FINST_MASK)) |
| 2616 | s->flags |= SN_FINST_R; |
| 2617 | return 0; |
| 2618 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2619 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2620 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2621 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2622 | if (!(s->flags & SN_ERR_MASK)) |
| 2623 | s->flags |= SN_ERR_CLICL; |
| 2624 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2625 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2626 | goto failed_keep_alive; |
| 2627 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2628 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2629 | 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] | 2630 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2631 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2632 | msg->msg_state = HTTP_MSG_ERROR; |
| 2633 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2634 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2635 | session_inc_http_err_ctr(s); |
| 2636 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2637 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2638 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2639 | if (s->listener->counters) |
| 2640 | s->listener->counters->failed_req++; |
| 2641 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2642 | if (!(s->flags & SN_FINST_MASK)) |
| 2643 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2644 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2645 | } |
| 2646 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2647 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2648 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2649 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2650 | #ifdef TCP_QUICKACK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 2651 | if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && conn_ctrl_ready(__objt_conn(s->req->prod->end))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2652 | /* We need more data, we have to re-enable quick-ack in case we |
| 2653 | * previously disabled it, otherwise we might cause the client |
| 2654 | * to delay next data. |
| 2655 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2656 | setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2657 | } |
| 2658 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2659 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2660 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2661 | /* If the client starts to talk, let's fall back to |
| 2662 | * request timeout processing. |
| 2663 | */ |
| 2664 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2665 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2666 | } |
| 2667 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2668 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2669 | if (!tick_isset(req->analyse_exp)) { |
| 2670 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2671 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2672 | tick_isset(s->be->timeout.httpka)) |
| 2673 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2674 | else |
| 2675 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2676 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2677 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2678 | /* we're not ready yet */ |
| 2679 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2680 | |
| 2681 | failed_keep_alive: |
| 2682 | /* Here we process low-level errors for keep-alive requests. In |
| 2683 | * short, if the request is not the first one and it experiences |
| 2684 | * a timeout, read error or shutdown, we just silently close so |
| 2685 | * that the client can try again. |
| 2686 | */ |
| 2687 | txn->status = 0; |
| 2688 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2689 | req->analysers = 0; |
| 2690 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2691 | s->logs.level = 0; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2692 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2693 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2694 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2695 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2696 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2697 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2698 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2699 | * 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] | 2700 | * 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] | 2701 | * 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] | 2702 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2703 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2704 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2705 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2706 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2707 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2708 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2709 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2710 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2711 | /* kill the pending keep-alive timeout */ |
| 2712 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2713 | req->analyse_exp = TICK_ETERNITY; |
| 2714 | } |
| 2715 | |
| 2716 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2717 | /* Maybe we found in invalid header name while we were configured not |
| 2718 | * to block on that, so we have to capture it now. |
| 2719 | */ |
| 2720 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2721 | 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] | 2722 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2723 | /* |
| 2724 | * 1: identify the method |
| 2725 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2726 | 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] | 2727 | |
| 2728 | /* we can make use of server redirect on GET and HEAD */ |
| 2729 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2730 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2731 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2732 | /* |
| 2733 | * 2: check if the URI matches the monitor_uri. |
| 2734 | * We have to do this for every request which gets in, because |
| 2735 | * the monitor-uri is defined by the frontend. |
| 2736 | */ |
| 2737 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2738 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2739 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2740 | s->fe->monitor_uri, |
| 2741 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2742 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2743 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2744 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2745 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2746 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2747 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2748 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2749 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2750 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2751 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2752 | 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] | 2753 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2754 | ret = acl_pass(ret); |
| 2755 | if (cond->pol == ACL_COND_UNLESS) |
| 2756 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2757 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2758 | if (ret) { |
| 2759 | /* we fail this request, let's return 503 service unavail */ |
| 2760 | txn->status = 503; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2761 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2762 | if (!(s->flags & SN_ERR_MASK)) |
| 2763 | 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] | 2764 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2765 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2766 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2767 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2768 | /* nothing to fail, let's reply normaly */ |
| 2769 | txn->status = 200; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2770 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2771 | if (!(s->flags & SN_ERR_MASK)) |
| 2772 | 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] | 2773 | goto return_prx_cond; |
| 2774 | } |
| 2775 | |
| 2776 | /* |
| 2777 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2778 | * Note: we cannot log anymore if the request has been |
| 2779 | * classified as invalid. |
| 2780 | */ |
| 2781 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2782 | /* we have a complete HTTP request that we must log */ |
| 2783 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2784 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2785 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2786 | if (urilen >= REQURI_LEN) |
| 2787 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2788 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2789 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2790 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2791 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2792 | s->do_log(s); |
| 2793 | } else { |
| 2794 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2795 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2796 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2797 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2798 | /* 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] | 2799 | 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] | 2800 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2801 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2802 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2803 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2804 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2805 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2806 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2807 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2808 | |
| 2809 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2810 | 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] | 2811 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2812 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2813 | * we have what looks like a proxied connection instead of a connection, |
| 2814 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2815 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2816 | * happen to do that despite being non-standard :-( |
| 2817 | * We consider that a request not beginning with either '/' or '*' is |
| 2818 | * a proxied connection, which covers both "scheme://location" and |
| 2819 | * CONNECT ip:port. |
| 2820 | */ |
| 2821 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2822 | 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] | 2823 | txn->flags |= TX_USE_PX_CONN; |
| 2824 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2825 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2826 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2827 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2828 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2829 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2830 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2831 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2832 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2833 | /* 6: determine the transfer-length. |
| 2834 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2835 | * the presence of a message-body in a REQUEST and its transfer length |
| 2836 | * must be determined that way (in order of precedence) : |
| 2837 | * 1. The presence of a message-body in a request is signaled by the |
| 2838 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2839 | * in the request's header fields. When a request message contains |
| 2840 | * both a message-body of non-zero length and a method that does |
| 2841 | * not define any semantics for that request message-body, then an |
| 2842 | * origin server SHOULD either ignore the message-body or respond |
| 2843 | * with an appropriate error message (e.g., 413). A proxy or |
| 2844 | * gateway, when presented the same request, SHOULD either forward |
| 2845 | * the request inbound with the message- body or ignore the |
| 2846 | * message-body when determining a response. |
| 2847 | * |
| 2848 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2849 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2850 | * transfer-length is defined by the use of this transfer-coding. |
| 2851 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2852 | * transfer-coding is not present, the transfer-length is defined |
| 2853 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2854 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2855 | * 3. If a Content-Length header field is present, its decimal value in |
| 2856 | * OCTETs represents both the entity-length and the transfer-length. |
| 2857 | * If a message is received with both a Transfer-Encoding header |
| 2858 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2859 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2860 | * 4. By the server closing the connection. (Closing the connection |
| 2861 | * cannot be used to indicate the end of a request body, since that |
| 2862 | * would leave no possibility for the server to send back a response.) |
| 2863 | * |
| 2864 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2865 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2866 | * it is terminated by closing the connection. When the "chunked" |
| 2867 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2868 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2869 | */ |
| 2870 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2871 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2872 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2873 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2874 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2875 | 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] | 2876 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2877 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2878 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2879 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2880 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2881 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2882 | break; |
| 2883 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2884 | } |
| 2885 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2886 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2887 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2888 | 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] | 2889 | signed long long cl; |
| 2890 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2891 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2892 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2893 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2894 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2895 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2896 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2897 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2898 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2899 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2900 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2901 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2902 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2903 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2904 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2905 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2906 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2907 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2908 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2909 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2910 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2911 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2912 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2913 | } |
| 2914 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2915 | /* bodyless requests have a known length */ |
| 2916 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2917 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2918 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2919 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2920 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2921 | req->analyse_exp = TICK_ETERNITY; |
| 2922 | return 1; |
| 2923 | |
| 2924 | return_bad_req: |
| 2925 | /* We centralize bad requests processing here */ |
| 2926 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2927 | /* we detected a parsing error. We want to archive this request |
| 2928 | * in the dedicated proxy area for later troubleshooting. |
| 2929 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2930 | 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] | 2931 | } |
| 2932 | |
| 2933 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2934 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2935 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2936 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2937 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2938 | if (s->listener->counters) |
| 2939 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2940 | |
| 2941 | return_prx_cond: |
| 2942 | if (!(s->flags & SN_ERR_MASK)) |
| 2943 | s->flags |= SN_ERR_PRXCOND; |
| 2944 | if (!(s->flags & SN_FINST_MASK)) |
| 2945 | s->flags |= SN_FINST_R; |
| 2946 | |
| 2947 | req->analysers = 0; |
| 2948 | req->analyse_exp = TICK_ETERNITY; |
| 2949 | return 0; |
| 2950 | } |
| 2951 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2952 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2953 | /* This function prepares an applet to handle the stats. It can deal with the |
| 2954 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 2955 | * and program a response message if something was unexpected. It cannot fail |
| 2956 | * 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] | 2957 | * 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] | 2958 | * s->target which is supposed to already point to the stats applet. The caller |
| 2959 | * is expected to have already assigned an appctx to the session. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2960 | */ |
| 2961 | int http_handle_stats(struct session *s, struct channel *req) |
| 2962 | { |
| 2963 | struct stats_admin_rule *stats_admin_rule; |
| 2964 | struct stream_interface *si = s->rep->prod; |
| 2965 | struct http_txn *txn = &s->txn; |
| 2966 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2967 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 2968 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2969 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2970 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2971 | appctx = si_appctx(si); |
| 2972 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 2973 | appctx->st1 = appctx->st2 = 0; |
| 2974 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 2975 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | af3cf70 | 2014-04-22 22:19:53 +0200 | [diff] [blame] | 2976 | if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD)) |
| 2977 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2978 | |
| 2979 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 2980 | lookup = uri + uri_auth->uri_len; |
| 2981 | |
| 2982 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 2983 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2984 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2985 | break; |
| 2986 | } |
| 2987 | } |
| 2988 | |
| 2989 | if (uri_auth->refresh) { |
| 2990 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 2991 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2992 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2993 | break; |
| 2994 | } |
| 2995 | } |
| 2996 | } |
| 2997 | |
| 2998 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 2999 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3000 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3001 | break; |
| 3002 | } |
| 3003 | } |
| 3004 | |
| 3005 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3006 | if (memcmp(h, ";st=", 4) == 0) { |
| 3007 | int i; |
| 3008 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3009 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3010 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3011 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3012 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3013 | break; |
| 3014 | } |
| 3015 | } |
| 3016 | break; |
| 3017 | } |
| 3018 | } |
| 3019 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3020 | appctx->ctx.stats.scope_str = 0; |
| 3021 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3022 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3023 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3024 | int itx = 0; |
| 3025 | const char *h2; |
| 3026 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3027 | const char *err; |
| 3028 | |
| 3029 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3030 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3031 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3032 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3033 | itx++; |
| 3034 | h++; |
| 3035 | } |
| 3036 | |
| 3037 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3038 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3039 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3040 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3041 | /* 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] | 3042 | memcpy(scope_txt, h2, itx); |
| 3043 | scope_txt[itx] = '\0'; |
| 3044 | err = invalid_char(scope_txt); |
| 3045 | if (err) { |
| 3046 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3047 | appctx->ctx.stats.scope_str = 0; |
| 3048 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3049 | } |
| 3050 | break; |
| 3051 | } |
| 3052 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3053 | |
| 3054 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3055 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3056 | int ret = 1; |
| 3057 | |
| 3058 | if (stats_admin_rule->cond) { |
| 3059 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 3060 | ret = acl_pass(ret); |
| 3061 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3062 | ret = !ret; |
| 3063 | } |
| 3064 | |
| 3065 | if (ret) { |
| 3066 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3067 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3068 | break; |
| 3069 | } |
| 3070 | } |
| 3071 | |
| 3072 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3073 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3074 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3075 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3076 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3077 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3078 | */ |
| 3079 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 3080 | struct hdr_ctx ctx; |
| 3081 | ctx.idx = 0; |
| 3082 | /* Expect is allowed in 1.1, look for it */ |
| 3083 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 3084 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3085 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3086 | } |
| 3087 | } |
| 3088 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3089 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3090 | } |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3091 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3092 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3093 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3094 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3095 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3096 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3097 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3098 | else { |
| 3099 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3100 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3101 | } |
| 3102 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3103 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3104 | return 1; |
| 3105 | } |
| 3106 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3107 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3108 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3109 | */ |
| 3110 | static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
| 3111 | { |
| 3112 | #ifdef IP_TOS |
| 3113 | if (from.ss_family == AF_INET) |
| 3114 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3115 | #endif |
| 3116 | #ifdef IPV6_TCLASS |
| 3117 | if (from.ss_family == AF_INET6) { |
| 3118 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3119 | /* v4-mapped addresses need IP_TOS */ |
| 3120 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3121 | else |
| 3122 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3123 | } |
| 3124 | #endif |
| 3125 | } |
| 3126 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3127 | /* Executes the http-request rules <rules> for session <s>, proxy <px> and |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3128 | * transaction <txn>. Returns the first rule that prevents further processing |
| 3129 | * of the request (auth, deny, ...) or NULL if it executed all rules or stopped |
| 3130 | * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny |
| 3131 | * rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3132 | */ |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3133 | static struct http_req_rule * |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3134 | 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] | 3135 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3136 | struct connection *cli_conn; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3137 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3138 | struct hdr_ctx ctx; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3139 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3140 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3141 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3142 | continue; |
| 3143 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3144 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3145 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3146 | int ret; |
| 3147 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3148 | 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] | 3149 | ret = acl_pass(ret); |
| 3150 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3151 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3152 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3153 | |
| 3154 | if (!ret) /* condition not matched */ |
| 3155 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3156 | } |
| 3157 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3158 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3159 | switch (rule->action) { |
| 3160 | case HTTP_REQ_ACT_ALLOW: |
| 3161 | return NULL; /* "allow" rules are OK */ |
| 3162 | |
| 3163 | case HTTP_REQ_ACT_DENY: |
| 3164 | txn->flags |= TX_CLDENY; |
| 3165 | return rule; |
| 3166 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3167 | case HTTP_REQ_ACT_TARPIT: |
| 3168 | txn->flags |= TX_CLTARPIT; |
| 3169 | return rule; |
| 3170 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3171 | case HTTP_REQ_ACT_AUTH: |
| 3172 | return rule; |
| 3173 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3174 | case HTTP_REQ_ACT_REDIR: |
| 3175 | return rule; |
| 3176 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3177 | case HTTP_REQ_ACT_SET_NICE: |
| 3178 | s->task->nice = rule->arg.nice; |
| 3179 | break; |
| 3180 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3181 | case HTTP_REQ_ACT_SET_TOS: |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3182 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3183 | 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] | 3184 | break; |
| 3185 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3186 | case HTTP_REQ_ACT_SET_MARK: |
| 3187 | #ifdef SO_MARK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3188 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3189 | 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] | 3190 | #endif |
| 3191 | break; |
| 3192 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3193 | case HTTP_REQ_ACT_SET_LOGL: |
| 3194 | s->logs.level = rule->arg.loglevel; |
| 3195 | break; |
| 3196 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3197 | case HTTP_REQ_ACT_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3198 | case HTTP_REQ_ACT_SET_HDR: |
| 3199 | ctx.idx = 0; |
| 3200 | /* remove all occurrences of the header */ |
| 3201 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3202 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3203 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3204 | } |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3205 | if (rule->action == HTTP_REQ_ACT_DEL_HDR) |
| 3206 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3207 | /* now fall through to header addition */ |
| 3208 | |
| 3209 | case HTTP_REQ_ACT_ADD_HDR: |
| 3210 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3211 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3212 | trash.len = rule->arg.hdr_add.name_len; |
| 3213 | trash.str[trash.len++] = ':'; |
| 3214 | trash.str[trash.len++] = ' '; |
| 3215 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
| 3216 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3217 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3218 | |
| 3219 | case HTTP_REQ_ACT_DEL_ACL: |
| 3220 | case HTTP_REQ_ACT_DEL_MAP: { |
| 3221 | struct pat_ref *ref; |
| 3222 | char *key; |
| 3223 | int len; |
| 3224 | |
| 3225 | /* collect reference */ |
| 3226 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3227 | if (!ref) |
| 3228 | continue; |
| 3229 | |
| 3230 | /* collect key */ |
| 3231 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3232 | key = trash.str; |
| 3233 | key[len] = '\0'; |
| 3234 | |
| 3235 | /* perform update */ |
| 3236 | /* returned code: 1=ok, 0=ko */ |
| 3237 | pat_ref_delete(ref, key); |
| 3238 | |
| 3239 | break; |
| 3240 | } |
| 3241 | |
| 3242 | case HTTP_REQ_ACT_ADD_ACL: { |
| 3243 | struct pat_ref *ref; |
| 3244 | char *key; |
| 3245 | struct chunk *trash_key; |
| 3246 | int len; |
| 3247 | |
| 3248 | trash_key = get_trash_chunk(); |
| 3249 | |
| 3250 | /* collect reference */ |
| 3251 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3252 | if (!ref) |
| 3253 | continue; |
| 3254 | |
| 3255 | /* collect key */ |
| 3256 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3257 | key = trash_key->str; |
| 3258 | key[len] = '\0'; |
| 3259 | |
| 3260 | /* perform update */ |
| 3261 | /* add entry only if it does not already exist */ |
| 3262 | if (pat_ref_find_elt(ref, key) == NULL) |
Willy Tarreau | a631fc8 | 2014-04-26 23:33:51 +0200 | [diff] [blame^] | 3263 | pat_ref_add(ref, key, NULL, 0, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3264 | |
| 3265 | break; |
| 3266 | } |
| 3267 | |
| 3268 | case HTTP_REQ_ACT_SET_MAP: { |
| 3269 | struct pat_ref *ref; |
| 3270 | char *key, *value; |
| 3271 | struct chunk *trash_key, *trash_value; |
| 3272 | int len; |
| 3273 | |
| 3274 | trash_key = get_trash_chunk(); |
| 3275 | trash_value = get_trash_chunk(); |
| 3276 | |
| 3277 | /* collect reference */ |
| 3278 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3279 | if (!ref) |
| 3280 | continue; |
| 3281 | |
| 3282 | /* collect key */ |
| 3283 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3284 | key = trash_key->str; |
| 3285 | key[len] = '\0'; |
| 3286 | |
| 3287 | /* collect value */ |
| 3288 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3289 | value = trash_value->str; |
| 3290 | value[len] = '\0'; |
| 3291 | |
| 3292 | /* perform update */ |
| 3293 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3294 | /* update entry if it exists */ |
| 3295 | pat_ref_set(ref, key, value, NULL); |
| 3296 | else |
| 3297 | /* insert a new entry */ |
Willy Tarreau | a631fc8 | 2014-04-26 23:33:51 +0200 | [diff] [blame^] | 3298 | pat_ref_add(ref, key, value, 0, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3299 | |
| 3300 | break; |
| 3301 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3302 | |
| 3303 | case HTTP_REQ_ACT_CUSTOM_CONT: |
| 3304 | rule->action_ptr(rule, px, s, txn); |
| 3305 | break; |
| 3306 | |
| 3307 | case HTTP_REQ_ACT_CUSTOM_STOP: |
| 3308 | rule->action_ptr(rule, px, s, txn); |
| 3309 | return rule; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3310 | } |
| 3311 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3312 | |
| 3313 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 418c1a0 | 2012-12-25 20:52:58 +0100 | [diff] [blame] | 3314 | return NULL; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3315 | } |
| 3316 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3317 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3318 | /* Executes the http-response rules <rules> for session <s>, proxy <px> and |
| 3319 | * transaction <txn>. Returns the first rule that prevents further processing |
| 3320 | * of the response (deny, ...) or NULL if it executed all rules or stopped |
| 3321 | * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny |
| 3322 | * rule. |
| 3323 | */ |
| 3324 | static struct http_res_rule * |
| 3325 | http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 3326 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3327 | struct connection *cli_conn; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3328 | struct http_res_rule *rule; |
| 3329 | struct hdr_ctx ctx; |
| 3330 | |
| 3331 | list_for_each_entry(rule, rules, list) { |
| 3332 | if (rule->action >= HTTP_RES_ACT_MAX) |
| 3333 | continue; |
| 3334 | |
| 3335 | /* check optional condition */ |
| 3336 | if (rule->cond) { |
| 3337 | int ret; |
| 3338 | |
| 3339 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 3340 | ret = acl_pass(ret); |
| 3341 | |
| 3342 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3343 | ret = !ret; |
| 3344 | |
| 3345 | if (!ret) /* condition not matched */ |
| 3346 | continue; |
| 3347 | } |
| 3348 | |
| 3349 | |
| 3350 | switch (rule->action) { |
| 3351 | case HTTP_RES_ACT_ALLOW: |
| 3352 | return NULL; /* "allow" rules are OK */ |
| 3353 | |
| 3354 | case HTTP_RES_ACT_DENY: |
| 3355 | txn->flags |= TX_SVDENY; |
| 3356 | return rule; |
| 3357 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3358 | case HTTP_RES_ACT_SET_NICE: |
| 3359 | s->task->nice = rule->arg.nice; |
| 3360 | break; |
| 3361 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3362 | case HTTP_RES_ACT_SET_TOS: |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3363 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3364 | 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] | 3365 | break; |
| 3366 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3367 | case HTTP_RES_ACT_SET_MARK: |
| 3368 | #ifdef SO_MARK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3369 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3370 | 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] | 3371 | #endif |
| 3372 | break; |
| 3373 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3374 | case HTTP_RES_ACT_SET_LOGL: |
| 3375 | s->logs.level = rule->arg.loglevel; |
| 3376 | break; |
| 3377 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3378 | case HTTP_RES_ACT_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3379 | case HTTP_RES_ACT_SET_HDR: |
| 3380 | ctx.idx = 0; |
| 3381 | /* remove all occurrences of the header */ |
| 3382 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3383 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3384 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3385 | } |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3386 | if (rule->action == HTTP_RES_ACT_DEL_HDR) |
| 3387 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3388 | /* now fall through to header addition */ |
| 3389 | |
| 3390 | case HTTP_RES_ACT_ADD_HDR: |
| 3391 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3392 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3393 | trash.len = rule->arg.hdr_add.name_len; |
| 3394 | trash.str[trash.len++] = ':'; |
| 3395 | trash.str[trash.len++] = ' '; |
| 3396 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
| 3397 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3398 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3399 | |
| 3400 | case HTTP_RES_ACT_DEL_ACL: |
| 3401 | case HTTP_RES_ACT_DEL_MAP: { |
| 3402 | struct pat_ref *ref; |
| 3403 | char *key; |
| 3404 | int len; |
| 3405 | |
| 3406 | /* collect reference */ |
| 3407 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3408 | if (!ref) |
| 3409 | continue; |
| 3410 | |
| 3411 | /* collect key */ |
| 3412 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3413 | key = trash.str; |
| 3414 | key[len] = '\0'; |
| 3415 | |
| 3416 | /* perform update */ |
| 3417 | /* returned code: 1=ok, 0=ko */ |
| 3418 | pat_ref_delete(ref, key); |
| 3419 | |
| 3420 | break; |
| 3421 | } |
| 3422 | |
| 3423 | case HTTP_RES_ACT_ADD_ACL: { |
| 3424 | struct pat_ref *ref; |
| 3425 | char *key; |
| 3426 | struct chunk *trash_key; |
| 3427 | int len; |
| 3428 | |
| 3429 | trash_key = get_trash_chunk(); |
| 3430 | |
| 3431 | /* collect reference */ |
| 3432 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3433 | if (!ref) |
| 3434 | continue; |
| 3435 | |
| 3436 | /* collect key */ |
| 3437 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3438 | key = trash_key->str; |
| 3439 | key[len] = '\0'; |
| 3440 | |
| 3441 | /* perform update */ |
| 3442 | /* check if the entry already exists */ |
| 3443 | if (pat_ref_find_elt(ref, key) == NULL) |
Willy Tarreau | a631fc8 | 2014-04-26 23:33:51 +0200 | [diff] [blame^] | 3444 | pat_ref_add(ref, key, NULL, 0, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3445 | |
| 3446 | break; |
| 3447 | } |
| 3448 | |
| 3449 | case HTTP_RES_ACT_SET_MAP: { |
| 3450 | struct pat_ref *ref; |
| 3451 | char *key, *value; |
| 3452 | struct chunk *trash_key, *trash_value; |
| 3453 | int len; |
| 3454 | |
| 3455 | trash_key = get_trash_chunk(); |
| 3456 | trash_value = get_trash_chunk(); |
| 3457 | |
| 3458 | /* collect reference */ |
| 3459 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3460 | if (!ref) |
| 3461 | continue; |
| 3462 | |
| 3463 | /* collect key */ |
| 3464 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3465 | key = trash_key->str; |
| 3466 | key[len] = '\0'; |
| 3467 | |
| 3468 | /* collect value */ |
| 3469 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3470 | value = trash_value->str; |
| 3471 | value[len] = '\0'; |
| 3472 | |
| 3473 | /* perform update */ |
| 3474 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3475 | /* update entry if it exists */ |
| 3476 | pat_ref_set(ref, key, value, NULL); |
| 3477 | else |
| 3478 | /* insert a new entry */ |
Willy Tarreau | a631fc8 | 2014-04-26 23:33:51 +0200 | [diff] [blame^] | 3479 | pat_ref_add(ref, key, value, 0, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3480 | |
| 3481 | break; |
| 3482 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3483 | |
| 3484 | case HTTP_RES_ACT_CUSTOM_CONT: |
| 3485 | rule->action_ptr(rule, px, s, txn); |
| 3486 | break; |
| 3487 | |
| 3488 | case HTTP_RES_ACT_CUSTOM_STOP: |
| 3489 | rule->action_ptr(rule, px, s, txn); |
| 3490 | return rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3491 | } |
| 3492 | } |
| 3493 | |
| 3494 | /* we reached the end of the rules, nothing to report */ |
| 3495 | return NULL; |
| 3496 | } |
| 3497 | |
| 3498 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3499 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3500 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3501 | * as too large a request to build a valid response. |
| 3502 | */ |
| 3503 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn) |
| 3504 | { |
| 3505 | struct http_msg *msg = &txn->req; |
| 3506 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3507 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3508 | |
| 3509 | /* build redirect message */ |
| 3510 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3511 | case 308: |
| 3512 | msg_fmt = HTTP_308; |
| 3513 | break; |
| 3514 | case 307: |
| 3515 | msg_fmt = HTTP_307; |
| 3516 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3517 | case 303: |
| 3518 | msg_fmt = HTTP_303; |
| 3519 | break; |
| 3520 | case 301: |
| 3521 | msg_fmt = HTTP_301; |
| 3522 | break; |
| 3523 | case 302: |
| 3524 | default: |
| 3525 | msg_fmt = HTTP_302; |
| 3526 | break; |
| 3527 | } |
| 3528 | |
| 3529 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3530 | return 0; |
| 3531 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3532 | location = trash.str + trash.len; |
| 3533 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3534 | switch(rule->type) { |
| 3535 | case REDIRECT_TYPE_SCHEME: { |
| 3536 | const char *path; |
| 3537 | const char *host; |
| 3538 | struct hdr_ctx ctx; |
| 3539 | int pathlen; |
| 3540 | int hostlen; |
| 3541 | |
| 3542 | host = ""; |
| 3543 | hostlen = 0; |
| 3544 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3545 | 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] | 3546 | host = ctx.line + ctx.val; |
| 3547 | hostlen = ctx.vlen; |
| 3548 | } |
| 3549 | |
| 3550 | path = http_get_path(txn); |
| 3551 | /* build message using path */ |
| 3552 | if (path) { |
| 3553 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3554 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3555 | int qs = 0; |
| 3556 | while (qs < pathlen) { |
| 3557 | if (path[qs] == '?') { |
| 3558 | pathlen = qs; |
| 3559 | break; |
| 3560 | } |
| 3561 | qs++; |
| 3562 | } |
| 3563 | } |
| 3564 | } else { |
| 3565 | path = "/"; |
| 3566 | pathlen = 1; |
| 3567 | } |
| 3568 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3569 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3570 | /* check if we can add scheme + "://" + host + path */ |
| 3571 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3572 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3573 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3574 | /* add scheme */ |
| 3575 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3576 | trash.len += rule->rdr_len; |
| 3577 | } |
| 3578 | else { |
| 3579 | /* add scheme with executing log format */ |
| 3580 | 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] | 3581 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3582 | /* check if we can add scheme + "://" + host + path */ |
| 3583 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 3584 | return 0; |
| 3585 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3586 | /* add "://" */ |
| 3587 | memcpy(trash.str + trash.len, "://", 3); |
| 3588 | trash.len += 3; |
| 3589 | |
| 3590 | /* add host */ |
| 3591 | memcpy(trash.str + trash.len, host, hostlen); |
| 3592 | trash.len += hostlen; |
| 3593 | |
| 3594 | /* add path */ |
| 3595 | memcpy(trash.str + trash.len, path, pathlen); |
| 3596 | trash.len += pathlen; |
| 3597 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3598 | /* 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] | 3599 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3600 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3601 | if (trash.len > trash.size - 5) |
| 3602 | return 0; |
| 3603 | trash.str[trash.len] = '/'; |
| 3604 | trash.len++; |
| 3605 | } |
| 3606 | |
| 3607 | break; |
| 3608 | } |
| 3609 | case REDIRECT_TYPE_PREFIX: { |
| 3610 | const char *path; |
| 3611 | int pathlen; |
| 3612 | |
| 3613 | path = http_get_path(txn); |
| 3614 | /* build message using path */ |
| 3615 | if (path) { |
| 3616 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3617 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3618 | int qs = 0; |
| 3619 | while (qs < pathlen) { |
| 3620 | if (path[qs] == '?') { |
| 3621 | pathlen = qs; |
| 3622 | break; |
| 3623 | } |
| 3624 | qs++; |
| 3625 | } |
| 3626 | } |
| 3627 | } else { |
| 3628 | path = "/"; |
| 3629 | pathlen = 1; |
| 3630 | } |
| 3631 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3632 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3633 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 3634 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3635 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3636 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3637 | * add anything, otherwise it makes it hard for the user to |
| 3638 | * configure a self-redirection. |
| 3639 | */ |
| 3640 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 3641 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3642 | trash.len += rule->rdr_len; |
| 3643 | } |
| 3644 | } |
| 3645 | else { |
| 3646 | /* add prefix with executing log format */ |
| 3647 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 3648 | |
| 3649 | /* Check length */ |
| 3650 | if (trash.len + pathlen > trash.size - 4) |
| 3651 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | /* add path */ |
| 3655 | memcpy(trash.str + trash.len, path, pathlen); |
| 3656 | trash.len += pathlen; |
| 3657 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3658 | /* 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] | 3659 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3660 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3661 | if (trash.len > trash.size - 5) |
| 3662 | return 0; |
| 3663 | trash.str[trash.len] = '/'; |
| 3664 | trash.len++; |
| 3665 | } |
| 3666 | |
| 3667 | break; |
| 3668 | } |
| 3669 | case REDIRECT_TYPE_LOCATION: |
| 3670 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3671 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3672 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 3673 | return 0; |
| 3674 | |
| 3675 | /* add location */ |
| 3676 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3677 | trash.len += rule->rdr_len; |
| 3678 | } |
| 3679 | else { |
| 3680 | /* add location with executing log format */ |
| 3681 | 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] | 3682 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3683 | /* Check left length */ |
| 3684 | if (trash.len > trash.size - 4) |
| 3685 | return 0; |
| 3686 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3687 | break; |
| 3688 | } |
| 3689 | |
| 3690 | if (rule->cookie_len) { |
| 3691 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 3692 | trash.len += 14; |
| 3693 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 3694 | trash.len += rule->cookie_len; |
| 3695 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 3696 | trash.len += 2; |
| 3697 | } |
| 3698 | |
| 3699 | /* add end of headers and the keep-alive/close status. |
| 3700 | * We may choose to set keep-alive if the Location begins |
| 3701 | * with a slash, because the client will come back to the |
| 3702 | * same server. |
| 3703 | */ |
| 3704 | txn->status = rule->code; |
| 3705 | /* let's log the request time */ |
| 3706 | s->logs.tv_request = now; |
| 3707 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3708 | if (*location == '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3709 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3710 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
| 3711 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3712 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3713 | /* keep-alive possible */ |
| 3714 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
| 3715 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3716 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3717 | trash.len += 30; |
| 3718 | } else { |
| 3719 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 3720 | trash.len += 24; |
| 3721 | } |
| 3722 | } |
| 3723 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 3724 | trash.len += 4; |
| 3725 | bo_inject(txn->rsp.chn, trash.str, trash.len); |
| 3726 | /* "eat" the request */ |
| 3727 | bi_fast_delete(txn->req.chn->buf, msg->sov); |
Willy Tarreau | 6d8bac7 | 2014-04-25 12:19:32 +0200 | [diff] [blame] | 3728 | msg->next -= msg->sov; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3729 | msg->sov = 0; |
| 3730 | txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY; |
| 3731 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3732 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3733 | txn->rsp.msg_state = HTTP_MSG_DONE; |
| 3734 | } else { |
| 3735 | /* keep-alive not possible */ |
| 3736 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3737 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3738 | trash.len += 29; |
| 3739 | } else { |
| 3740 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3741 | trash.len += 23; |
| 3742 | } |
| 3743 | stream_int_retnclose(txn->req.chn->prod, &trash); |
| 3744 | txn->req.chn->analysers = 0; |
| 3745 | } |
| 3746 | |
| 3747 | if (!(s->flags & SN_ERR_MASK)) |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 3748 | s->flags |= SN_ERR_LOCAL; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3749 | if (!(s->flags & SN_FINST_MASK)) |
| 3750 | s->flags |= SN_FINST_R; |
| 3751 | |
| 3752 | return 1; |
| 3753 | } |
| 3754 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3755 | /* This stream analyser runs all HTTP request processing which is common to |
| 3756 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3757 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3758 | * 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] | 3759 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3760 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3761 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3762 | 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] | 3763 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3764 | struct http_txn *txn = &s->txn; |
| 3765 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3766 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3767 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3768 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3769 | struct cond_wordlist *wl; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3770 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3771 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3772 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3773 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3774 | return 0; |
| 3775 | } |
| 3776 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3777 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3778 | req->analyse_exp = TICK_ETERNITY; |
| 3779 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3780 | 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] | 3781 | now_ms, __FUNCTION__, |
| 3782 | s, |
| 3783 | req, |
| 3784 | req->rex, req->wex, |
| 3785 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3786 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3787 | req->analysers); |
| 3788 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3789 | /* first check whether we have some ACLs set to block this request */ |
| 3790 | list_for_each_entry(cond, &px->block_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3791 | int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 3792 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3793 | ret = acl_pass(ret); |
| 3794 | if (cond->pol == ACL_COND_UNLESS) |
| 3795 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 3796 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3797 | if (ret) { |
| 3798 | txn->status = 403; |
| 3799 | /* let's log the request time */ |
| 3800 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3801 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3802 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3803 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3804 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3805 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3806 | |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 3807 | /* just in case we have some per-backend tracking */ |
| 3808 | session_inc_be_http_req_ctr(s); |
| 3809 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3810 | /* evaluate http-request rules */ |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3811 | http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 3812 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3813 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3814 | if (!http_req_last_rule) { |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3815 | if (stats_check_uri(s->rep->prod, txn, px)) { |
| 3816 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 1fbe1c9 | 2013-12-01 09:35:41 +0100 | [diff] [blame] | 3817 | if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) { |
| 3818 | txn->status = 500; |
| 3819 | s->logs.tv_request = now; |
| 3820 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3821 | |
Willy Tarreau | 1fbe1c9 | 2013-12-01 09:35:41 +0100 | [diff] [blame] | 3822 | if (!(s->flags & SN_ERR_MASK)) |
| 3823 | s->flags |= SN_ERR_RESOURCE; |
| 3824 | goto return_prx_cond; |
| 3825 | } |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3826 | /* parse the whole stats request and extract the relevant information */ |
| 3827 | http_handle_stats(s, req); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3828 | http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn); |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3829 | } |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3830 | } |
| 3831 | |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3832 | /* only apply req{,i}{rep/deny/tarpit} if the request was not yet |
| 3833 | * blocked by an http-request rule. |
| 3834 | */ |
| 3835 | if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3836 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3837 | goto return_bad_req; |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3838 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3839 | |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3840 | /* return a 403 if either rule has blocked */ |
| 3841 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3842 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3843 | txn->status = 403; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3844 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3845 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3846 | session_inc_http_err_ctr(s); |
Willy Tarreau | 687ba13 | 2013-11-16 10:13:35 +0100 | [diff] [blame] | 3847 | s->fe->fe_counters.denied_req++; |
| 3848 | if (s->fe != s->be) |
| 3849 | s->be->be_counters.denied_req++; |
| 3850 | if (s->listener->counters) |
| 3851 | s->listener->counters->denied_req++; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3852 | goto return_prx_cond; |
| 3853 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3854 | |
| 3855 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3856 | * which may be the same as the connect timeout if unspecified. |
| 3857 | * If unset, then set it to zero because we really want it to |
| 3858 | * eventually expire. We build the tarpit as an analyser. |
| 3859 | */ |
| 3860 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3861 | channel_erase(s->req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3862 | /* wipe the request out so that we can drop the connection early |
| 3863 | * if the client closes first. |
| 3864 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3865 | channel_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3866 | req->analysers = 0; /* remove switching rules etc... */ |
| 3867 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3868 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3869 | if (!req->analyse_exp) |
| 3870 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3871 | session_inc_http_err_ctr(s); |
Willy Tarreau | 687ba13 | 2013-11-16 10:13:35 +0100 | [diff] [blame] | 3872 | s->fe->fe_counters.denied_req++; |
| 3873 | if (s->fe != s->be) |
| 3874 | s->be->be_counters.denied_req++; |
| 3875 | if (s->listener->counters) |
| 3876 | s->listener->counters->denied_req++; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3877 | return 1; |
| 3878 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3879 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3880 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3881 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3882 | * only change if both the request and the config reference something else. |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3883 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3884 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3885 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3886 | * if FE and BE have the same settings (common). The method consists in |
| 3887 | * checking if options changed between the two calls (implying that either |
| 3888 | * one is non-null, or one of them is non-null and we are there for the first |
| 3889 | * time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 3890 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3891 | |
Willy Tarreau | 416ce61 | 2014-01-31 15:45:34 +0100 | [diff] [blame] | 3892 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3893 | ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) { |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3894 | int tmp = TX_CON_WANT_KAL; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3895 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3896 | if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 3897 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
| 3898 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 3899 | tmp = TX_CON_WANT_TUN; |
| 3900 | |
| 3901 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 3902 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 3903 | tmp = TX_CON_WANT_TUN; |
| 3904 | } |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3905 | |
| 3906 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3907 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 3908 | /* option httpclose + server_close => forceclose */ |
| 3909 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 3910 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 3911 | tmp = TX_CON_WANT_CLO; |
| 3912 | else |
| 3913 | tmp = TX_CON_WANT_SCL; |
| 3914 | } |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3915 | |
| 3916 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
| 3917 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3918 | tmp = TX_CON_WANT_CLO; |
| 3919 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3920 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 3921 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3922 | |
Willy Tarreau | 416ce61 | 2014-01-31 15:45:34 +0100 | [diff] [blame] | 3923 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 3924 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3925 | /* parse the Connection header and possibly clean it */ |
| 3926 | int to_del = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3927 | if ((msg->flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 3928 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 3929 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3930 | to_del |= 2; /* remove "keep-alive" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3931 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3932 | to_del |= 1; /* remove "close" */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3933 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3934 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3935 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3936 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 3937 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 3938 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 3939 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3940 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3941 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 3942 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3943 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 3944 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3945 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3946 | /* we can be blocked here because the request needs to be authenticated, |
| 3947 | * either to pass or to access stats. |
| 3948 | */ |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3949 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 3950 | char *realm = http_req_last_rule->arg.auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3951 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3952 | if (!realm) |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3953 | realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3954 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3955 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3956 | txn->status = 401; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3957 | stream_int_retnclose(req->prod, &trash); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3958 | /* on 401 we still count one error, because normal browsing |
| 3959 | * won't significantly increase the counter but brute force |
| 3960 | * attempts will. |
| 3961 | */ |
| 3962 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3963 | goto return_prx_cond; |
| 3964 | } |
| 3965 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3966 | /* add request headers from the rule sets in the same order */ |
| 3967 | list_for_each_entry(wl, &px->req_add, list) { |
| 3968 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3969 | 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] | 3970 | ret = acl_pass(ret); |
| 3971 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3972 | ret = !ret; |
| 3973 | if (!ret) |
| 3974 | continue; |
| 3975 | } |
| 3976 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3977 | 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] | 3978 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3979 | } |
| 3980 | |
| 3981 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) { |
| 3982 | if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn)) |
| 3983 | goto return_bad_req; |
| 3984 | req->analyse_exp = TICK_ETERNITY; |
| 3985 | return 1; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3986 | } |
| 3987 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3988 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_CUSTOM_STOP) { |
| 3989 | req->analyse_exp = TICK_ETERNITY; |
| 3990 | return 1; |
| 3991 | } |
| 3992 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3993 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3994 | /* process the stats request now */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3995 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3996 | s->fe->fe_counters.intercepted_req++; |
| 3997 | |
| 3998 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 3999 | s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy |
| 4000 | if (!(s->flags & SN_FINST_MASK)) |
| 4001 | s->flags |= SN_FINST_R; |
| 4002 | |
| 4003 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4004 | |
| 4005 | /* we may want to compress the stats page */ |
| 4006 | if (s->fe->comp || s->be->comp) |
| 4007 | select_compression_request_header(s, req->buf); |
| 4008 | |
| 4009 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
| 4010 | req->analysers = AN_REQ_HTTP_XFER_BODY | AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_BE | AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4011 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4012 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4013 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4014 | /* check whether we have some ACLs set to redirect this request */ |
| 4015 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4016 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4017 | int ret; |
| 4018 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 4019 | 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] | 4020 | ret = acl_pass(ret); |
| 4021 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4022 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4023 | if (!ret) |
| 4024 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4025 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4026 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4027 | goto return_bad_req; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 4028 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4029 | req->analyse_exp = TICK_ETERNITY; |
| 4030 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4031 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4032 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4033 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4034 | * If this happens, then the data will not come immediately, so we must |
| 4035 | * send all what we have without waiting. Note that due to the small gain |
| 4036 | * 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] | 4037 | * 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] | 4038 | * itself once used. |
| 4039 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4040 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4041 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4042 | /* that's OK for us now, let's move on to next analysers */ |
| 4043 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4044 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4045 | return_bad_req: |
| 4046 | /* We centralize bad requests processing here */ |
| 4047 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4048 | /* we detected a parsing error. We want to archive this request |
| 4049 | * in the dedicated proxy area for later troubleshooting. |
| 4050 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4051 | 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] | 4052 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4053 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4054 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4055 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4056 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4057 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4058 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4059 | if (s->listener->counters) |
| 4060 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4061 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4062 | return_prx_cond: |
| 4063 | if (!(s->flags & SN_ERR_MASK)) |
| 4064 | s->flags |= SN_ERR_PRXCOND; |
| 4065 | if (!(s->flags & SN_FINST_MASK)) |
| 4066 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4067 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4068 | req->analysers = 0; |
| 4069 | req->analyse_exp = TICK_ETERNITY; |
| 4070 | return 0; |
| 4071 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4072 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4073 | /* This function performs all the processing enabled for the current request. |
| 4074 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4075 | * needs more data, encounters an error, or wants to immediately abort the |
| 4076 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 4077 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4078 | 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] | 4079 | { |
| 4080 | struct http_txn *txn = &s->txn; |
| 4081 | struct http_msg *msg = &txn->req; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4082 | struct connection *cli_conn = objt_conn(req->prod->end); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4083 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4084 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4085 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4086 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4087 | return 0; |
| 4088 | } |
| 4089 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4090 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4091 | now_ms, __FUNCTION__, |
| 4092 | s, |
| 4093 | req, |
| 4094 | req->rex, req->wex, |
| 4095 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4096 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4097 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4098 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 4099 | if (s->fe->comp || s->be->comp) |
| 4100 | select_compression_request_header(s, req->buf); |
| 4101 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4102 | /* |
| 4103 | * Right now, we know that we have processed the entire headers |
| 4104 | * and that unwanted requests have been filtered out. We can do |
| 4105 | * whatever we want with the remaining request. Also, now we |
| 4106 | * may have separate values for ->fe, ->be. |
| 4107 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4108 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4109 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4110 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4111 | * incoming request. Note that this requires that a connection is |
| 4112 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4113 | */ |
| 4114 | 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] | 4115 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4116 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4117 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4118 | /* Note that for now we don't reuse existing proxy connections */ |
| 4119 | if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4120 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4121 | txn->status = 500; |
| 4122 | req->analysers = 0; |
| 4123 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
| 4124 | |
| 4125 | if (!(s->flags & SN_ERR_MASK)) |
| 4126 | s->flags |= SN_ERR_RESOURCE; |
| 4127 | if (!(s->flags & SN_FINST_MASK)) |
| 4128 | s->flags |= SN_FINST_R; |
| 4129 | |
| 4130 | return 0; |
| 4131 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4132 | |
| 4133 | path = http_get_path(txn); |
| 4134 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4135 | 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] | 4136 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4137 | /* if the path was found, we have to remove everything between |
| 4138 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4139 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4140 | * u_l characters by a single "/". |
| 4141 | */ |
| 4142 | if (path) { |
| 4143 | char *cur_ptr = req->buf->p; |
| 4144 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4145 | int delta; |
| 4146 | |
| 4147 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4148 | http_msg_move_end(&txn->req, delta); |
| 4149 | cur_end += delta; |
| 4150 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4151 | goto return_bad_req; |
| 4152 | } |
| 4153 | else { |
| 4154 | char *cur_ptr = req->buf->p; |
| 4155 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4156 | int delta; |
| 4157 | |
| 4158 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4159 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4160 | http_msg_move_end(&txn->req, delta); |
| 4161 | cur_end += delta; |
| 4162 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4163 | goto return_bad_req; |
| 4164 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4165 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4166 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4167 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4168 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4169 | * Note that doing so might move headers in the request, but |
| 4170 | * the fields will stay coherent and the URI will not move. |
| 4171 | * This should only be performed in the backend. |
| 4172 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 4173 | 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] | 4174 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 4175 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4176 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4177 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4178 | * 8: the appsession cookie was looked up very early in 1.2, |
| 4179 | * so let's do the same now. |
| 4180 | */ |
| 4181 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4182 | /* It needs to look into the URI unless persistence must be ignored */ |
| 4183 | 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] | 4184 | 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] | 4185 | } |
| 4186 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4187 | /* add unique-id if "header-unique-id" is specified */ |
| 4188 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4189 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 4190 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4191 | goto return_bad_req; |
| 4192 | s->unique_id[0] = '\0'; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4193 | 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] | 4194 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4195 | |
| 4196 | if (s->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4197 | chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 4198 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4199 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4200 | 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] | 4201 | goto return_bad_req; |
| 4202 | } |
| 4203 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4204 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4205 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4206 | * asks for it. |
| 4207 | */ |
| 4208 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4209 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4210 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Cyril Bonté | a32d275 | 2012-05-29 23:27:41 +0200 | [diff] [blame] | 4211 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name, |
| 4212 | 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] | 4213 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4214 | /* The header is set to be added only if none is present |
| 4215 | * and we found it, so don't do anything. |
| 4216 | */ |
| 4217 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4218 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4219 | /* Add an X-Forwarded-For header unless the source IP is |
| 4220 | * in the 'except' network range. |
| 4221 | */ |
| 4222 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4223 | (((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] | 4224 | != s->fe->except_net.s_addr) && |
| 4225 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4226 | (((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] | 4227 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4228 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4229 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4230 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4231 | |
| 4232 | /* Note: we rely on the backend to get the header name to be used for |
| 4233 | * x-forwarded-for, because the header is really meant for the backends. |
| 4234 | * However, if the backend did not specify any option, we have to rely |
| 4235 | * on the frontend's header name. |
| 4236 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4237 | if (s->be->fwdfor_hdr_len) { |
| 4238 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4239 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4240 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4241 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4242 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4243 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4244 | 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] | 4245 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4246 | 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] | 4247 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4248 | } |
| 4249 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4250 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4251 | /* FIXME: for the sake of completeness, we should also support |
| 4252 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4253 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4254 | int len; |
| 4255 | char pn[INET6_ADDRSTRLEN]; |
| 4256 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4257 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4258 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4259 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4260 | /* Note: we rely on the backend to get the header name to be used for |
| 4261 | * x-forwarded-for, because the header is really meant for the backends. |
| 4262 | * However, if the backend did not specify any option, we have to rely |
| 4263 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4264 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4265 | if (s->be->fwdfor_hdr_len) { |
| 4266 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4267 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4268 | } else { |
| 4269 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4270 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4271 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4272 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4273 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4274 | 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] | 4275 | goto return_bad_req; |
| 4276 | } |
| 4277 | } |
| 4278 | |
| 4279 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4280 | * 10: add X-Original-To if either the frontend or the backend |
| 4281 | * asks for it. |
| 4282 | */ |
| 4283 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 4284 | |
| 4285 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4286 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4287 | /* Add an X-Original-To header unless the destination IP is |
| 4288 | * in the 'except' network range. |
| 4289 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4290 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4291 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4292 | if (cli_conn->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4293 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4294 | (((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] | 4295 | != s->fe->except_to.s_addr) && |
| 4296 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4297 | (((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] | 4298 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4299 | int len; |
| 4300 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4301 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4302 | |
| 4303 | /* Note: we rely on the backend to get the header name to be used for |
| 4304 | * x-original-to, because the header is really meant for the backends. |
| 4305 | * However, if the backend did not specify any option, we have to rely |
| 4306 | * on the frontend's header name. |
| 4307 | */ |
| 4308 | if (s->be->orgto_hdr_len) { |
| 4309 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4310 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4311 | } else { |
| 4312 | len = s->fe->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4313 | memcpy(trash.str, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4314 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4315 | 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] | 4316 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4317 | 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] | 4318 | goto return_bad_req; |
| 4319 | } |
| 4320 | } |
| 4321 | } |
| 4322 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4323 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4324 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4325 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4326 | * "Upgrade" is present in the Connection header. |
| 4327 | */ |
| 4328 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4329 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4330 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4331 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4332 | unsigned int want_flags = 0; |
| 4333 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4334 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4335 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4336 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4337 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4338 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4339 | want_flags |= TX_CON_CLO_SET; |
| 4340 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4341 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4342 | ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
| 4343 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4344 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4345 | want_flags |= TX_CON_KAL_SET; |
| 4346 | } |
| 4347 | |
| 4348 | 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] | 4349 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4350 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4351 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4352 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4353 | /* If we have no server assigned yet and we're balancing on url_param |
| 4354 | * with a POST request, we may be interested in checking the body for |
| 4355 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4356 | */ |
| 4357 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 4358 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4359 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4360 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4361 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4362 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4363 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4364 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4365 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4366 | #ifdef TCP_QUICKACK |
| 4367 | /* We expect some data from the client. Unless we know for sure |
| 4368 | * we already have a full request, we have to re-enable quick-ack |
| 4369 | * in case we previously disabled it, otherwise we might cause |
| 4370 | * the client to delay further data. |
| 4371 | */ |
| 4372 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4373 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4374 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4375 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4376 | 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] | 4377 | #endif |
| 4378 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4379 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4380 | /************************************************************* |
| 4381 | * OK, that's finished for the headers. We have done what we * |
| 4382 | * could. Let's switch to the DATA state. * |
| 4383 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4384 | req->analyse_exp = TICK_ETERNITY; |
| 4385 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4386 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4387 | /* if the server closes the connection, we want to immediately react |
| 4388 | * and close the socket to save packets and syscalls. |
| 4389 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4390 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
| 4391 | req->cons->flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4392 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4393 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4394 | /* OK let's go on with the BODY now */ |
| 4395 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4396 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4397 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4398 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4399 | /* we detected a parsing error. We want to archive this request |
| 4400 | * in the dedicated proxy area for later troubleshooting. |
| 4401 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4402 | 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] | 4403 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4404 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4405 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4406 | txn->status = 400; |
| 4407 | req->analysers = 0; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4408 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4409 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4410 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4411 | if (s->listener->counters) |
| 4412 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4413 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4414 | if (!(s->flags & SN_ERR_MASK)) |
| 4415 | s->flags |= SN_ERR_PRXCOND; |
| 4416 | if (!(s->flags & SN_FINST_MASK)) |
| 4417 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4418 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4419 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4420 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4421 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4422 | * returns zero, at the beginning because it prevents any other processing |
| 4423 | * from occurring, and at the end because it terminates the request. |
| 4424 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4425 | 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] | 4426 | { |
| 4427 | struct http_txn *txn = &s->txn; |
| 4428 | |
| 4429 | /* This connection is being tarpitted. The CLIENT side has |
| 4430 | * already set the connect expiration date to the right |
| 4431 | * timeout. We just have to check that the client is still |
| 4432 | * there and that the timeout has not expired. |
| 4433 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4434 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4435 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4436 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4437 | return 0; |
| 4438 | |
| 4439 | /* We will set the queue timer to the time spent, just for |
| 4440 | * logging purposes. We fake a 500 server error, so that the |
| 4441 | * attacker will not suspect his connection has been tarpitted. |
| 4442 | * It will not cause trouble to the logs because we can exclude |
| 4443 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4444 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4445 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4446 | |
| 4447 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4448 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4449 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4450 | |
| 4451 | req->analysers = 0; |
| 4452 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4453 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4454 | if (!(s->flags & SN_ERR_MASK)) |
| 4455 | s->flags |= SN_ERR_PRXCOND; |
| 4456 | if (!(s->flags & SN_FINST_MASK)) |
| 4457 | s->flags |= SN_FINST_T; |
| 4458 | return 0; |
| 4459 | } |
| 4460 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4461 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4462 | * for either the buffer to be full, or the full advertised contents to have |
| 4463 | * reached the buffer. It must only be called after the standard HTTP request |
| 4464 | * processing has occurred, because it expects the request to be parsed and will |
| 4465 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4466 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4467 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4468 | * 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] | 4469 | */ |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4470 | 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] | 4471 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4472 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4473 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4474 | |
| 4475 | /* We have to parse the HTTP request body to find any required data. |
| 4476 | * "balance url_param check_post" should have been the only way to get |
| 4477 | * into this. We were brought here after HTTP header analysis, so all |
| 4478 | * related structures are ready. |
| 4479 | */ |
| 4480 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4481 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4482 | /* This is the first call */ |
| 4483 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4484 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4485 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4486 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4487 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4488 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4489 | */ |
| 4490 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4491 | struct hdr_ctx ctx; |
| 4492 | ctx.idx = 0; |
| 4493 | /* Expect is allowed in 1.1, look for it */ |
| 4494 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4495 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 4496 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 4497 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4498 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4499 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4500 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4501 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4502 | /* 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] | 4503 | * req->buf->p still points to the beginning of the message. We |
| 4504 | * must save the body in msg->next because it survives buffer |
| 4505 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4506 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4507 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4508 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4509 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4510 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4511 | else |
| 4512 | msg->msg_state = HTTP_MSG_DATA; |
| 4513 | } |
| 4514 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4515 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4516 | /* We're in content-length mode, we just have to wait for enough data. */ |
| 4517 | if (req->buf->i - msg->sov < msg->body_len) |
| 4518 | goto missing_data; |
| 4519 | |
| 4520 | /* OK we have everything we need now */ |
| 4521 | goto http_end; |
| 4522 | } |
| 4523 | |
| 4524 | /* OK here we're parsing a chunked-encoded message */ |
| 4525 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4526 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4527 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4528 | * 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] | 4529 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4530 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4531 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4532 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4533 | if (!ret) |
| 4534 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4535 | else if (ret < 0) { |
| 4536 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4537 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4538 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4539 | } |
| 4540 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4541 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4542 | * 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] | 4543 | * 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] | 4544 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4545 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4546 | goto http_end; |
| 4547 | |
Willy Tarreau | 226071e | 2014-04-10 11:55:45 +0200 | [diff] [blame] | 4548 | 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] | 4549 | goto http_end; |
| 4550 | |
| 4551 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4552 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4553 | * we have the maximum we can expect. |
| 4554 | */ |
| 4555 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4556 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4557 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4558 | 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] | 4559 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4560 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4561 | |
| 4562 | if (!(s->flags & SN_ERR_MASK)) |
| 4563 | s->flags |= SN_ERR_CLITO; |
| 4564 | if (!(s->flags & SN_FINST_MASK)) |
| 4565 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4566 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4567 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4568 | |
| 4569 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4570 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4571 | /* Not enough data. We'll re-use the http-request |
| 4572 | * timeout here. Ideally, we should set the timeout |
| 4573 | * relative to the accept() date. We just set the |
| 4574 | * request timeout once at the beginning of the |
| 4575 | * request. |
| 4576 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4577 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4578 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4579 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4580 | return 0; |
| 4581 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4582 | |
| 4583 | http_end: |
| 4584 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4585 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4586 | req->analysers &= ~an_bit; |
| 4587 | req->analyse_exp = TICK_ETERNITY; |
| 4588 | return 1; |
| 4589 | |
| 4590 | return_bad_req: /* let's centralize all bad requests */ |
| 4591 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4592 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4593 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4594 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4595 | if (!(s->flags & SN_ERR_MASK)) |
| 4596 | s->flags |= SN_ERR_PRXCOND; |
| 4597 | if (!(s->flags & SN_FINST_MASK)) |
| 4598 | s->flags |= SN_FINST_R; |
| 4599 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4600 | return_err_msg: |
| 4601 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4602 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4603 | if (s->listener->counters) |
| 4604 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4605 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4606 | } |
| 4607 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4608 | /* send a server's name with an outgoing request over an established connection. |
| 4609 | * Note: this function is designed to be called once the request has been scheduled |
| 4610 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4611 | * proceeding. |
| 4612 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4613 | 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] | 4614 | |
| 4615 | struct hdr_ctx ctx; |
| 4616 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4617 | char *hdr_name = be->server_id_hdr_name; |
| 4618 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4619 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4620 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4621 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4622 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4623 | ctx.idx = 0; |
| 4624 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4625 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4626 | if (old_o) { |
| 4627 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4628 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4629 | txn->req.next += old_o; |
| 4630 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4631 | } |
| 4632 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4633 | old_i = chn->buf->i; |
| 4634 | 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] | 4635 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4636 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4640 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4641 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4642 | hdr_val += hdr_name_len; |
| 4643 | *hdr_val++ = ':'; |
| 4644 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4645 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4646 | 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] | 4647 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4648 | if (old_o) { |
| 4649 | /* If this was a forwarded request, we must readjust the amount of |
| 4650 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4651 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4652 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4653 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4654 | old_o += chn->buf->i - old_i; |
| 4655 | b_adv(chn->buf, old_o); |
| 4656 | txn->req.next -= old_o; |
| 4657 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4658 | } |
| 4659 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4660 | return 0; |
| 4661 | } |
| 4662 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4663 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4664 | * right now but it works. |
| 4665 | */ |
| 4666 | void http_end_txn_clean_session(struct session *s) |
| 4667 | { |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4668 | int prev_status = s->txn.status; |
| 4669 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4670 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4671 | * server's connections. We need a safer way to reinitialize buffer |
| 4672 | * flags. We also need a more accurate method for computing per-request |
| 4673 | * data. |
| 4674 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4675 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4676 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4677 | * to the server. |
| 4678 | */ |
| 4679 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4680 | !si_conn_ready(s->req->cons)) { |
| 4681 | s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4682 | si_shutr(s->req->cons); |
| 4683 | si_shutw(s->req->cons); |
| 4684 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4685 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4686 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4687 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4688 | if (unlikely(s->srv_conn)) |
| 4689 | sess_change_server(s, NULL); |
| 4690 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4691 | |
| 4692 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4693 | session_process_counters(s); |
Willy Tarreau | f333834 | 2014-01-28 21:40:28 +0100 | [diff] [blame] | 4694 | session_stop_content_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4695 | |
| 4696 | if (s->txn.status) { |
| 4697 | int n; |
| 4698 | |
| 4699 | n = s->txn.status / 100; |
| 4700 | if (n < 1 || n > 5) |
| 4701 | n = 0; |
| 4702 | |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4703 | if (s->fe->mode == PR_MODE_HTTP) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4704 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4705 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4706 | s->fe->fe_counters.p.http.comp_rsp++; |
| 4707 | } |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 4708 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4709 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4710 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4711 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4712 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4713 | s->be->be_counters.p.http.comp_rsp++; |
| 4714 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4715 | } |
| 4716 | |
| 4717 | /* don't count other requests' data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4718 | s->logs.bytes_in -= s->req->buf->i; |
| 4719 | s->logs.bytes_out -= s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4720 | |
| 4721 | /* let's do a final log if we need it */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 4722 | if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4723 | !(s->flags & SN_MONITOR) && |
| 4724 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 4725 | s->do_log(s); |
| 4726 | } |
| 4727 | |
| 4728 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4729 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4730 | tv_zero(&s->logs.tv_request); |
| 4731 | s->logs.t_queue = -1; |
| 4732 | s->logs.t_connect = -1; |
| 4733 | s->logs.t_data = -1; |
| 4734 | s->logs.t_close = 0; |
| 4735 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4736 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4737 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4738 | s->logs.bytes_in = s->req->total = s->req->buf->i; |
| 4739 | s->logs.bytes_out = s->rep->total = s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4740 | |
| 4741 | if (s->pend_pos) |
| 4742 | pendconn_free(s->pend_pos); |
| 4743 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4744 | if (objt_server(s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4745 | if (s->flags & SN_CURR_SESS) { |
| 4746 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4747 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4748 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4749 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 4750 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4751 | } |
| 4752 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4753 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4754 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4755 | /* only release our endpoint if we don't intend to reuse the |
| 4756 | * connection. |
| 4757 | */ |
| 4758 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4759 | !si_conn_ready(s->req->cons)) { |
| 4760 | si_release_endpoint(s->req->cons); |
| 4761 | } |
| 4762 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4763 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4764 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 4765 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4766 | s->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | c920096 | 2013-12-31 23:03:09 +0100 | [diff] [blame] | 4767 | s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4768 | s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT); |
| 4769 | s->rep->flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT); |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4770 | 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] | 4771 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4772 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4773 | s->txn.meth = 0; |
| 4774 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4775 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4776 | |
| 4777 | if (prev_status == 401 || prev_status == 407) { |
| 4778 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 4779 | * a chance of being able to send the visitor again to the same |
| 4780 | * server over the same connection. This is required by some |
| 4781 | * broken protocols such as NTLM, and anyway whenever there is |
| 4782 | * an opportunity for sending the challenge to the proper place, |
| 4783 | * it's better to do it (at least it helps with debugging). |
| 4784 | */ |
| 4785 | s->txn.flags |= TX_PREFER_LAST; |
| 4786 | } |
| 4787 | |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 4788 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4789 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 4790 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4791 | if (s->fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4792 | s->req->flags |= CF_NEVER_WAIT; |
| 4793 | s->rep->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4794 | } |
| 4795 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4796 | /* if the request buffer is not empty, it means we're |
| 4797 | * about to process another request, so send pending |
| 4798 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4799 | * Just don't do this if the buffer is close to be full, |
| 4800 | * because the request will wait for it to flush a little |
| 4801 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4802 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4803 | if (s->req->buf->i) { |
| 4804 | if (s->rep->buf->o && |
| 4805 | !buffer_full(s->rep->buf, global.tune.maxrewrite) && |
| 4806 | bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4807 | s->rep->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4808 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4809 | |
| 4810 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4811 | channel_auto_read(s->req); |
| 4812 | channel_auto_close(s->req); |
| 4813 | channel_auto_read(s->rep); |
| 4814 | channel_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4815 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 4816 | /* we're in keep-alive with an idle connection, monitor it */ |
| 4817 | si_idle_conn(s->req->cons); |
| 4818 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 4819 | s->req->analysers = s->listener->analysers; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4820 | s->rep->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4821 | } |
| 4822 | |
| 4823 | |
| 4824 | /* This function updates the request state machine according to the response |
| 4825 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4826 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4827 | * it is only used to find when a request/response couple is complete. Both |
| 4828 | * this function and its equivalent should loop until both return zero. It |
| 4829 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4830 | */ |
| 4831 | int http_sync_req_state(struct session *s) |
| 4832 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4833 | struct channel *chn = s->req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4834 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4835 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4836 | unsigned int old_state = txn->req.msg_state; |
| 4837 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4838 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 4839 | return 0; |
| 4840 | |
| 4841 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4842 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4843 | * We can shut the read side unless we want to abort_on_close, |
| 4844 | * or we have a POST request. The issue with POST requests is |
| 4845 | * that some browsers still send a CRLF after the request, and |
| 4846 | * this CRLF must be read so that it does not remain in the kernel |
| 4847 | * buffers, otherwise a close could cause an RST on some systems |
| 4848 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4849 | * Note that if we're using keep-alive on the client side, we'd |
| 4850 | * rather poll now and keep the polling enabled for the whole |
| 4851 | * session's life than enabling/disabling it between each |
| 4852 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4853 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4854 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4855 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4856 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 4857 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4858 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4859 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4860 | /* if the server closes the connection, we want to immediately react |
| 4861 | * and close the socket to save packets and syscalls. |
| 4862 | */ |
| 4863 | chn->cons->flags |= SI_FL_NOHALF; |
| 4864 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4865 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4866 | goto wait_other_side; |
| 4867 | |
| 4868 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4869 | /* The server has not finished to respond, so we |
| 4870 | * don't want to move in order not to upset it. |
| 4871 | */ |
| 4872 | goto wait_other_side; |
| 4873 | } |
| 4874 | |
| 4875 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4876 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4877 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4878 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4879 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4880 | goto wait_other_side; |
| 4881 | } |
| 4882 | |
| 4883 | /* When we get here, it means that both the request and the |
| 4884 | * response have finished receiving. Depending on the connection |
| 4885 | * mode, we'll have to wait for the last bytes to leave in either |
| 4886 | * direction, and sometimes for a close to be effective. |
| 4887 | */ |
| 4888 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4889 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4890 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4891 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 4892 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4893 | } |
| 4894 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4895 | /* Option forceclose is set, or either side wants to close, |
| 4896 | * let's enforce it now that we're not expecting any new |
| 4897 | * data to come. The caller knows the session is complete |
| 4898 | * once both states are CLOSED. |
| 4899 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4900 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4901 | channel_shutr_now(chn); |
| 4902 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4903 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4904 | } |
| 4905 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4906 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 4907 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4908 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4909 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 4910 | channel_auto_read(chn); |
| 4911 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4912 | chn->flags |= CF_NEVER_WAIT; |
| 4913 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4914 | } |
| 4915 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4916 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4917 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4918 | chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4919 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4920 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4921 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4922 | goto http_msg_closing; |
| 4923 | } |
| 4924 | else { |
| 4925 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4926 | goto http_msg_closed; |
| 4927 | } |
| 4928 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4929 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4930 | } |
| 4931 | |
| 4932 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4933 | http_msg_closing: |
| 4934 | /* nothing else to forward, just waiting for the output buffer |
| 4935 | * to be empty and for the shutw_now to take effect. |
| 4936 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4937 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4938 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4939 | goto http_msg_closed; |
| 4940 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4941 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4942 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4943 | goto wait_other_side; |
| 4944 | } |
| 4945 | } |
| 4946 | |
| 4947 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4948 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4949 | /* see above in MSG_DONE why we only do this in these states */ |
| 4950 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4951 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4952 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 4953 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4954 | goto wait_other_side; |
| 4955 | } |
| 4956 | |
| 4957 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4958 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4959 | } |
| 4960 | |
| 4961 | |
| 4962 | /* This function updates the response state machine according to the request |
| 4963 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4964 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4965 | * it is only used to find when a request/response couple is complete. Both |
| 4966 | * this function and its equivalent should loop until both return zero. It |
| 4967 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4968 | */ |
| 4969 | int http_sync_res_state(struct session *s) |
| 4970 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4971 | struct channel *chn = s->rep; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4972 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4973 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4974 | unsigned int old_state = txn->rsp.msg_state; |
| 4975 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4976 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4977 | return 0; |
| 4978 | |
| 4979 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4980 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4981 | * still monitor the server connection for a possible close |
| 4982 | * while the request is being uploaded, so we don't disable |
| 4983 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4984 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4985 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4986 | |
| 4987 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4988 | goto wait_other_side; |
| 4989 | |
| 4990 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4991 | /* The client seems to still be sending data, probably |
| 4992 | * because we got an error response during an upload. |
| 4993 | * We have the choice of either breaking the connection |
| 4994 | * or letting it pass through. Let's do the later. |
| 4995 | */ |
| 4996 | goto wait_other_side; |
| 4997 | } |
| 4998 | |
| 4999 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5000 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5001 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5002 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5003 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5004 | goto wait_other_side; |
| 5005 | } |
| 5006 | |
| 5007 | /* When we get here, it means that both the request and the |
| 5008 | * response have finished receiving. Depending on the connection |
| 5009 | * mode, we'll have to wait for the last bytes to leave in either |
| 5010 | * direction, and sometimes for a close to be effective. |
| 5011 | */ |
| 5012 | |
| 5013 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5014 | /* Server-close mode : shut read and wait for the request |
| 5015 | * side to close its output buffer. The caller will detect |
| 5016 | * when we're in DONE and the other is in CLOSED and will |
| 5017 | * catch that for the final cleanup. |
| 5018 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5019 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5020 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5021 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5022 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5023 | /* Option forceclose is set, or either side wants to close, |
| 5024 | * let's enforce it now that we're not expecting any new |
| 5025 | * data to come. The caller knows the session is complete |
| 5026 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5027 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5028 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5029 | channel_shutr_now(chn); |
| 5030 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5031 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5032 | } |
| 5033 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5034 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5035 | * need to forward remaining data. Keep-alive will need to monitor |
| 5036 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5037 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5038 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5039 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5040 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5041 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5042 | } |
| 5043 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5044 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5045 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5046 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5047 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5048 | goto http_msg_closing; |
| 5049 | } |
| 5050 | else { |
| 5051 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5052 | goto http_msg_closed; |
| 5053 | } |
| 5054 | } |
| 5055 | goto wait_other_side; |
| 5056 | } |
| 5057 | |
| 5058 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5059 | http_msg_closing: |
| 5060 | /* nothing else to forward, just waiting for the output buffer |
| 5061 | * to be empty and for the shutw_now to take effect. |
| 5062 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5063 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5064 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5065 | goto http_msg_closed; |
| 5066 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5067 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5068 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5069 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5070 | if (objt_server(s->target)) |
| 5071 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5072 | goto wait_other_side; |
| 5073 | } |
| 5074 | } |
| 5075 | |
| 5076 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5077 | http_msg_closed: |
| 5078 | /* drop any pending data */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5079 | bi_erase(chn); |
| 5080 | channel_auto_close(chn); |
| 5081 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5082 | goto wait_other_side; |
| 5083 | } |
| 5084 | |
| 5085 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5086 | /* We force the response to leave immediately if we're waiting for the |
| 5087 | * other side, since there is no pending shutdown to push it out. |
| 5088 | */ |
| 5089 | if (!channel_is_empty(chn)) |
| 5090 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5091 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5092 | } |
| 5093 | |
| 5094 | |
| 5095 | /* Resync the request and response state machines. Return 1 if either state |
| 5096 | * changes. |
| 5097 | */ |
| 5098 | int http_resync_states(struct session *s) |
| 5099 | { |
| 5100 | struct http_txn *txn = &s->txn; |
| 5101 | int old_req_state = txn->req.msg_state; |
| 5102 | int old_res_state = txn->rsp.msg_state; |
| 5103 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5104 | http_sync_req_state(s); |
| 5105 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5106 | if (!http_sync_res_state(s)) |
| 5107 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5108 | if (!http_sync_req_state(s)) |
| 5109 | break; |
| 5110 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5111 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5112 | /* OK, both state machines agree on a compatible state. |
| 5113 | * There are a few cases we're interested in : |
| 5114 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5115 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5116 | * directions, so let's simply disable both analysers. |
| 5117 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5118 | * request. |
| 5119 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5120 | * with server-close mode means we've completed one request and we |
| 5121 | * must re-initialize the server connection. |
| 5122 | */ |
| 5123 | |
| 5124 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5125 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5126 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5127 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 5128 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5129 | channel_auto_close(s->req); |
| 5130 | channel_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5131 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5132 | channel_auto_close(s->rep); |
| 5133 | channel_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5134 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5135 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
| 5136 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5137 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5138 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5139 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5140 | channel_auto_close(s->rep); |
| 5141 | channel_auto_read(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5142 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5143 | channel_abort(s->req); |
| 5144 | channel_auto_close(s->req); |
| 5145 | channel_auto_read(s->req); |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5146 | bi_erase(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5147 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5148 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5149 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5150 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5151 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5152 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5153 | /* server-close/keep-alive: terminate this transaction, |
| 5154 | * possibly killing the server connection and reinitialize |
| 5155 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5156 | */ |
| 5157 | http_end_txn_clean_session(s); |
| 5158 | } |
| 5159 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5160 | return txn->req.msg_state != old_req_state || |
| 5161 | txn->rsp.msg_state != old_res_state; |
| 5162 | } |
| 5163 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5164 | /* This function is an analyser which forwards request body (including chunk |
| 5165 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5166 | * zero byte. The only situation where it must not be called is when we're in |
| 5167 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5168 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5169 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5170 | * 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] | 5171 | * 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] | 5172 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5173 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5174 | 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] | 5175 | { |
| 5176 | struct http_txn *txn = &s->txn; |
| 5177 | struct http_msg *msg = &s->txn.req; |
| 5178 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5179 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5180 | return 0; |
| 5181 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5182 | 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] | 5183 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5184 | /* Output closed while we were sending data. We must abort and |
| 5185 | * wake the other side up. |
| 5186 | */ |
| 5187 | msg->msg_state = HTTP_MSG_ERROR; |
| 5188 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5189 | return 1; |
| 5190 | } |
| 5191 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5192 | /* Note that we don't have to send 100-continue back because we don't |
| 5193 | * need the data to complete our job, and it's up to the server to |
| 5194 | * decide whether to return 100, 417 or anything else in return of |
| 5195 | * an "Expect: 100-continue" header. |
| 5196 | */ |
| 5197 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5198 | if (msg->sov) { |
| 5199 | /* we have msg->sov which points to the first byte of message |
| 5200 | * body, and req->buf.p still points to the beginning of the |
| 5201 | * message. We forward the headers now, as we don't need them |
| 5202 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5203 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5204 | b_adv(req->buf, msg->sov); |
| 5205 | msg->next -= msg->sov; |
| 5206 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5207 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5208 | /* The previous analysers guarantee that the state is somewhere |
| 5209 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 5210 | * msg->next are always correct. |
| 5211 | */ |
| 5212 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5213 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 5214 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5215 | else |
| 5216 | msg->msg_state = HTTP_MSG_DATA; |
| 5217 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5218 | } |
| 5219 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5220 | /* Some post-connect processing might want us to refrain from starting to |
| 5221 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5222 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5223 | */ |
| 5224 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
| 5225 | if (!(s->rep->flags & CF_READ_ATTACHED)) { |
| 5226 | channel_auto_connect(req); |
| 5227 | goto missing_data; |
| 5228 | } |
| 5229 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5230 | } |
| 5231 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5232 | /* in most states, we should abort in case of early close */ |
| 5233 | channel_auto_close(req); |
| 5234 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5235 | if (req->to_forward) { |
| 5236 | /* We can't process the buffer's contents yet */ |
| 5237 | req->flags |= CF_WAKE_WRITE; |
| 5238 | goto missing_data; |
| 5239 | } |
| 5240 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5241 | while (1) { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5242 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5243 | /* must still forward */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5244 | /* we may have some pending data starting at req->buf->p */ |
| 5245 | if (msg->chunk_len > req->buf->i - msg->next) { |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5246 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5247 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5248 | } |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5249 | msg->next += msg->chunk_len; |
| 5250 | msg->chunk_len = 0; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5251 | |
| 5252 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5253 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5254 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5255 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5256 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5257 | } |
| 5258 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5259 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 5260 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5261 | * TRAILERS state. |
| 5262 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5263 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5264 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5265 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5266 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5267 | else if (ret < 0) { |
| 5268 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5269 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5270 | 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] | 5271 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5272 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5273 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5274 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5275 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5276 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5277 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5278 | |
| 5279 | if (ret == 0) |
| 5280 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5281 | else if (ret < 0) { |
| 5282 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5283 | if (msg->err_pos >= 0) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5284 | 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] | 5285 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5286 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5287 | /* we're in MSG_CHUNK_SIZE now */ |
| 5288 | } |
| 5289 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5290 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5291 | |
| 5292 | if (ret == 0) |
| 5293 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5294 | else if (ret < 0) { |
| 5295 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5296 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5297 | 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] | 5298 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5299 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5300 | /* we're in HTTP_MSG_DONE now */ |
| 5301 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5302 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5303 | int old_state = msg->msg_state; |
| 5304 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5305 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5306 | |
| 5307 | /* we may have some pending data starting at req->buf->p |
| 5308 | * such as last chunk of data or trailers. |
| 5309 | */ |
| 5310 | b_adv(req->buf, msg->next); |
| 5311 | msg->next = 0; |
| 5312 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5313 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5314 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5315 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5316 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5317 | if (http_resync_states(s)) { |
| 5318 | /* some state changes occurred, maybe the analyser |
| 5319 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 5320 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5321 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5322 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5323 | /* request errors are most likely due to |
| 5324 | * the server aborting the transfer. |
| 5325 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5326 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5327 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5328 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5329 | 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] | 5330 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5331 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5332 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5333 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5334 | |
| 5335 | /* If "option abortonclose" is set on the backend, we |
| 5336 | * want to monitor the client's connection and forward |
| 5337 | * any shutdown notification to the server, which will |
| 5338 | * decide whether to close or to go on processing the |
| 5339 | * request. |
| 5340 | */ |
| 5341 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5342 | channel_auto_read(req); |
| 5343 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5344 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5345 | else if (s->txn.meth == HTTP_METH_POST) { |
| 5346 | /* POST requests may require to read extra CRLF |
| 5347 | * sent by broken browsers and which could cause |
| 5348 | * an RST to be sent upon close on some systems |
| 5349 | * (eg: Linux). |
| 5350 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5351 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5352 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5353 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5354 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5355 | } |
| 5356 | } |
| 5357 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5358 | missing_data: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5359 | /* we may have some pending data starting at req->buf->p */ |
| 5360 | b_adv(req->buf, msg->next); |
| 5361 | msg->next = 0; |
| 5362 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
| 5363 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5364 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5365 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5366 | if (!(s->flags & SN_ERR_MASK)) |
| 5367 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5368 | if (!(s->flags & SN_FINST_MASK)) { |
| 5369 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5370 | s->flags |= SN_FINST_H; |
| 5371 | else |
| 5372 | s->flags |= SN_FINST_D; |
| 5373 | } |
| 5374 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5375 | s->fe->fe_counters.cli_aborts++; |
| 5376 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5377 | if (objt_server(s->target)) |
| 5378 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5379 | |
| 5380 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5381 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5382 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5383 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5384 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5385 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5386 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5387 | /* 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] | 5388 | * 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] | 5389 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5390 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5391 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5392 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5393 | /* 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] | 5394 | * 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] | 5395 | * 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] | 5396 | * modes are already handled by the stream sock layer. We must not do |
| 5397 | * this in content-length mode because it could present the MSG_MORE |
| 5398 | * flag with the last block of forwarded data, which would cause an |
| 5399 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5400 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5401 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5402 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5403 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5404 | return 0; |
| 5405 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5406 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5407 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5408 | if (s->listener->counters) |
| 5409 | s->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5410 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5411 | return_bad_req_stats_ok: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5412 | /* we may have some pending data starting at req->buf->p */ |
| 5413 | b_adv(req->buf, msg->next); |
| 5414 | msg->next = 0; |
| 5415 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5416 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5417 | if (txn->status) { |
| 5418 | /* Note: we don't send any error if some data were already sent */ |
| 5419 | stream_int_retnclose(req->prod, NULL); |
| 5420 | } else { |
| 5421 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5422 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5423 | } |
| 5424 | req->analysers = 0; |
| 5425 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5426 | |
| 5427 | if (!(s->flags & SN_ERR_MASK)) |
| 5428 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5429 | if (!(s->flags & SN_FINST_MASK)) { |
| 5430 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5431 | s->flags |= SN_FINST_H; |
| 5432 | else |
| 5433 | s->flags |= SN_FINST_D; |
| 5434 | } |
| 5435 | return 0; |
| 5436 | |
| 5437 | aborted_xfer: |
| 5438 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5439 | if (txn->status) { |
| 5440 | /* Note: we don't send any error if some data were already sent */ |
| 5441 | stream_int_retnclose(req->prod, NULL); |
| 5442 | } else { |
| 5443 | txn->status = 502; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5444 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5445 | } |
| 5446 | req->analysers = 0; |
| 5447 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5448 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5449 | s->fe->fe_counters.srv_aborts++; |
| 5450 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5451 | if (objt_server(s->target)) |
| 5452 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5453 | |
| 5454 | if (!(s->flags & SN_ERR_MASK)) |
| 5455 | s->flags |= SN_ERR_SRVCL; |
| 5456 | if (!(s->flags & SN_FINST_MASK)) { |
| 5457 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5458 | s->flags |= SN_FINST_H; |
| 5459 | else |
| 5460 | s->flags |= SN_FINST_D; |
| 5461 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5462 | return 0; |
| 5463 | } |
| 5464 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5465 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5466 | * processing can continue on next analysers, or zero if it either needs more |
| 5467 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 5468 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 5469 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5470 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5471 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5472 | 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] | 5473 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5474 | struct http_txn *txn = &s->txn; |
| 5475 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5476 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5477 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5478 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5479 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5480 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5481 | 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] | 5482 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5483 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5484 | rep, |
| 5485 | rep->rex, rep->wex, |
| 5486 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5487 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5488 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5489 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5490 | /* |
| 5491 | * Now parse the partial (or complete) lines. |
| 5492 | * We will check the response syntax, and also join multi-line |
| 5493 | * headers. An index of all the lines will be elaborated while |
| 5494 | * parsing. |
| 5495 | * |
| 5496 | * For the parsing, we use a 28 states FSM. |
| 5497 | * |
| 5498 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5499 | * rep->buf->p = beginning of response |
| 5500 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5501 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5502 | * msg->eol = end of current header or line (LF or CRLF) |
| 5503 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5504 | */ |
| 5505 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5506 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5507 | /* There's a protected area at the end of the buffer for rewriting |
| 5508 | * purposes. We don't want to start to parse the request if the |
| 5509 | * protected area is affected, because we may have to move processed |
| 5510 | * data later, which is much more complicated. |
| 5511 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5512 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5513 | if (unlikely(!channel_reserved(rep))) { |
| 5514 | /* some data has still not left the buffer, wake us once that's done */ |
| 5515 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5516 | goto abort_response; |
| 5517 | channel_dont_close(rep); |
| 5518 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5519 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5520 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5521 | } |
| 5522 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5523 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5524 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5525 | buffer_slow_realign(rep->buf); |
| 5526 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5527 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5528 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5529 | } |
| 5530 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5531 | /* 1: we might have to print this header in debug mode */ |
| 5532 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5533 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5534 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5535 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5536 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5537 | sol = rep->buf->p; |
| 5538 | 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] | 5539 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5540 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5541 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5542 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5543 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5544 | while (cur_idx) { |
| 5545 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5546 | debug_hdr("srvhdr", s, sol, eol); |
| 5547 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5548 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5549 | } |
| 5550 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5551 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5552 | /* |
| 5553 | * Now we quickly check if we have found a full valid response. |
| 5554 | * If not so, we check the FD and buffer states before leaving. |
| 5555 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5556 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5557 | * responses are checked first. |
| 5558 | * |
| 5559 | * Depending on whether the client is still there or not, we |
| 5560 | * may send an error response back or not. Note that normally |
| 5561 | * we should only check for HTTP status there, and check I/O |
| 5562 | * errors somewhere else. |
| 5563 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5564 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5565 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5566 | /* Invalid response */ |
| 5567 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5568 | /* we detected a parsing error. We want to archive this response |
| 5569 | * in the dedicated proxy area for later troubleshooting. |
| 5570 | */ |
| 5571 | hdr_response_bad: |
| 5572 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5573 | 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] | 5574 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5575 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5576 | if (objt_server(s->target)) { |
| 5577 | objt_server(s->target)->counters.failed_resp++; |
| 5578 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5579 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5580 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5581 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5582 | rep->analysers = 0; |
| 5583 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5584 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5585 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5586 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5587 | |
| 5588 | if (!(s->flags & SN_ERR_MASK)) |
| 5589 | s->flags |= SN_ERR_PRXCOND; |
| 5590 | if (!(s->flags & SN_FINST_MASK)) |
| 5591 | s->flags |= SN_FINST_H; |
| 5592 | |
| 5593 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5594 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5595 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5596 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5597 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5598 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5599 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5600 | goto hdr_response_bad; |
| 5601 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5602 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5603 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5604 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5605 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5606 | 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] | 5607 | else if (txn->flags & TX_NOT_FIRST) |
| 5608 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5609 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5610 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5611 | if (objt_server(s->target)) { |
| 5612 | objt_server(s->target)->counters.failed_resp++; |
| 5613 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5614 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5615 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5616 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5617 | rep->analysers = 0; |
| 5618 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5619 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5620 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5621 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5622 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5623 | if (!(s->flags & SN_ERR_MASK)) |
| 5624 | s->flags |= SN_ERR_SRVCL; |
| 5625 | if (!(s->flags & SN_FINST_MASK)) |
| 5626 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5627 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5628 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5629 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5630 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5631 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5632 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5633 | 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] | 5634 | else if (txn->flags & TX_NOT_FIRST) |
| 5635 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5636 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5637 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5638 | if (objt_server(s->target)) { |
| 5639 | objt_server(s->target)->counters.failed_resp++; |
| 5640 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5641 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5642 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5643 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5644 | rep->analysers = 0; |
| 5645 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5646 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5647 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5648 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5649 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5650 | if (!(s->flags & SN_ERR_MASK)) |
| 5651 | s->flags |= SN_ERR_SRVTO; |
| 5652 | if (!(s->flags & SN_FINST_MASK)) |
| 5653 | s->flags |= SN_FINST_H; |
| 5654 | return 0; |
| 5655 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5656 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5657 | /* client abort with an abortonclose */ |
| 5658 | else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
| 5659 | s->fe->fe_counters.cli_aborts++; |
| 5660 | s->be->be_counters.cli_aborts++; |
| 5661 | if (objt_server(s->target)) |
| 5662 | objt_server(s->target)->counters.cli_aborts++; |
| 5663 | |
| 5664 | rep->analysers = 0; |
| 5665 | channel_auto_close(rep); |
| 5666 | |
| 5667 | txn->status = 400; |
| 5668 | bi_erase(rep); |
| 5669 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400)); |
| 5670 | |
| 5671 | if (!(s->flags & SN_ERR_MASK)) |
| 5672 | s->flags |= SN_ERR_CLICL; |
| 5673 | if (!(s->flags & SN_FINST_MASK)) |
| 5674 | s->flags |= SN_FINST_H; |
| 5675 | |
| 5676 | /* process_session() will take care of the error */ |
| 5677 | return 0; |
| 5678 | } |
| 5679 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5680 | /* 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] | 5681 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5682 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5683 | 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] | 5684 | else if (txn->flags & TX_NOT_FIRST) |
| 5685 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5686 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5687 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5688 | if (objt_server(s->target)) { |
| 5689 | objt_server(s->target)->counters.failed_resp++; |
| 5690 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5691 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5692 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5693 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5694 | rep->analysers = 0; |
| 5695 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5696 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5697 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5698 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5699 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5700 | if (!(s->flags & SN_ERR_MASK)) |
| 5701 | s->flags |= SN_ERR_SRVCL; |
| 5702 | if (!(s->flags & SN_FINST_MASK)) |
| 5703 | s->flags |= SN_FINST_H; |
| 5704 | return 0; |
| 5705 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5706 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5707 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5708 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5709 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5710 | 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] | 5711 | else if (txn->flags & TX_NOT_FIRST) |
| 5712 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5713 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5714 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5715 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5716 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5717 | |
| 5718 | if (!(s->flags & SN_ERR_MASK)) |
| 5719 | s->flags |= SN_ERR_CLICL; |
| 5720 | if (!(s->flags & SN_FINST_MASK)) |
| 5721 | s->flags |= SN_FINST_H; |
| 5722 | |
| 5723 | /* process_session() will take care of the error */ |
| 5724 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5725 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5726 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5727 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5728 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5729 | return 0; |
| 5730 | } |
| 5731 | |
| 5732 | /* More interesting part now : we know that we have a complete |
| 5733 | * response which at least looks like HTTP. We have an indicator |
| 5734 | * of each header's length, so we can parse them quickly. |
| 5735 | */ |
| 5736 | |
| 5737 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5738 | 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] | 5739 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5740 | /* |
| 5741 | * 1: get the status code |
| 5742 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5743 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5744 | if (n < 1 || n > 5) |
| 5745 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5746 | /* when the client triggers a 4xx from the server, it's most often due |
| 5747 | * to a missing object or permission. These events should be tracked |
| 5748 | * because if they happen often, it may indicate a brute force or a |
| 5749 | * vulnerability scan. |
| 5750 | */ |
| 5751 | if (n == 4) |
| 5752 | session_inc_http_err_ctr(s); |
| 5753 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5754 | if (objt_server(s->target)) |
| 5755 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5756 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5757 | /* check if the response is HTTP/1.1 or above */ |
| 5758 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5759 | ((rep->buf->p[5] > '1') || |
| 5760 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5761 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5762 | |
| 5763 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5764 | 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] | 5765 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5766 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5767 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5768 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5769 | 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] | 5770 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5771 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5772 | * and 505 are triggered on demand by client request, so we must not |
| 5773 | * count them as server failures. |
| 5774 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5775 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5776 | 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] | 5777 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5778 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5779 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5780 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5781 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5782 | /* |
| 5783 | * 2: check for cacheability. |
| 5784 | */ |
| 5785 | |
| 5786 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5787 | case 100: |
| 5788 | /* |
| 5789 | * We may be facing a 100-continue response, in which case this |
| 5790 | * is not the right response, and we're waiting for the next one. |
| 5791 | * Let's allow this response to go to the client and wait for the |
| 5792 | * next one. |
| 5793 | */ |
| 5794 | hdr_idx_init(&txn->hdr_idx); |
| 5795 | msg->next -= channel_forward(rep, msg->next); |
| 5796 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5797 | txn->status = 0; |
| 5798 | s->logs.t_data = -1; /* was not a response yet */ |
| 5799 | goto next_one; |
| 5800 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5801 | case 200: |
| 5802 | case 203: |
| 5803 | case 206: |
| 5804 | case 300: |
| 5805 | case 301: |
| 5806 | case 410: |
| 5807 | /* RFC2616 @13.4: |
| 5808 | * "A response received with a status code of |
| 5809 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 5810 | * by a cache (...) unless a cache-control |
| 5811 | * directive prohibits caching." |
| 5812 | * |
| 5813 | * RFC2616 @9.5: POST method : |
| 5814 | * "Responses to this method are not cacheable, |
| 5815 | * unless the response includes appropriate |
| 5816 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5817 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5818 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5819 | ((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] | 5820 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 5821 | break; |
| 5822 | default: |
| 5823 | break; |
| 5824 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5825 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5826 | /* |
| 5827 | * 3: we may need to capture headers |
| 5828 | */ |
| 5829 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 5830 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5831 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5832 | txn->rsp.cap, s->fe->rsp_cap); |
| 5833 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5834 | /* 4: determine the transfer-length. |
| 5835 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 5836 | * the presence of a message-body in a RESPONSE and its transfer length |
| 5837 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5838 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5839 | * All responses to the HEAD request method MUST NOT include a |
| 5840 | * message-body, even though the presence of entity-header fields |
| 5841 | * might lead one to believe they do. All 1xx (informational), 204 |
| 5842 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 5843 | * message-body. All other responses do include a message-body, |
| 5844 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5845 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5846 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 5847 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 5848 | * always terminated by the first empty line after the header fields, |
| 5849 | * regardless of the entity-header fields present in the message. |
| 5850 | * |
| 5851 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 5852 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 5853 | * transfer-length is defined by the use of this transfer-coding. |
| 5854 | * If a Transfer-Encoding header field is present and the "chunked" |
| 5855 | * transfer-coding is not present, the transfer-length is defined by |
| 5856 | * the sender closing the connection. |
| 5857 | * |
| 5858 | * 3. If a Content-Length header field is present, its decimal value in |
| 5859 | * OCTETs represents both the entity-length and the transfer-length. |
| 5860 | * If a message is received with both a Transfer-Encoding header |
| 5861 | * field and a Content-Length header field, the latter MUST be ignored. |
| 5862 | * |
| 5863 | * 4. If the message uses the media type "multipart/byteranges", and |
| 5864 | * the transfer-length is not otherwise specified, then this self- |
| 5865 | * delimiting media type defines the transfer-length. This media |
| 5866 | * type MUST NOT be used unless the sender knows that the recipient |
| 5867 | * can parse it; the presence in a request of a Range header with |
| 5868 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 5869 | * client can parse multipart/byteranges responses. |
| 5870 | * |
| 5871 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5872 | */ |
| 5873 | |
| 5874 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5875 | * 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] | 5876 | * 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] | 5877 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5878 | */ |
| 5879 | if (txn->meth == HTTP_METH_HEAD || |
| 5880 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5881 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5882 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 5883 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5884 | goto skip_content_length; |
| 5885 | } |
| 5886 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5887 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5888 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5889 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5890 | 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] | 5891 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5892 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5893 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5894 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5895 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5896 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5897 | break; |
| 5898 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5899 | } |
| 5900 | |
| 5901 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 5902 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5903 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5904 | 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] | 5905 | signed long long cl; |
| 5906 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5907 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5908 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5909 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5910 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5911 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5912 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5913 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5914 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5915 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5916 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5917 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5918 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5919 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5920 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5921 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5922 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5923 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5924 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5925 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5926 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5927 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5928 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5929 | } |
| 5930 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5931 | if (s->fe->comp || s->be->comp) |
| 5932 | select_compression_response_header(s, rep->buf); |
| 5933 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5934 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5935 | /* Now we have to check if we need to modify the Connection header. |
| 5936 | * This is more difficult on the response than it is on the request, |
| 5937 | * because we can have two different HTTP versions and we don't know |
| 5938 | * how the client will interprete a response. For instance, let's say |
| 5939 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5940 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5941 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5942 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5943 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5944 | * indicating how a request MAY be understood by the client. In case |
| 5945 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5946 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5947 | * will fall back to explicit close. Note that we won't take risks with |
| 5948 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5949 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5950 | */ |
| 5951 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5952 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5953 | txn->status == 101)) { |
| 5954 | /* Either we've established an explicit tunnel, or we're |
| 5955 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5956 | * to understand the next protocols. We have to switch to tunnel |
| 5957 | * mode, so that we transfer the request and responses then let |
| 5958 | * this protocol pass unmodified. When we later implement specific |
| 5959 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5960 | * header which contains information about that protocol for |
| 5961 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5962 | */ |
| 5963 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5964 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5965 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5966 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5967 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 5968 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5969 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5970 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5971 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 5972 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5973 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 5974 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5975 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 5976 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5977 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5978 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5979 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5980 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5981 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5982 | /* now adjust header transformations depending on current state */ |
| 5983 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5984 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5985 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5986 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5987 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5988 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5989 | else { /* SCL / KAL */ |
| 5990 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5991 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5992 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5993 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5994 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5995 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5996 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5997 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5998 | /* Some keep-alive responses are converted to Server-close if |
| 5999 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6000 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6001 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6002 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6003 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6004 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6005 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6006 | } |
| 6007 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6008 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6009 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6010 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6011 | /* end of job, return OK */ |
| 6012 | rep->analysers &= ~an_bit; |
| 6013 | rep->analyse_exp = TICK_ETERNITY; |
| 6014 | channel_auto_close(rep); |
| 6015 | return 1; |
| 6016 | |
| 6017 | abort_keep_alive: |
| 6018 | /* A keep-alive request to the server failed on a network error. |
| 6019 | * The client is required to retry. We need to close without returning |
| 6020 | * any other information so that the client retries. |
| 6021 | */ |
| 6022 | txn->status = 0; |
| 6023 | rep->analysers = 0; |
| 6024 | s->req->analysers = 0; |
| 6025 | channel_auto_close(rep); |
| 6026 | s->logs.logwait = 0; |
| 6027 | s->logs.level = 0; |
| 6028 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 6029 | bi_erase(rep); |
| 6030 | stream_int_retnclose(rep->cons, NULL); |
| 6031 | return 0; |
| 6032 | } |
| 6033 | |
| 6034 | /* This function performs all the processing enabled for the current response. |
| 6035 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 6036 | * and updates s->rep->analysers. It might make sense to explode it into several |
| 6037 | * other functions. It works like process_request (see indications above). |
| 6038 | */ |
| 6039 | int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px) |
| 6040 | { |
| 6041 | struct http_txn *txn = &s->txn; |
| 6042 | struct http_msg *msg = &txn->rsp; |
| 6043 | struct proxy *cur_proxy; |
| 6044 | struct cond_wordlist *wl; |
| 6045 | struct http_res_rule *http_res_last_rule = NULL; |
| 6046 | |
| 6047 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 6048 | now_ms, __FUNCTION__, |
| 6049 | s, |
| 6050 | rep, |
| 6051 | rep->rex, rep->wex, |
| 6052 | rep->flags, |
| 6053 | rep->buf->i, |
| 6054 | rep->analysers); |
| 6055 | |
| 6056 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6057 | return 0; |
| 6058 | |
| 6059 | rep->analysers &= ~an_bit; |
| 6060 | rep->analyse_exp = TICK_ETERNITY; |
| 6061 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6062 | /* The stats applet needs to adjust the Connection header but we don't |
| 6063 | * apply any filter there. |
| 6064 | */ |
| 6065 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) |
| 6066 | goto skip_filters; |
| 6067 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6068 | /* |
| 6069 | * We will have to evaluate the filters. |
| 6070 | * As opposed to version 1.2, now they will be evaluated in the |
| 6071 | * filters order and not in the header order. This means that |
| 6072 | * each filter has to be validated among all headers. |
| 6073 | * |
| 6074 | * Filters are tried with ->be first, then with ->fe if it is |
| 6075 | * different from ->be. |
| 6076 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6077 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6078 | cur_proxy = s->be; |
| 6079 | while (1) { |
| 6080 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6081 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6082 | /* evaluate http-response rules */ |
| 6083 | if (!http_res_last_rule) |
| 6084 | http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s, txn); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 6085 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6086 | /* try headers filters */ |
| 6087 | if (rule_set->rsp_exp != NULL) { |
| 6088 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6089 | return_bad_resp: |
| 6090 | if (objt_server(s->target)) { |
| 6091 | objt_server(s->target)->counters.failed_resp++; |
| 6092 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6093 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6094 | s->be->be_counters.failed_resp++; |
| 6095 | return_srv_prx_502: |
| 6096 | rep->analysers = 0; |
| 6097 | txn->status = 502; |
| 6098 | s->logs.t_data = -1; /* was not a valid response */ |
| 6099 | rep->prod->flags |= SI_FL_NOLINGER; |
| 6100 | bi_erase(rep); |
| 6101 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
| 6102 | if (!(s->flags & SN_ERR_MASK)) |
| 6103 | s->flags |= SN_ERR_PRXCOND; |
| 6104 | if (!(s->flags & SN_FINST_MASK)) |
| 6105 | s->flags |= SN_FINST_H; |
| 6106 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6107 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6108 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6109 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6110 | /* has the response been denied ? */ |
| 6111 | if (txn->flags & TX_SVDENY) { |
| 6112 | if (objt_server(s->target)) |
| 6113 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6114 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6115 | s->be->be_counters.denied_resp++; |
| 6116 | s->fe->fe_counters.denied_resp++; |
| 6117 | if (s->listener->counters) |
| 6118 | s->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6119 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6120 | goto return_srv_prx_502; |
| 6121 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6122 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6123 | /* add response headers from the rule sets in the same order */ |
| 6124 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
| 6125 | if (txn->status < 200) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6126 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6127 | if (wl->cond) { |
| 6128 | int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 6129 | ret = acl_pass(ret); |
| 6130 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6131 | ret = !ret; |
| 6132 | if (!ret) |
| 6133 | continue; |
| 6134 | } |
| 6135 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6136 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6137 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6138 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6139 | /* check whether we're already working on the frontend */ |
| 6140 | if (cur_proxy == s->fe) |
| 6141 | break; |
| 6142 | cur_proxy = s->fe; |
| 6143 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6144 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6145 | /* OK that's all we can do for 1xx responses */ |
| 6146 | if (unlikely(txn->status < 200)) |
| 6147 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6148 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6149 | /* |
| 6150 | * Now check for a server cookie. |
| 6151 | */ |
| 6152 | if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name || |
| 6153 | (s->be->options & PR_O_CHK_CACHE)) |
| 6154 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6155 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6156 | /* |
| 6157 | * Check for cache-control or pragma headers if required. |
| 6158 | */ |
| 6159 | if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) |
| 6160 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6161 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6162 | /* |
| 6163 | * Add server cookie in the response if needed |
| 6164 | */ |
| 6165 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6166 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 6167 | (!(s->flags & SN_DIRECT) || |
| 6168 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6169 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6170 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6171 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6172 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 6173 | !(s->flags & SN_IGNORE_PRST)) { |
| 6174 | /* the server is known, it's not the one the client requested, or the |
| 6175 | * cookie's last seen date needs to be refreshed. We have to |
| 6176 | * insert a set-cookie here, except if we want to insert only on POST |
| 6177 | * requests and this one isn't. Note that servers which don't have cookies |
| 6178 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6179 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6180 | if (!objt_server(s->target)->cookie) { |
| 6181 | chunk_printf(&trash, |
| 6182 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6183 | s->be->cookie_name); |
| 6184 | } |
| 6185 | else { |
| 6186 | 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] | 6187 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6188 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6189 | /* emit last_date, which is mandatory */ |
| 6190 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6191 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6192 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6193 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6194 | if (s->be->cookie_maxlife) { |
| 6195 | /* emit first_date, which is either the original one or |
| 6196 | * the current date. |
| 6197 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6198 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6199 | s30tob64(txn->cookie_first_date ? |
| 6200 | txn->cookie_first_date >> 2 : |
| 6201 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6202 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6203 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6204 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6205 | chunk_appendf(&trash, "; path=/"); |
| 6206 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6207 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6208 | if (s->be->cookie_domain) |
| 6209 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6210 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6211 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6212 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6213 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6214 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6215 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6216 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6217 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6218 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6219 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6220 | txn->flags &= ~TX_SCK_MASK; |
| 6221 | if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT)) |
| 6222 | /* the server did not change, only the date was updated */ |
| 6223 | txn->flags |= TX_SCK_UPDATED; |
| 6224 | else |
| 6225 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6226 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6227 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6228 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6229 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6230 | * 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] | 6231 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6232 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6233 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6234 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6235 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6236 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6237 | "Cache-control: private", 22) < 0)) |
| 6238 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6239 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6240 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6241 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6242 | /* |
| 6243 | * Check if result will be cacheable with a cookie. |
| 6244 | * We'll block the response if security checks have caught |
| 6245 | * nasty things such as a cacheable cookie. |
| 6246 | */ |
| 6247 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6248 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6249 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6250 | /* we're in presence of a cacheable response containing |
| 6251 | * a set-cookie header. We'll block it as requested by |
| 6252 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6253 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6254 | if (objt_server(s->target)) |
| 6255 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6256 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6257 | s->be->be_counters.denied_resp++; |
| 6258 | s->fe->fe_counters.denied_resp++; |
| 6259 | if (s->listener->counters) |
| 6260 | s->listener->counters->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6261 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6262 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6263 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6264 | send_log(s->be, LOG_ALERT, |
| 6265 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6266 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6267 | goto return_srv_prx_502; |
| 6268 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6269 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6270 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6271 | /* |
| 6272 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6273 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6274 | * not to have to deal with some client bugs : some of them fail an upgrade |
| 6275 | * if anything but "Upgrade" is present in the Connection header. |
| 6276 | */ |
| 6277 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 6278 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 6279 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6280 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6281 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6282 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6283 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6284 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6285 | /* we want a keep-alive response here. Keep-alive header |
| 6286 | * required if either side is not 1.1. |
| 6287 | */ |
| 6288 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6289 | want_flags |= TX_CON_KAL_SET; |
| 6290 | } |
| 6291 | else { |
| 6292 | /* we want a close response here. Close header required if |
| 6293 | * the server is 1.1, regardless of the client. |
| 6294 | */ |
| 6295 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6296 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6297 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6298 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6299 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6300 | http_change_connection_header(txn, msg, want_flags); |
| 6301 | } |
| 6302 | |
| 6303 | skip_header_mangling: |
| 6304 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
| 6305 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 6306 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6307 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6308 | /* if the user wants to log as soon as possible, without counting |
| 6309 | * bytes from the server, then this is the right moment. We have |
| 6310 | * to temporarily assign bytes_out to log what we currently have. |
| 6311 | */ |
| 6312 | if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 6313 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6314 | s->logs.bytes_out = txn->rsp.eoh; |
| 6315 | s->do_log(s); |
| 6316 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6317 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6318 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6319 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6320 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6321 | /* This function is an analyser which forwards response body (including chunk |
| 6322 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6323 | * zero byte. The only situation where it must not be called is when we're in |
| 6324 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6325 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6326 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 6327 | * 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] | 6328 | * |
| 6329 | * It is capable of compressing response data both in content-length mode and |
| 6330 | * in chunked mode. The state machines follows different flows depending on |
| 6331 | * whether content-length and chunked modes are used, since there are no |
| 6332 | * trailers in content-length : |
| 6333 | * |
| 6334 | * chk-mode cl-mode |
| 6335 | * ,----- BODY -----. |
| 6336 | * / \ |
| 6337 | * V size > 0 V chk-mode |
| 6338 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6339 | * | | size == 0 | last byte | |
| 6340 | * | v final crlf v inspected | |
| 6341 | * | TRAILERS -----------> DONE | |
| 6342 | * | | |
| 6343 | * `----------------------------------------------' |
| 6344 | * |
| 6345 | * Compression only happens in the DATA state, and must be flushed in final |
| 6346 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6347 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6348 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6349 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6350 | 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] | 6351 | { |
| 6352 | struct http_txn *txn = &s->txn; |
| 6353 | struct http_msg *msg = &s->txn.rsp; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6354 | static struct buffer *tmpbuf = NULL; |
| 6355 | int compressing = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6356 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6357 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6358 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6359 | return 0; |
| 6360 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6361 | 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] | 6362 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 6363 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6364 | /* Output closed while we were sending data. We must abort and |
| 6365 | * wake the other side up. |
| 6366 | */ |
| 6367 | msg->msg_state = HTTP_MSG_ERROR; |
| 6368 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6369 | return 1; |
| 6370 | } |
| 6371 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6372 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6373 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6374 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6375 | if (msg->sov) { |
| 6376 | /* we have msg->sov which points to the first byte of message |
| 6377 | * body, and res->buf.p still points to the beginning of the |
| 6378 | * message. We forward the headers now, as we don't need them |
| 6379 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6380 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6381 | b_adv(res->buf, msg->sov); |
| 6382 | msg->next -= msg->sov; |
| 6383 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6384 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6385 | /* The previous analysers guarantee that the state is somewhere |
| 6386 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 6387 | * msg->next are always correct. |
| 6388 | */ |
| 6389 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 6390 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 6391 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6392 | else |
| 6393 | msg->msg_state = HTTP_MSG_DATA; |
| 6394 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6395 | } |
| 6396 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6397 | if (res->to_forward) { |
| 6398 | /* We can't process the buffer's contents yet */ |
| 6399 | res->flags |= CF_WAKE_WRITE; |
| 6400 | goto missing_data; |
| 6401 | } |
| 6402 | |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6403 | if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) { |
| 6404 | /* We need a compression buffer in the DATA state to put the |
| 6405 | * output of compressed data, and in CRLF state to let the |
| 6406 | * TRAILERS state finish the job of removing the trailing CRLF. |
| 6407 | */ |
| 6408 | if (unlikely(tmpbuf == NULL)) { |
| 6409 | /* this is the first time we need the compression buffer */ |
| 6410 | tmpbuf = pool_alloc2(pool2_buffer); |
| 6411 | if (tmpbuf == NULL) |
| 6412 | goto aborted_xfer; /* no memory */ |
| 6413 | } |
| 6414 | |
| 6415 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6416 | if (ret < 0) { |
| 6417 | res->flags |= CF_WAKE_WRITE; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6418 | goto missing_data; /* not enough spaces in buffers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6419 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6420 | compressing = 1; |
| 6421 | } |
| 6422 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6423 | while (1) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6424 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6425 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6426 | /* we may have some pending data starting at res->buf->p */ |
| 6427 | if (unlikely(s->comp_algo)) { |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 6428 | ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6429 | if (ret < 0) |
| 6430 | goto aborted_xfer; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6431 | |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6432 | if (msg->chunk_len) { |
| 6433 | /* input empty or output full */ |
| 6434 | if (res->buf->i > msg->next) |
| 6435 | res->flags |= CF_WAKE_WRITE; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6436 | goto missing_data; |
| 6437 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6438 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6439 | else { |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6440 | if (msg->chunk_len > res->buf->i - msg->next) { |
| 6441 | /* output full */ |
| 6442 | res->flags |= CF_WAKE_WRITE; |
| 6443 | goto missing_data; |
| 6444 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6445 | msg->next += msg->chunk_len; |
| 6446 | msg->chunk_len = 0; |
| 6447 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6448 | |
| 6449 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6450 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6451 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6452 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6453 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6454 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6455 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6456 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6457 | |
| 6458 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 6459 | /* we want the CRLF after the data */ |
| 6460 | |
| 6461 | ret = http_skip_chunk_crlf(msg); |
| 6462 | if (ret == 0) |
| 6463 | goto missing_data; |
| 6464 | else if (ret < 0) { |
| 6465 | if (msg->err_pos >= 0) |
| 6466 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe); |
| 6467 | goto return_bad_res; |
| 6468 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6469 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 6470 | |
| 6471 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6472 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 6473 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6474 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6475 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6476 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6477 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6478 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6479 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6480 | else if (ret < 0) { |
| 6481 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6482 | 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] | 6483 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6484 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 6485 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6486 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6487 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6488 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6489 | if (unlikely(compressing)) { |
| 6490 | /* we need to flush output contents before syncing FSMs */ |
| 6491 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6492 | compressing = 0; |
| 6493 | } |
| 6494 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6495 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6496 | if (ret == 0) |
| 6497 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6498 | else if (ret < 0) { |
| 6499 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6500 | 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] | 6501 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6502 | } |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6503 | /* we're in HTTP_MSG_DONE now, fall through */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6504 | |
| 6505 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6506 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6507 | if (unlikely(compressing)) { |
| 6508 | /* we need to flush output contents before syncing FSMs */ |
| 6509 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6510 | compressing = 0; |
| 6511 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6512 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6513 | /* we may have some pending data starting at res->buf->p |
| 6514 | * such as a last chunk of data or trailers. |
| 6515 | */ |
| 6516 | b_adv(res->buf, msg->next); |
| 6517 | msg->next = 0; |
| 6518 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6519 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6520 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6521 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6522 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6523 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6524 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6525 | if (http_resync_states(s)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6526 | /* some state changes occurred, maybe the analyser |
| 6527 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6528 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6529 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6530 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6531 | /* response errors are most likely due to |
| 6532 | * the client aborting the transfer. |
| 6533 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6534 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6535 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6536 | if (msg->err_pos >= 0) |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6537 | 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] | 6538 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6539 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6540 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6541 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6542 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6543 | } |
| 6544 | } |
| 6545 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6546 | missing_data: |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6547 | /* we may have some pending data starting at res->buf->p */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6548 | if (unlikely(compressing)) { |
| 6549 | 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] | 6550 | compressing = 0; |
| 6551 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6552 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6553 | if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) { |
| 6554 | b_adv(res->buf, msg->next); |
| 6555 | msg->next = 0; |
| 6556 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6557 | } |
| 6558 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6559 | if (res->flags & CF_SHUTW) |
| 6560 | goto aborted_xfer; |
| 6561 | |
| 6562 | /* stop waiting for data if the input is closed before the end. If the |
| 6563 | * client side was already closed, it means that the client has aborted, |
| 6564 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6565 | * server abort. |
| 6566 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6567 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | b2c6a78 | 2014-04-23 20:29:01 +0200 | [diff] [blame] | 6568 | if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6569 | goto aborted_xfer; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6570 | if (!(s->flags & SN_ERR_MASK)) |
| 6571 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6572 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6573 | if (objt_server(s->target)) |
| 6574 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6575 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6576 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6577 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6578 | /* we need to obey the req analyser, so if it leaves, we must too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6579 | if (!s->req->analysers) |
| 6580 | goto return_bad_res; |
| 6581 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6582 | /* 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] | 6583 | * 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] | 6584 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 6585 | * close. |
| 6586 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6587 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6588 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6589 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6590 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6591 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6592 | /* 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] | 6593 | * 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] | 6594 | * 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] | 6595 | * modes are already handled by the stream sock layer. We must not do |
| 6596 | * this in content-length mode because it could present the MSG_MORE |
| 6597 | * flag with the last block of forwarded data, which would cause an |
| 6598 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6599 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6600 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6601 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6602 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6603 | /* the session handler will take care of timeouts and errors */ |
| 6604 | return 0; |
| 6605 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6606 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6607 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6608 | if (objt_server(s->target)) |
| 6609 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6610 | |
| 6611 | return_bad_res_stats_ok: |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 6612 | if (unlikely(compressing)) { |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6613 | 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] | 6614 | compressing = 0; |
| 6615 | } |
| 6616 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6617 | /* we may have some pending data starting at res->buf->p */ |
| 6618 | if (s->comp_algo == NULL) { |
| 6619 | b_adv(res->buf, msg->next); |
| 6620 | msg->next = 0; |
| 6621 | } |
| 6622 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6623 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6624 | /* don't send any error message as we're in the body */ |
| 6625 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6626 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6627 | 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] | 6628 | if (objt_server(s->target)) |
| 6629 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6630 | |
| 6631 | if (!(s->flags & SN_ERR_MASK)) |
| 6632 | s->flags |= SN_ERR_PRXCOND; |
| 6633 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6634 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6635 | return 0; |
| 6636 | |
| 6637 | aborted_xfer: |
Willy Tarreau | 6fef8ae | 2014-04-22 21:22:06 +0200 | [diff] [blame] | 6638 | if (unlikely(compressing)) { |
| 6639 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
| 6640 | compressing = 0; |
| 6641 | } |
| 6642 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6643 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6644 | /* don't send any error message as we're in the body */ |
| 6645 | stream_int_retnclose(res->cons, NULL); |
| 6646 | res->analysers = 0; |
| 6647 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 6648 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6649 | s->fe->fe_counters.cli_aborts++; |
| 6650 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6651 | if (objt_server(s->target)) |
| 6652 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6653 | |
| 6654 | if (!(s->flags & SN_ERR_MASK)) |
| 6655 | s->flags |= SN_ERR_CLICL; |
| 6656 | if (!(s->flags & SN_FINST_MASK)) |
| 6657 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6658 | return 0; |
| 6659 | } |
| 6660 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6661 | /* Iterate the same filter through all request headers. |
| 6662 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6663 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6664 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6665 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6666 | 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] | 6667 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6668 | char term; |
| 6669 | char *cur_ptr, *cur_end, *cur_next; |
| 6670 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6671 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6672 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6673 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6674 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6675 | last_hdr = 0; |
| 6676 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6677 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6678 | old_idx = 0; |
| 6679 | |
| 6680 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6681 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6682 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6683 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6684 | (exp->action == ACT_ALLOW || |
| 6685 | exp->action == ACT_DENY || |
| 6686 | exp->action == ACT_TARPIT)) |
| 6687 | return 0; |
| 6688 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6689 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6690 | if (!cur_idx) |
| 6691 | break; |
| 6692 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6693 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6694 | cur_ptr = cur_next; |
| 6695 | cur_end = cur_ptr + cur_hdr->len; |
| 6696 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6697 | |
| 6698 | /* Now we have one header between cur_ptr and cur_end, |
| 6699 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6700 | */ |
| 6701 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6702 | /* The annoying part is that pattern matching needs |
| 6703 | * that we modify the contents to null-terminate all |
| 6704 | * strings before testing them. |
| 6705 | */ |
| 6706 | |
| 6707 | term = *cur_end; |
| 6708 | *cur_end = '\0'; |
| 6709 | |
| 6710 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6711 | switch (exp->action) { |
| 6712 | case ACT_SETBE: |
| 6713 | /* It is not possible to jump a second time. |
| 6714 | * FIXME: should we return an HTTP/500 here so that |
| 6715 | * the admin knows there's a problem ? |
| 6716 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6717 | if (s->be != s->fe) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6718 | break; |
| 6719 | |
| 6720 | /* Swithing Proxy */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6721 | session_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6722 | last_hdr = 1; |
| 6723 | break; |
| 6724 | |
| 6725 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6726 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6727 | last_hdr = 1; |
| 6728 | break; |
| 6729 | |
| 6730 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6731 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6732 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6733 | break; |
| 6734 | |
| 6735 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6736 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6737 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6738 | break; |
| 6739 | |
| 6740 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6741 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6742 | 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] | 6743 | /* FIXME: if the user adds a newline in the replacement, the |
| 6744 | * index will not be recalculated for now, and the new line |
| 6745 | * will not be counted as a new header. |
| 6746 | */ |
| 6747 | |
| 6748 | cur_end += delta; |
| 6749 | cur_next += delta; |
| 6750 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6751 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6752 | break; |
| 6753 | |
| 6754 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6755 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6756 | cur_next += delta; |
| 6757 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6758 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6759 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6760 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6761 | cur_hdr->len = 0; |
| 6762 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6763 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6764 | break; |
| 6765 | |
| 6766 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6767 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6768 | if (cur_end) |
| 6769 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6770 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6771 | /* keep the link from this header to next one in case of later |
| 6772 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6773 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6774 | old_idx = cur_idx; |
| 6775 | } |
| 6776 | return 0; |
| 6777 | } |
| 6778 | |
| 6779 | |
| 6780 | /* Apply the filter to the request line. |
| 6781 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6782 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6783 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6784 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6785 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6786 | 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] | 6787 | { |
| 6788 | char term; |
| 6789 | char *cur_ptr, *cur_end; |
| 6790 | int done; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6791 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6792 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6793 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6794 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6795 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6796 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6797 | (exp->action == ACT_ALLOW || |
| 6798 | exp->action == ACT_DENY || |
| 6799 | exp->action == ACT_TARPIT)) |
| 6800 | return 0; |
| 6801 | else if (exp->action == ACT_REMOVE) |
| 6802 | return 0; |
| 6803 | |
| 6804 | done = 0; |
| 6805 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6806 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6807 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6808 | |
| 6809 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6810 | |
| 6811 | /* The annoying part is that pattern matching needs |
| 6812 | * that we modify the contents to null-terminate all |
| 6813 | * strings before testing them. |
| 6814 | */ |
| 6815 | |
| 6816 | term = *cur_end; |
| 6817 | *cur_end = '\0'; |
| 6818 | |
| 6819 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6820 | switch (exp->action) { |
| 6821 | case ACT_SETBE: |
| 6822 | /* It is not possible to jump a second time. |
| 6823 | * FIXME: should we return an HTTP/500 here so that |
| 6824 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6825 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6826 | if (s->be != s->fe) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6827 | break; |
| 6828 | |
| 6829 | /* Swithing Proxy */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6830 | session_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6831 | done = 1; |
| 6832 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6833 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6834 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6835 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6836 | done = 1; |
| 6837 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6838 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6839 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6840 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6841 | done = 1; |
| 6842 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6843 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6844 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6845 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6846 | done = 1; |
| 6847 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6848 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6849 | case ACT_REPLACE: |
| 6850 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6851 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6852 | 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] | 6853 | /* FIXME: if the user adds a newline in the replacement, the |
| 6854 | * index will not be recalculated for now, and the new line |
| 6855 | * will not be counted as a new header. |
| 6856 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6857 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6858 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6859 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6860 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6861 | HTTP_MSG_RQMETH, |
| 6862 | cur_ptr, cur_end + 1, |
| 6863 | NULL, NULL); |
| 6864 | if (unlikely(!cur_end)) |
| 6865 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6866 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6867 | /* we have a full request and we know that we have either a CR |
| 6868 | * or an LF at <ptr>. |
| 6869 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6870 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6871 | 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] | 6872 | /* there is no point trying this regex on headers */ |
| 6873 | return 1; |
| 6874 | } |
| 6875 | } |
| 6876 | *cur_end = term; /* restore the string terminator */ |
| 6877 | return done; |
| 6878 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6879 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6880 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6881 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6882 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6883 | * 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] | 6884 | * 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] | 6885 | * unparsable request. Since it can manage the switch to another backend, it |
| 6886 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6887 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6888 | 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] | 6889 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6890 | struct http_txn *txn = &s->txn; |
| 6891 | struct hdr_exp *exp; |
| 6892 | |
| 6893 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6894 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6895 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6896 | /* |
| 6897 | * The interleaving of transformations and verdicts |
| 6898 | * makes it difficult to decide to continue or stop |
| 6899 | * the evaluation. |
| 6900 | */ |
| 6901 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6902 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6903 | break; |
| 6904 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6905 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6906 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6907 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6908 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6909 | |
| 6910 | /* if this filter had a condition, evaluate it now and skip to |
| 6911 | * next filter if the condition does not match. |
| 6912 | */ |
| 6913 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 6914 | 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] | 6915 | ret = acl_pass(ret); |
| 6916 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6917 | ret = !ret; |
| 6918 | |
| 6919 | if (!ret) |
| 6920 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6921 | } |
| 6922 | |
| 6923 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6924 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6925 | if (unlikely(ret < 0)) |
| 6926 | return -1; |
| 6927 | |
| 6928 | if (likely(ret == 0)) { |
| 6929 | /* The filter did not match the request, it can be |
| 6930 | * iterated through all headers. |
| 6931 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6932 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6933 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6934 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6935 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6936 | } |
| 6937 | |
| 6938 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6939 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6940 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6941 | * Try to retrieve the server associated to the appsession. |
| 6942 | * If the server is found, it's assigned to the session. |
| 6943 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6944 | void manage_client_side_appsession(struct session *s, const char *buf, int len) { |
| 6945 | struct http_txn *txn = &s->txn; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6946 | appsess *asession = NULL; |
| 6947 | char *sessid_temp = NULL; |
| 6948 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6949 | if (len > s->be->appsession_len) { |
| 6950 | len = s->be->appsession_len; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6951 | } |
| 6952 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6953 | if (s->be->options2 & PR_O2_AS_REQL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6954 | /* 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] | 6955 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6956 | /* 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] | 6957 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6958 | } |
| 6959 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6960 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6961 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6962 | 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] | 6963 | return; |
| 6964 | } |
| 6965 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6966 | memcpy(txn->sessid, buf, len); |
| 6967 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6968 | } |
| 6969 | |
| 6970 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 6971 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6972 | 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] | 6973 | return; |
| 6974 | } |
| 6975 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6976 | memcpy(sessid_temp, buf, len); |
| 6977 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6978 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6979 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6980 | /* free previously allocated memory */ |
| 6981 | pool_free2(apools.sessid, sessid_temp); |
| 6982 | |
| 6983 | if (asession != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6984 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
| 6985 | if (!(s->be->options2 & PR_O2_AS_REQL)) |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6986 | asession->request_count++; |
| 6987 | |
| 6988 | if (asession->serverid != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6989 | struct server *srv = s->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6990 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6991 | while (srv) { |
| 6992 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 6993 | if ((srv->state & SRV_RUNNING) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6994 | (s->be->options & PR_O_PERSIST) || |
| 6995 | (s->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6996 | /* we found the server and it's usable */ |
| 6997 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 6998 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6999 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 7000 | s->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 7001 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7002 | break; |
| 7003 | } else { |
| 7004 | txn->flags &= ~TX_CK_MASK; |
| 7005 | txn->flags |= TX_CK_DOWN; |
| 7006 | } |
| 7007 | } |
| 7008 | srv = srv->next; |
| 7009 | } |
| 7010 | } |
| 7011 | } |
| 7012 | } |
| 7013 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7014 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7015 | * same way as with headers above except that the semi-colon also ends a token. |
| 7016 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7017 | * return a valid result. |
| 7018 | */ |
| 7019 | char *find_cookie_value_end(char *s, const char *e) |
| 7020 | { |
| 7021 | int quoted, qdpair; |
| 7022 | |
| 7023 | quoted = qdpair = 0; |
| 7024 | for (; s < e; s++) { |
| 7025 | if (qdpair) qdpair = 0; |
| 7026 | else if (quoted) { |
| 7027 | if (*s == '\\') qdpair = 1; |
| 7028 | else if (*s == '"') quoted = 0; |
| 7029 | } |
| 7030 | else if (*s == '"') quoted = 1; |
| 7031 | else if (*s == ',' || *s == ';') return s; |
| 7032 | } |
| 7033 | return s; |
| 7034 | } |
| 7035 | |
| 7036 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7037 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7038 | * the first delimiter is updated if required. The function tries as much as |
| 7039 | * possible to respect the following principles : |
| 7040 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7041 | * colon, in which case <next> is simply removed |
| 7042 | * - set exactly one space character after the new first delimiter, unless |
| 7043 | * there are not enough characters in the block being moved to do so. |
| 7044 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7045 | * one. |
| 7046 | * |
| 7047 | * It is the caller's responsibility to ensure that : |
| 7048 | * - <from> points to a valid delimiter or the colon ; |
| 7049 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7050 | * - there are non-space chars before <from> ; |
| 7051 | * - there is a CR/LF at or after <next>. |
| 7052 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7053 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7054 | { |
| 7055 | char *prev = *from; |
| 7056 | |
| 7057 | if (*prev == ':') { |
| 7058 | /* We're removing the first value, preserve the colon and add a |
| 7059 | * space if possible. |
| 7060 | */ |
| 7061 | if (!http_is_crlf[(unsigned char)*next]) |
| 7062 | next++; |
| 7063 | prev++; |
| 7064 | if (prev < next) |
| 7065 | *prev++ = ' '; |
| 7066 | |
| 7067 | while (http_is_spht[(unsigned char)*next]) |
| 7068 | next++; |
| 7069 | } else { |
| 7070 | /* Remove useless spaces before the old delimiter. */ |
| 7071 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 7072 | prev--; |
| 7073 | *from = prev; |
| 7074 | |
| 7075 | /* copy the delimiter and if possible a space if we're |
| 7076 | * not at the end of the line. |
| 7077 | */ |
| 7078 | if (!http_is_crlf[(unsigned char)*next]) { |
| 7079 | *prev++ = *next++; |
| 7080 | if (prev + 1 < next) |
| 7081 | *prev++ = ' '; |
| 7082 | while (http_is_spht[(unsigned char)*next]) |
| 7083 | next++; |
| 7084 | } |
| 7085 | } |
| 7086 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7087 | } |
| 7088 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7089 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7090 | * 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] | 7091 | * desirable to call it only when needed. This code is quite complex because |
| 7092 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7093 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7094 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7095 | void manage_client_side_cookies(struct session *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7096 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7097 | struct http_txn *txn = &s->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7098 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7099 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7100 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7101 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7102 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7103 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7104 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7105 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7106 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7107 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7108 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7109 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7110 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7111 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7112 | hdr_beg = hdr_next; |
| 7113 | hdr_end = hdr_beg + cur_hdr->len; |
| 7114 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7115 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7116 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7117 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7118 | * "Cookie:" headers. |
| 7119 | */ |
| 7120 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7121 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7122 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7123 | old_idx = cur_idx; |
| 7124 | continue; |
| 7125 | } |
| 7126 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7127 | del_from = NULL; /* nothing to be deleted */ |
| 7128 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7129 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7130 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7131 | * attributes whose name begin with a '$', and associate them with |
| 7132 | * the right cookie, if we want to delete this cookie. |
| 7133 | * So there are 3 cases for each cookie read : |
| 7134 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7135 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7136 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7137 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7138 | * "special" cookie. |
| 7139 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7140 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7141 | * *MUST* delete it. |
| 7142 | * |
| 7143 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7144 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7145 | * the RFC explicitly allows spaces before it, and not within the |
| 7146 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7147 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7148 | * after the equal sign too, resulting in some (rare) buggy |
| 7149 | * implementations trying to do that. So let's do what servers do. |
| 7150 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7151 | * allowed quoted strings in values, with any possible character |
| 7152 | * after a backslash, including control chars and delimitors, which |
| 7153 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7154 | * within values even without quotes. |
| 7155 | * |
| 7156 | * We have to keep multiple pointers in order to support cookie |
| 7157 | * removal at the beginning, middle or end of header without |
| 7158 | * corrupting the header. All of these headers are valid : |
| 7159 | * |
| 7160 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7161 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7162 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7163 | * | | | | | | | | | |
| 7164 | * | | | | | | | | hdr_end <--+ |
| 7165 | * | | | | | | | +--> next |
| 7166 | * | | | | | | +----> val_end |
| 7167 | * | | | | | +-----------> val_beg |
| 7168 | * | | | | +--------------> equal |
| 7169 | * | | | +----------------> att_end |
| 7170 | * | | +---------------------> att_beg |
| 7171 | * | +--------------------------> prev |
| 7172 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7173 | */ |
| 7174 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7175 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7176 | /* Iterate through all cookies on this line */ |
| 7177 | |
| 7178 | /* find att_beg */ |
| 7179 | att_beg = prev + 1; |
| 7180 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7181 | att_beg++; |
| 7182 | |
| 7183 | /* find att_end : this is the first character after the last non |
| 7184 | * space before the equal. It may be equal to hdr_end. |
| 7185 | */ |
| 7186 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7187 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7188 | while (equal < hdr_end) { |
| 7189 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7190 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7191 | if (http_is_spht[(unsigned char)*equal++]) |
| 7192 | continue; |
| 7193 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7194 | } |
| 7195 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7196 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7197 | * is between <att_beg> and <equal>, both may be identical. |
| 7198 | */ |
| 7199 | |
| 7200 | /* look for end of cookie if there is an equal sign */ |
| 7201 | if (equal < hdr_end && *equal == '=') { |
| 7202 | /* look for the beginning of the value */ |
| 7203 | val_beg = equal + 1; |
| 7204 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7205 | val_beg++; |
| 7206 | |
| 7207 | /* find the end of the value, respecting quotes */ |
| 7208 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7209 | |
| 7210 | /* make val_end point to the first white space or delimitor after the value */ |
| 7211 | val_end = next; |
| 7212 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7213 | val_end--; |
| 7214 | } else { |
| 7215 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7216 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7217 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7218 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7219 | * they will automatically be removed if a header before them is removed, |
| 7220 | * since they're supposed to be linked together. |
| 7221 | */ |
| 7222 | if (*att_beg == '$') |
| 7223 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7224 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7225 | /* Ignore cookies with no equal sign */ |
| 7226 | if (equal == next) { |
| 7227 | /* This is not our cookie, so we must preserve it. But if we already |
| 7228 | * scheduled another cookie for removal, we cannot remove the |
| 7229 | * complete header, but we can remove the previous block itself. |
| 7230 | */ |
| 7231 | preserve_hdr = 1; |
| 7232 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7233 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7234 | val_end += delta; |
| 7235 | next += delta; |
| 7236 | hdr_end += delta; |
| 7237 | hdr_next += delta; |
| 7238 | cur_hdr->len += delta; |
| 7239 | http_msg_move_end(&txn->req, delta); |
| 7240 | prev = del_from; |
| 7241 | del_from = NULL; |
| 7242 | } |
| 7243 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7244 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7245 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7246 | /* if there are spaces around the equal sign, we need to |
| 7247 | * strip them otherwise we'll get trouble for cookie captures, |
| 7248 | * or even for rewrites. Since this happens extremely rarely, |
| 7249 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7250 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7251 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7252 | int stripped_before = 0; |
| 7253 | int stripped_after = 0; |
| 7254 | |
| 7255 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7256 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7257 | equal += stripped_before; |
| 7258 | val_beg += stripped_before; |
| 7259 | } |
| 7260 | |
| 7261 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7262 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7263 | val_beg += stripped_after; |
| 7264 | stripped_before += stripped_after; |
| 7265 | } |
| 7266 | |
| 7267 | val_end += stripped_before; |
| 7268 | next += stripped_before; |
| 7269 | hdr_end += stripped_before; |
| 7270 | hdr_next += stripped_before; |
| 7271 | cur_hdr->len += stripped_before; |
| 7272 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7273 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7274 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7275 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7276 | /* First, let's see if we want to capture this cookie. We check |
| 7277 | * that we don't already have a client side cookie, because we |
| 7278 | * can only capture one. Also as an optimisation, we ignore |
| 7279 | * cookies shorter than the declared name. |
| 7280 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7281 | if (s->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7282 | (val_end - att_beg >= s->fe->capture_namelen) && |
| 7283 | memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7284 | int log_len = val_end - att_beg; |
| 7285 | |
| 7286 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7287 | Alert("HTTP logging : out of memory.\n"); |
| 7288 | } else { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7289 | if (log_len > s->fe->capture_len) |
| 7290 | log_len = s->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7291 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7292 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7293 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7294 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7295 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7296 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7297 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7298 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7299 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7300 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7301 | * For cookies in prefix mode, the form is : |
| 7302 | * |
| 7303 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7304 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7305 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7306 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7307 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7308 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7309 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7310 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7311 | * have the server ID between val_beg and delim, and the original cookie between |
| 7312 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7313 | * |
| 7314 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7315 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7316 | * | || || | |+-> next |
| 7317 | * | || || | +--> val_end |
| 7318 | * | || || +---------> delim |
| 7319 | * | || |+------------> val_beg |
| 7320 | * | || +-------------> att_end = equal |
| 7321 | * | |+-----------------> att_beg |
| 7322 | * | +------------------> prev |
| 7323 | * +-------------------------> hdr_beg |
| 7324 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7325 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7326 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7327 | for (delim = val_beg; delim < val_end; delim++) |
| 7328 | if (*delim == COOKIE_DELIM) |
| 7329 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7330 | } else { |
| 7331 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7332 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7333 | /* Now check if the cookie contains a date field, which would |
| 7334 | * appear after a vertical bar ('|') just after the server name |
| 7335 | * and before the delimiter. |
| 7336 | */ |
| 7337 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7338 | if (vbar1) { |
| 7339 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7340 | * right is the last seen date. It is a base64 encoded |
| 7341 | * 30-bit value representing the UNIX date since the |
| 7342 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7343 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7344 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7345 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7346 | if (val_end - vbar1 >= 5) { |
| 7347 | val = b64tos30(vbar1); |
| 7348 | if (val > 0) |
| 7349 | txn->cookie_last_date = val << 2; |
| 7350 | } |
| 7351 | /* look for a second vertical bar */ |
| 7352 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7353 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7354 | val = b64tos30(vbar1 + 1); |
| 7355 | if (val > 0) |
| 7356 | txn->cookie_first_date = val << 2; |
| 7357 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7358 | } |
| 7359 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7360 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7361 | /* if the cookie has an expiration date and the proxy wants to check |
| 7362 | * it, then we do that now. We first check if the cookie is too old, |
| 7363 | * then only if it has expired. We detect strict overflow because the |
| 7364 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7365 | * in the future are ignored if their offset is beyond one day. This |
| 7366 | * allows an admin to fix timezone issues without expiring everyone |
| 7367 | * and at the same time avoids keeping unwanted side effects for too |
| 7368 | * long. |
| 7369 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7370 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7371 | (((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] | 7372 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7373 | txn->flags &= ~TX_CK_MASK; |
| 7374 | txn->flags |= TX_CK_OLD; |
| 7375 | delim = val_beg; // let's pretend we have not found the cookie |
| 7376 | txn->cookie_first_date = 0; |
| 7377 | txn->cookie_last_date = 0; |
| 7378 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7379 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7380 | (((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] | 7381 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7382 | txn->flags &= ~TX_CK_MASK; |
| 7383 | txn->flags |= TX_CK_EXPIRED; |
| 7384 | delim = val_beg; // let's pretend we have not found the cookie |
| 7385 | txn->cookie_first_date = 0; |
| 7386 | txn->cookie_last_date = 0; |
| 7387 | } |
| 7388 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7389 | /* Here, we'll look for the first running server which supports the cookie. |
| 7390 | * This allows to share a same cookie between several servers, for example |
| 7391 | * to dedicate backup servers to specific servers only. |
| 7392 | * However, to prevent clients from sticking to cookie-less backup server |
| 7393 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7394 | * empty cookies and mark them as invalid. |
| 7395 | * The same behaviour is applied when persistence must be ignored. |
| 7396 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7397 | if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7398 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7399 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7400 | while (srv) { |
| 7401 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7402 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 7403 | if ((srv->state & SRV_RUNNING) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7404 | (s->be->options & PR_O_PERSIST) || |
| 7405 | (s->flags & SN_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7406 | /* we found the server and we can use it */ |
| 7407 | txn->flags &= ~TX_CK_MASK; |
| 7408 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7409 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 7410 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7411 | break; |
| 7412 | } else { |
| 7413 | /* we found a server, but it's down, |
| 7414 | * mark it as such and go on in case |
| 7415 | * another one is available. |
| 7416 | */ |
| 7417 | txn->flags &= ~TX_CK_MASK; |
| 7418 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7419 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7420 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7421 | srv = srv->next; |
| 7422 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7423 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7424 | 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] | 7425 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7426 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7427 | if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7428 | txn->flags |= TX_CK_UNUSED; |
| 7429 | else |
| 7430 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7431 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7432 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7433 | /* depending on the cookie mode, we may have to either : |
| 7434 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7435 | * the server never sees it ; |
| 7436 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7437 | * application cookie so that it does not get accidentely removed later, |
| 7438 | * if we're in cookie prefix mode |
| 7439 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7440 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7441 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7442 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7443 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7444 | val_end += delta; |
| 7445 | next += delta; |
| 7446 | hdr_end += delta; |
| 7447 | hdr_next += delta; |
| 7448 | cur_hdr->len += delta; |
| 7449 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7450 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7451 | del_from = NULL; |
| 7452 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7453 | } |
| 7454 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7455 | (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] | 7456 | del_from = prev; |
| 7457 | } |
| 7458 | } else { |
| 7459 | /* This is not our cookie, so we must preserve it. But if we already |
| 7460 | * scheduled another cookie for removal, we cannot remove the |
| 7461 | * complete header, but we can remove the previous block itself. |
| 7462 | */ |
| 7463 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7464 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7465 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7466 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7467 | if (att_beg >= del_from) |
| 7468 | att_beg += delta; |
| 7469 | if (att_end >= del_from) |
| 7470 | att_end += delta; |
| 7471 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7472 | val_end += delta; |
| 7473 | next += delta; |
| 7474 | hdr_end += delta; |
| 7475 | hdr_next += delta; |
| 7476 | cur_hdr->len += delta; |
| 7477 | http_msg_move_end(&txn->req, delta); |
| 7478 | prev = del_from; |
| 7479 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7480 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7481 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7482 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7483 | /* Look for the appsession cookie unless persistence must be ignored */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7484 | if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7485 | int cmp_len, value_len; |
| 7486 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7487 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7488 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 7489 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 7490 | value_begin = att_beg + s->be->appsession_name_len; |
| 7491 | value_len = val_end - att_beg - s->be->appsession_name_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7492 | } else { |
| 7493 | cmp_len = att_end - att_beg; |
| 7494 | value_begin = val_beg; |
| 7495 | value_len = val_end - val_beg; |
| 7496 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7497 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7498 | /* let's see if the cookie is our appcookie */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7499 | if (cmp_len == s->be->appsession_name_len && |
| 7500 | memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) { |
| 7501 | manage_client_side_appsession(s, value_begin, value_len); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7502 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7503 | } |
| 7504 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7505 | /* continue with next cookie on this header line */ |
| 7506 | att_beg = next; |
| 7507 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7508 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7509 | /* There are no more cookies on this line. |
| 7510 | * We may still have one (or several) marked for deletion at the |
| 7511 | * end of the line. We must do this now in two ways : |
| 7512 | * - if some cookies must be preserved, we only delete from the |
| 7513 | * mark to the end of line ; |
| 7514 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7515 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7516 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7517 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7518 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7519 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7520 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7521 | cur_hdr->len += delta; |
| 7522 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7523 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7524 | |
| 7525 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7526 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7527 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7528 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7529 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7530 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7531 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7532 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7533 | } |
| 7534 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7535 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7536 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7537 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7538 | } |
| 7539 | |
| 7540 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7541 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7542 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7543 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7544 | 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] | 7545 | { |
| 7546 | char term; |
| 7547 | char *cur_ptr, *cur_end, *cur_next; |
| 7548 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7549 | struct http_txn *txn = &s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7550 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7551 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7552 | |
| 7553 | last_hdr = 0; |
| 7554 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7555 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7556 | old_idx = 0; |
| 7557 | |
| 7558 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7559 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7560 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7561 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7562 | (exp->action == ACT_ALLOW || |
| 7563 | exp->action == ACT_DENY)) |
| 7564 | return 0; |
| 7565 | |
| 7566 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7567 | if (!cur_idx) |
| 7568 | break; |
| 7569 | |
| 7570 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7571 | cur_ptr = cur_next; |
| 7572 | cur_end = cur_ptr + cur_hdr->len; |
| 7573 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7574 | |
| 7575 | /* Now we have one header between cur_ptr and cur_end, |
| 7576 | * and the next header starts at cur_next. |
| 7577 | */ |
| 7578 | |
| 7579 | /* The annoying part is that pattern matching needs |
| 7580 | * that we modify the contents to null-terminate all |
| 7581 | * strings before testing them. |
| 7582 | */ |
| 7583 | |
| 7584 | term = *cur_end; |
| 7585 | *cur_end = '\0'; |
| 7586 | |
| 7587 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 7588 | switch (exp->action) { |
| 7589 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7590 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7591 | last_hdr = 1; |
| 7592 | break; |
| 7593 | |
| 7594 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7595 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7596 | last_hdr = 1; |
| 7597 | break; |
| 7598 | |
| 7599 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7600 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 7601 | 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] | 7602 | /* FIXME: if the user adds a newline in the replacement, the |
| 7603 | * index will not be recalculated for now, and the new line |
| 7604 | * will not be counted as a new header. |
| 7605 | */ |
| 7606 | |
| 7607 | cur_end += delta; |
| 7608 | cur_next += delta; |
| 7609 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7610 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7611 | break; |
| 7612 | |
| 7613 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7614 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7615 | cur_next += delta; |
| 7616 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7617 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7618 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7619 | txn->hdr_idx.used--; |
| 7620 | cur_hdr->len = 0; |
| 7621 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7622 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7623 | break; |
| 7624 | |
| 7625 | } |
| 7626 | } |
| 7627 | if (cur_end) |
| 7628 | *cur_end = term; /* restore the string terminator */ |
| 7629 | |
| 7630 | /* keep the link from this header to next one in case of later |
| 7631 | * removal of next header. |
| 7632 | */ |
| 7633 | old_idx = cur_idx; |
| 7634 | } |
| 7635 | return 0; |
| 7636 | } |
| 7637 | |
| 7638 | |
| 7639 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7640 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7641 | * or -1 if a replacement resulted in an invalid status line. |
| 7642 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7643 | 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] | 7644 | { |
| 7645 | char term; |
| 7646 | char *cur_ptr, *cur_end; |
| 7647 | int done; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7648 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7649 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7650 | |
| 7651 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7652 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7653 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7654 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7655 | (exp->action == ACT_ALLOW || |
| 7656 | exp->action == ACT_DENY)) |
| 7657 | return 0; |
| 7658 | else if (exp->action == ACT_REMOVE) |
| 7659 | return 0; |
| 7660 | |
| 7661 | done = 0; |
| 7662 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7663 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7664 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7665 | |
| 7666 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7667 | |
| 7668 | /* The annoying part is that pattern matching needs |
| 7669 | * that we modify the contents to null-terminate all |
| 7670 | * strings before testing them. |
| 7671 | */ |
| 7672 | |
| 7673 | term = *cur_end; |
| 7674 | *cur_end = '\0'; |
| 7675 | |
| 7676 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 7677 | switch (exp->action) { |
| 7678 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7679 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7680 | done = 1; |
| 7681 | break; |
| 7682 | |
| 7683 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7684 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7685 | done = 1; |
| 7686 | break; |
| 7687 | |
| 7688 | case ACT_REPLACE: |
| 7689 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7690 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 7691 | 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] | 7692 | /* FIXME: if the user adds a newline in the replacement, the |
| 7693 | * index will not be recalculated for now, and the new line |
| 7694 | * will not be counted as a new header. |
| 7695 | */ |
| 7696 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7697 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7698 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7699 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7700 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7701 | cur_ptr, cur_end + 1, |
| 7702 | NULL, NULL); |
| 7703 | if (unlikely(!cur_end)) |
| 7704 | return -1; |
| 7705 | |
| 7706 | /* we have a full respnse and we know that we have either a CR |
| 7707 | * or an LF at <ptr>. |
| 7708 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7709 | 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] | 7710 | 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] | 7711 | /* there is no point trying this regex on headers */ |
| 7712 | return 1; |
| 7713 | } |
| 7714 | } |
| 7715 | *cur_end = term; /* restore the string terminator */ |
| 7716 | return done; |
| 7717 | } |
| 7718 | |
| 7719 | |
| 7720 | |
| 7721 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7722 | * 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] | 7723 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7724 | * unparsable response. |
| 7725 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7726 | 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] | 7727 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7728 | struct http_txn *txn = &s->txn; |
| 7729 | struct hdr_exp *exp; |
| 7730 | |
| 7731 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7732 | int ret; |
| 7733 | |
| 7734 | /* |
| 7735 | * The interleaving of transformations and verdicts |
| 7736 | * makes it difficult to decide to continue or stop |
| 7737 | * the evaluation. |
| 7738 | */ |
| 7739 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7740 | if (txn->flags & TX_SVDENY) |
| 7741 | break; |
| 7742 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7743 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7744 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7745 | exp->action == ACT_PASS)) { |
| 7746 | exp = exp->next; |
| 7747 | continue; |
| 7748 | } |
| 7749 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7750 | /* if this filter had a condition, evaluate it now and skip to |
| 7751 | * next filter if the condition does not match. |
| 7752 | */ |
| 7753 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7754 | 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] | 7755 | ret = acl_pass(ret); |
| 7756 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7757 | ret = !ret; |
| 7758 | if (!ret) |
| 7759 | continue; |
| 7760 | } |
| 7761 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7762 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7763 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7764 | if (unlikely(ret < 0)) |
| 7765 | return -1; |
| 7766 | |
| 7767 | if (likely(ret == 0)) { |
| 7768 | /* The filter did not match the response, it can be |
| 7769 | * iterated through all headers. |
| 7770 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7771 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7772 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7773 | } |
| 7774 | return 0; |
| 7775 | } |
| 7776 | |
| 7777 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7778 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7779 | * 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] | 7780 | * desirable to call it only when needed. This function is also used when we |
| 7781 | * 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] | 7782 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7783 | void manage_server_side_cookies(struct session *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7784 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7785 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7786 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7787 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7788 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7789 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7790 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7791 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7792 | /* Iterate through the headers. |
| 7793 | * we start with the start line. |
| 7794 | */ |
| 7795 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7796 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7797 | |
| 7798 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7799 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7800 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7801 | |
| 7802 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7803 | hdr_beg = hdr_next; |
| 7804 | hdr_end = hdr_beg + cur_hdr->len; |
| 7805 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7806 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7807 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7808 | * next header starts at hdr_next. We're only interested in |
| 7809 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7810 | */ |
| 7811 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7812 | is_cookie2 = 0; |
| 7813 | prev = hdr_beg + 10; |
| 7814 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7815 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7816 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7817 | if (!val) { |
| 7818 | old_idx = cur_idx; |
| 7819 | continue; |
| 7820 | } |
| 7821 | is_cookie2 = 1; |
| 7822 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7823 | } |
| 7824 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7825 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7826 | * <prev> points to the colon. |
| 7827 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7828 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7829 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7830 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 7831 | * check-cache is enabled) and we are not interested in checking |
| 7832 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7833 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7834 | if (s->be->cookie_name == NULL && |
| 7835 | s->be->appsession_name == NULL && |
| 7836 | s->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7837 | return; |
| 7838 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7839 | /* OK so now we know we have to process this response cookie. |
| 7840 | * The format of the Set-Cookie header is slightly different |
| 7841 | * from the format of the Cookie header in that it does not |
| 7842 | * support the comma as a cookie delimiter (thus the header |
| 7843 | * cannot be folded) because the Expires attribute described in |
| 7844 | * the original Netscape's spec may contain an unquoted date |
| 7845 | * with a comma inside. We have to live with this because |
| 7846 | * many browsers don't support Max-Age and some browsers don't |
| 7847 | * support quoted strings. However the Set-Cookie2 header is |
| 7848 | * clean. |
| 7849 | * |
| 7850 | * We have to keep multiple pointers in order to support cookie |
| 7851 | * removal at the beginning, middle or end of header without |
| 7852 | * corrupting the header (in case of set-cookie2). A special |
| 7853 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 7854 | * fields after the first semi-colon. The <next> pointer points |
| 7855 | * either to the end of line (set-cookie) or next unquoted comma |
| 7856 | * (set-cookie2). All of these headers are valid : |
| 7857 | * |
| 7858 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 7859 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7860 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7861 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 7862 | * | | | | | | | | | | |
| 7863 | * | | | | | | | | +-> next hdr_end <--+ |
| 7864 | * | | | | | | | +------------> scav |
| 7865 | * | | | | | | +--------------> val_end |
| 7866 | * | | | | | +--------------------> val_beg |
| 7867 | * | | | | +----------------------> equal |
| 7868 | * | | | +------------------------> att_end |
| 7869 | * | | +----------------------------> att_beg |
| 7870 | * | +------------------------------> prev |
| 7871 | * +-----------------------------------------> hdr_beg |
| 7872 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7873 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7874 | for (; prev < hdr_end; prev = next) { |
| 7875 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7876 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7877 | /* find att_beg */ |
| 7878 | att_beg = prev + 1; |
| 7879 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7880 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7881 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7882 | /* find att_end : this is the first character after the last non |
| 7883 | * space before the equal. It may be equal to hdr_end. |
| 7884 | */ |
| 7885 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7886 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7887 | while (equal < hdr_end) { |
| 7888 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 7889 | break; |
| 7890 | if (http_is_spht[(unsigned char)*equal++]) |
| 7891 | continue; |
| 7892 | att_end = equal; |
| 7893 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7894 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7895 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7896 | * is between <att_beg> and <equal>, both may be identical. |
| 7897 | */ |
| 7898 | |
| 7899 | /* look for end of cookie if there is an equal sign */ |
| 7900 | if (equal < hdr_end && *equal == '=') { |
| 7901 | /* look for the beginning of the value */ |
| 7902 | val_beg = equal + 1; |
| 7903 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7904 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7905 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7906 | /* find the end of the value, respecting quotes */ |
| 7907 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7908 | |
| 7909 | /* make val_end point to the first white space or delimitor after the value */ |
| 7910 | val_end = next; |
| 7911 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7912 | val_end--; |
| 7913 | } else { |
| 7914 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7915 | val_beg = val_end = next = equal; |
| 7916 | } |
| 7917 | |
| 7918 | if (next < hdr_end) { |
| 7919 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7920 | * a colon or semi-colon before the end. So skip all attr-value |
| 7921 | * pairs and look for the next comma. For Set-Cookie, since |
| 7922 | * commas are permitted in values, skip to the end. |
| 7923 | */ |
| 7924 | if (is_cookie2) |
| 7925 | next = find_hdr_value_end(next, hdr_end); |
| 7926 | else |
| 7927 | next = hdr_end; |
| 7928 | } |
| 7929 | |
| 7930 | /* Now everything is as on the diagram above */ |
| 7931 | |
| 7932 | /* Ignore cookies with no equal sign */ |
| 7933 | if (equal == val_end) |
| 7934 | continue; |
| 7935 | |
| 7936 | /* If there are spaces around the equal sign, we need to |
| 7937 | * strip them otherwise we'll get trouble for cookie captures, |
| 7938 | * or even for rewrites. Since this happens extremely rarely, |
| 7939 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7940 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7941 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7942 | int stripped_before = 0; |
| 7943 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7944 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7945 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7946 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7947 | equal += stripped_before; |
| 7948 | val_beg += stripped_before; |
| 7949 | } |
| 7950 | |
| 7951 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7952 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7953 | val_beg += stripped_after; |
| 7954 | stripped_before += stripped_after; |
| 7955 | } |
| 7956 | |
| 7957 | val_end += stripped_before; |
| 7958 | next += stripped_before; |
| 7959 | hdr_end += stripped_before; |
| 7960 | hdr_next += stripped_before; |
| 7961 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7962 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7963 | } |
| 7964 | |
| 7965 | /* First, let's see if we want to capture this cookie. We check |
| 7966 | * that we don't already have a server side cookie, because we |
| 7967 | * can only capture one. Also as an optimisation, we ignore |
| 7968 | * cookies shorter than the declared name. |
| 7969 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7970 | if (s->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7971 | txn->srv_cookie == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7972 | (val_end - att_beg >= s->fe->capture_namelen) && |
| 7973 | memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7974 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 7975 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7976 | Alert("HTTP logging : out of memory.\n"); |
| 7977 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7978 | else { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7979 | if (log_len > s->fe->capture_len) |
| 7980 | log_len = s->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7981 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7982 | txn->srv_cookie[log_len] = 0; |
| 7983 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7984 | } |
| 7985 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7986 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7987 | /* now check if we need to process it for persistence */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7988 | if (!(s->flags & SN_IGNORE_PRST) && |
| 7989 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7990 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7991 | /* assume passive cookie by default */ |
| 7992 | txn->flags &= ~TX_SCK_MASK; |
| 7993 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7994 | |
| 7995 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7996 | * this occurrence because we'll insert another one later. |
| 7997 | * 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] | 7998 | * a direct access. |
| 7999 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8000 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8001 | /* The "preserve" flag was set, we don't want to touch the |
| 8002 | * server's cookie. |
| 8003 | */ |
| 8004 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8005 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 8006 | ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8007 | /* this cookie must be deleted */ |
| 8008 | if (*prev == ':' && next == hdr_end) { |
| 8009 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8010 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8011 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8012 | txn->hdr_idx.used--; |
| 8013 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8014 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8015 | hdr_next += delta; |
| 8016 | http_msg_move_end(&txn->rsp, delta); |
| 8017 | /* note: while both invalid now, <next> and <hdr_end> |
| 8018 | * are still equal, so the for() will stop as expected. |
| 8019 | */ |
| 8020 | } else { |
| 8021 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8022 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8023 | next = prev; |
| 8024 | hdr_end += delta; |
| 8025 | hdr_next += delta; |
| 8026 | cur_hdr->len += delta; |
| 8027 | http_msg_move_end(&txn->rsp, delta); |
| 8028 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8029 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8030 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8031 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8032 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8033 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8034 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8035 | * with this server since we know it. |
| 8036 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8037 | 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] | 8038 | next += delta; |
| 8039 | hdr_end += delta; |
| 8040 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8041 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8042 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8043 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8044 | txn->flags &= ~TX_SCK_MASK; |
| 8045 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8046 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8047 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8048 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8049 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8050 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8051 | 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] | 8052 | next += delta; |
| 8053 | hdr_end += delta; |
| 8054 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8055 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8056 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8057 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8058 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8059 | txn->flags &= ~TX_SCK_MASK; |
| 8060 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8061 | } |
| 8062 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 8063 | /* 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] | 8064 | else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8065 | int cmp_len, value_len; |
| 8066 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8067 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8068 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 8069 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 8070 | value_begin = att_beg + s->be->appsession_name_len; |
| 8071 | 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] | 8072 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8073 | cmp_len = att_end - att_beg; |
| 8074 | value_begin = val_beg; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8075 | value_len = MIN(s->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8076 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8077 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8078 | if ((cmp_len == s->be->appsession_name_len) && |
| 8079 | (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] | 8080 | /* free a possibly previously allocated memory */ |
| 8081 | pool_free2(apools.sessid, txn->sessid); |
| 8082 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8083 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8084 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8085 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8086 | 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] | 8087 | return; |
| 8088 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8089 | memcpy(txn->sessid, value_begin, value_len); |
| 8090 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8091 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8092 | } |
| 8093 | /* that's done for this cookie, check the next one on the same |
| 8094 | * line when next != hdr_end (only if is_cookie2). |
| 8095 | */ |
| 8096 | } |
| 8097 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8098 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8099 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8100 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8101 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8102 | appsess *asession = NULL; |
| 8103 | /* only do insert, if lookup fails */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8104 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8105 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 8106 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8107 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 8108 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8109 | 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] | 8110 | return; |
| 8111 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8112 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 8113 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8114 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 8115 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8116 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8117 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8118 | return; |
| 8119 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8120 | memcpy(asession->sessid, txn->sessid, s->be->appsession_len); |
| 8121 | asession->sessid[s->be->appsession_len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8122 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8123 | server_id_len = strlen(objt_server(s->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8124 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8125 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8126 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8127 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8128 | return; |
| 8129 | } |
| 8130 | asession->serverid[0] = '\0'; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8131 | memcpy(asession->serverid, objt_server(s->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8132 | |
| 8133 | asession->request_count = 0; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8134 | appsession_hash_insert(&(s->be->htbl_proxy), asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8135 | } |
| 8136 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8137 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8138 | asession->request_count++; |
| 8139 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8140 | } |
| 8141 | |
| 8142 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8143 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8144 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8145 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8146 | void check_response_for_cacheability(struct session *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8147 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8148 | struct http_txn *txn = &s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8149 | char *p1, *p2; |
| 8150 | |
| 8151 | char *cur_ptr, *cur_end, *cur_next; |
| 8152 | int cur_idx; |
| 8153 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8154 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8155 | return; |
| 8156 | |
| 8157 | /* Iterate through the headers. |
| 8158 | * we start with the start line. |
| 8159 | */ |
| 8160 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8161 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8162 | |
| 8163 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8164 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8165 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8166 | |
| 8167 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8168 | cur_ptr = cur_next; |
| 8169 | cur_end = cur_ptr + cur_hdr->len; |
| 8170 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8171 | |
| 8172 | /* We have one full header between cur_ptr and cur_end, and the |
| 8173 | * next header starts at cur_next. We're only interested in |
| 8174 | * "Cookie:" headers. |
| 8175 | */ |
| 8176 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8177 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8178 | if (val) { |
| 8179 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8180 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8181 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8182 | return; |
| 8183 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8184 | } |
| 8185 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8186 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8187 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8188 | continue; |
| 8189 | |
| 8190 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8191 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8192 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8193 | |
| 8194 | if (p1 >= cur_end) /* no more info */ |
| 8195 | continue; |
| 8196 | |
| 8197 | /* p1 is at the beginning of the value */ |
| 8198 | p2 = p1; |
| 8199 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8200 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8201 | p2++; |
| 8202 | |
| 8203 | /* we have a complete value between p1 and p2 */ |
| 8204 | if (p2 < cur_end && *p2 == '=') { |
| 8205 | /* we have something of the form no-cache="set-cookie" */ |
| 8206 | if ((cur_end - p1 >= 21) && |
| 8207 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8208 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8209 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8210 | continue; |
| 8211 | } |
| 8212 | |
| 8213 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8214 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8215 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8216 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8217 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8218 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8219 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8220 | return; |
| 8221 | } |
| 8222 | |
| 8223 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8224 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8225 | continue; |
| 8226 | } |
| 8227 | } |
| 8228 | } |
| 8229 | |
| 8230 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8231 | /* |
| 8232 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 8233 | * If the server is found, it's assigned to the session. |
| 8234 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8235 | 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] | 8236 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8237 | char *end_params, *first_param, *cur_param, *next_param; |
| 8238 | char separator; |
| 8239 | int value_len; |
| 8240 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8241 | int mode = s->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8242 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8243 | if (s->be->appsession_name == NULL || |
| 8244 | (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] | 8245 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8246 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8247 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8248 | first_param = NULL; |
| 8249 | switch (mode) { |
| 8250 | case PR_O2_AS_M_PP: |
| 8251 | first_param = memchr(begin, ';', len); |
| 8252 | break; |
| 8253 | case PR_O2_AS_M_QS: |
| 8254 | first_param = memchr(begin, '?', len); |
| 8255 | break; |
| 8256 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8257 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8258 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8259 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8260 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8261 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8262 | switch (mode) { |
| 8263 | case PR_O2_AS_M_PP: |
| 8264 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 8265 | end_params = (char *) begin + len; |
| 8266 | } |
| 8267 | separator = ';'; |
| 8268 | break; |
| 8269 | case PR_O2_AS_M_QS: |
| 8270 | end_params = (char *) begin + len; |
| 8271 | separator = '&'; |
| 8272 | break; |
| 8273 | default: |
| 8274 | /* unknown mode, shouldn't happen */ |
| 8275 | return; |
| 8276 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8277 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8278 | cur_param = next_param = end_params; |
| 8279 | while (cur_param > first_param) { |
| 8280 | cur_param--; |
| 8281 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 8282 | /* let's see if this is the appsession parameter */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8283 | if ((cur_param + s->be->appsession_name_len + 1 < next_param) && |
| 8284 | ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') && |
| 8285 | (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] | 8286 | /* Cool... it's the right one */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8287 | cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 8288 | value_len = MIN(s->be->appsession_len, next_param - cur_param); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8289 | if (value_len > 0) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8290 | manage_client_side_appsession(s, cur_param, value_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8291 | } |
| 8292 | break; |
| 8293 | } |
| 8294 | next_param = cur_param; |
| 8295 | } |
| 8296 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8297 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 8298 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8299 | appsession_hash_dump(&(s->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8300 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8301 | } |
| 8302 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8303 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8304 | * 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] | 8305 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8306 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8307 | * 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] | 8308 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8309 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8310 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8311 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8312 | 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] | 8313 | { |
| 8314 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8315 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8316 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8317 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8318 | if (!uri_auth) |
| 8319 | return 0; |
| 8320 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8321 | 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] | 8322 | return 0; |
| 8323 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8324 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8325 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8326 | return 0; |
| 8327 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8328 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8329 | return 0; |
| 8330 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8331 | return 1; |
| 8332 | } |
| 8333 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8334 | /* |
| 8335 | * 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] | 8336 | * By default it tries to report the error position as msg->err_pos. However if |
| 8337 | * this one is not set, it will then report msg->next, which is the last known |
| 8338 | * 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] | 8339 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8340 | */ |
| 8341 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8342 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8343 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8344 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8345 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8346 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8347 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8348 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8349 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8350 | len1 = MIN(len1, es->len); |
| 8351 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8352 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8353 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8354 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8355 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8356 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8357 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8358 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8359 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8360 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8361 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8362 | es->when = date; // user-visible date |
| 8363 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8364 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8365 | es->oe = other_end; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8366 | if (objt_conn(s->req->prod->end)) |
| 8367 | es->src = __objt_conn(s->req->prod->end)->addr.from; |
| 8368 | else |
| 8369 | memset(&es->src, 0, sizeof(es->src)); |
| 8370 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8371 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8372 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8373 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8374 | es->s_flags = s->flags; |
| 8375 | es->t_flags = s->txn.flags; |
| 8376 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8377 | es->b_out = chn->buf->o; |
| 8378 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8379 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8380 | es->m_clen = msg->chunk_len; |
| 8381 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8382 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8383 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8384 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8385 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8386 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8387 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8388 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8389 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8390 | * 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] | 8391 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8392 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8393 | * 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] | 8394 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8395 | 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] | 8396 | struct hdr_idx *idx, int occ, |
| 8397 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8398 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8399 | struct hdr_ctx local_ctx; |
| 8400 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8401 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8402 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8403 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8404 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8405 | if (!ctx) { |
| 8406 | local_ctx.idx = 0; |
| 8407 | ctx = &local_ctx; |
| 8408 | } |
| 8409 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8410 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8411 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8412 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8413 | occ--; |
| 8414 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8415 | *vptr = ctx->line + ctx->val; |
| 8416 | *vlen = ctx->vlen; |
| 8417 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8418 | } |
| 8419 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8420 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8421 | } |
| 8422 | |
| 8423 | /* negative occurrence, we scan all the list then walk back */ |
| 8424 | if (-occ > MAX_HDR_HISTORY) |
| 8425 | return 0; |
| 8426 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8427 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8428 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8429 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8430 | len_hist[hist_ptr] = ctx->vlen; |
| 8431 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8432 | hist_ptr = 0; |
| 8433 | found++; |
| 8434 | } |
| 8435 | if (-occ > found) |
| 8436 | return 0; |
| 8437 | /* 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] | 8438 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8439 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8440 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8441 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8442 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8443 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8444 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8445 | *vptr = ptr_hist[hist_ptr]; |
| 8446 | *vlen = len_hist[hist_ptr]; |
| 8447 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8448 | } |
| 8449 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8450 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8451 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8452 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8453 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8454 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8455 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8456 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8457 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8458 | * line header values and does not stop at commas. |
| 8459 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8460 | */ |
| 8461 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8462 | struct hdr_idx *idx, int occ, |
| 8463 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8464 | { |
| 8465 | struct hdr_ctx local_ctx; |
| 8466 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8467 | int len_hist[MAX_HDR_HISTORY]; |
| 8468 | unsigned int hist_ptr; |
| 8469 | int found; |
| 8470 | |
| 8471 | if (!ctx) { |
| 8472 | local_ctx.idx = 0; |
| 8473 | ctx = &local_ctx; |
| 8474 | } |
| 8475 | |
| 8476 | if (occ >= 0) { |
| 8477 | /* search from the beginning */ |
| 8478 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8479 | occ--; |
| 8480 | if (occ <= 0) { |
| 8481 | *vptr = ctx->line + ctx->val; |
| 8482 | *vlen = ctx->vlen; |
| 8483 | return 1; |
| 8484 | } |
| 8485 | } |
| 8486 | return 0; |
| 8487 | } |
| 8488 | |
| 8489 | /* negative occurrence, we scan all the list then walk back */ |
| 8490 | if (-occ > MAX_HDR_HISTORY) |
| 8491 | return 0; |
| 8492 | |
| 8493 | found = hist_ptr = 0; |
| 8494 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8495 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8496 | len_hist[hist_ptr] = ctx->vlen; |
| 8497 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8498 | hist_ptr = 0; |
| 8499 | found++; |
| 8500 | } |
| 8501 | if (-occ > found) |
| 8502 | return 0; |
| 8503 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8504 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8505 | */ |
| 8506 | hist_ptr += occ; |
| 8507 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8508 | hist_ptr -= MAX_HDR_HISTORY; |
| 8509 | *vptr = ptr_hist[hist_ptr]; |
| 8510 | *vlen = len_hist[hist_ptr]; |
| 8511 | return 1; |
| 8512 | } |
| 8513 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8514 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8515 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8516 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8517 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8518 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8519 | 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] | 8520 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8521 | int max; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8522 | 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] | 8523 | dir, |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8524 | objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1, |
| 8525 | objt_conn(s->req->cons->end) ? (unsigned short)objt_conn(s->req->cons->end)->t.sock.fd : -1); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8526 | |
| 8527 | for (max = 0; start + max < end; max++) |
| 8528 | if (start[max] == '\r' || start[max] == '\n') |
| 8529 | break; |
| 8530 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8531 | UBOUND(max, trash.size - trash.len - 3); |
| 8532 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8533 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8534 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8535 | } |
| 8536 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8537 | /* |
| 8538 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 8539 | * the required fields are properly allocated and that we only need to (re)init |
| 8540 | * them. This should be used before processing any new request. |
| 8541 | */ |
| 8542 | void http_init_txn(struct session *s) |
| 8543 | { |
| 8544 | struct http_txn *txn = &s->txn; |
| 8545 | struct proxy *fe = s->fe; |
| 8546 | |
| 8547 | txn->flags = 0; |
| 8548 | txn->status = -1; |
| 8549 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8550 | txn->cookie_first_date = 0; |
| 8551 | txn->cookie_last_date = 0; |
| 8552 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8553 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8554 | 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] | 8555 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8556 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8557 | 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] | 8558 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 8559 | txn->req.chunk_len = 0LL; |
| 8560 | txn->req.body_len = 0LL; |
| 8561 | txn->rsp.chunk_len = 0LL; |
| 8562 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8563 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8564 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 8565 | txn->req.chn = s->req; |
| 8566 | txn->rsp.chn = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8567 | |
| 8568 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8569 | |
| 8570 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8571 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8572 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8573 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8574 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8575 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 8576 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8577 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8578 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 8579 | |
| 8580 | if (txn->hdr_idx.v) |
| 8581 | hdr_idx_init(&txn->hdr_idx); |
| 8582 | } |
| 8583 | |
| 8584 | /* to be used at the end of a transaction */ |
| 8585 | void http_end_txn(struct session *s) |
| 8586 | { |
| 8587 | struct http_txn *txn = &s->txn; |
| 8588 | |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8589 | /* release any possible compression context */ |
| 8590 | if (s->flags & SN_COMP_READY) |
| 8591 | s->comp_algo->end(&s->comp_ctx); |
| 8592 | s->comp_algo = NULL; |
| 8593 | s->flags &= ~SN_COMP_READY; |
| 8594 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8595 | /* these ones will have been dynamically allocated */ |
| 8596 | pool_free2(pool2_requri, txn->uri); |
| 8597 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8598 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8599 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8600 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8601 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8602 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8603 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8604 | txn->uri = NULL; |
| 8605 | txn->srv_cookie = NULL; |
| 8606 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8607 | |
| 8608 | if (txn->req.cap) { |
| 8609 | struct cap_hdr *h; |
| 8610 | for (h = s->fe->req_cap; h; h = h->next) |
| 8611 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 8612 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 8613 | } |
| 8614 | |
| 8615 | if (txn->rsp.cap) { |
| 8616 | struct cap_hdr *h; |
| 8617 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 8618 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 8619 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 8620 | } |
| 8621 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8622 | } |
| 8623 | |
| 8624 | /* to be used at the end of a transaction to prepare a new one */ |
| 8625 | void http_reset_txn(struct session *s) |
| 8626 | { |
| 8627 | http_end_txn(s); |
| 8628 | http_init_txn(s); |
| 8629 | |
| 8630 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8631 | s->logs.logwait = s->fe->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8632 | s->logs.level = 0; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 8633 | session_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8634 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8635 | /* re-init store persistence */ |
| 8636 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8637 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8638 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8639 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8640 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 8641 | s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8642 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8643 | /* We must trim any excess data from the response buffer, because we |
| 8644 | * may have blocked an invalid response from a server that we don't |
| 8645 | * want to accidentely forward once we disable the analysers, nor do |
| 8646 | * we want those data to come along with next response. A typical |
| 8647 | * example of such data would be from a buggy server responding to |
| 8648 | * a HEAD with some data, or sending more than the advertised |
| 8649 | * content-length. |
| 8650 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8651 | if (unlikely(s->rep->buf->i)) |
| 8652 | s->rep->buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8653 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8654 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8655 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8656 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8657 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8658 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8659 | |
| 8660 | s->req->rex = TICK_ETERNITY; |
| 8661 | s->req->wex = TICK_ETERNITY; |
| 8662 | s->req->analyse_exp = TICK_ETERNITY; |
| 8663 | s->rep->rex = TICK_ETERNITY; |
| 8664 | s->rep->wex = TICK_ETERNITY; |
| 8665 | s->rep->analyse_exp = TICK_ETERNITY; |
| 8666 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8667 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8668 | void free_http_req_rules(struct list *r) { |
| 8669 | struct http_req_rule *tr, *pr; |
| 8670 | |
| 8671 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8672 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8673 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8674 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8675 | |
| 8676 | free(pr); |
| 8677 | } |
| 8678 | } |
| 8679 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8680 | /* parse an "http-request" rule */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8681 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8682 | { |
| 8683 | struct http_req_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8684 | struct http_req_action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8685 | int cur_arg; |
| 8686 | |
| 8687 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 8688 | if (!rule) { |
| 8689 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8690 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8691 | } |
| 8692 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8693 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8694 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 8695 | cur_arg = 1; |
| 8696 | } else if (!strcmp(args[0], "deny")) { |
| 8697 | rule->action = HTTP_REQ_ACT_DENY; |
| 8698 | cur_arg = 1; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8699 | } else if (!strcmp(args[0], "tarpit")) { |
| 8700 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 8701 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8702 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8703 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8704 | cur_arg = 1; |
| 8705 | |
| 8706 | while(*args[cur_arg]) { |
| 8707 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8708 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8709 | cur_arg+=2; |
| 8710 | continue; |
| 8711 | } else |
| 8712 | break; |
| 8713 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8714 | } else if (!strcmp(args[0], "set-nice")) { |
| 8715 | rule->action = HTTP_REQ_ACT_SET_NICE; |
| 8716 | cur_arg = 1; |
| 8717 | |
| 8718 | if (!*args[cur_arg] || |
| 8719 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8720 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8721 | file, linenum, args[0]); |
| 8722 | goto out_err; |
| 8723 | } |
| 8724 | rule->arg.nice = atoi(args[cur_arg]); |
| 8725 | if (rule->arg.nice < -1024) |
| 8726 | rule->arg.nice = -1024; |
| 8727 | else if (rule->arg.nice > 1024) |
| 8728 | rule->arg.nice = 1024; |
| 8729 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8730 | } else if (!strcmp(args[0], "set-tos")) { |
| 8731 | #ifdef IP_TOS |
| 8732 | char *err; |
| 8733 | rule->action = HTTP_REQ_ACT_SET_TOS; |
| 8734 | cur_arg = 1; |
| 8735 | |
| 8736 | if (!*args[cur_arg] || |
| 8737 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8738 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8739 | file, linenum, args[0]); |
| 8740 | goto out_err; |
| 8741 | } |
| 8742 | |
| 8743 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8744 | if (err && *err != '\0') { |
| 8745 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8746 | file, linenum, err, args[0]); |
| 8747 | goto out_err; |
| 8748 | } |
| 8749 | cur_arg++; |
| 8750 | #else |
| 8751 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8752 | goto out_err; |
| 8753 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8754 | } else if (!strcmp(args[0], "set-mark")) { |
| 8755 | #ifdef SO_MARK |
| 8756 | char *err; |
| 8757 | rule->action = HTTP_REQ_ACT_SET_MARK; |
| 8758 | cur_arg = 1; |
| 8759 | |
| 8760 | if (!*args[cur_arg] || |
| 8761 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8762 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8763 | file, linenum, args[0]); |
| 8764 | goto out_err; |
| 8765 | } |
| 8766 | |
| 8767 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8768 | if (err && *err != '\0') { |
| 8769 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8770 | file, linenum, err, args[0]); |
| 8771 | goto out_err; |
| 8772 | } |
| 8773 | cur_arg++; |
| 8774 | global.last_checks |= LSTCHK_NETADM; |
| 8775 | #else |
| 8776 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8777 | goto out_err; |
| 8778 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8779 | } else if (!strcmp(args[0], "set-log-level")) { |
| 8780 | rule->action = HTTP_REQ_ACT_SET_LOGL; |
| 8781 | cur_arg = 1; |
| 8782 | |
| 8783 | if (!*args[cur_arg] || |
| 8784 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8785 | bad_log_level: |
| 8786 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8787 | file, linenum, args[0]); |
| 8788 | goto out_err; |
| 8789 | } |
| 8790 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8791 | rule->arg.loglevel = -1; |
| 8792 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8793 | goto bad_log_level; |
| 8794 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8795 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 8796 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 8797 | cur_arg = 1; |
| 8798 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 8799 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8800 | (*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] | 8801 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8802 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8803 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8804 | } |
| 8805 | |
| 8806 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8807 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8808 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 8809 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8810 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 8811 | 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] | 8812 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 8813 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8814 | free(proxy->conf.lfs_file); |
| 8815 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8816 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8817 | cur_arg += 2; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8818 | } else if (strcmp(args[0], "del-header") == 0) { |
| 8819 | rule->action = HTTP_REQ_ACT_DEL_HDR; |
| 8820 | cur_arg = 1; |
| 8821 | |
| 8822 | if (!*args[cur_arg] || |
| 8823 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8824 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8825 | file, linenum, args[0]); |
| 8826 | goto out_err; |
| 8827 | } |
| 8828 | |
| 8829 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8830 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8831 | |
| 8832 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8833 | free(proxy->conf.lfs_file); |
| 8834 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8835 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8836 | cur_arg += 1; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8837 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8838 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 8839 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8840 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8841 | 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] | 8842 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8843 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 8844 | goto out_err; |
| 8845 | } |
| 8846 | |
| 8847 | /* this redirect rule might already contain a parsed condition which |
| 8848 | * we'll pass to the http-request rule. |
| 8849 | */ |
| 8850 | rule->action = HTTP_REQ_ACT_REDIR; |
| 8851 | rule->arg.redir = redir; |
| 8852 | rule->cond = redir->cond; |
| 8853 | redir->cond = NULL; |
| 8854 | cur_arg = 2; |
| 8855 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8856 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 8857 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 8858 | rule->action = HTTP_REQ_ACT_ADD_ACL; |
| 8859 | /* |
| 8860 | * '+ 8' for 'add-acl(' |
| 8861 | * '- 9' for 'add-acl(' + trailing ')' |
| 8862 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8863 | 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] | 8864 | |
| 8865 | cur_arg = 1; |
| 8866 | |
| 8867 | if (!*args[cur_arg] || |
| 8868 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8869 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8870 | file, linenum, args[0]); |
| 8871 | goto out_err; |
| 8872 | } |
| 8873 | |
| 8874 | LIST_INIT(&rule->arg.map.key); |
| 8875 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8876 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 8877 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 8878 | file, linenum); |
| 8879 | free(proxy->conf.lfs_file); |
| 8880 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8881 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8882 | cur_arg += 1; |
| 8883 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 8884 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
| 8885 | rule->action = HTTP_REQ_ACT_DEL_ACL; |
| 8886 | /* |
| 8887 | * '+ 8' for 'del-acl(' |
| 8888 | * '- 9' for 'del-acl(' + trailing ')' |
| 8889 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8890 | 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] | 8891 | |
| 8892 | cur_arg = 1; |
| 8893 | |
| 8894 | if (!*args[cur_arg] || |
| 8895 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8896 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8897 | file, linenum, args[0]); |
| 8898 | goto out_err; |
| 8899 | } |
| 8900 | |
| 8901 | LIST_INIT(&rule->arg.map.key); |
| 8902 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8903 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 8904 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 8905 | file, linenum); |
| 8906 | free(proxy->conf.lfs_file); |
| 8907 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8908 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8909 | cur_arg += 1; |
| 8910 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 8911 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
| 8912 | rule->action = HTTP_REQ_ACT_DEL_MAP; |
| 8913 | /* |
| 8914 | * '+ 8' for 'del-map(' |
| 8915 | * '- 9' for 'del-map(' + trailing ')' |
| 8916 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8917 | 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] | 8918 | |
| 8919 | cur_arg = 1; |
| 8920 | |
| 8921 | if (!*args[cur_arg] || |
| 8922 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8923 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8924 | file, linenum, args[0]); |
| 8925 | goto out_err; |
| 8926 | } |
| 8927 | |
| 8928 | LIST_INIT(&rule->arg.map.key); |
| 8929 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8930 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 8931 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 8932 | file, linenum); |
| 8933 | free(proxy->conf.lfs_file); |
| 8934 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8935 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8936 | cur_arg += 1; |
| 8937 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 8938 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 8939 | rule->action = HTTP_REQ_ACT_SET_MAP; |
| 8940 | /* |
| 8941 | * '+ 8' for 'set-map(' |
| 8942 | * '- 9' for 'set-map(' + trailing ')' |
| 8943 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8944 | 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] | 8945 | |
| 8946 | cur_arg = 1; |
| 8947 | |
| 8948 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8949 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 8950 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8951 | file, linenum, args[0]); |
| 8952 | goto out_err; |
| 8953 | } |
| 8954 | |
| 8955 | LIST_INIT(&rule->arg.map.key); |
| 8956 | LIST_INIT(&rule->arg.map.value); |
| 8957 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8958 | |
| 8959 | /* key pattern */ |
| 8960 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 8961 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 8962 | file, linenum); |
| 8963 | |
| 8964 | /* value pattern */ |
| 8965 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 8966 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 8967 | file, linenum); |
| 8968 | free(proxy->conf.lfs_file); |
| 8969 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8970 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8971 | |
| 8972 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8973 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 8974 | char *errmsg = NULL; |
| 8975 | cur_arg = 1; |
| 8976 | /* try in the module list */ |
| 8977 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 8978 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8979 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 8980 | free(errmsg); |
| 8981 | goto out_err; |
| 8982 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8983 | } else { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8984 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', '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] | 8985 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8986 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8987 | } |
| 8988 | |
| 8989 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8990 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8991 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8992 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8993 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 8994 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 8995 | file, linenum, args[0], errmsg); |
| 8996 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8997 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8998 | } |
| 8999 | rule->cond = cond; |
| 9000 | } |
| 9001 | else if (*args[cur_arg]) { |
| 9002 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9003 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9004 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9005 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9006 | } |
| 9007 | |
| 9008 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9009 | out_err: |
| 9010 | free(rule); |
| 9011 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9012 | } |
| 9013 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9014 | /* parse an "http-respose" rule */ |
| 9015 | struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 9016 | { |
| 9017 | struct http_res_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9018 | struct http_res_action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9019 | int cur_arg; |
| 9020 | |
| 9021 | rule = calloc(1, sizeof(*rule)); |
| 9022 | if (!rule) { |
| 9023 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9024 | goto out_err; |
| 9025 | } |
| 9026 | |
| 9027 | if (!strcmp(args[0], "allow")) { |
| 9028 | rule->action = HTTP_RES_ACT_ALLOW; |
| 9029 | cur_arg = 1; |
| 9030 | } else if (!strcmp(args[0], "deny")) { |
| 9031 | rule->action = HTTP_RES_ACT_DENY; |
| 9032 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9033 | } else if (!strcmp(args[0], "set-nice")) { |
| 9034 | rule->action = HTTP_RES_ACT_SET_NICE; |
| 9035 | cur_arg = 1; |
| 9036 | |
| 9037 | if (!*args[cur_arg] || |
| 9038 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9039 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9040 | file, linenum, args[0]); |
| 9041 | goto out_err; |
| 9042 | } |
| 9043 | rule->arg.nice = atoi(args[cur_arg]); |
| 9044 | if (rule->arg.nice < -1024) |
| 9045 | rule->arg.nice = -1024; |
| 9046 | else if (rule->arg.nice > 1024) |
| 9047 | rule->arg.nice = 1024; |
| 9048 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9049 | } else if (!strcmp(args[0], "set-tos")) { |
| 9050 | #ifdef IP_TOS |
| 9051 | char *err; |
| 9052 | rule->action = HTTP_RES_ACT_SET_TOS; |
| 9053 | cur_arg = 1; |
| 9054 | |
| 9055 | if (!*args[cur_arg] || |
| 9056 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9057 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9058 | file, linenum, args[0]); |
| 9059 | goto out_err; |
| 9060 | } |
| 9061 | |
| 9062 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9063 | if (err && *err != '\0') { |
| 9064 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9065 | file, linenum, err, args[0]); |
| 9066 | goto out_err; |
| 9067 | } |
| 9068 | cur_arg++; |
| 9069 | #else |
| 9070 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9071 | goto out_err; |
| 9072 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9073 | } else if (!strcmp(args[0], "set-mark")) { |
| 9074 | #ifdef SO_MARK |
| 9075 | char *err; |
| 9076 | rule->action = HTTP_RES_ACT_SET_MARK; |
| 9077 | cur_arg = 1; |
| 9078 | |
| 9079 | if (!*args[cur_arg] || |
| 9080 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9081 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9082 | file, linenum, args[0]); |
| 9083 | goto out_err; |
| 9084 | } |
| 9085 | |
| 9086 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9087 | if (err && *err != '\0') { |
| 9088 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9089 | file, linenum, err, args[0]); |
| 9090 | goto out_err; |
| 9091 | } |
| 9092 | cur_arg++; |
| 9093 | global.last_checks |= LSTCHK_NETADM; |
| 9094 | #else |
| 9095 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9096 | goto out_err; |
| 9097 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9098 | } else if (!strcmp(args[0], "set-log-level")) { |
| 9099 | rule->action = HTTP_RES_ACT_SET_LOGL; |
| 9100 | cur_arg = 1; |
| 9101 | |
| 9102 | if (!*args[cur_arg] || |
| 9103 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9104 | bad_log_level: |
| 9105 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9106 | file, linenum, args[0]); |
| 9107 | goto out_err; |
| 9108 | } |
| 9109 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9110 | rule->arg.loglevel = -1; |
| 9111 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 9112 | goto bad_log_level; |
| 9113 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9114 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9115 | rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR; |
| 9116 | cur_arg = 1; |
| 9117 | |
| 9118 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9119 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9120 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9121 | file, linenum, args[0]); |
| 9122 | goto out_err; |
| 9123 | } |
| 9124 | |
| 9125 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9126 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9127 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9128 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9129 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9130 | 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] | 9131 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9132 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9133 | free(proxy->conf.lfs_file); |
| 9134 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9135 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9136 | cur_arg += 2; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9137 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9138 | rule->action = HTTP_RES_ACT_DEL_HDR; |
| 9139 | cur_arg = 1; |
| 9140 | |
| 9141 | if (!*args[cur_arg] || |
| 9142 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9143 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9144 | file, linenum, args[0]); |
| 9145 | goto out_err; |
| 9146 | } |
| 9147 | |
| 9148 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9149 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9150 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9151 | proxy->conf.args.ctx = ARGC_HRS; |
| 9152 | free(proxy->conf.lfs_file); |
| 9153 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9154 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9155 | cur_arg += 1; |
| 9156 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9157 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9158 | rule->action = HTTP_RES_ACT_ADD_ACL; |
| 9159 | /* |
| 9160 | * '+ 8' for 'add-acl(' |
| 9161 | * '- 9' for 'add-acl(' + trailing ')' |
| 9162 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9163 | 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] | 9164 | |
| 9165 | cur_arg = 1; |
| 9166 | |
| 9167 | if (!*args[cur_arg] || |
| 9168 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9169 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9170 | file, linenum, args[0]); |
| 9171 | goto out_err; |
| 9172 | } |
| 9173 | |
| 9174 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9175 | proxy->conf.args.ctx = ARGC_HRS; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9176 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9177 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9178 | file, linenum); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9179 | free(proxy->conf.lfs_file); |
| 9180 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9181 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9182 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9183 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9184 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9185 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
| 9186 | rule->action = HTTP_RES_ACT_DEL_ACL; |
| 9187 | /* |
| 9188 | * '+ 8' for 'del-acl(' |
| 9189 | * '- 9' for 'del-acl(' + trailing ')' |
| 9190 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9191 | 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] | 9192 | |
| 9193 | cur_arg = 1; |
| 9194 | |
| 9195 | if (!*args[cur_arg] || |
| 9196 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9197 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9198 | file, linenum, args[0]); |
| 9199 | goto out_err; |
| 9200 | } |
| 9201 | |
| 9202 | LIST_INIT(&rule->arg.map.key); |
| 9203 | proxy->conf.args.ctx = ARGC_HRS; |
| 9204 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9205 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9206 | file, linenum); |
| 9207 | free(proxy->conf.lfs_file); |
| 9208 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9209 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9210 | cur_arg += 1; |
| 9211 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9212 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
| 9213 | rule->action = HTTP_RES_ACT_DEL_MAP; |
| 9214 | /* |
| 9215 | * '+ 8' for 'del-map(' |
| 9216 | * '- 9' for 'del-map(' + trailing ')' |
| 9217 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9218 | 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] | 9219 | |
| 9220 | cur_arg = 1; |
| 9221 | |
| 9222 | if (!*args[cur_arg] || |
| 9223 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9224 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9225 | file, linenum, args[0]); |
| 9226 | goto out_err; |
| 9227 | } |
| 9228 | |
| 9229 | LIST_INIT(&rule->arg.map.key); |
| 9230 | proxy->conf.args.ctx = ARGC_HRS; |
| 9231 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9232 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9233 | file, linenum); |
| 9234 | free(proxy->conf.lfs_file); |
| 9235 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9236 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9237 | cur_arg += 1; |
| 9238 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9239 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9240 | rule->action = HTTP_RES_ACT_SET_MAP; |
| 9241 | /* |
| 9242 | * '+ 8' for 'set-map(' |
| 9243 | * '- 9' for 'set-map(' + trailing ')' |
| 9244 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9245 | 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] | 9246 | |
| 9247 | cur_arg = 1; |
| 9248 | |
| 9249 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9250 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9251 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9252 | file, linenum, args[0]); |
| 9253 | goto out_err; |
| 9254 | } |
| 9255 | |
| 9256 | LIST_INIT(&rule->arg.map.key); |
| 9257 | LIST_INIT(&rule->arg.map.value); |
| 9258 | |
| 9259 | proxy->conf.args.ctx = ARGC_HRS; |
| 9260 | |
| 9261 | /* key pattern */ |
| 9262 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9263 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9264 | file, linenum); |
| 9265 | |
| 9266 | /* value pattern */ |
| 9267 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9268 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9269 | file, linenum); |
| 9270 | |
| 9271 | free(proxy->conf.lfs_file); |
| 9272 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9273 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9274 | |
| 9275 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9276 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9277 | char *errmsg = NULL; |
| 9278 | cur_arg = 1; |
| 9279 | /* try in the module list */ |
| 9280 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9281 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9282 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9283 | free(errmsg); |
| 9284 | goto out_err; |
| 9285 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9286 | } else { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9287 | Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'del-header', 'set-header', '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] | 9288 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 9289 | goto out_err; |
| 9290 | } |
| 9291 | |
| 9292 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9293 | struct acl_cond *cond; |
| 9294 | char *errmsg = NULL; |
| 9295 | |
| 9296 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9297 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9298 | file, linenum, args[0], errmsg); |
| 9299 | free(errmsg); |
| 9300 | goto out_err; |
| 9301 | } |
| 9302 | rule->cond = cond; |
| 9303 | } |
| 9304 | else if (*args[cur_arg]) { |
| 9305 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9306 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9307 | file, linenum, args[0], args[cur_arg]); |
| 9308 | goto out_err; |
| 9309 | } |
| 9310 | |
| 9311 | return rule; |
| 9312 | out_err: |
| 9313 | free(rule); |
| 9314 | return NULL; |
| 9315 | } |
| 9316 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9317 | /* 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] | 9318 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
| 9319 | * dynamic log-format rule instead of a static string. |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9320 | */ |
| 9321 | 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] | 9322 | const char **args, char **errmsg, int use_fmt) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9323 | { |
| 9324 | struct redirect_rule *rule; |
| 9325 | int cur_arg; |
| 9326 | int type = REDIRECT_TYPE_NONE; |
| 9327 | int code = 302; |
| 9328 | const char *destination = NULL; |
| 9329 | const char *cookie = NULL; |
| 9330 | int cookie_set = 0; |
| 9331 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9332 | struct acl_cond *cond = NULL; |
| 9333 | |
| 9334 | cur_arg = 0; |
| 9335 | while (*(args[cur_arg])) { |
| 9336 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9337 | if (!*args[cur_arg + 1]) |
| 9338 | goto missing_arg; |
| 9339 | |
| 9340 | type = REDIRECT_TYPE_LOCATION; |
| 9341 | cur_arg++; |
| 9342 | destination = args[cur_arg]; |
| 9343 | } |
| 9344 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9345 | if (!*args[cur_arg + 1]) |
| 9346 | goto missing_arg; |
| 9347 | |
| 9348 | type = REDIRECT_TYPE_PREFIX; |
| 9349 | cur_arg++; |
| 9350 | destination = args[cur_arg]; |
| 9351 | } |
| 9352 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9353 | if (!*args[cur_arg + 1]) |
| 9354 | goto missing_arg; |
| 9355 | |
| 9356 | type = REDIRECT_TYPE_SCHEME; |
| 9357 | cur_arg++; |
| 9358 | destination = args[cur_arg]; |
| 9359 | } |
| 9360 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9361 | if (!*args[cur_arg + 1]) |
| 9362 | goto missing_arg; |
| 9363 | |
| 9364 | cur_arg++; |
| 9365 | cookie = args[cur_arg]; |
| 9366 | cookie_set = 1; |
| 9367 | } |
| 9368 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9369 | if (!*args[cur_arg + 1]) |
| 9370 | goto missing_arg; |
| 9371 | |
| 9372 | cur_arg++; |
| 9373 | cookie = args[cur_arg]; |
| 9374 | cookie_set = 0; |
| 9375 | } |
| 9376 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9377 | if (!*args[cur_arg + 1]) |
| 9378 | goto missing_arg; |
| 9379 | |
| 9380 | cur_arg++; |
| 9381 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9382 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9383 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9384 | "'%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] | 9385 | args[cur_arg - 1], args[cur_arg]); |
| 9386 | return NULL; |
| 9387 | } |
| 9388 | } |
| 9389 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9390 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9391 | } |
| 9392 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9393 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9394 | } |
| 9395 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9396 | strcmp(args[cur_arg], "unless") == 0) { |
| 9397 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 9398 | if (!cond) { |
| 9399 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9400 | return NULL; |
| 9401 | } |
| 9402 | break; |
| 9403 | } |
| 9404 | else { |
| 9405 | memprintf(errmsg, |
| 9406 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9407 | args[cur_arg]); |
| 9408 | return NULL; |
| 9409 | } |
| 9410 | cur_arg++; |
| 9411 | } |
| 9412 | |
| 9413 | if (type == REDIRECT_TYPE_NONE) { |
| 9414 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9415 | return NULL; |
| 9416 | } |
| 9417 | |
| 9418 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 9419 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9420 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9421 | |
| 9422 | if (!use_fmt) { |
| 9423 | /* old-style static redirect rule */ |
| 9424 | rule->rdr_str = strdup(destination); |
| 9425 | rule->rdr_len = strlen(destination); |
| 9426 | } |
| 9427 | else { |
| 9428 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9429 | |
| 9430 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9431 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9432 | * makes it hard for the user to configure a self-redirection. |
| 9433 | */ |
| 9434 | proxy->conf.args.ctx = ARGC_RDR; |
| 9435 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9436 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9437 | (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9438 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9439 | free(curproxy->conf.lfs_file); |
| 9440 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9441 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9442 | } |
| 9443 | } |
| 9444 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9445 | if (cookie) { |
| 9446 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9447 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9448 | */ |
| 9449 | rule->cookie_len = strlen(cookie); |
| 9450 | if (cookie_set) { |
| 9451 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9452 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9453 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9454 | rule->cookie_len += 9; |
| 9455 | } else { |
| 9456 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9457 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9458 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9459 | rule->cookie_len += 20; |
| 9460 | } |
| 9461 | } |
| 9462 | rule->type = type; |
| 9463 | rule->code = code; |
| 9464 | rule->flags = flags; |
| 9465 | LIST_INIT(&rule->list); |
| 9466 | return rule; |
| 9467 | |
| 9468 | missing_arg: |
| 9469 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9470 | return NULL; |
| 9471 | } |
| 9472 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9473 | /************************************************************************/ |
| 9474 | /* The code below is dedicated to ACL parsing and matching */ |
| 9475 | /************************************************************************/ |
| 9476 | |
| 9477 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9478 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9479 | * which means that a request or response is ready. If some data is missing, |
| 9480 | * 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] | 9481 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9482 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9483 | * |
| 9484 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9485 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9486 | * decide whether or not an HTTP message is present ; |
| 9487 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9488 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9489 | * 1 if an HTTP message is ready |
| 9490 | */ |
| 9491 | static int |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9492 | 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] | 9493 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9494 | { |
| 9495 | struct http_txn *txn = l7; |
| 9496 | struct http_msg *msg = &txn->req; |
| 9497 | |
| 9498 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 9499 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 9500 | */ |
| 9501 | |
| 9502 | if (unlikely(!s || !txn)) |
| 9503 | return 0; |
| 9504 | |
| 9505 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9506 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9507 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9508 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9509 | if (unlikely(!s->req)) |
| 9510 | return 0; |
| 9511 | |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9512 | /* If the buffer does not leave enough free space at the end, |
| 9513 | * we must first realign it. |
| 9514 | */ |
| 9515 | if (s->req->buf->p > s->req->buf->data && |
| 9516 | s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite) |
| 9517 | buffer_slow_realign(s->req->buf); |
| 9518 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9519 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9520 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9521 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9522 | |
| 9523 | /* Try to decode HTTP request */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9524 | if (likely(msg->next < s->req->buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9525 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9526 | |
| 9527 | /* Still no valid request ? */ |
| 9528 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9529 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9530 | buffer_full(s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9531 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9532 | } |
| 9533 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9534 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9535 | return 0; |
| 9536 | } |
| 9537 | |
| 9538 | /* OK we just got a valid HTTP request. We have some minor |
| 9539 | * preparation to perform so that further checks can rely |
| 9540 | * on HTTP tests. |
| 9541 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9542 | |
| 9543 | /* If the request was parsed but was too large, we must absolutely |
| 9544 | * return an error so that it is not processed. At the moment this |
| 9545 | * cannot happen, but if the parsers are to change in the future, |
| 9546 | * we want this check to be maintained. |
| 9547 | */ |
| 9548 | if (unlikely(s->req->buf->i + s->req->buf->p > |
| 9549 | s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) { |
| 9550 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9551 | smp->data.uint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9552 | return 1; |
| 9553 | } |
| 9554 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9555 | 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] | 9556 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 9557 | s->flags |= SN_REDIRECTABLE; |
| 9558 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9559 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9560 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9561 | } |
| 9562 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9563 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9564 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9565 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9566 | |
| 9567 | /* otherwise everything's ready for the request */ |
| 9568 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9569 | else { |
| 9570 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9571 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9572 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9573 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9574 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9575 | } |
| 9576 | |
| 9577 | /* everything's OK */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9578 | smp->data.uint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9579 | return 1; |
| 9580 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9581 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9582 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9583 | 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] | 9584 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9585 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9586 | 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] | 9587 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9588 | |
| 9589 | /* 1. Check on METHOD |
| 9590 | * We use the pre-parsed method if it is known, and store its number as an |
| 9591 | * integer. If it is unknown, we use the pointer and the length. |
| 9592 | */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 9593 | static int pat_parse_meth(const char *text, struct pattern *pattern, char **err) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9594 | { |
| 9595 | int len, meth; |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 9596 | struct chunk *trash; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9597 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9598 | len = strlen(text); |
| 9599 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9600 | |
| 9601 | pattern->val.i = meth; |
| 9602 | if (meth == HTTP_METH_OTHER) { |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 9603 | trash = get_trash_chunk(); |
| 9604 | if (trash->size < len) { |
| 9605 | memprintf(err, "no space avalaible in the buffer. expect %d, provides %d", |
| 9606 | len, trash->size); |
| 9607 | return 0; |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 9608 | } |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 9609 | pattern->ptr.str = trash->str; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9610 | pattern->len = len; |
| 9611 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9612 | else { |
| 9613 | pattern->ptr.str = NULL; |
| 9614 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9615 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9616 | return 1; |
| 9617 | } |
| 9618 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9619 | /* This function fetches the method of current HTTP request and stores |
| 9620 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 9621 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 9622 | * in <len> and <ptr> is NULL ; |
| 9623 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 9624 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9625 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9626 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9627 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9628 | smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9629 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9630 | { |
| 9631 | int meth; |
| 9632 | struct http_txn *txn = l7; |
| 9633 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9634 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9635 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9636 | meth = txn->meth; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9637 | smp->type = SMP_T_METH; |
| 9638 | smp->data.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9639 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9640 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9641 | /* ensure the indexes are not affected */ |
| 9642 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9643 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9644 | smp->data.meth.str.len = txn->req.sl.rq.m_l; |
| 9645 | smp->data.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9646 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9647 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9648 | return 1; |
| 9649 | } |
| 9650 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9651 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9652 | 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] | 9653 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9654 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9655 | struct pattern_list *lst; |
| 9656 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9657 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9658 | list_for_each_entry(lst, &expr->patterns, list) { |
| 9659 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9660 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9661 | /* well-known method */ |
| 9662 | if (pattern->val.i != HTTP_METH_OTHER) { |
| 9663 | if (smp->data.meth.meth == pattern->val.i) |
| 9664 | return pattern; |
| 9665 | else |
| 9666 | continue; |
| 9667 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9668 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9669 | /* Other method, we must compare the strings */ |
| 9670 | if (pattern->len != smp->data.meth.str.len) |
| 9671 | continue; |
| 9672 | |
| 9673 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 9674 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0) || |
| 9675 | (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0)) |
| 9676 | return pattern; |
| 9677 | } |
| 9678 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9679 | } |
| 9680 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9681 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9682 | smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9683 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9684 | { |
| 9685 | struct http_txn *txn = l7; |
| 9686 | char *ptr; |
| 9687 | int len; |
| 9688 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9689 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9690 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9691 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9692 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9693 | |
| 9694 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9695 | if (len <= 0) |
| 9696 | return 0; |
| 9697 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9698 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9699 | smp->data.str.str = ptr; |
| 9700 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9701 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9702 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9703 | return 1; |
| 9704 | } |
| 9705 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9706 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9707 | smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9708 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9709 | { |
| 9710 | struct http_txn *txn = l7; |
| 9711 | char *ptr; |
| 9712 | int len; |
| 9713 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9714 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9715 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9716 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9717 | return 0; |
| 9718 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9719 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9720 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9721 | |
| 9722 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9723 | if (len <= 0) |
| 9724 | return 0; |
| 9725 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9726 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9727 | smp->data.str.str = ptr; |
| 9728 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9729 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9730 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9731 | return 1; |
| 9732 | } |
| 9733 | |
| 9734 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9735 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9736 | smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9737 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9738 | { |
| 9739 | struct http_txn *txn = l7; |
| 9740 | char *ptr; |
| 9741 | int len; |
| 9742 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9743 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9744 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9745 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9746 | return 0; |
| 9747 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9748 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9749 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9750 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9751 | smp->type = SMP_T_UINT; |
| 9752 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9753 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9754 | return 1; |
| 9755 | } |
| 9756 | |
| 9757 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9758 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9759 | smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9760 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9761 | { |
| 9762 | struct http_txn *txn = l7; |
| 9763 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9764 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9765 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9766 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9767 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9768 | 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] | 9769 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9770 | return 1; |
| 9771 | } |
| 9772 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9773 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9774 | smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9775 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9776 | { |
| 9777 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9778 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9779 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9780 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9781 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 9782 | 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] | 9783 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 9784 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9785 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9786 | smp->type = SMP_T_IPV4; |
| 9787 | smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9788 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9789 | return 1; |
| 9790 | } |
| 9791 | |
| 9792 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9793 | smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9794 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9795 | { |
| 9796 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9797 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9798 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9799 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9800 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 9801 | 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] | 9802 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 9803 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9804 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9805 | smp->type = SMP_T_UINT; |
| 9806 | smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 9807 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9808 | return 1; |
| 9809 | } |
| 9810 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9811 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9812 | * Accepts an optional argument of type string containing the header field name, |
| 9813 | * and an optional argument of type signed or unsigned integer to request an |
| 9814 | * 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] | 9815 | * headers are considered from the first one. It does not stop on commas and |
| 9816 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9817 | */ |
| 9818 | static int |
| 9819 | smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9820 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9821 | { |
| 9822 | struct http_txn *txn = l7; |
| 9823 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9824 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
| 9825 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 9826 | int occ = 0; |
| 9827 | const char *name_str = NULL; |
| 9828 | int name_len = 0; |
| 9829 | |
| 9830 | if (!ctx) { |
| 9831 | /* first call */ |
| 9832 | ctx = &static_hdr_ctx; |
| 9833 | ctx->idx = 0; |
| 9834 | smp->ctx.a[0] = ctx; |
| 9835 | } |
| 9836 | |
| 9837 | if (args) { |
| 9838 | if (args[0].type != ARGT_STR) |
| 9839 | return 0; |
| 9840 | name_str = args[0].data.str.str; |
| 9841 | name_len = args[0].data.str.len; |
| 9842 | |
| 9843 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 9844 | occ = args[1].data.uint; |
| 9845 | } |
| 9846 | |
| 9847 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9848 | |
| 9849 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 9850 | /* search for header from the beginning */ |
| 9851 | ctx->idx = 0; |
| 9852 | |
| 9853 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 9854 | /* no explicit occurrence and single fetch => last header by default */ |
| 9855 | occ = -1; |
| 9856 | |
| 9857 | if (!occ) |
| 9858 | /* prepare to report multiple occurrences for ACL fetches */ |
| 9859 | smp->flags |= SMP_F_NOT_LAST; |
| 9860 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9861 | smp->type = SMP_T_STR; |
| 9862 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9863 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
| 9864 | return 1; |
| 9865 | |
| 9866 | smp->flags &= ~SMP_F_NOT_LAST; |
| 9867 | return 0; |
| 9868 | } |
| 9869 | |
| 9870 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 9871 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 9872 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9873 | */ |
| 9874 | static int |
| 9875 | smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9876 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9877 | { |
| 9878 | struct http_txn *txn = l7; |
| 9879 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9880 | struct hdr_ctx ctx; |
| 9881 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 9882 | int cnt; |
| 9883 | |
| 9884 | if (!args || args->type != ARGT_STR) |
| 9885 | return 0; |
| 9886 | |
| 9887 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9888 | |
| 9889 | ctx.idx = 0; |
| 9890 | cnt = 0; |
| 9891 | while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx)) |
| 9892 | cnt++; |
| 9893 | |
| 9894 | smp->type = SMP_T_UINT; |
| 9895 | smp->data.uint = cnt; |
| 9896 | smp->flags = SMP_F_VOL_HDR; |
| 9897 | return 1; |
| 9898 | } |
| 9899 | |
| 9900 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9901 | * Accepts an optional argument of type string containing the header field name, |
| 9902 | * and an optional argument of type signed or unsigned integer to request an |
| 9903 | * 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] | 9904 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9905 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9906 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9907 | smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9908 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9909 | { |
| 9910 | struct http_txn *txn = l7; |
| 9911 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9912 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9913 | 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] | 9914 | int occ = 0; |
| 9915 | const char *name_str = NULL; |
| 9916 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9917 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9918 | if (!ctx) { |
| 9919 | /* first call */ |
| 9920 | ctx = &static_hdr_ctx; |
| 9921 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 9922 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9923 | } |
| 9924 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9925 | if (args) { |
| 9926 | if (args[0].type != ARGT_STR) |
| 9927 | return 0; |
| 9928 | name_str = args[0].data.str.str; |
| 9929 | name_len = args[0].data.str.len; |
| 9930 | |
| 9931 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 9932 | occ = args[1].data.uint; |
| 9933 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9934 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9935 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9936 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9937 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9938 | /* search for header from the beginning */ |
| 9939 | ctx->idx = 0; |
| 9940 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9941 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 9942 | /* no explicit occurrence and single fetch => last header by default */ |
| 9943 | occ = -1; |
| 9944 | |
| 9945 | if (!occ) |
| 9946 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9947 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 9948 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9949 | smp->type = SMP_T_STR; |
| 9950 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9951 | 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] | 9952 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9953 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9954 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9955 | return 0; |
| 9956 | } |
| 9957 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9958 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9959 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9960 | */ |
| 9961 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9962 | smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9963 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9964 | { |
| 9965 | struct http_txn *txn = l7; |
| 9966 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9967 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9968 | 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] | 9969 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9970 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9971 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9972 | return 0; |
| 9973 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9974 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9975 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9976 | ctx.idx = 0; |
| 9977 | cnt = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9978 | 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] | 9979 | cnt++; |
| 9980 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9981 | smp->type = SMP_T_UINT; |
| 9982 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9983 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9984 | return 1; |
| 9985 | } |
| 9986 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9987 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 9988 | * takes a mandatory argument of type string and an optional one of type int |
| 9989 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 9990 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9991 | */ |
| 9992 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9993 | smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9994 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9995 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9996 | int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9997 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9998 | if (ret > 0) { |
| 9999 | smp->type = SMP_T_UINT; |
| 10000 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10001 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10002 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10003 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10004 | } |
| 10005 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10006 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10007 | * and an optional one of type int to designate a specific occurrence. |
| 10008 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10009 | */ |
| 10010 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10011 | smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10012 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10013 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10014 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10015 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10016 | while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) { |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10017 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 10018 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10019 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10020 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10021 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10022 | if (smp->data.str.len < temp->size - 1) { |
| 10023 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 10024 | temp->str[smp->data.str.len] = '\0'; |
| 10025 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 10026 | smp->type = SMP_T_IPV6; |
| 10027 | break; |
| 10028 | } |
| 10029 | } |
| 10030 | } |
| 10031 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10032 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10033 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10034 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10035 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10036 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10037 | } |
| 10038 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10039 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10040 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10041 | */ |
| 10042 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 10043 | smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10044 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10045 | { |
| 10046 | struct http_txn *txn = l7; |
| 10047 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10048 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10049 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10050 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10051 | 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] | 10052 | ptr = http_get_path(txn); |
| 10053 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10054 | return 0; |
| 10055 | |
| 10056 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10057 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10058 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10059 | |
| 10060 | while (ptr < end && *ptr != '?') |
| 10061 | ptr++; |
| 10062 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10063 | smp->data.str.len = ptr - smp->data.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10064 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10065 | return 1; |
| 10066 | } |
| 10067 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10068 | /* This produces a concatenation of the first occurrence of the Host header |
| 10069 | * followed by the path component if it begins with a slash ('/'). This means |
| 10070 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10071 | * If no Host header is found, then the path is returned as-is. The returned |
| 10072 | * 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] | 10073 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10074 | */ |
| 10075 | static int |
| 10076 | smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10077 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10078 | { |
| 10079 | struct http_txn *txn = l7; |
| 10080 | char *ptr, *end, *beg; |
| 10081 | struct hdr_ctx ctx; |
| 10082 | |
| 10083 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10084 | |
| 10085 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10086 | if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen) |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10087 | return smp_fetch_path(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10088 | |
| 10089 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 10090 | memcpy(trash.str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10091 | smp->type = SMP_T_STR; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 10092 | smp->data.str.str = trash.str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10093 | smp->data.str.len = ctx.vlen; |
| 10094 | |
| 10095 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10096 | 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] | 10097 | beg = http_get_path(txn); |
| 10098 | if (!beg) |
| 10099 | beg = end; |
| 10100 | |
| 10101 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10102 | |
| 10103 | if (beg < ptr && *beg == '/') { |
| 10104 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 10105 | smp->data.str.len += ptr - beg; |
| 10106 | } |
| 10107 | |
| 10108 | smp->flags = SMP_F_VOL_1ST; |
| 10109 | return 1; |
| 10110 | } |
| 10111 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10112 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10113 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10114 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10115 | * 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] | 10116 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10117 | * 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] | 10118 | * high-traffic sites without having to store whole paths. |
| 10119 | */ |
| 10120 | static int |
| 10121 | smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10122 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10123 | { |
| 10124 | struct http_txn *txn = l7; |
| 10125 | struct hdr_ctx ctx; |
| 10126 | unsigned int hash = 0; |
| 10127 | char *ptr, *beg, *end; |
| 10128 | int len; |
| 10129 | |
| 10130 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10131 | |
| 10132 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10133 | 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] | 10134 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10135 | ptr = ctx.line + ctx.val; |
| 10136 | len = ctx.vlen; |
| 10137 | while (len--) |
| 10138 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10139 | } |
| 10140 | |
| 10141 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10142 | 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] | 10143 | beg = http_get_path(txn); |
| 10144 | if (!beg) |
| 10145 | beg = end; |
| 10146 | |
| 10147 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10148 | |
| 10149 | if (beg < ptr && *beg == '/') { |
| 10150 | while (beg < ptr) |
| 10151 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10152 | } |
| 10153 | hash = full_hash(hash); |
| 10154 | |
| 10155 | smp->type = SMP_T_UINT; |
| 10156 | smp->data.uint = hash; |
| 10157 | smp->flags = SMP_F_VOL_1ST; |
| 10158 | return 1; |
| 10159 | } |
| 10160 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10161 | /* 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] | 10162 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10163 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10164 | * 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] | 10165 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10166 | * 8 bytes would still work. |
| 10167 | */ |
| 10168 | static int |
| 10169 | smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10170 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10171 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10172 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10173 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
| 10174 | |
| 10175 | if (!cli_conn) |
| 10176 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10177 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10178 | if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10179 | return 0; |
| 10180 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10181 | temp = get_trash_chunk(); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10182 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 10183 | temp->len += sizeof(smp->data.uint); |
| 10184 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10185 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10186 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10187 | 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] | 10188 | temp->len += 4; |
| 10189 | break; |
| 10190 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10191 | 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] | 10192 | temp->len += 16; |
| 10193 | break; |
| 10194 | default: |
| 10195 | return 0; |
| 10196 | } |
| 10197 | |
| 10198 | smp->data.str = *temp; |
| 10199 | smp->type = SMP_T_BIN; |
| 10200 | return 1; |
| 10201 | } |
| 10202 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10203 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10204 | smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10205 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10206 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10207 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10208 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10209 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10210 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10211 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10212 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10213 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 10214 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10215 | return 1; |
| 10216 | } |
| 10217 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10218 | /* return a valid test if the current request is the first one on the connection */ |
| 10219 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10220 | smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10221 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10222 | { |
| 10223 | if (!s) |
| 10224 | return 0; |
| 10225 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10226 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 10227 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10228 | return 1; |
| 10229 | } |
| 10230 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10231 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10232 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10233 | smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10234 | const struct arg *args, struct sample *smp, const char *kw) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10235 | { |
| 10236 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10237 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10238 | return 0; |
| 10239 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10240 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10241 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10242 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10243 | return 0; |
| 10244 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10245 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10246 | 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] | 10247 | return 1; |
| 10248 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10249 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10250 | /* Accepts exactly 1 argument of type userlist */ |
| 10251 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10252 | smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10253 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10254 | { |
| 10255 | |
| 10256 | if (!args || args->type != ARGT_USR) |
| 10257 | return 0; |
| 10258 | |
| 10259 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10260 | |
| 10261 | if (!get_http_auth(l4)) |
| 10262 | return 0; |
| 10263 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10264 | /* if the user does not belong to the userlist or has a wrong password, |
| 10265 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10266 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10267 | */ |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10268 | if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass)) |
| 10269 | return 0; |
| 10270 | |
| 10271 | /* pat_match_auth() will need the user list */ |
| 10272 | smp->ctx.a[0] = args->data.usr; |
| 10273 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10274 | smp->type = SMP_T_STR; |
| 10275 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10276 | smp->data.str.str = l4->txn.auth.user; |
| 10277 | smp->data.str.len = strlen(l4->txn.auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10278 | |
| 10279 | return 1; |
| 10280 | } |
| 10281 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10282 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10283 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10284 | * the cookie value is returned into *value and *value_l, and the function |
| 10285 | * returns a pointer to the next pointer to search from if the value was found. |
| 10286 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10287 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10288 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10289 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10290 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10291 | * list is expected. |
| 10292 | */ |
| 10293 | static char * |
| 10294 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10295 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10296 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10297 | { |
| 10298 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10299 | char *next; |
| 10300 | |
| 10301 | /* we search at least a cookie name followed by an equal, and more |
| 10302 | * generally something like this : |
| 10303 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10304 | */ |
| 10305 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10306 | /* Iterate through all cookies on this line */ |
| 10307 | |
| 10308 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 10309 | att_beg++; |
| 10310 | |
| 10311 | /* find att_end : this is the first character after the last non |
| 10312 | * space before the equal. It may be equal to hdr_end. |
| 10313 | */ |
| 10314 | equal = att_end = att_beg; |
| 10315 | |
| 10316 | while (equal < hdr_end) { |
| 10317 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10318 | break; |
| 10319 | if (http_is_spht[(unsigned char)*equal++]) |
| 10320 | continue; |
| 10321 | att_end = equal; |
| 10322 | } |
| 10323 | |
| 10324 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10325 | * is between <att_beg> and <equal>, both may be identical. |
| 10326 | */ |
| 10327 | |
| 10328 | /* look for end of cookie if there is an equal sign */ |
| 10329 | if (equal < hdr_end && *equal == '=') { |
| 10330 | /* look for the beginning of the value */ |
| 10331 | val_beg = equal + 1; |
| 10332 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 10333 | val_beg++; |
| 10334 | |
| 10335 | /* find the end of the value, respecting quotes */ |
| 10336 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10337 | |
| 10338 | /* make val_end point to the first white space or delimitor after the value */ |
| 10339 | val_end = next; |
| 10340 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 10341 | val_end--; |
| 10342 | } else { |
| 10343 | val_beg = val_end = next = equal; |
| 10344 | } |
| 10345 | |
| 10346 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10347 | * they will automatically be removed if a header before them is removed, |
| 10348 | * since they're supposed to be linked together. |
| 10349 | */ |
| 10350 | if (*att_beg == '$') |
| 10351 | continue; |
| 10352 | |
| 10353 | /* Ignore cookies with no equal sign */ |
| 10354 | if (equal == next) |
| 10355 | continue; |
| 10356 | |
| 10357 | /* Now we have the cookie name between att_beg and att_end, and |
| 10358 | * its value between val_beg and val_end. |
| 10359 | */ |
| 10360 | |
| 10361 | if (att_end - att_beg == cookie_name_l && |
| 10362 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10363 | /* let's return this value and indicate where to go on from */ |
| 10364 | *value = val_beg; |
| 10365 | *value_l = val_end - val_beg; |
| 10366 | return next + 1; |
| 10367 | } |
| 10368 | |
| 10369 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10370 | if (!list) |
| 10371 | break; |
| 10372 | } |
| 10373 | |
| 10374 | return NULL; |
| 10375 | } |
| 10376 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10377 | /* Fetch a captured HTTP request header. The index is the position of |
| 10378 | * the "capture" option in the configuration file |
| 10379 | */ |
| 10380 | static int |
| 10381 | smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10382 | const struct arg *args, struct sample *smp, const char *kw) |
| 10383 | { |
| 10384 | struct proxy *fe = l4->fe; |
| 10385 | struct http_txn *txn = l7; |
| 10386 | int idx; |
| 10387 | |
| 10388 | if (!args || args->type != ARGT_UINT) |
| 10389 | return 0; |
| 10390 | |
| 10391 | idx = args->data.uint; |
| 10392 | |
| 10393 | if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL) |
| 10394 | return 0; |
| 10395 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10396 | smp->type = SMP_T_STR; |
| 10397 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10398 | smp->data.str.str = txn->req.cap[idx]; |
| 10399 | smp->data.str.len = strlen(txn->req.cap[idx]); |
| 10400 | |
| 10401 | return 1; |
| 10402 | } |
| 10403 | |
| 10404 | /* Fetch a captured HTTP response header. The index is the position of |
| 10405 | * the "capture" option in the configuration file |
| 10406 | */ |
| 10407 | static int |
| 10408 | smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10409 | const struct arg *args, struct sample *smp, const char *kw) |
| 10410 | { |
| 10411 | struct proxy *fe = l4->fe; |
| 10412 | struct http_txn *txn = l7; |
| 10413 | int idx; |
| 10414 | |
| 10415 | if (!args || args->type != ARGT_UINT) |
| 10416 | return 0; |
| 10417 | |
| 10418 | idx = args->data.uint; |
| 10419 | |
| 10420 | if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL) |
| 10421 | return 0; |
| 10422 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10423 | smp->type = SMP_T_STR; |
| 10424 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10425 | smp->data.str.str = txn->rsp.cap[idx]; |
| 10426 | smp->data.str.len = strlen(txn->rsp.cap[idx]); |
| 10427 | |
| 10428 | return 1; |
| 10429 | } |
| 10430 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10431 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 10432 | static int |
| 10433 | smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10434 | const struct arg *args, struct sample *smp, const char *kw) |
| 10435 | { |
| 10436 | struct chunk *temp; |
| 10437 | struct http_txn *txn = l7; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10438 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10439 | |
| 10440 | if (!txn->uri) |
| 10441 | return 0; |
| 10442 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10443 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10444 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10445 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10446 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10447 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10448 | temp = get_trash_chunk(); |
| 10449 | temp->str = txn->uri; |
| 10450 | temp->len = ptr - txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10451 | smp->data.str = *temp; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10452 | smp->type = SMP_T_STR; |
| 10453 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10454 | |
| 10455 | return 1; |
| 10456 | |
| 10457 | } |
| 10458 | |
| 10459 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 10460 | static int |
| 10461 | smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10462 | const struct arg *args, struct sample *smp, const char *kw) |
| 10463 | { |
| 10464 | struct chunk *temp; |
| 10465 | struct http_txn *txn = l7; |
| 10466 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10467 | |
| 10468 | if (!txn->uri) |
| 10469 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10470 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10471 | ptr = txn->uri; |
| 10472 | |
| 10473 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10474 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10475 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10476 | if (!*ptr) |
| 10477 | return 0; |
| 10478 | |
| 10479 | ptr++; /* skip the space */ |
| 10480 | |
| 10481 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10482 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10483 | if (!ptr) |
| 10484 | return 0; |
| 10485 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 10486 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10487 | |
| 10488 | smp->data.str = *temp; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10489 | smp->data.str.len = ptr - temp->str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10490 | smp->type = SMP_T_STR; |
| 10491 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10492 | |
| 10493 | return 1; |
| 10494 | } |
| 10495 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10496 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 10497 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10498 | */ |
| 10499 | static int |
| 10500 | smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10501 | const struct arg *args, struct sample *smp, const char *kw) |
| 10502 | { |
| 10503 | struct http_txn *txn = l7; |
| 10504 | |
| 10505 | if (txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
| 10506 | return 0; |
| 10507 | |
| 10508 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 10509 | smp->data.str.str = "HTTP/1.1"; |
| 10510 | else |
| 10511 | smp->data.str.str = "HTTP/1.0"; |
| 10512 | |
| 10513 | smp->data.str.len = 8; |
| 10514 | smp->type = SMP_T_STR; |
| 10515 | smp->flags = SMP_F_CONST; |
| 10516 | return 1; |
| 10517 | |
| 10518 | } |
| 10519 | |
| 10520 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 10521 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10522 | */ |
| 10523 | static int |
| 10524 | smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10525 | const struct arg *args, struct sample *smp, const char *kw) |
| 10526 | { |
| 10527 | struct http_txn *txn = l7; |
| 10528 | |
| 10529 | if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
| 10530 | return 0; |
| 10531 | |
| 10532 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
| 10533 | smp->data.str.str = "HTTP/1.1"; |
| 10534 | else |
| 10535 | smp->data.str.str = "HTTP/1.0"; |
| 10536 | |
| 10537 | smp->data.str.len = 8; |
| 10538 | smp->type = SMP_T_STR; |
| 10539 | smp->flags = SMP_F_CONST; |
| 10540 | return 1; |
| 10541 | |
| 10542 | } |
| 10543 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10544 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10545 | /* 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] | 10546 | * 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] | 10547 | * 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] | 10548 | * 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] | 10549 | * 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] | 10550 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 10551 | * that no iterating is desired, then only last value is fetched if any. |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 10552 | * The returned sample is of type CSTR. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10553 | */ |
| 10554 | static int |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10555 | smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10556 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10557 | { |
| 10558 | struct http_txn *txn = l7; |
| 10559 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10560 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10561 | const struct http_msg *msg; |
| 10562 | const char *hdr_name; |
| 10563 | int hdr_name_len; |
| 10564 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10565 | int occ = 0; |
| 10566 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10567 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10568 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10569 | return 0; |
| 10570 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10571 | if (!ctx) { |
| 10572 | /* first call */ |
| 10573 | ctx = &static_hdr_ctx; |
| 10574 | ctx->idx = 0; |
| 10575 | smp->ctx.a[2] = ctx; |
| 10576 | } |
| 10577 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10578 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10579 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10580 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10581 | msg = &txn->req; |
| 10582 | hdr_name = "Cookie"; |
| 10583 | hdr_name_len = 6; |
| 10584 | } else { |
| 10585 | msg = &txn->rsp; |
| 10586 | hdr_name = "Set-Cookie"; |
| 10587 | hdr_name_len = 10; |
| 10588 | } |
| 10589 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10590 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10591 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 10592 | occ = -1; |
| 10593 | |
| 10594 | /* OK so basically here, either we want only one value and it's the |
| 10595 | * last one, or we want to iterate over all of them and we fetch the |
| 10596 | * next one. |
| 10597 | */ |
| 10598 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10599 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10600 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10601 | /* search for the header from the beginning, we must first initialize |
| 10602 | * the search parameters. |
| 10603 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10604 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10605 | ctx->idx = 0; |
| 10606 | } |
| 10607 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10608 | smp->flags |= SMP_F_VOL_HDR; |
| 10609 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10610 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10611 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 10612 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10613 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 10614 | goto out; |
| 10615 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10616 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10617 | continue; |
| 10618 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10619 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 10620 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10621 | } |
| 10622 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10623 | smp->type = SMP_T_STR; |
| 10624 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10625 | 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] | 10626 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10627 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10628 | &smp->data.str.str, |
| 10629 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10630 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10631 | found = 1; |
| 10632 | if (occ >= 0) { |
| 10633 | /* one value was returned into smp->data.str.{str,len} */ |
| 10634 | smp->flags |= SMP_F_NOT_LAST; |
| 10635 | return 1; |
| 10636 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10637 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10638 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10639 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10640 | /* all cookie headers and values were scanned. If we're looking for the |
| 10641 | * last occurrence, we may return it now. |
| 10642 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10643 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10644 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10645 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10646 | } |
| 10647 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10648 | /* 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] | 10649 | * 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] | 10650 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 10651 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10652 | */ |
| 10653 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10654 | smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10655 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10656 | { |
| 10657 | struct http_txn *txn = l7; |
| 10658 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10659 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10660 | const struct http_msg *msg; |
| 10661 | const char *hdr_name; |
| 10662 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10663 | int cnt; |
| 10664 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10665 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10666 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10667 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10668 | return 0; |
| 10669 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10670 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10671 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10672 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10673 | msg = &txn->req; |
| 10674 | hdr_name = "Cookie"; |
| 10675 | hdr_name_len = 6; |
| 10676 | } else { |
| 10677 | msg = &txn->rsp; |
| 10678 | hdr_name = "Set-Cookie"; |
| 10679 | hdr_name_len = 10; |
| 10680 | } |
| 10681 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10682 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 10683 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10684 | ctx.idx = 0; |
| 10685 | cnt = 0; |
| 10686 | |
| 10687 | while (1) { |
| 10688 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 10689 | if (!val_beg) { |
| 10690 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 10691 | break; |
| 10692 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10693 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10694 | continue; |
| 10695 | |
| 10696 | val_beg = ctx.line + ctx.val; |
| 10697 | val_end = val_beg + ctx.vlen; |
| 10698 | } |
| 10699 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10700 | smp->type = SMP_T_STR; |
| 10701 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10702 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10703 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10704 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10705 | &smp->data.str.str, |
| 10706 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10707 | cnt++; |
| 10708 | } |
| 10709 | } |
| 10710 | |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 10711 | smp->type = SMP_T_UINT; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10712 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10713 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10714 | return 1; |
| 10715 | } |
| 10716 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10717 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 10718 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 10719 | */ |
| 10720 | static int |
| 10721 | smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10722 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10723 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10724 | int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10725 | |
| 10726 | if (ret > 0) { |
| 10727 | smp->type = SMP_T_UINT; |
| 10728 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10729 | } |
| 10730 | |
| 10731 | return ret; |
| 10732 | } |
| 10733 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10734 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 10735 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10736 | /************************************************************************/ |
| 10737 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10738 | /* |
| 10739 | * Given a path string and its length, find the position of beginning of the |
| 10740 | * query string. Returns NULL if no query string is found in the path. |
| 10741 | * |
| 10742 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 10743 | * |
| 10744 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 10745 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10746 | 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] | 10747 | { |
| 10748 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 10749 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10750 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10751 | return p ? p + 1 : NULL; |
| 10752 | } |
| 10753 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10754 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10755 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10756 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10757 | } |
| 10758 | |
| 10759 | /* |
| 10760 | * Given a url parameter, find the starting position of the first occurence, |
| 10761 | * or NULL if the parameter is not found. |
| 10762 | * |
| 10763 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 10764 | * the function will return query_string+8. |
| 10765 | */ |
| 10766 | static char* |
| 10767 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10768 | char* url_param_name, size_t url_param_name_l, |
| 10769 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10770 | { |
| 10771 | char *pos, *last; |
| 10772 | |
| 10773 | pos = query_string; |
| 10774 | last = query_string + query_string_l - url_param_name_l - 1; |
| 10775 | |
| 10776 | while (pos <= last) { |
| 10777 | if (pos[url_param_name_l] == '=') { |
| 10778 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 10779 | return pos; |
| 10780 | pos += url_param_name_l + 1; |
| 10781 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10782 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10783 | pos++; |
| 10784 | pos++; |
| 10785 | } |
| 10786 | return NULL; |
| 10787 | } |
| 10788 | |
| 10789 | /* |
| 10790 | * Given a url parameter name, returns its value and size into *value and |
| 10791 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 10792 | * zero is returned and value/value_l are not touched. |
| 10793 | */ |
| 10794 | static int |
| 10795 | find_url_param_value(char* path, size_t path_l, |
| 10796 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10797 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10798 | { |
| 10799 | char *query_string, *qs_end; |
| 10800 | char *arg_start; |
| 10801 | char *value_start, *value_end; |
| 10802 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10803 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10804 | if (!query_string) |
| 10805 | return 0; |
| 10806 | |
| 10807 | qs_end = path + path_l; |
| 10808 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10809 | url_param_name, url_param_name_l, |
| 10810 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10811 | if (!arg_start) |
| 10812 | return 0; |
| 10813 | |
| 10814 | value_start = arg_start + url_param_name_l + 1; |
| 10815 | value_end = value_start; |
| 10816 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10817 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10818 | value_end++; |
| 10819 | |
| 10820 | *value = value_start; |
| 10821 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 10822 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10823 | } |
| 10824 | |
| 10825 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10826 | smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10827 | const struct arg *args, struct sample *smp, const char *kw) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10828 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10829 | char delim = '?'; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10830 | struct http_txn *txn = l7; |
| 10831 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10832 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10833 | if (!args || args[0].type != ARGT_STR || |
| 10834 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10835 | return 0; |
| 10836 | |
| 10837 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10838 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10839 | if (args[1].type) |
| 10840 | delim = *args[1].data.str.str; |
| 10841 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10842 | 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] | 10843 | args->data.str.str, args->data.str.len, |
| 10844 | &smp->data.str.str, &smp->data.str.len, |
| 10845 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10846 | return 0; |
| 10847 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10848 | smp->type = SMP_T_STR; |
| 10849 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10850 | return 1; |
| 10851 | } |
| 10852 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 10853 | /* Return the signed integer value for the specified url parameter (see url_param |
| 10854 | * above). |
| 10855 | */ |
| 10856 | static int |
| 10857 | smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10858 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 10859 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10860 | int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 10861 | |
| 10862 | if (ret > 0) { |
| 10863 | smp->type = SMP_T_UINT; |
| 10864 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10865 | } |
| 10866 | |
| 10867 | return ret; |
| 10868 | } |
| 10869 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10870 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10871 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10872 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10873 | * entry. If no Host header is found, then the path is used. The resulting value |
| 10874 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 10875 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 10876 | * high-traffic sites without having to store whole paths. |
| 10877 | * this differs from the base32 functions in that it includes the url parameters |
| 10878 | * as well as the path |
| 10879 | */ |
| 10880 | static int |
| 10881 | smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 10882 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10883 | { |
| 10884 | struct http_txn *txn = l7; |
| 10885 | struct hdr_ctx ctx; |
| 10886 | unsigned int hash = 0; |
| 10887 | char *ptr, *beg, *end; |
| 10888 | int len; |
| 10889 | |
| 10890 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10891 | |
| 10892 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10893 | 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] | 10894 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10895 | ptr = ctx.line + ctx.val; |
| 10896 | len = ctx.vlen; |
| 10897 | while (len--) |
| 10898 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10899 | } |
| 10900 | |
| 10901 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10902 | 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] | 10903 | beg = http_get_path(txn); |
| 10904 | if (!beg) |
| 10905 | beg = end; |
| 10906 | |
| 10907 | for (ptr = beg; ptr < end ; ptr++); |
| 10908 | |
| 10909 | if (beg < ptr && *beg == '/') { |
| 10910 | while (beg < ptr) |
| 10911 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10912 | } |
| 10913 | hash = full_hash(hash); |
| 10914 | |
| 10915 | smp->type = SMP_T_UINT; |
| 10916 | smp->data.uint = hash; |
| 10917 | smp->flags = SMP_F_VOL_1ST; |
| 10918 | return 1; |
| 10919 | } |
| 10920 | |
| 10921 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 10922 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10923 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 10924 | * on the source address length. The URL hash is stored before the address so |
| 10925 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10926 | * 8 bytes would still work. |
| 10927 | */ |
| 10928 | static int |
| 10929 | smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 10930 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10931 | { |
| 10932 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10933 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10934 | |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 10935 | if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10936 | return 0; |
| 10937 | |
| 10938 | temp = get_trash_chunk(); |
| 10939 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 10940 | temp->len += sizeof(smp->data.uint); |
| 10941 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10942 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10943 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10944 | 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] | 10945 | temp->len += 4; |
| 10946 | break; |
| 10947 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10948 | 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] | 10949 | temp->len += 16; |
| 10950 | break; |
| 10951 | default: |
| 10952 | return 0; |
| 10953 | } |
| 10954 | |
| 10955 | smp->data.str = *temp; |
| 10956 | smp->type = SMP_T_BIN; |
| 10957 | return 1; |
| 10958 | } |
| 10959 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10960 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 10961 | * keyword. These keywords support an optional positive or negative occurrence |
| 10962 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 10963 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 10964 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 10965 | * error message in case of error, that the caller is responsible for freeing. |
| 10966 | * The initial location must either be freeable or NULL. |
| 10967 | */ |
| 10968 | static int val_hdr(struct arg *arg, char **err_msg) |
| 10969 | { |
| 10970 | 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] | 10971 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10972 | return 0; |
| 10973 | } |
| 10974 | return 1; |
| 10975 | } |
| 10976 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 10977 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 10978 | * adds an optional offset found in args[0] and emits a string representing |
| 10979 | * the date in RFC-1123/5322 format. |
| 10980 | */ |
| 10981 | static int sample_conv_http_date(const struct arg *args, struct sample *smp) |
| 10982 | { |
| 10983 | const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; |
| 10984 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 10985 | struct chunk *temp; |
| 10986 | struct tm *tm; |
| 10987 | time_t curr_date = smp->data.uint; |
| 10988 | |
| 10989 | /* add offset */ |
| 10990 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 10991 | curr_date += args[0].data.sint; |
| 10992 | |
| 10993 | tm = gmtime(&curr_date); |
| 10994 | |
| 10995 | temp = get_trash_chunk(); |
| 10996 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 10997 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 10998 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 10999 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11000 | |
| 11001 | smp->data.str = *temp; |
| 11002 | smp->type = SMP_T_STR; |
| 11003 | return 1; |
| 11004 | } |
| 11005 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11006 | /* Match language range with language tag. RFC2616 14.4: |
| 11007 | * |
| 11008 | * A language-range matches a language-tag if it exactly equals |
| 11009 | * the tag, or if it exactly equals a prefix of the tag such |
| 11010 | * that the first tag character following the prefix is "-". |
| 11011 | * |
| 11012 | * Return 1 if the strings match, else return 0. |
| 11013 | */ |
| 11014 | static inline int language_range_match(const char *range, int range_len, |
| 11015 | const char *tag, int tag_len) |
| 11016 | { |
| 11017 | const char *end = range + range_len; |
| 11018 | const char *tend = tag + tag_len; |
| 11019 | while (range < end) { |
| 11020 | if (*range == '-' && tag == tend) |
| 11021 | return 1; |
| 11022 | if (*range != *tag || tag == tend) |
| 11023 | return 0; |
| 11024 | range++; |
| 11025 | tag++; |
| 11026 | } |
| 11027 | /* Return true only if the last char of the tag is matched. */ |
| 11028 | return tag == tend; |
| 11029 | } |
| 11030 | |
| 11031 | /* Arguments: The list of expected value, the number of parts returned and the separator */ |
| 11032 | static int sample_conv_q_prefered(const struct arg *args, struct sample *smp) |
| 11033 | { |
| 11034 | const char *al = smp->data.str.str; |
| 11035 | const char *end = al + smp->data.str.len; |
| 11036 | const char *token; |
| 11037 | int toklen; |
| 11038 | int qvalue; |
| 11039 | const char *str; |
| 11040 | const char *w; |
| 11041 | int best_q = 0; |
| 11042 | |
| 11043 | /* Set the constant to the sample, because the output of the |
| 11044 | * function will be peek in the constant configuration string. |
| 11045 | */ |
| 11046 | smp->flags |= SMP_F_CONST; |
| 11047 | smp->data.str.size = 0; |
| 11048 | smp->data.str.str = ""; |
| 11049 | smp->data.str.len = 0; |
| 11050 | |
| 11051 | /* Parse the accept language */ |
| 11052 | while (1) { |
| 11053 | |
| 11054 | /* Jump spaces, quit if the end is detected. */ |
| 11055 | while (al < end && isspace(*al)) |
| 11056 | al++; |
| 11057 | if (al >= end) |
| 11058 | break; |
| 11059 | |
| 11060 | /* Start of the fisrt word. */ |
| 11061 | token = al; |
| 11062 | |
| 11063 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
| 11064 | while (al < end && *al != ';' && *al != ',' && !isspace(*al)) |
| 11065 | al++; |
| 11066 | if (al == token) |
| 11067 | goto expect_comma; |
| 11068 | |
| 11069 | /* Length of the token. */ |
| 11070 | toklen = al - token; |
| 11071 | qvalue = 1000; |
| 11072 | |
| 11073 | /* Check if the token exists in the list. If the token not exists, |
| 11074 | * jump to the next token. |
| 11075 | */ |
| 11076 | str = args[0].data.str.str; |
| 11077 | w = str; |
| 11078 | while (1) { |
| 11079 | if (*str == ';' || *str == '\0') { |
| 11080 | if (language_range_match(token, toklen, w, str-w)) |
| 11081 | goto look_for_q; |
| 11082 | if (*str == '\0') |
| 11083 | goto expect_comma; |
| 11084 | w = str + 1; |
| 11085 | } |
| 11086 | str++; |
| 11087 | } |
| 11088 | goto expect_comma; |
| 11089 | |
| 11090 | look_for_q: |
| 11091 | |
| 11092 | /* Jump spaces, quit if the end is detected. */ |
| 11093 | while (al < end && isspace(*al)) |
| 11094 | al++; |
| 11095 | if (al >= end) |
| 11096 | goto process_value; |
| 11097 | |
| 11098 | /* If ',' is found, process the result */ |
| 11099 | if (*al == ',') |
| 11100 | goto process_value; |
| 11101 | |
| 11102 | /* If the character is different from ';', look |
| 11103 | * for the end of the header part in best effort. |
| 11104 | */ |
| 11105 | if (*al != ';') |
| 11106 | goto expect_comma; |
| 11107 | |
| 11108 | /* Assumes that the char is ';', now expect "q=". */ |
| 11109 | al++; |
| 11110 | |
| 11111 | /* Jump spaces, process value if the end is detected. */ |
| 11112 | while (al < end && isspace(*al)) |
| 11113 | al++; |
| 11114 | if (al >= end) |
| 11115 | goto process_value; |
| 11116 | |
| 11117 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11118 | if (*al != 'q') |
| 11119 | goto process_value; |
| 11120 | al++; |
| 11121 | |
| 11122 | /* Jump spaces, process value if the end is detected. */ |
| 11123 | while (al < end && isspace(*al)) |
| 11124 | al++; |
| 11125 | if (al >= end) |
| 11126 | goto process_value; |
| 11127 | |
| 11128 | /* Expect '='. If no '=', continue in best effort */ |
| 11129 | if (*al != '=') |
| 11130 | goto process_value; |
| 11131 | al++; |
| 11132 | |
| 11133 | /* Jump spaces, process value if the end is detected. */ |
| 11134 | while (al < end && isspace(*al)) |
| 11135 | al++; |
| 11136 | if (al >= end) |
| 11137 | goto process_value; |
| 11138 | |
| 11139 | /* Parse the q value. */ |
| 11140 | qvalue = parse_qvalue(al, &al); |
| 11141 | |
| 11142 | process_value: |
| 11143 | |
| 11144 | /* If the new q value is the best q value, then store the associated |
| 11145 | * language in the response. If qvalue is the biggest value (1000), |
| 11146 | * break the process. |
| 11147 | */ |
| 11148 | if (qvalue > best_q) { |
| 11149 | smp->data.str.str = (char *)w; |
| 11150 | smp->data.str.len = str - w; |
| 11151 | if (qvalue >= 1000) |
| 11152 | break; |
| 11153 | best_q = qvalue; |
| 11154 | } |
| 11155 | |
| 11156 | expect_comma: |
| 11157 | |
| 11158 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11159 | while (al < end && *al != ',') |
| 11160 | al++; |
| 11161 | if (al >= end) |
| 11162 | break; |
| 11163 | |
| 11164 | /* Comma is found, jump it and restart the analyzer. */ |
| 11165 | al++; |
| 11166 | } |
| 11167 | |
| 11168 | /* Set default value if required. */ |
| 11169 | if (smp->data.str.len == 0 && args[1].type == ARGT_STR) { |
| 11170 | smp->data.str.str = args[1].data.str.str; |
| 11171 | smp->data.str.len = args[1].data.str.len; |
| 11172 | } |
| 11173 | |
| 11174 | /* Return true only if a matching language was found. */ |
| 11175 | return smp->data.str.len != 0; |
| 11176 | } |
| 11177 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 11178 | /* |
| 11179 | * Return the struct http_req_action_kw associated to a keyword. |
| 11180 | */ |
| 11181 | struct http_req_action_kw *action_http_req_custom(const char *kw) |
| 11182 | { |
| 11183 | if (!LIST_ISEMPTY(&http_req_keywords.list)) { |
| 11184 | struct http_req_action_kw_list *kw_list; |
| 11185 | int i; |
| 11186 | |
| 11187 | list_for_each_entry(kw_list, &http_req_keywords.list, list) { |
| 11188 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11189 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11190 | return &kw_list->kw[i]; |
| 11191 | } |
| 11192 | } |
| 11193 | } |
| 11194 | return NULL; |
| 11195 | } |
| 11196 | |
| 11197 | /* |
| 11198 | * Return the struct http_res_action_kw associated to a keyword. |
| 11199 | */ |
| 11200 | struct http_res_action_kw *action_http_res_custom(const char *kw) |
| 11201 | { |
| 11202 | if (!LIST_ISEMPTY(&http_res_keywords.list)) { |
| 11203 | struct http_res_action_kw_list *kw_list; |
| 11204 | int i; |
| 11205 | |
| 11206 | list_for_each_entry(kw_list, &http_res_keywords.list, list) { |
| 11207 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11208 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11209 | return &kw_list->kw[i]; |
| 11210 | } |
| 11211 | } |
| 11212 | } |
| 11213 | return NULL; |
| 11214 | } |
| 11215 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11216 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11217 | /* All supported ACL keywords must be declared here. */ |
| 11218 | /************************************************************************/ |
| 11219 | |
| 11220 | /* Note: must not be declared <const> as its list will be overwritten. |
| 11221 | * Please take care of keeping this list alphabetically sorted. |
| 11222 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11223 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11224 | { "base", "base", PAT_MATCH_STR }, |
| 11225 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 11226 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 11227 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 11228 | { "base_end", "base", PAT_MATCH_END }, |
| 11229 | { "base_len", "base", PAT_MATCH_LEN }, |
| 11230 | { "base_reg", "base", PAT_MATCH_REG }, |
| 11231 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11232 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11233 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 11234 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 11235 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 11236 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 11237 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 11238 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 11239 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 11240 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11241 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11242 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 11243 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 11244 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 11245 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 11246 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 11247 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 11248 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 11249 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11250 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11251 | /* these two declarations uses strings with list storage (in place |
| 11252 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 11253 | * and delete functions are relative to the list management. The parse |
| 11254 | * and match method are related to the corresponding fetch methods. This |
| 11255 | * is very particular ACL declaration mode. |
| 11256 | */ |
| 11257 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 11258 | { "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] | 11259 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11260 | { "path", "path", PAT_MATCH_STR }, |
| 11261 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 11262 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 11263 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 11264 | { "path_end", "path", PAT_MATCH_END }, |
| 11265 | { "path_len", "path", PAT_MATCH_LEN }, |
| 11266 | { "path_reg", "path", PAT_MATCH_REG }, |
| 11267 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11268 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11269 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 11270 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11271 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11272 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 11273 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 11274 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 11275 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 11276 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 11277 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 11278 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 11279 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11280 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11281 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 11282 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 11283 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 11284 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 11285 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 11286 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 11287 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 11288 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11289 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11290 | { "url", "url", PAT_MATCH_STR }, |
| 11291 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 11292 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 11293 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 11294 | { "url_end", "url", PAT_MATCH_END }, |
| 11295 | { "url_len", "url", PAT_MATCH_LEN }, |
| 11296 | { "url_reg", "url", PAT_MATCH_REG }, |
| 11297 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11298 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11299 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 11300 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 11301 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 11302 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 11303 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 11304 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 11305 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 11306 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11307 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 11308 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11309 | }}; |
| 11310 | |
| 11311 | /************************************************************************/ |
| 11312 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11313 | /************************************************************************/ |
| 11314 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11315 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11316 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11317 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11318 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 11319 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11320 | /* capture are allocated and are permanent in the session */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11321 | { "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] | 11322 | |
| 11323 | /* retrieve these captures from the HTTP logs */ |
| 11324 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11325 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11326 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 11327 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11328 | { "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] | 11329 | { "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] | 11330 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11331 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 11332 | * are only here to match the ACL's name, are request-only and are used |
| 11333 | * for ACL compatibility only. |
| 11334 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11335 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11336 | { "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] | 11337 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11338 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11339 | |
| 11340 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 11341 | * only here to match the ACL's name, are request-only and are used for |
| 11342 | * ACL compatibility only. |
| 11343 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11344 | { "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] | 11345 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11346 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11347 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11348 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 11349 | { "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] | 11350 | { "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] | 11351 | { "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] | 11352 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11353 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11354 | |
| 11355 | /* HTTP protocol on the request path */ |
| 11356 | { "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] | 11357 | { "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] | 11358 | |
| 11359 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11360 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11361 | { "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] | 11362 | |
| 11363 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11364 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 11365 | { "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] | 11366 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 11367 | /* 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] | 11368 | { "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] | 11369 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11370 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11371 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11372 | { "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] | 11373 | { "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] | 11374 | { "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] | 11375 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11376 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11377 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11378 | |
| 11379 | /* 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] | 11380 | { "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] | 11381 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11382 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11383 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11384 | { "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] | 11385 | { "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] | 11386 | { "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] | 11387 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11388 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 11389 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11390 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 11391 | /* 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] | 11392 | { "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] | 11393 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11394 | { "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] | 11395 | { "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] | 11396 | |
| 11397 | /* 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] | 11398 | { "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] | 11399 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11400 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 11401 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 11402 | |
| 11403 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11404 | { "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] | 11405 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11406 | { "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] | 11407 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 11408 | { "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] | 11409 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 11410 | { "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] | 11411 | { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 11412 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11413 | }}; |
| 11414 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11415 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11416 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 11417 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11418 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR}, |
| 11419 | { "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] | 11420 | { NULL, NULL, 0, 0, 0 }, |
| 11421 | }}; |
| 11422 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11423 | __attribute__((constructor)) |
| 11424 | static void __http_protocol_init(void) |
| 11425 | { |
| 11426 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11427 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11428 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11429 | } |
| 11430 | |
| 11431 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 11432 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 11433 | * Local variables: |
| 11434 | * c-indent-level: 8 |
| 11435 | * c-basic-offset: 8 |
| 11436 | * End: |
| 11437 | */ |