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> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 51 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 53 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 55 | #include <proto/hdr_idx.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 56 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 58 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 60 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 61 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 63 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | #include <proto/task.h> |
| 65 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 66 | const char HTTP_100[] = |
| 67 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 68 | |
| 69 | const struct chunk http_100_chunk = { |
| 70 | .str = (char *)&HTTP_100, |
| 71 | .len = sizeof(HTTP_100)-1 |
| 72 | }; |
| 73 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 74 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 75 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 76 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 77 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 78 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 79 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 80 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 81 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 82 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 83 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 84 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 85 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 86 | |
| 87 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 88 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 89 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 90 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 91 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 92 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 93 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 94 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 95 | const char *HTTP_401_fmt = |
| 96 | "HTTP/1.0 401 Unauthorized\r\n" |
| 97 | "Cache-Control: no-cache\r\n" |
| 98 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 99 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 100 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 101 | "\r\n" |
| 102 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 103 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 104 | const char *HTTP_407_fmt = |
| 105 | "HTTP/1.0 407 Unauthorized\r\n" |
| 106 | "Cache-Control: no-cache\r\n" |
| 107 | "Connection: close\r\n" |
| 108 | "Content-Type: text/html\r\n" |
| 109 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 110 | "\r\n" |
| 111 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 112 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 113 | |
| 114 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 115 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 116 | [HTTP_ERR_400] = 400, |
| 117 | [HTTP_ERR_403] = 403, |
| 118 | [HTTP_ERR_408] = 408, |
| 119 | [HTTP_ERR_500] = 500, |
| 120 | [HTTP_ERR_502] = 502, |
| 121 | [HTTP_ERR_503] = 503, |
| 122 | [HTTP_ERR_504] = 504, |
| 123 | }; |
| 124 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 125 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 126 | [HTTP_ERR_200] = |
| 127 | "HTTP/1.0 200 OK\r\n" |
| 128 | "Cache-Control: no-cache\r\n" |
| 129 | "Connection: close\r\n" |
| 130 | "Content-Type: text/html\r\n" |
| 131 | "\r\n" |
| 132 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 133 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 134 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 135 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 136 | "Cache-Control: no-cache\r\n" |
| 137 | "Connection: close\r\n" |
| 138 | "Content-Type: text/html\r\n" |
| 139 | "\r\n" |
| 140 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 141 | |
| 142 | [HTTP_ERR_403] = |
| 143 | "HTTP/1.0 403 Forbidden\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>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 149 | |
| 150 | [HTTP_ERR_408] = |
| 151 | "HTTP/1.0 408 Request Time-out\r\n" |
| 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>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 157 | |
| 158 | [HTTP_ERR_500] = |
| 159 | "HTTP/1.0 500 Server Error\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>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 165 | |
| 166 | [HTTP_ERR_502] = |
| 167 | "HTTP/1.0 502 Bad Gateway\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>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 173 | |
| 174 | [HTTP_ERR_503] = |
| 175 | "HTTP/1.0 503 Service Unavailable\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>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 181 | |
| 182 | [HTTP_ERR_504] = |
| 183 | "HTTP/1.0 504 Gateway Time-out\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>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 189 | |
| 190 | }; |
| 191 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 192 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 193 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 194 | [STAT_STATUS_DENY] = "DENY", |
| 195 | [STAT_STATUS_DONE] = "DONE", |
| 196 | [STAT_STATUS_ERRP] = "ERRP", |
| 197 | [STAT_STATUS_EXCD] = "EXCD", |
| 198 | [STAT_STATUS_NONE] = "NONE", |
| 199 | [STAT_STATUS_PART] = "PART", |
| 200 | [STAT_STATUS_UNKN] = "UNKN", |
| 201 | }; |
| 202 | |
| 203 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 204 | /* We must put the messages here since GCC cannot initialize consts depending |
| 205 | * on strlen(). |
| 206 | */ |
| 207 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 208 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 209 | #define FD_SETS_ARE_BITFIELDS |
| 210 | #ifdef FD_SETS_ARE_BITFIELDS |
| 211 | /* |
| 212 | * This map is used with all the FD_* macros to check whether a particular bit |
| 213 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 214 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 215 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 216 | * exclusively to the macros. |
| 217 | */ |
| 218 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 219 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 220 | |
| 221 | #else |
| 222 | #error "Check if your OS uses bitfields for fd_sets" |
| 223 | #endif |
| 224 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 225 | void init_proto_http() |
| 226 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 227 | int i; |
| 228 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 229 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 230 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 231 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 232 | if (!http_err_msgs[msg]) { |
| 233 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 234 | abort(); |
| 235 | } |
| 236 | |
| 237 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 238 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 239 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 240 | |
| 241 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 242 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 243 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 244 | * printable characters above. |
| 245 | */ |
| 246 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 247 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 248 | for (i = 0; i < 32; i++) { |
| 249 | FD_SET(i, hdr_encode_map); |
| 250 | FD_SET(i, url_encode_map); |
| 251 | } |
| 252 | for (i = 127; i < 256; i++) { |
| 253 | FD_SET(i, hdr_encode_map); |
| 254 | FD_SET(i, url_encode_map); |
| 255 | } |
| 256 | |
| 257 | tmp = "\"#{|}"; |
| 258 | while (*tmp) { |
| 259 | FD_SET(*tmp, hdr_encode_map); |
| 260 | tmp++; |
| 261 | } |
| 262 | |
| 263 | tmp = "\"#"; |
| 264 | while (*tmp) { |
| 265 | FD_SET(*tmp, url_encode_map); |
| 266 | tmp++; |
| 267 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 268 | |
| 269 | /* memory allocations */ |
| 270 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 271 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 272 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 273 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 274 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 275 | /* |
| 276 | * We have 26 list of methods (1 per first letter), each of which can have |
| 277 | * up to 3 entries (2 valid, 1 null). |
| 278 | */ |
| 279 | struct http_method_desc { |
| 280 | http_meth_t meth; |
| 281 | int len; |
| 282 | const char text[8]; |
| 283 | }; |
| 284 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 285 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 286 | ['C' - 'A'] = { |
| 287 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 288 | }, |
| 289 | ['D' - 'A'] = { |
| 290 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 291 | }, |
| 292 | ['G' - 'A'] = { |
| 293 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 294 | }, |
| 295 | ['H' - 'A'] = { |
| 296 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 297 | }, |
| 298 | ['P' - 'A'] = { |
| 299 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 300 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 301 | }, |
| 302 | ['T' - 'A'] = { |
| 303 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 304 | }, |
| 305 | /* rest is empty like this : |
| 306 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 307 | */ |
| 308 | }; |
| 309 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 310 | /* 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] | 311 | * 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] | 312 | * RFC2616 for those chars. |
| 313 | */ |
| 314 | |
| 315 | const char http_is_spht[256] = { |
| 316 | [' '] = 1, ['\t'] = 1, |
| 317 | }; |
| 318 | |
| 319 | const char http_is_crlf[256] = { |
| 320 | ['\r'] = 1, ['\n'] = 1, |
| 321 | }; |
| 322 | |
| 323 | const char http_is_lws[256] = { |
| 324 | [' '] = 1, ['\t'] = 1, |
| 325 | ['\r'] = 1, ['\n'] = 1, |
| 326 | }; |
| 327 | |
| 328 | const char http_is_sep[256] = { |
| 329 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 330 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 331 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 332 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 333 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 334 | }; |
| 335 | |
| 336 | const char http_is_ctl[256] = { |
| 337 | [0 ... 31] = 1, |
| 338 | [127] = 1, |
| 339 | }; |
| 340 | |
| 341 | /* |
| 342 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 343 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 344 | * them correctly. Instead, define every non-CTL char's status. |
| 345 | */ |
| 346 | const char http_is_token[256] = { |
| 347 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 348 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 349 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 350 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 351 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 352 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 353 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 354 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 355 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 356 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 357 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 358 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 359 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 360 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 361 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 362 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 363 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 364 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 365 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 366 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 367 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 368 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 369 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 370 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 371 | }; |
| 372 | |
| 373 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 374 | /* |
| 375 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 376 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 377 | */ |
| 378 | const char http_is_ver_token[256] = { |
| 379 | ['.'] = 1, ['/'] = 1, |
| 380 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 381 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 382 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 383 | }; |
| 384 | |
| 385 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 386 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 387 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 388 | */ |
| 389 | #if defined(DEBUG_FSM) |
| 390 | static void http_silent_debug(int line, struct session *s) |
| 391 | { |
| 392 | int size = 0; |
David du Colombier | 7af4605 | 2012-05-16 14:16:48 +0200 | [diff] [blame] | 393 | size += snprintf(trash + size, trashlen - size, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 394 | "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n", |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 395 | line, |
| 396 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 397 | s->req->buf.data, s->req->buf.size, s->req->l, s->req->w, s->req->r, s->req->buf.p, s->req->buf.o, s->req->to_forward, s->txn.flags); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 398 | write(-1, trash, size); |
| 399 | size = 0; |
David du Colombier | 7af4605 | 2012-05-16 14:16:48 +0200 | [diff] [blame] | 400 | size += snprintf(trash + size, trashlen - size, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 401 | " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n", |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 402 | line, |
| 403 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 404 | s->rep->buf.data, s->rep->buf.size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf.p, s->rep->buf.o, s->rep->to_forward); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 405 | |
| 406 | write(-1, trash, size); |
| 407 | } |
| 408 | #else |
| 409 | #define http_silent_debug(l,s) do { } while (0) |
| 410 | #endif |
| 411 | |
| 412 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 413 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 414 | * 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] | 415 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 416 | * of headers is automatically adjusted. The number of bytes added is returned |
| 417 | * on success, otherwise <0 is returned indicating an error. |
| 418 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 419 | 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] | 420 | { |
| 421 | int bytes, len; |
| 422 | |
| 423 | len = strlen(text); |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 424 | 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] | 425 | if (!bytes) |
| 426 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 427 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 428 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 429 | } |
| 430 | |
| 431 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 432 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 433 | * 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] | 434 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 435 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 436 | * of headers is automatically adjusted. The number of bytes added is returned |
| 437 | * on success, otherwise <0 is returned indicating an error. |
| 438 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 439 | int http_header_add_tail2(struct http_msg *msg, |
| 440 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 441 | { |
| 442 | int bytes; |
| 443 | |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 444 | 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] | 445 | if (!bytes) |
| 446 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 447 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 448 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 449 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 450 | |
| 451 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 452 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 453 | * If so, returns the position of the first non-space character relative to |
| 454 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 455 | * to return a pointer to the place after the first space. Returns 0 if the |
| 456 | * header name does not match. Checks are case-insensitive. |
| 457 | */ |
| 458 | int http_header_match2(const char *hdr, const char *end, |
| 459 | const char *name, int len) |
| 460 | { |
| 461 | const char *val; |
| 462 | |
| 463 | if (hdr + len >= end) |
| 464 | return 0; |
| 465 | if (hdr[len] != ':') |
| 466 | return 0; |
| 467 | if (strncasecmp(hdr, name, len) != 0) |
| 468 | return 0; |
| 469 | val = hdr + len + 1; |
| 470 | while (val < end && HTTP_IS_SPHT(*val)) |
| 471 | val++; |
| 472 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 473 | return len + 2; /* we may replace starting from second space */ |
| 474 | return val - hdr; |
| 475 | } |
| 476 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 477 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 478 | * par 2.2 for more information. Note that it requires a valid header to return |
| 479 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 480 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 481 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 482 | { |
| 483 | int quoted, qdpair; |
| 484 | |
| 485 | quoted = qdpair = 0; |
| 486 | for (; s < e; s++) { |
| 487 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 488 | else if (quoted) { |
| 489 | if (*s == '\\') qdpair = 1; |
| 490 | else if (*s == '"') quoted = 0; |
| 491 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 492 | else if (*s == '"') quoted = 1; |
| 493 | else if (*s == ',') return s; |
| 494 | } |
| 495 | return s; |
| 496 | } |
| 497 | |
| 498 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 499 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 500 | * structure holds everything necessary to use the header and find next |
| 501 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 502 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 503 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 504 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 505 | * is NULL when searching for a new values of a header, the current header is |
| 506 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 507 | */ |
| 508 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 509 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 510 | struct hdr_ctx *ctx) |
| 511 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 512 | char *eol, *sov; |
| 513 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 514 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 515 | cur_idx = ctx->idx; |
| 516 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 517 | /* We have previously returned a value, let's search |
| 518 | * another one on the same line. |
| 519 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 520 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 521 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 522 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 523 | eol = sol + idx->v[cur_idx].len; |
| 524 | |
| 525 | if (sov >= eol) |
| 526 | /* no more values in this header */ |
| 527 | goto next_hdr; |
| 528 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 529 | /* values remaining for this header, skip the comma but save it |
| 530 | * for later use (eg: for header deletion). |
| 531 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 532 | sov++; |
| 533 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 534 | sov++; |
| 535 | |
| 536 | goto return_hdr; |
| 537 | } |
| 538 | |
| 539 | /* first request for this header */ |
| 540 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 541 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 542 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 543 | while (cur_idx) { |
| 544 | eol = sol + idx->v[cur_idx].len; |
| 545 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 546 | if (len == 0) { |
| 547 | /* No argument was passed, we want any header. |
| 548 | * To achieve this, we simply build a fake request. */ |
| 549 | while (sol + len < eol && sol[len] != ':') |
| 550 | len++; |
| 551 | name = sol; |
| 552 | } |
| 553 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 554 | if ((len < eol - sol) && |
| 555 | (sol[len] == ':') && |
| 556 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 557 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 558 | sov = sol + len + 1; |
| 559 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 560 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 561 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 562 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 563 | ctx->prev = old_idx; |
| 564 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 565 | ctx->idx = cur_idx; |
| 566 | ctx->val = sov - sol; |
| 567 | |
| 568 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 569 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 570 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 571 | eol--; |
| 572 | ctx->tws++; |
| 573 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 574 | ctx->vlen = eol - sov; |
| 575 | return 1; |
| 576 | } |
| 577 | next_hdr: |
| 578 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 579 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 580 | cur_idx = idx->v[cur_idx].next; |
| 581 | } |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 586 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 587 | struct hdr_ctx *ctx) |
| 588 | { |
| 589 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 590 | } |
| 591 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 592 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 593 | * the http_find_header functions. The value is removed, as well as surrounding |
| 594 | * 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] | 595 | * 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] | 596 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 597 | * no more header, so any processing may stop. The ctx is always left in a form |
| 598 | * that can be handled by http_find_header2() to find next occurrence. |
| 599 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 600 | 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] | 601 | { |
| 602 | int cur_idx = ctx->idx; |
| 603 | char *sol = ctx->line; |
| 604 | struct hdr_idx_elem *hdr; |
| 605 | int delta, skip_comma; |
| 606 | |
| 607 | if (!cur_idx) |
| 608 | return 0; |
| 609 | |
| 610 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 611 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 612 | /* This was the only value of the header, we must now remove it entirely. */ |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 613 | 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] | 614 | http_msg_move_end(msg, delta); |
| 615 | idx->used--; |
| 616 | hdr->len = 0; /* unused entry */ |
| 617 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 618 | if (idx->tail == ctx->idx) |
| 619 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 620 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 621 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 622 | 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] | 623 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 624 | return ctx->idx; |
| 625 | } |
| 626 | |
| 627 | /* 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] | 628 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 629 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 630 | */ |
| 631 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 632 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 633 | delta = buffer_replace2(&msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 634 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 635 | NULL, 0); |
| 636 | hdr->len += delta; |
| 637 | http_msg_move_end(msg, delta); |
| 638 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 639 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 640 | return ctx->idx; |
| 641 | } |
| 642 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 643 | /* This function handles a server error at the stream interface level. The |
| 644 | * stream interface is assumed to be already in a closed state. An optional |
| 645 | * message is copied into the input buffer, and an HTTP status code stored. |
| 646 | * 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] | 647 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 648 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 649 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 650 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 651 | { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 652 | channel_auto_read(si->ob); |
| 653 | channel_abort(si->ob); |
| 654 | channel_auto_close(si->ob); |
| 655 | channel_erase(si->ob); |
| 656 | channel_auto_close(si->ib); |
| 657 | channel_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 658 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 659 | t->txn.status = status; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 660 | bo_inject(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 661 | } |
| 662 | if (!(t->flags & SN_ERR_MASK)) |
| 663 | t->flags |= err; |
| 664 | if (!(t->flags & SN_FINST_MASK)) |
| 665 | t->flags |= finst; |
| 666 | } |
| 667 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 668 | /* This function returns the appropriate error location for the given session |
| 669 | * and message. |
| 670 | */ |
| 671 | |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 672 | struct chunk *http_error_message(struct session *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 673 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 674 | if (s->be->errmsg[msgnum].str) |
| 675 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 676 | else if (s->fe->errmsg[msgnum].str) |
| 677 | return &s->fe->errmsg[msgnum]; |
| 678 | else |
| 679 | return &http_err_chunks[msgnum]; |
| 680 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 681 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 682 | /* |
| 683 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 684 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 685 | */ |
| 686 | static http_meth_t find_http_meth(const char *str, const int len) |
| 687 | { |
| 688 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 689 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 690 | |
| 691 | m = ((unsigned)*str - 'A'); |
| 692 | |
| 693 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 694 | for (h = http_methods[m]; h->len > 0; h++) { |
| 695 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 696 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 697 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 698 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 699 | }; |
| 700 | return HTTP_METH_OTHER; |
| 701 | } |
| 702 | return HTTP_METH_NONE; |
| 703 | |
| 704 | } |
| 705 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 706 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 707 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 708 | * It is returned otherwise. |
| 709 | */ |
| 710 | static char * |
| 711 | http_get_path(struct http_txn *txn) |
| 712 | { |
| 713 | char *ptr, *end; |
| 714 | |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 715 | ptr = txn->req.chn->buf.p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 716 | end = ptr + txn->req.sl.rq.u_l; |
| 717 | |
| 718 | if (ptr >= end) |
| 719 | return NULL; |
| 720 | |
| 721 | /* RFC2616, par. 5.1.2 : |
| 722 | * Request-URI = "*" | absuri | abspath | authority |
| 723 | */ |
| 724 | |
| 725 | if (*ptr == '*') |
| 726 | return NULL; |
| 727 | |
| 728 | if (isalpha((unsigned char)*ptr)) { |
| 729 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 730 | ptr++; |
| 731 | while (ptr < end && |
| 732 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 733 | ptr++; |
| 734 | /* skip '://' */ |
| 735 | if (ptr == end || *ptr++ != ':') |
| 736 | return NULL; |
| 737 | if (ptr == end || *ptr++ != '/') |
| 738 | return NULL; |
| 739 | if (ptr == end || *ptr++ != '/') |
| 740 | return NULL; |
| 741 | } |
| 742 | /* skip [user[:passwd]@]host[:[port]] */ |
| 743 | |
| 744 | while (ptr < end && *ptr != '/') |
| 745 | ptr++; |
| 746 | |
| 747 | if (ptr == end) |
| 748 | return NULL; |
| 749 | |
| 750 | /* OK, we got the '/' ! */ |
| 751 | return ptr; |
| 752 | } |
| 753 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 754 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 755 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 756 | * left unchanged and will follow normal proxy processing. |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 757 | * NOTE: this function is designed to support being called once data are scheduled |
| 758 | * for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 759 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 760 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 761 | { |
| 762 | struct http_txn *txn; |
| 763 | struct chunk rdr; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 764 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 765 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 766 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 767 | |
| 768 | /* 1: create the response header */ |
| 769 | rdr.len = strlen(HTTP_302); |
| 770 | rdr.str = trash; |
David du Colombier | 7af4605 | 2012-05-16 14:16:48 +0200 | [diff] [blame] | 771 | rdr.size = trashlen; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 772 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 773 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 774 | srv = target_srv(&s->target); |
| 775 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 776 | /* 2: add the server's prefix */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 777 | if (rdr.len + srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 778 | return; |
| 779 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 780 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 781 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 782 | memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len); |
| 783 | rdr.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 784 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 785 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 786 | /* 3: add the request URI. Since it was already forwarded, we need |
| 787 | * to temporarily rewind the buffer. |
| 788 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 789 | txn = &s->txn; |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 790 | b_rew(&s->req->buf, rewind = s->req->buf.o); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 791 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 792 | path = http_get_path(txn); |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 793 | 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] | 794 | |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 795 | b_adv(&s->req->buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 796 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 797 | if (!path) |
| 798 | return; |
| 799 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 800 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 801 | return; |
| 802 | |
| 803 | memcpy(rdr.str + rdr.len, path, len); |
| 804 | rdr.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 805 | |
| 806 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 807 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 808 | rdr.len += 29; |
| 809 | } else { |
| 810 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 811 | rdr.len += 23; |
| 812 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 813 | |
| 814 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 815 | si_shutr(si); |
| 816 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 817 | si->err_type = SI_ET_NONE; |
| 818 | si->err_loc = NULL; |
| 819 | si->state = SI_ST_CLO; |
| 820 | |
| 821 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 822 | http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 823 | |
| 824 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 825 | if (srv) |
| 826 | srv_inc_sess_ctr(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 827 | } |
| 828 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 829 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 830 | * that the server side is closed. Note that err_type is actually a |
| 831 | * bitmask, where almost only aborts may be cumulated with other |
| 832 | * values. We consider that aborted operations are more important |
| 833 | * than timeouts or errors due to the fact that nobody else in the |
| 834 | * logs might explain incomplete retries. All others should avoid |
| 835 | * being cumulated. It should normally not be possible to have multiple |
| 836 | * aborts at once, but just in case, the first one in sequence is reported. |
| 837 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 838 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 839 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 840 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 841 | |
| 842 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 843 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 844 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 845 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 846 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 847 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 848 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 849 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 850 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 851 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 852 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 853 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 854 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 855 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 856 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 857 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 858 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 859 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 860 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 861 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 862 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 863 | } |
| 864 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 865 | extern const char sess_term_cond[8]; |
| 866 | extern const char sess_fin_state[8]; |
| 867 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 868 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 869 | struct pool_head *pool2_capture; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 870 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 871 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 872 | /* |
| 873 | * Capture headers from message starting at <som> according to header list |
| 874 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 875 | */ |
| 876 | void capture_headers(char *som, struct hdr_idx *idx, |
| 877 | char **cap, struct cap_hdr *cap_hdr) |
| 878 | { |
| 879 | char *eol, *sol, *col, *sov; |
| 880 | int cur_idx; |
| 881 | struct cap_hdr *h; |
| 882 | int len; |
| 883 | |
| 884 | sol = som + hdr_idx_first_pos(idx); |
| 885 | cur_idx = hdr_idx_first_idx(idx); |
| 886 | |
| 887 | while (cur_idx) { |
| 888 | eol = sol + idx->v[cur_idx].len; |
| 889 | |
| 890 | col = sol; |
| 891 | while (col < eol && *col != ':') |
| 892 | col++; |
| 893 | |
| 894 | sov = col + 1; |
| 895 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 896 | sov++; |
| 897 | |
| 898 | for (h = cap_hdr; h; h = h->next) { |
| 899 | if ((h->namelen == col - sol) && |
| 900 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 901 | if (cap[h->index] == NULL) |
| 902 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 903 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 904 | |
| 905 | if (cap[h->index] == NULL) { |
| 906 | Alert("HTTP capture : out of memory.\n"); |
| 907 | continue; |
| 908 | } |
| 909 | |
| 910 | len = eol - sov; |
| 911 | if (len > h->len) |
| 912 | len = h->len; |
| 913 | |
| 914 | memcpy(cap[h->index], sov, len); |
| 915 | cap[h->index][len]=0; |
| 916 | } |
| 917 | } |
| 918 | sol = eol + idx->v[cur_idx].cr + 1; |
| 919 | cur_idx = idx->v[cur_idx].next; |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 924 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 925 | */ |
| 926 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 927 | |
| 928 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 929 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 930 | */ |
| 931 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 932 | ptr++; \ |
| 933 | if (likely(ptr < end)) \ |
| 934 | goto good; \ |
| 935 | else { \ |
| 936 | state = (st); \ |
| 937 | goto http_msg_ood; \ |
| 938 | } \ |
| 939 | } while (0) |
| 940 | |
| 941 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 942 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 943 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 944 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 945 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 946 | * will give undefined results. |
| 947 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 948 | * and that msg->sol points to the beginning of the response. |
| 949 | * If a complete line is found (which implies that at least one CR or LF is |
| 950 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 951 | * returned indicating an incomplete line (which does not mean that parts have |
| 952 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 953 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 954 | * upon next call. |
| 955 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 956 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 957 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 958 | * 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] | 959 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 960 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 961 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 962 | unsigned int state, const char *ptr, const char *end, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 963 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 964 | { |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 965 | const char *msg_start = msg->chn->buf.p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 966 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 967 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 968 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 969 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 970 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 971 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 972 | |
| 973 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 974 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 975 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 976 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 977 | state = HTTP_MSG_ERROR; |
| 978 | break; |
| 979 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 980 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 981 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 982 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 983 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 984 | goto http_msg_rpcode; |
| 985 | } |
| 986 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 987 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 988 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 989 | state = HTTP_MSG_ERROR; |
| 990 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 991 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 992 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 993 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 994 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 995 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 996 | |
| 997 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 998 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 999 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1000 | } |
| 1001 | |
| 1002 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1003 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1004 | http_msg_rsp_reason: |
| 1005 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1006 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1007 | msg->sl.st.r_l = 0; |
| 1008 | goto http_msg_rpline_eol; |
| 1009 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1010 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1011 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1012 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1013 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1014 | goto http_msg_rpreason; |
| 1015 | } |
| 1016 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1017 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1018 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1019 | goto http_msg_rsp_reason; |
| 1020 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1021 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1022 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1023 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1024 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1025 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1026 | http_msg_rpline_eol: |
| 1027 | /* We have seen the end of line. Note that we do not |
| 1028 | * necessarily have the \n yet, but at least we know that we |
| 1029 | * have EITHER \r OR \n, otherwise the response would not be |
| 1030 | * complete. We can then record the response length and return |
| 1031 | * to the caller which will be able to register it. |
| 1032 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1033 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1034 | return ptr; |
| 1035 | |
| 1036 | #ifdef DEBUG_FULL |
| 1037 | default: |
| 1038 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1039 | exit(1); |
| 1040 | #endif |
| 1041 | } |
| 1042 | |
| 1043 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1044 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1045 | if (ret_state) |
| 1046 | *ret_state = state; |
| 1047 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1048 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1049 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1050 | } |
| 1051 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1052 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1053 | * This function parses a request line between <ptr> and <end>, starting with |
| 1054 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1055 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1056 | * will give undefined results. |
| 1057 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1058 | * and that msg->sol points to the beginning of the request. |
| 1059 | * If a complete line is found (which implies that at least one CR or LF is |
| 1060 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1061 | * returned indicating an incomplete line (which does not mean that parts have |
| 1062 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1063 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1064 | * upon next call. |
| 1065 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1066 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1067 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1068 | * 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] | 1069 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1070 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1071 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1072 | unsigned int state, const char *ptr, const char *end, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1073 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1074 | { |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 1075 | const char *msg_start = msg->chn->buf.p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1076 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1077 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1078 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1079 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1080 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1081 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1082 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1083 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1084 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1085 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1086 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1087 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1088 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1089 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1090 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1091 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1092 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1093 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1094 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1095 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1096 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1097 | msg->sl.rq.v_l = 0; |
| 1098 | goto http_msg_rqline_eol; |
| 1099 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1100 | state = HTTP_MSG_ERROR; |
| 1101 | break; |
| 1102 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1103 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1104 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1105 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1106 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1107 | goto http_msg_rquri; |
| 1108 | } |
| 1109 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1110 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1111 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1112 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1113 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1114 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1115 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1116 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1117 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1118 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1119 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1120 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1121 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1122 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1123 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1124 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1125 | /* non-ASCII chars are forbidden unless option |
| 1126 | * accept-invalid-http-request is enabled in the frontend. |
| 1127 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1128 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1129 | if (msg->err_pos < -1) |
| 1130 | goto invalid_char; |
| 1131 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1132 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1133 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1134 | } |
| 1135 | |
| 1136 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1137 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1138 | goto http_msg_req09_uri_e; |
| 1139 | } |
| 1140 | |
| 1141 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1142 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1143 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1144 | state = HTTP_MSG_ERROR; |
| 1145 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1146 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1147 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1148 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1149 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1150 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1151 | goto http_msg_rqver; |
| 1152 | } |
| 1153 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1154 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1155 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1156 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1157 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1158 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1159 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1160 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1161 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1162 | |
| 1163 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1164 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1165 | http_msg_rqline_eol: |
| 1166 | /* We have seen the end of line. Note that we do not |
| 1167 | * necessarily have the \n yet, but at least we know that we |
| 1168 | * have EITHER \r OR \n, otherwise the request would not be |
| 1169 | * complete. We can then record the request length and return |
| 1170 | * to the caller which will be able to register it. |
| 1171 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1172 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1173 | return ptr; |
| 1174 | } |
| 1175 | |
| 1176 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1177 | state = HTTP_MSG_ERROR; |
| 1178 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1179 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1180 | #ifdef DEBUG_FULL |
| 1181 | default: |
| 1182 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1183 | exit(1); |
| 1184 | #endif |
| 1185 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1186 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1187 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1188 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1189 | if (ret_state) |
| 1190 | *ret_state = state; |
| 1191 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1192 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1193 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1194 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1195 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1196 | /* |
| 1197 | * Returns the data from Authorization header. Function may be called more |
| 1198 | * than once so data is stored in txn->auth_data. When no header is found |
| 1199 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1200 | * searching again for something we are unable to find anyway. |
| 1201 | */ |
| 1202 | |
| 1203 | char get_http_auth_buff[BUFSIZE]; |
| 1204 | |
| 1205 | int |
| 1206 | get_http_auth(struct session *s) |
| 1207 | { |
| 1208 | |
| 1209 | struct http_txn *txn = &s->txn; |
| 1210 | struct chunk auth_method; |
| 1211 | struct hdr_ctx ctx; |
| 1212 | char *h, *p; |
| 1213 | int len; |
| 1214 | |
| 1215 | #ifdef DEBUG_AUTH |
| 1216 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1217 | #endif |
| 1218 | |
| 1219 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1220 | return 0; |
| 1221 | |
| 1222 | if (txn->auth.method) |
| 1223 | return 1; |
| 1224 | |
| 1225 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1226 | |
| 1227 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1228 | |
| 1229 | if (txn->flags & TX_USE_PX_CONN) { |
| 1230 | h = "Proxy-Authorization"; |
| 1231 | len = strlen(h); |
| 1232 | } else { |
| 1233 | h = "Authorization"; |
| 1234 | len = strlen(h); |
| 1235 | } |
| 1236 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 1237 | 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] | 1238 | return 0; |
| 1239 | |
| 1240 | h = ctx.line + ctx.val; |
| 1241 | |
| 1242 | p = memchr(h, ' ', ctx.vlen); |
| 1243 | if (!p || p == h) |
| 1244 | return 0; |
| 1245 | |
| 1246 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1247 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1248 | |
| 1249 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1250 | |
| 1251 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
| 1252 | get_http_auth_buff, BUFSIZE - 1); |
| 1253 | |
| 1254 | if (len < 0) |
| 1255 | return 0; |
| 1256 | |
| 1257 | |
| 1258 | get_http_auth_buff[len] = '\0'; |
| 1259 | |
| 1260 | p = strchr(get_http_auth_buff, ':'); |
| 1261 | |
| 1262 | if (!p) |
| 1263 | return 0; |
| 1264 | |
| 1265 | txn->auth.user = get_http_auth_buff; |
| 1266 | *p = '\0'; |
| 1267 | txn->auth.pass = p+1; |
| 1268 | |
| 1269 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1270 | return 1; |
| 1271 | } |
| 1272 | |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1276 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1277 | /* |
| 1278 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1279 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1280 | * ensuring that the message does not wrap. The function can be preempted |
| 1281 | * everywhere when data are missing and recalled at the exact same location |
| 1282 | * with no information loss. The message may even be realigned between two |
| 1283 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1284 | * 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] | 1285 | * fields. Note that msg->sol will be initialized after completing the first |
| 1286 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1287 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1288 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1289 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1290 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1291 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1292 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1293 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1294 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1295 | state = msg->msg_state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1296 | buf = &msg->chn->buf; |
| 1297 | ptr = buf->p + msg->next; |
| 1298 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1299 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1300 | if (unlikely(ptr >= end)) |
| 1301 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1302 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1303 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1304 | /* |
| 1305 | * First, states that are specific to the response only. |
| 1306 | * We check them first so that request and headers are |
| 1307 | * closer to each other (accessed more often). |
| 1308 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1309 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1310 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1311 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1312 | /* we have a start of message, but we have to check |
| 1313 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1314 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1315 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1316 | if (unlikely(ptr != buf->p)) { |
| 1317 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1318 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1319 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1320 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1321 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1322 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1323 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1324 | hdr_idx_init(idx); |
| 1325 | state = HTTP_MSG_RPVER; |
| 1326 | goto http_msg_rpver; |
| 1327 | } |
| 1328 | |
| 1329 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1330 | goto http_msg_invalid; |
| 1331 | |
| 1332 | if (unlikely(*ptr == '\n')) |
| 1333 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1334 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1335 | /* stop here */ |
| 1336 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1337 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1338 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1339 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1340 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1341 | /* stop here */ |
| 1342 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1343 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1344 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1345 | case HTTP_MSG_RPVER_SP: |
| 1346 | case HTTP_MSG_RPCODE: |
| 1347 | case HTTP_MSG_RPCODE_SP: |
| 1348 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1349 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1350 | state, ptr, end, |
| 1351 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1352 | if (unlikely(!ptr)) |
| 1353 | return; |
| 1354 | |
| 1355 | /* we have a full response and we know that we have either a CR |
| 1356 | * or an LF at <ptr>. |
| 1357 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1358 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1359 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1360 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1361 | if (likely(*ptr == '\r')) |
| 1362 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1363 | goto http_msg_rpline_end; |
| 1364 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1365 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1366 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1367 | /* msg->sol must point to the first of CR or LF. */ |
| 1368 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1369 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1370 | /* stop here */ |
| 1371 | |
| 1372 | /* |
| 1373 | * Second, states that are specific to the request only |
| 1374 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1375 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1376 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1377 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1378 | /* we have a start of message, but we have to check |
| 1379 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1380 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1381 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1382 | if (likely(ptr != buf->p)) { |
| 1383 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1384 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1385 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1386 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1387 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1388 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1389 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1390 | state = HTTP_MSG_RQMETH; |
| 1391 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1392 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1393 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1394 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1395 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1396 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1397 | if (unlikely(*ptr == '\n')) |
| 1398 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1399 | 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] | 1400 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1401 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1402 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1403 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1404 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1405 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1406 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1407 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1408 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1409 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1410 | case HTTP_MSG_RQMETH_SP: |
| 1411 | case HTTP_MSG_RQURI: |
| 1412 | case HTTP_MSG_RQURI_SP: |
| 1413 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1414 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1415 | state, ptr, end, |
| 1416 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1417 | if (unlikely(!ptr)) |
| 1418 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1419 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1420 | /* we have a full request and we know that we have either a CR |
| 1421 | * or an LF at <ptr>. |
| 1422 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1423 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1424 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1425 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1426 | if (likely(*ptr == '\r')) |
| 1427 | 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] | 1428 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1429 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1430 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1431 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1432 | /* check for HTTP/0.9 request : no version information available. |
| 1433 | * msg->sol must point to the first of CR or LF. |
| 1434 | */ |
| 1435 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1436 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1437 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1438 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1439 | 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] | 1440 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1441 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1442 | /* |
| 1443 | * Common states below |
| 1444 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1445 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1446 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1447 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1448 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1449 | goto http_msg_hdr_name; |
| 1450 | } |
| 1451 | |
| 1452 | if (likely(*ptr == '\r')) |
| 1453 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1454 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1455 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1456 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1457 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1458 | /* assumes msg->sol points to the first char */ |
| 1459 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1460 | 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] | 1461 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1462 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1463 | 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] | 1464 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1465 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1466 | goto http_msg_invalid; |
| 1467 | |
| 1468 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1469 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1470 | |
| 1471 | /* and we still accept this non-token character */ |
| 1472 | 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] | 1473 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1474 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1475 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1476 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1477 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1478 | 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] | 1479 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1480 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1481 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1482 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1483 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1484 | goto http_msg_hdr_val; |
| 1485 | } |
| 1486 | |
| 1487 | if (likely(*ptr == '\r')) |
| 1488 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1489 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1490 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1491 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1492 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1493 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1494 | 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] | 1495 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1496 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1497 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1498 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1499 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1500 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1501 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1502 | goto http_msg_hdr_l1_sp; |
| 1503 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1504 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1505 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1506 | goto http_msg_complete_header; |
| 1507 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1508 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1509 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1510 | /* assumes msg->sol points to the first char, and msg->sov |
| 1511 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1512 | */ |
| 1513 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1514 | 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] | 1515 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1516 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1517 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1518 | * real header end in case it ends with lots of LWS, but is this |
| 1519 | * really needed ? |
| 1520 | */ |
| 1521 | if (likely(*ptr == '\r')) |
| 1522 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1523 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1524 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1525 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1526 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1527 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1528 | 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] | 1529 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1530 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1531 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1532 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1533 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1534 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1535 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1536 | goto http_msg_hdr_val; |
| 1537 | } |
| 1538 | http_msg_complete_header: |
| 1539 | /* |
| 1540 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1541 | * Assumes msg->sol points to the first char, msg->sov points |
| 1542 | * to the first character of the value and msg->eol to the |
| 1543 | * first CR or LF so we know how the line ends. We insert last |
| 1544 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1545 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1546 | 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] | 1547 | idx, idx->tail) < 0)) |
| 1548 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1549 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1550 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1551 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1552 | goto http_msg_hdr_name; |
| 1553 | } |
| 1554 | |
| 1555 | if (likely(*ptr == '\r')) |
| 1556 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1557 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1558 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1559 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1560 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1561 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1562 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1563 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1564 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1565 | msg->eoh = msg->sol; |
| 1566 | msg->sol = 0; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1567 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1568 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1569 | |
| 1570 | case HTTP_MSG_ERROR: |
| 1571 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1572 | break; |
| 1573 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1574 | #ifdef DEBUG_FULL |
| 1575 | default: |
| 1576 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1577 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1578 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1579 | } |
| 1580 | http_msg_ood: |
| 1581 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1582 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1583 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1584 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1585 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1586 | http_msg_invalid: |
| 1587 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1588 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1589 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1590 | return; |
| 1591 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1592 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1593 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1594 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1595 | * nothing is done and 1 is returned. |
| 1596 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1597 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1598 | { |
| 1599 | int delta; |
| 1600 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1601 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1602 | |
| 1603 | if (msg->sl.rq.v_l != 0) |
| 1604 | return 1; |
| 1605 | |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 1606 | cur_end = msg->chn->buf.p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1607 | delta = 0; |
| 1608 | |
| 1609 | if (msg->sl.rq.u_l == 0) { |
| 1610 | /* if no URI was set, add "/" */ |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 1611 | delta = buffer_replace2(&msg->chn->buf, cur_end, cur_end, " /", 2); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1612 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1613 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1614 | } |
| 1615 | /* add HTTP version */ |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 1616 | 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] | 1617 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1618 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1619 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1620 | HTTP_MSG_RQMETH, |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 1621 | msg->chn->buf.p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1622 | NULL, NULL); |
| 1623 | if (unlikely(!cur_end)) |
| 1624 | return 0; |
| 1625 | |
| 1626 | /* we have a full HTTP/1.0 request now and we know that |
| 1627 | * we have either a CR or an LF at <ptr>. |
| 1628 | */ |
| 1629 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1630 | return 1; |
| 1631 | } |
| 1632 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1633 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1634 | * and "keep-alive" values. If we already know that some headers may safely |
| 1635 | * 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] | 1636 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1637 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| 1638 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1639 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1640 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1641 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1642 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1643 | 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] | 1644 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1645 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1646 | const char *hdr_val = "Connection"; |
| 1647 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1648 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1649 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1650 | return; |
| 1651 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1652 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1653 | hdr_val = "Proxy-Connection"; |
| 1654 | hdr_len = 16; |
| 1655 | } |
| 1656 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1657 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1658 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 1659 | 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] | 1660 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1661 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1662 | if (to_del & 2) |
| 1663 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1664 | else |
| 1665 | txn->flags |= TX_CON_KAL_SET; |
| 1666 | } |
| 1667 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1668 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1669 | if (to_del & 1) |
| 1670 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1671 | else |
| 1672 | txn->flags |= TX_CON_CLO_SET; |
| 1673 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1674 | } |
| 1675 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1676 | txn->flags |= TX_HDR_CONN_PRS; |
| 1677 | return; |
| 1678 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1679 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1680 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1681 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1682 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1683 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1684 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1685 | 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] | 1686 | { |
| 1687 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1688 | const char *hdr_val = "Connection"; |
| 1689 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1690 | |
| 1691 | ctx.idx = 0; |
| 1692 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1693 | |
| 1694 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1695 | hdr_val = "Proxy-Connection"; |
| 1696 | hdr_len = 16; |
| 1697 | } |
| 1698 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1699 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 1700 | 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] | 1701 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1702 | if (wanted & TX_CON_KAL_SET) |
| 1703 | txn->flags |= TX_CON_KAL_SET; |
| 1704 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1705 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1706 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1707 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1708 | if (wanted & TX_CON_CLO_SET) |
| 1709 | txn->flags |= TX_CON_CLO_SET; |
| 1710 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1711 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1712 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1713 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1714 | |
| 1715 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1716 | return; |
| 1717 | |
| 1718 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1719 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1720 | hdr_val = "Connection: close"; |
| 1721 | hdr_len = 17; |
| 1722 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1723 | hdr_val = "Proxy-Connection: close"; |
| 1724 | hdr_len = 23; |
| 1725 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1726 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1730 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1731 | hdr_val = "Connection: keep-alive"; |
| 1732 | hdr_len = 22; |
| 1733 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1734 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1735 | hdr_len = 28; |
| 1736 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1737 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1738 | } |
| 1739 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1740 | } |
| 1741 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1742 | /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1743 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1744 | * so that we know we can forward between ->sol and ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1745 | * 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] | 1746 | * 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] | 1747 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1748 | int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1749 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1750 | const struct buffer *buf = &msg->chn->buf; |
| 1751 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1752 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1753 | const char *end = buf->data + buf->size; |
| 1754 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1755 | unsigned int chunk = 0; |
| 1756 | |
| 1757 | /* The chunk size is in the following form, though we are only |
| 1758 | * interested in the size and CRLF : |
| 1759 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1760 | */ |
| 1761 | while (1) { |
| 1762 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1763 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1764 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1765 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1766 | if (c < 0) /* not a hex digit anymore */ |
| 1767 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1768 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1769 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1770 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1771 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1772 | chunk = (chunk << 4) + c; |
| 1773 | } |
| 1774 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1775 | /* empty size not allowed */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1776 | if (ptr == ptr_old) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1777 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1778 | |
| 1779 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1780 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1781 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1782 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1783 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1784 | } |
| 1785 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1786 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1787 | * for the end of chunk size. |
| 1788 | */ |
| 1789 | while (1) { |
| 1790 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1791 | /* we now have a CR or an LF at ptr */ |
| 1792 | if (likely(*ptr == '\r')) { |
| 1793 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1794 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1795 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1796 | return 0; |
| 1797 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1798 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1799 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1800 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1801 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1802 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1803 | /* done */ |
| 1804 | break; |
| 1805 | } |
| 1806 | else if (*ptr == ';') { |
| 1807 | /* chunk extension, ends at next CRLF */ |
| 1808 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1809 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1810 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1811 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1812 | |
| 1813 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1814 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1815 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1816 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1817 | return 0; |
| 1818 | } |
| 1819 | /* we have a CRLF now, loop above */ |
| 1820 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1821 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1822 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1823 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1824 | } |
| 1825 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1826 | /* OK we found our CRLF and now <ptr> points to the next byte, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1827 | * which may or may not be present. We save that into ->next and |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1828 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1829 | */ |
Willy Tarreau | b8ffd37 | 2012-09-27 15:08:56 +0200 | [diff] [blame] | 1830 | if (ptr < ptr_old) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1831 | msg->sov += buf->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1832 | msg->sov += ptr - ptr_old; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1833 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1834 | msg->chunk_len = chunk; |
| 1835 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1836 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1837 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1838 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1839 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1840 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1841 | } |
| 1842 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1843 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1844 | * 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] | 1845 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 1846 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 1847 | * If not enough data are available, the function does not change anything |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1848 | * except maybe msg->next and msg->sov if it could parse some lines, and returns |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1849 | * zero. If a parse error is encountered, the function returns < 0 and does not |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1850 | * change anything except maybe msg->next and msg->sov. Note that the message |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1851 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 1852 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1853 | * forwarding, and that the difference between msg->sol and msg->sov exactly |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1854 | * matches the length of trailers already parsed and not forwarded. It is also |
| 1855 | * important to note that this function is designed to be able to parse wrapped |
| 1856 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1857 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1858 | int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1859 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1860 | const struct buffer *buf = &msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1861 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1862 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1863 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1864 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1865 | const char *ptr = b_ptr(buf, msg->next); |
| 1866 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1867 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1868 | |
| 1869 | /* scan current line and stop at LF or CRLF */ |
| 1870 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1871 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1872 | return 0; |
| 1873 | |
| 1874 | if (*ptr == '\n') { |
| 1875 | if (!p1) |
| 1876 | p1 = ptr; |
| 1877 | p2 = ptr; |
| 1878 | break; |
| 1879 | } |
| 1880 | |
| 1881 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1882 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1883 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1884 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1885 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1886 | p1 = ptr; |
| 1887 | } |
| 1888 | |
| 1889 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1890 | if (ptr >= buf->data + buf->size) |
| 1891 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | /* after LF; point to beginning of next line */ |
| 1895 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1896 | if (p2 >= buf->data + buf->size) |
| 1897 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1898 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1899 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1900 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1901 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1902 | |
| 1903 | /* schedule this line for forwarding */ |
| 1904 | msg->sov += bytes; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1905 | if (msg->sov >= buf->size) |
| 1906 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1907 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1908 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1909 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1910 | * Everything was scheduled for forwarding, there's nothing |
| 1911 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 1912 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1913 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1914 | msg->msg_state = HTTP_MSG_DONE; |
| 1915 | return 1; |
| 1916 | } |
| 1917 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1918 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1919 | } |
| 1920 | } |
| 1921 | |
| 1922 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 1923 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1924 | * ->sol, ->next in order to include this part into the next forwarding phase. |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1925 | * 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] | 1926 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 1927 | * not enough data are available, the function does not change anything and |
| 1928 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 1929 | * does not change anything. Note: this function is designed to parse wrapped |
| 1930 | * CRLF at the end of the buffer. |
| 1931 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1932 | int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1933 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1934 | const struct buffer *buf = &msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1935 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1936 | int bytes; |
| 1937 | |
| 1938 | /* NB: we'll check data availabilty at the end. It's not a |
| 1939 | * problem because whatever we match first will be checked |
| 1940 | * against the correct length. |
| 1941 | */ |
| 1942 | bytes = 1; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1943 | ptr = buf->p; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1944 | if (*ptr == '\r') { |
| 1945 | bytes++; |
| 1946 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1947 | if (ptr >= buf->data + buf->size) |
| 1948 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1949 | } |
| 1950 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1951 | if (bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1952 | return 0; |
| 1953 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1954 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1955 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1956 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1957 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1958 | |
| 1959 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1960 | if (ptr >= buf->data + buf->size) |
| 1961 | ptr = buf->data; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1962 | /* prepare the CRLF to be forwarded (between ->sol and ->sov) */ |
| 1963 | msg->sol = 0; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1964 | msg->sov = msg->next = bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1965 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 1966 | return 1; |
| 1967 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1968 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1969 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 1970 | * processing can continue on next analysers, or zero if it either needs more |
| 1971 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 1972 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 1973 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1974 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1975 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 1976 | 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] | 1977 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1978 | /* |
| 1979 | * We will parse the partial (or complete) lines. |
| 1980 | * We will check the request syntax, and also join multi-line |
| 1981 | * headers. An index of all the lines will be elaborated while |
| 1982 | * parsing. |
| 1983 | * |
| 1984 | * For the parsing, we use a 28 states FSM. |
| 1985 | * |
| 1986 | * Here is the information we currently have : |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 1987 | * req->buf.p = beginning of request |
| 1988 | * req->buf.p + msg->eoh = end of processed headers / start of current one |
| 1989 | * req->buf.p + req->buf.i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1990 | * msg->eol = end of current header or line (LF or CRLF) |
| 1991 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1992 | * |
| 1993 | * At end of parsing, we may perform a capture of the error (if any), and |
| 1994 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 1995 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 1996 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1997 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1998 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1999 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2000 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2001 | struct http_txn *txn = &s->txn; |
| 2002 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2003 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2004 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2005 | 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] | 2006 | now_ms, __FUNCTION__, |
| 2007 | s, |
| 2008 | req, |
| 2009 | req->rex, req->wex, |
| 2010 | req->flags, |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2011 | req->buf.i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2012 | req->analysers); |
| 2013 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2014 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2015 | s->srv_error = http_return_srv_error; |
| 2016 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2017 | /* There's a protected area at the end of the buffer for rewriting |
| 2018 | * purposes. We don't want to start to parse the request if the |
| 2019 | * protected area is affected, because we may have to move processed |
| 2020 | * data later, which is much more complicated. |
| 2021 | */ |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2022 | if (buffer_not_empty(&req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2023 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 2024 | unlikely(channel_full(req) || |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2025 | bi_end(&req->buf) < b_ptr(&req->buf, msg->next) || |
| 2026 | bi_end(&req->buf) > req->buf.data + req->buf.size - global.tune.maxrewrite)) { |
| 2027 | if (req->buf.o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2028 | 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] | 2029 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2030 | /* 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] | 2031 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2032 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2033 | return 0; |
| 2034 | } |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2035 | if (bi_end(&req->buf) < b_ptr(&req->buf, msg->next) || |
| 2036 | bi_end(&req->buf) > req->buf.data + req->buf.size - global.tune.maxrewrite) |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 2037 | buffer_slow_realign(&msg->chn->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2038 | } |
| 2039 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2040 | /* Note that we have the same problem with the response ; we |
| 2041 | * may want to send a redirect, error or anything which requires |
| 2042 | * some spare space. So we'll ensure that we have at least |
| 2043 | * maxrewrite bytes available in the response buffer before |
| 2044 | * processing that one. This will only affect pipelined |
| 2045 | * keep-alive requests. |
| 2046 | */ |
| 2047 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 2048 | unlikely(channel_full(s->rep) || |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2049 | bi_end(&s->rep->buf) < b_ptr(&s->rep->buf, txn->rsp.next) || |
| 2050 | bi_end(&s->rep->buf) > s->rep->buf.data + s->rep->buf.size - global.tune.maxrewrite)) { |
| 2051 | if (s->rep->buf.o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2052 | 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] | 2053 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2054 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2055 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2056 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2057 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2058 | return 0; |
| 2059 | } |
| 2060 | } |
| 2061 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2062 | if (likely(msg->next < req->buf.i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2063 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2064 | } |
| 2065 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2066 | /* 1: we might have to print this header in debug mode */ |
| 2067 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2068 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2069 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2070 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2071 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2072 | sol = req->buf.p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2073 | /* this is a bit complex : in case of error on the request line, |
| 2074 | * we know that rq.l is still zero, so we display only the part |
| 2075 | * up to the end of the line (truncated by debug_hdr). |
| 2076 | */ |
| 2077 | 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] | 2078 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2079 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2080 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2081 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2082 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2083 | while (cur_idx) { |
| 2084 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2085 | debug_hdr("clihdr", s, sol, eol); |
| 2086 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2087 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2088 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2089 | } |
| 2090 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2091 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2092 | /* |
| 2093 | * Now we quickly check if we have found a full valid request. |
| 2094 | * If not so, we check the FD and buffer states before leaving. |
| 2095 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2096 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2097 | * requests are checked first. When waiting for a second request |
| 2098 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2099 | * we note the error in the session flags but don't set any state. |
| 2100 | * Since the error will be noted there, it will not be counted by |
| 2101 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2102 | * Last, we may increase some tracked counters' http request errors on |
| 2103 | * the cases that are deliberately the client's fault. For instance, |
| 2104 | * a timeout or connection reset is not counted as an error. However |
| 2105 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2106 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2107 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2108 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2109 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2110 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2111 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2112 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2113 | session_inc_http_req_ctr(s); |
| 2114 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2115 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2116 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2117 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2118 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2119 | /* 1: Since we are in header mode, if there's no space |
| 2120 | * left for headers, we won't be able to free more |
| 2121 | * later, so the session will never terminate. We |
| 2122 | * must terminate it now. |
| 2123 | */ |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 2124 | if (unlikely(buffer_full(&req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2125 | /* FIXME: check if URI is set and return Status |
| 2126 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2127 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2128 | session_inc_http_req_ctr(s); |
| 2129 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2130 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2131 | if (msg->err_pos < 0) |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2132 | msg->err_pos = req->buf.i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2133 | goto return_bad_req; |
| 2134 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2135 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2136 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2137 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2138 | if (!(s->flags & SN_ERR_MASK)) |
| 2139 | s->flags |= SN_ERR_CLICL; |
| 2140 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2141 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2142 | goto failed_keep_alive; |
| 2143 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2144 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2145 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2146 | 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] | 2147 | session_inc_http_err_ctr(s); |
| 2148 | } |
| 2149 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2150 | msg->msg_state = HTTP_MSG_ERROR; |
| 2151 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2152 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2153 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2154 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2155 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2156 | if (s->listener->counters) |
| 2157 | s->listener->counters->failed_req++; |
| 2158 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2159 | if (!(s->flags & SN_FINST_MASK)) |
| 2160 | s->flags |= SN_FINST_R; |
| 2161 | return 0; |
| 2162 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2163 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2164 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2165 | 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] | 2166 | if (!(s->flags & SN_ERR_MASK)) |
| 2167 | s->flags |= SN_ERR_CLITO; |
| 2168 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2169 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2170 | goto failed_keep_alive; |
| 2171 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2172 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2173 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2174 | 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] | 2175 | session_inc_http_err_ctr(s); |
| 2176 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2177 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2178 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2179 | msg->msg_state = HTTP_MSG_ERROR; |
| 2180 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2181 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2182 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2183 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2184 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2185 | if (s->listener->counters) |
| 2186 | s->listener->counters->failed_req++; |
| 2187 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2188 | if (!(s->flags & SN_FINST_MASK)) |
| 2189 | s->flags |= SN_FINST_R; |
| 2190 | return 0; |
| 2191 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2192 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2193 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2194 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2195 | if (!(s->flags & SN_ERR_MASK)) |
| 2196 | s->flags |= SN_ERR_CLICL; |
| 2197 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2198 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2199 | goto failed_keep_alive; |
| 2200 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2201 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2202 | 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] | 2203 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2204 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2205 | msg->msg_state = HTTP_MSG_ERROR; |
| 2206 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2207 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2208 | session_inc_http_err_ctr(s); |
| 2209 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2210 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2211 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2212 | if (s->listener->counters) |
| 2213 | s->listener->counters->failed_req++; |
| 2214 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2215 | if (!(s->flags & SN_FINST_MASK)) |
| 2216 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2217 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2218 | } |
| 2219 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2220 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2221 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2222 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2223 | #ifdef TCP_QUICKACK |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2224 | if (s->listener->options & LI_O_NOQUICKACK && req->buf.i) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2225 | /* We need more data, we have to re-enable quick-ack in case we |
| 2226 | * previously disabled it, otherwise we might cause the client |
| 2227 | * to delay next data. |
| 2228 | */ |
Willy Tarreau | fb7508a | 2012-05-21 16:47:54 +0200 | [diff] [blame] | 2229 | setsockopt(si_fd(&s->si[0]), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2230 | } |
| 2231 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2232 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2233 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2234 | /* If the client starts to talk, let's fall back to |
| 2235 | * request timeout processing. |
| 2236 | */ |
| 2237 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2238 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2239 | } |
| 2240 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2241 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2242 | if (!tick_isset(req->analyse_exp)) { |
| 2243 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2244 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2245 | tick_isset(s->be->timeout.httpka)) |
| 2246 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2247 | else |
| 2248 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2249 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2250 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2251 | /* we're not ready yet */ |
| 2252 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2253 | |
| 2254 | failed_keep_alive: |
| 2255 | /* Here we process low-level errors for keep-alive requests. In |
| 2256 | * short, if the request is not the first one and it experiences |
| 2257 | * a timeout, read error or shutdown, we just silently close so |
| 2258 | * that the client can try again. |
| 2259 | */ |
| 2260 | txn->status = 0; |
| 2261 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2262 | req->analysers = 0; |
| 2263 | s->logs.logwait = 0; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2264 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2265 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2266 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2267 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2268 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2269 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2270 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2271 | * 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] | 2272 | * 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] | 2273 | * 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] | 2274 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2275 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2276 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2277 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2278 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2279 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2280 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2281 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2282 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2283 | /* kill the pending keep-alive timeout */ |
| 2284 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2285 | req->analyse_exp = TICK_ETERNITY; |
| 2286 | } |
| 2287 | |
| 2288 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2289 | /* Maybe we found in invalid header name while we were configured not |
| 2290 | * to block on that, so we have to capture it now. |
| 2291 | */ |
| 2292 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2293 | 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] | 2294 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2295 | /* |
| 2296 | * 1: identify the method |
| 2297 | */ |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2298 | 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] | 2299 | |
| 2300 | /* we can make use of server redirect on GET and HEAD */ |
| 2301 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2302 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2303 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2304 | /* |
| 2305 | * 2: check if the URI matches the monitor_uri. |
| 2306 | * We have to do this for every request which gets in, because |
| 2307 | * the monitor-uri is defined by the frontend. |
| 2308 | */ |
| 2309 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2310 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2311 | !memcmp(req->buf.p + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2312 | s->fe->monitor_uri, |
| 2313 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2314 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2315 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2316 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2317 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2318 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2319 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2320 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2321 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2322 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2323 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2324 | 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] | 2325 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2326 | ret = acl_pass(ret); |
| 2327 | if (cond->pol == ACL_COND_UNLESS) |
| 2328 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2329 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2330 | if (ret) { |
| 2331 | /* we fail this request, let's return 503 service unavail */ |
| 2332 | txn->status = 503; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2333 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2334 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2335 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2336 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2337 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2338 | /* nothing to fail, let's reply normaly */ |
| 2339 | txn->status = 200; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2340 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2341 | goto return_prx_cond; |
| 2342 | } |
| 2343 | |
| 2344 | /* |
| 2345 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2346 | * Note: we cannot log anymore if the request has been |
| 2347 | * classified as invalid. |
| 2348 | */ |
| 2349 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2350 | /* we have a complete HTTP request that we must log */ |
| 2351 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2352 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2353 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2354 | if (urilen >= REQURI_LEN) |
| 2355 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2356 | memcpy(txn->uri, req->buf.p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2357 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2358 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2359 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2360 | s->do_log(s); |
| 2361 | } else { |
| 2362 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2363 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2364 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2365 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2366 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 2367 | s->unique_id = pool_alloc2(pool2_uniqueid); |
| 2368 | } |
| 2369 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2370 | /* 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] | 2371 | 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] | 2372 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2373 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2374 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2375 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2376 | ((req->buf.p[msg->sl.rq.v + 5] > '1') || |
| 2377 | ((req->buf.p[msg->sl.rq.v + 5] == '1') && |
| 2378 | (req->buf.p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2379 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2380 | |
| 2381 | /* "connection" has not been parsed yet */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2382 | txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2383 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2384 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2385 | * we have what looks like a proxied connection instead of a connection, |
| 2386 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2387 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2388 | * happen to do that despite being non-standard :-( |
| 2389 | * We consider that a request not beginning with either '/' or '*' is |
| 2390 | * a proxied connection, which covers both "scheme://location" and |
| 2391 | * CONNECT ip:port. |
| 2392 | */ |
| 2393 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2394 | 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] | 2395 | txn->flags |= TX_USE_PX_CONN; |
| 2396 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2397 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2398 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2399 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2400 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2401 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2402 | capture_headers(req->buf.p, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2403 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2404 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2405 | /* 6: determine the transfer-length. |
| 2406 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2407 | * the presence of a message-body in a REQUEST and its transfer length |
| 2408 | * must be determined that way (in order of precedence) : |
| 2409 | * 1. The presence of a message-body in a request is signaled by the |
| 2410 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2411 | * in the request's header fields. When a request message contains |
| 2412 | * both a message-body of non-zero length and a method that does |
| 2413 | * not define any semantics for that request message-body, then an |
| 2414 | * origin server SHOULD either ignore the message-body or respond |
| 2415 | * with an appropriate error message (e.g., 413). A proxy or |
| 2416 | * gateway, when presented the same request, SHOULD either forward |
| 2417 | * the request inbound with the message- body or ignore the |
| 2418 | * message-body when determining a response. |
| 2419 | * |
| 2420 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2421 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2422 | * transfer-length is defined by the use of this transfer-coding. |
| 2423 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2424 | * transfer-coding is not present, the transfer-length is defined |
| 2425 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2426 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2427 | * 3. If a Content-Length header field is present, its decimal value in |
| 2428 | * OCTETs represents both the entity-length and the transfer-length. |
| 2429 | * If a message is received with both a Transfer-Encoding header |
| 2430 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2431 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2432 | * 4. By the server closing the connection. (Closing the connection |
| 2433 | * cannot be used to indicate the end of a request body, since that |
| 2434 | * would leave no possibility for the server to send back a response.) |
| 2435 | * |
| 2436 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2437 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2438 | * it is terminated by closing the connection. When the "chunked" |
| 2439 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2440 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2441 | */ |
| 2442 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2443 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2444 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2445 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2446 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2447 | 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] | 2448 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2449 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2450 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2451 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2452 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2453 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2454 | break; |
| 2455 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2456 | } |
| 2457 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2458 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2459 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2460 | 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] | 2461 | signed long long cl; |
| 2462 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2463 | if (!ctx.vlen) { |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2464 | msg->err_pos = ctx.line + ctx.val - req->buf.p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2465 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2466 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2467 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2468 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2469 | msg->err_pos = ctx.line + ctx.val - req->buf.p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2470 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2471 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2472 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2473 | if (cl < 0) { |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2474 | msg->err_pos = ctx.line + ctx.val - req->buf.p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2475 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2476 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2477 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2478 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2479 | msg->err_pos = ctx.line + ctx.val - req->buf.p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2480 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2481 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2482 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2483 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2484 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2485 | } |
| 2486 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2487 | /* bodyless requests have a known length */ |
| 2488 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2489 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2490 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2491 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2492 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2493 | req->analyse_exp = TICK_ETERNITY; |
| 2494 | return 1; |
| 2495 | |
| 2496 | return_bad_req: |
| 2497 | /* We centralize bad requests processing here */ |
| 2498 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2499 | /* we detected a parsing error. We want to archive this request |
| 2500 | * in the dedicated proxy area for later troubleshooting. |
| 2501 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2502 | 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] | 2503 | } |
| 2504 | |
| 2505 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2506 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2507 | 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] | 2508 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2509 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2510 | if (s->listener->counters) |
| 2511 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2512 | |
| 2513 | return_prx_cond: |
| 2514 | if (!(s->flags & SN_ERR_MASK)) |
| 2515 | s->flags |= SN_ERR_PRXCOND; |
| 2516 | if (!(s->flags & SN_FINST_MASK)) |
| 2517 | s->flags |= SN_FINST_R; |
| 2518 | |
| 2519 | req->analysers = 0; |
| 2520 | req->analyse_exp = TICK_ETERNITY; |
| 2521 | return 0; |
| 2522 | } |
| 2523 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2524 | /* We reached the stats page through a POST request. |
| 2525 | * Parse the posted data and enable/disable servers if necessary. |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2526 | * Returns 1 if request was parsed or zero if it needs more data. |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2527 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2528 | int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req) |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2529 | { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2530 | struct proxy *px = NULL; |
| 2531 | struct server *sv = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2532 | |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2533 | char key[LINESIZE]; |
| 2534 | int action = ST_ADM_ACTION_NONE; |
| 2535 | int reprocess = 0; |
| 2536 | |
| 2537 | int total_servers = 0; |
| 2538 | int altered_servers = 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2539 | |
| 2540 | char *first_param, *cur_param, *next_param, *end_params; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 2541 | char *st_cur_param = NULL; |
| 2542 | char *st_next_param = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2543 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2544 | first_param = req->buf.p + txn->req.eoh + 2; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2545 | end_params = first_param + txn->req.body_len; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2546 | |
| 2547 | cur_param = next_param = end_params; |
| 2548 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2549 | if (end_params >= req->buf.data + req->buf.size - global.tune.maxrewrite) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2550 | /* Prevent buffer overflow */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2551 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2552 | return 1; |
| 2553 | } |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2554 | else if (end_params > req->buf.p + req->buf.i) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2555 | /* we need more data */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2556 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2557 | return 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2558 | } |
| 2559 | |
| 2560 | *end_params = '\0'; |
| 2561 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2562 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2563 | |
| 2564 | /* |
| 2565 | * Parse the parameters in reverse order to only store the last value. |
| 2566 | * From the html form, the backend and the action are at the end. |
| 2567 | */ |
| 2568 | while (cur_param > first_param) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2569 | char *value; |
| 2570 | int poffset, plen; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2571 | |
| 2572 | cur_param--; |
| 2573 | if ((*cur_param == '&') || (cur_param == first_param)) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2574 | reprocess_servers: |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2575 | /* Parse the key */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2576 | poffset = (cur_param != first_param ? 1 : 0); |
| 2577 | plen = next_param - cur_param + (cur_param == first_param ? 1 : 0); |
| 2578 | if ((plen > 0) && (plen <= sizeof(key))) { |
| 2579 | strncpy(key, cur_param + poffset, plen); |
| 2580 | key[plen - 1] = '\0'; |
| 2581 | } else { |
| 2582 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
| 2583 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | /* Parse the value */ |
| 2587 | value = key; |
| 2588 | while (*value != '\0' && *value != '=') { |
| 2589 | value++; |
| 2590 | } |
| 2591 | if (*value == '=') { |
| 2592 | /* Ok, a value is found, we can mark the end of the key */ |
| 2593 | *value++ = '\0'; |
| 2594 | } |
| 2595 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2596 | if (!url_decode(key) || !url_decode(value)) |
| 2597 | break; |
| 2598 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2599 | /* Now we can check the key to see what to do */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2600 | if (!px && (strcmp(key, "b") == 0)) { |
| 2601 | if ((px = findproxy(value, PR_CAP_BE)) == NULL) { |
| 2602 | /* the backend name is unknown or ambiguous (duplicate names) */ |
| 2603 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2604 | goto out; |
| 2605 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2606 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2607 | else if (!action && (strcmp(key, "action") == 0)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2608 | if (strcmp(value, "disable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2609 | action = ST_ADM_ACTION_DISABLE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2610 | } |
| 2611 | else if (strcmp(value, "enable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2612 | action = ST_ADM_ACTION_ENABLE; |
| 2613 | } |
Willy Tarreau | d728224 | 2012-06-04 00:22:44 +0200 | [diff] [blame] | 2614 | else if (strcmp(value, "stop") == 0) { |
| 2615 | action = ST_ADM_ACTION_STOP; |
| 2616 | } |
| 2617 | else if (strcmp(value, "start") == 0) { |
| 2618 | action = ST_ADM_ACTION_START; |
| 2619 | } |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2620 | else if (strcmp(value, "shutdown") == 0) { |
| 2621 | action = ST_ADM_ACTION_SHUTDOWN; |
| 2622 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2623 | else { |
| 2624 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2625 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2626 | } |
| 2627 | } |
| 2628 | else if (strcmp(key, "s") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2629 | if (!(px && action)) { |
| 2630 | /* |
| 2631 | * Indicates that we'll need to reprocess the parameters |
| 2632 | * as soon as backend and action are known |
| 2633 | */ |
| 2634 | if (!reprocess) { |
| 2635 | st_cur_param = cur_param; |
| 2636 | st_next_param = next_param; |
| 2637 | } |
| 2638 | reprocess = 1; |
| 2639 | } |
| 2640 | else if ((sv = findserver(px, value)) != NULL) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2641 | switch (action) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2642 | case ST_ADM_ACTION_DISABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2643 | if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2644 | /* Not already in maintenance, we can change the server state */ |
| 2645 | sv->state |= SRV_MAINTAIN; |
| 2646 | set_server_down(sv); |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2647 | altered_servers++; |
| 2648 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2649 | } |
| 2650 | break; |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2651 | case ST_ADM_ACTION_ENABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2652 | if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2653 | /* Already in maintenance, we can change the server state */ |
| 2654 | set_server_up(sv); |
Willy Tarreau | 7046130 | 2010-10-22 14:39:02 +0200 | [diff] [blame] | 2655 | sv->health = sv->rise; /* up, but will fall down at first failure */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2656 | altered_servers++; |
| 2657 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2658 | } |
Willy Tarreau | d728224 | 2012-06-04 00:22:44 +0200 | [diff] [blame] | 2659 | break; |
| 2660 | case ST_ADM_ACTION_STOP: |
| 2661 | case ST_ADM_ACTION_START: |
| 2662 | if (action == ST_ADM_ACTION_START) |
| 2663 | sv->uweight = sv->iweight; |
| 2664 | else |
| 2665 | sv->uweight = 0; |
| 2666 | |
| 2667 | if (px->lbprm.algo & BE_LB_PROP_DYN) { |
| 2668 | /* we must take care of not pushing the server to full throttle during slow starts */ |
| 2669 | if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN)) |
| 2670 | sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart; |
| 2671 | else |
| 2672 | sv->eweight = BE_WEIGHT_SCALE; |
| 2673 | sv->eweight *= sv->uweight; |
| 2674 | } else { |
| 2675 | sv->eweight = sv->uweight; |
| 2676 | } |
| 2677 | |
| 2678 | /* static LB algorithms are a bit harder to update */ |
| 2679 | if (px->lbprm.update_server_eweight) |
| 2680 | px->lbprm.update_server_eweight(sv); |
| 2681 | else if (sv->eweight) { |
| 2682 | if (px->lbprm.set_server_status_up) |
| 2683 | px->lbprm.set_server_status_up(sv); |
| 2684 | } |
| 2685 | else { |
| 2686 | if (px->lbprm.set_server_status_down) |
| 2687 | px->lbprm.set_server_status_down(sv); |
| 2688 | } |
| 2689 | altered_servers++; |
| 2690 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2691 | break; |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2692 | case ST_ADM_ACTION_SHUTDOWN: |
| 2693 | if (px->state != PR_STSTOPPED) { |
| 2694 | struct session *sess, *sess_bck; |
| 2695 | |
| 2696 | list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv) |
| 2697 | if (sess->srv_conn == sv) |
| 2698 | session_shutdown(sess, SN_ERR_KILLED); |
| 2699 | |
| 2700 | altered_servers++; |
| 2701 | total_servers++; |
| 2702 | } |
| 2703 | break; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2704 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2705 | } else { |
| 2706 | /* the server name is unknown or ambiguous (duplicate names) */ |
| 2707 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2708 | } |
| 2709 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2710 | if (reprocess && px && action) { |
| 2711 | /* Now, we know the backend and the action chosen by the user. |
| 2712 | * We can safely restart from the first server parameter |
| 2713 | * to reprocess them |
| 2714 | */ |
| 2715 | cur_param = st_cur_param; |
| 2716 | next_param = st_next_param; |
| 2717 | reprocess = 0; |
| 2718 | goto reprocess_servers; |
| 2719 | } |
| 2720 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2721 | next_param = cur_param; |
| 2722 | } |
| 2723 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2724 | |
| 2725 | if (total_servers == 0) { |
| 2726 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
| 2727 | } |
| 2728 | else if (altered_servers == 0) { |
| 2729 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2730 | } |
| 2731 | else if (altered_servers == total_servers) { |
| 2732 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
| 2733 | } |
| 2734 | else { |
| 2735 | si->applet.ctx.stats.st_code = STAT_STATUS_PART; |
| 2736 | } |
| 2737 | out: |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2738 | return 1; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2739 | } |
| 2740 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2741 | /* returns a pointer to the first rule which forbids access (deny or http_auth), |
| 2742 | * or NULL if everything's OK. |
| 2743 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2744 | static inline struct http_req_rule * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2745 | http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 2746 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2747 | struct http_req_rule *rule; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2748 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2749 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2750 | int ret = 1; |
| 2751 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2752 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2753 | continue; |
| 2754 | |
| 2755 | /* check condition, but only if attached */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2756 | if (rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2757 | 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] | 2758 | ret = acl_pass(ret); |
| 2759 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2760 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2761 | ret = !ret; |
| 2762 | } |
| 2763 | |
| 2764 | if (ret) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2765 | if (rule->action == HTTP_REQ_ACT_ALLOW) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2766 | return NULL; /* no problem */ |
| 2767 | else |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2768 | return rule; /* most likely a deny or auth rule */ |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2769 | } |
| 2770 | } |
| 2771 | return NULL; |
| 2772 | } |
| 2773 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2774 | /* This stream analyser runs all HTTP request processing which is common to |
| 2775 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2776 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2777 | * 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] | 2778 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2779 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2780 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2781 | 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] | 2782 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2783 | struct http_txn *txn = &s->txn; |
| 2784 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2785 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2786 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2787 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2788 | struct cond_wordlist *wl; |
Simon Horman | 70735c9 | 2011-06-07 11:07:50 +0900 | [diff] [blame] | 2789 | int do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2790 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2791 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2792 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2793 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2794 | return 0; |
| 2795 | } |
| 2796 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2797 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2798 | req->analyse_exp = TICK_ETERNITY; |
| 2799 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2800 | 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] | 2801 | now_ms, __FUNCTION__, |
| 2802 | s, |
| 2803 | req, |
| 2804 | req->rex, req->wex, |
| 2805 | req->flags, |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 2806 | req->buf.i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2807 | req->analysers); |
| 2808 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2809 | /* first check whether we have some ACLs set to block this request */ |
| 2810 | list_for_each_entry(cond, &px->block_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2811 | 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] | 2812 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2813 | ret = acl_pass(ret); |
| 2814 | if (cond->pol == ACL_COND_UNLESS) |
| 2815 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2816 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2817 | if (ret) { |
| 2818 | txn->status = 403; |
| 2819 | /* let's log the request time */ |
| 2820 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2821 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2822 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2823 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2824 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2825 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2826 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2827 | /* evaluate http-request rules */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2828 | http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 2829 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2830 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2831 | if (!http_req_last_rule) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2832 | do_stats = stats_check_uri(s->rep->prod, txn, px); |
| 2833 | if (do_stats) |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2834 | http_req_last_rule = http_check_access_rule(px, &px->uri_auth->http_req_rules, s, txn); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2835 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2836 | else |
| 2837 | do_stats = 0; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2838 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2839 | /* return a 403 if either rule has blocked */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2840 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) { |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2841 | txn->status = 403; |
| 2842 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2843 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2844 | session_inc_http_err_ctr(s); |
Willy Tarreau | 6da0f6d | 2011-01-06 18:19:50 +0100 | [diff] [blame] | 2845 | s->fe->fe_counters.denied_req++; |
| 2846 | if (an_bit == AN_REQ_HTTP_PROCESS_BE) |
| 2847 | s->be->be_counters.denied_req++; |
| 2848 | if (s->listener->counters) |
| 2849 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2850 | goto return_prx_cond; |
| 2851 | } |
| 2852 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2853 | /* try headers filters */ |
| 2854 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 2855 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2856 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2857 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2858 | /* has the request been denied ? */ |
| 2859 | if (txn->flags & TX_CLDENY) { |
| 2860 | /* no need to go further */ |
| 2861 | txn->status = 403; |
| 2862 | /* let's log the request time */ |
| 2863 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2864 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2865 | session_inc_http_err_ctr(s); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2866 | goto return_prx_cond; |
| 2867 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2868 | |
| 2869 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2870 | * which may be the same as the connect timeout if unspecified. |
| 2871 | * If unset, then set it to zero because we really want it to |
| 2872 | * eventually expire. We build the tarpit as an analyser. |
| 2873 | */ |
| 2874 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2875 | channel_erase(s->req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2876 | /* wipe the request out so that we can drop the connection early |
| 2877 | * if the client closes first. |
| 2878 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2879 | channel_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2880 | req->analysers = 0; /* remove switching rules etc... */ |
| 2881 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2882 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2883 | if (!req->analyse_exp) |
| 2884 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2885 | session_inc_http_err_ctr(s); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2886 | return 1; |
| 2887 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2888 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2889 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2890 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 2891 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2892 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 2893 | * in which headers are mangled. However, if another mode is set, it will |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2894 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 2895 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 2896 | * The method consists in checking if options changed between the two calls |
| 2897 | * (implying that either one is non-null, or one of them is non-null and we |
| 2898 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 2899 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2900 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 2901 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 2902 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 2903 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 2904 | (s->be->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)))) { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2905 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2906 | |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 2907 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE || |
| 2908 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2909 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2910 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 2911 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2912 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2913 | tmp = TX_CON_WANT_CLO; |
| 2914 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2915 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2916 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2917 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2918 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 2919 | /* parse the Connection header and possibly clean it */ |
| 2920 | int to_del = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2921 | if ((msg->flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 2922 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 2923 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2924 | to_del |= 2; /* remove "keep-alive" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2925 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2926 | to_del |= 1; /* remove "close" */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2927 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2928 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2929 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2930 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2931 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2932 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2933 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2934 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 2935 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2936 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 2937 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2938 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2939 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2940 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2941 | /* we can be blocked here because the request needs to be authenticated, |
| 2942 | * either to pass or to access stats. |
| 2943 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2944 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) { |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2945 | struct chunk msg; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2946 | char *realm = http_req_last_rule->http_auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2947 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2948 | if (!realm) |
| 2949 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 2950 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 2951 | sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm); |
David du Colombier | 7af4605 | 2012-05-16 14:16:48 +0200 | [diff] [blame] | 2952 | chunk_initlen(&msg, trash, trashlen, strlen(trash)); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2953 | txn->status = 401; |
| 2954 | stream_int_retnclose(req->prod, &msg); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2955 | /* on 401 we still count one error, because normal browsing |
| 2956 | * won't significantly increase the counter but brute force |
| 2957 | * attempts will. |
| 2958 | */ |
| 2959 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2960 | goto return_prx_cond; |
| 2961 | } |
| 2962 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2963 | /* add request headers from the rule sets in the same order */ |
| 2964 | list_for_each_entry(wl, &px->req_add, list) { |
| 2965 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2966 | 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] | 2967 | ret = acl_pass(ret); |
| 2968 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 2969 | ret = !ret; |
| 2970 | if (!ret) |
| 2971 | continue; |
| 2972 | } |
| 2973 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2974 | 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] | 2975 | goto return_bad_req; |
| 2976 | } |
| 2977 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2978 | if (do_stats) { |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2979 | struct stats_admin_rule *stats_admin_rule; |
| 2980 | |
| 2981 | /* We need to provide stats for this request. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2982 | * FIXME!!! that one is rather dangerous, we want to |
| 2983 | * make it follow standard rules (eg: clear req->analysers). |
| 2984 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2985 | |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2986 | /* now check whether we have some admin rules for this request */ |
| 2987 | list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) { |
| 2988 | int ret = 1; |
| 2989 | |
| 2990 | if (stats_admin_rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2991 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2992 | ret = acl_pass(ret); |
| 2993 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 2994 | ret = !ret; |
| 2995 | } |
| 2996 | |
| 2997 | if (ret) { |
| 2998 | /* no rule, or the rule matches */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2999 | s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3000 | break; |
| 3001 | } |
| 3002 | } |
| 3003 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3004 | /* Was the status page requested with a POST ? */ |
| 3005 | if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3006 | if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3007 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3008 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3009 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3010 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3011 | if (msg->flags & HTTP_MSGF_VER_11) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3012 | struct hdr_ctx ctx; |
| 3013 | ctx.idx = 0; |
| 3014 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 3015 | if (http_find_header2("Expect", 6, req->buf.p, &txn->hdr_idx, &ctx) && |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3016 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 3017 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3018 | } |
| 3019 | } |
| 3020 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3021 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3022 | } |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3023 | if (!http_process_req_stat_post(s->rep->prod, txn, req)) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3024 | /* we need more data */ |
| 3025 | req->analysers |= an_bit; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3026 | channel_dont_connect(req); |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3027 | return 0; |
| 3028 | } |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3029 | } else { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3030 | s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3031 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3032 | } |
| 3033 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3034 | s->logs.tv_request = now; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3035 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 3036 | stream_int_register_handler(s->rep->prod, &http_stats_applet); |
Willy Tarreau | 3cefd52 | 2012-08-30 15:49:18 +0200 | [diff] [blame] | 3037 | copy_target(&s->target, &s->rep->prod->conn.target); // for logging only |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3038 | s->rep->prod->conn.xprt_ctx = s; |
Willy Tarreau | bc4af05 | 2011-02-13 13:25:14 +0100 | [diff] [blame] | 3039 | s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3040 | req->analysers = 0; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 3041 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3042 | s->fe->fe_counters.intercepted_req++; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3043 | |
| 3044 | return 0; |
| 3045 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3046 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3047 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3048 | /* check whether we have some ACLs set to redirect this request */ |
| 3049 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3050 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3051 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3052 | if (rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3053 | 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] | 3054 | ret = acl_pass(ret); |
| 3055 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3056 | ret = !ret; |
| 3057 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3058 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3059 | if (ret) { |
David du Colombier | 7af4605 | 2012-05-16 14:16:48 +0200 | [diff] [blame] | 3060 | struct chunk rdr = { .str = trash, .size = trashlen, .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3061 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3062 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3063 | /* build redirect message */ |
| 3064 | switch(rule->code) { |
| 3065 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3066 | msg_fmt = HTTP_303; |
| 3067 | break; |
| 3068 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3069 | msg_fmt = HTTP_301; |
| 3070 | break; |
| 3071 | case 302: |
| 3072 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3073 | msg_fmt = HTTP_302; |
| 3074 | break; |
| 3075 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3076 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3077 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3078 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3079 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3080 | switch(rule->type) { |
Willy Tarreau | 2e1dca8 | 2012-09-12 08:43:15 +0200 | [diff] [blame] | 3081 | case REDIRECT_TYPE_SCHEME: { |
| 3082 | const char *path; |
| 3083 | const char *host; |
| 3084 | struct hdr_ctx ctx; |
| 3085 | int pathlen; |
| 3086 | int hostlen; |
| 3087 | |
| 3088 | host = ""; |
| 3089 | hostlen = 0; |
| 3090 | ctx.idx = 0; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 3091 | if (http_find_header2("Host", 4, txn->req.chn->buf.p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 2e1dca8 | 2012-09-12 08:43:15 +0200 | [diff] [blame] | 3092 | host = ctx.line + ctx.val; |
| 3093 | hostlen = ctx.vlen; |
| 3094 | } |
| 3095 | |
| 3096 | path = http_get_path(txn); |
| 3097 | /* build message using path */ |
| 3098 | if (path) { |
| 3099 | pathlen = txn->req.sl.rq.u_l + (req->buf.p + txn->req.sl.rq.u) - path; |
| 3100 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3101 | int qs = 0; |
| 3102 | while (qs < pathlen) { |
| 3103 | if (path[qs] == '?') { |
| 3104 | pathlen = qs; |
| 3105 | break; |
| 3106 | } |
| 3107 | qs++; |
| 3108 | } |
| 3109 | } |
| 3110 | } else { |
| 3111 | path = "/"; |
| 3112 | pathlen = 1; |
| 3113 | } |
| 3114 | |
| 3115 | /* check if we can add scheme + "://" + host + path */ |
| 3116 | if (rdr.len + rule->rdr_len + 3 + hostlen + pathlen > rdr.size - 4) |
| 3117 | goto return_bad_req; |
| 3118 | |
| 3119 | /* add scheme */ |
| 3120 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3121 | rdr.len += rule->rdr_len; |
| 3122 | |
| 3123 | /* add "://" */ |
| 3124 | memcpy(rdr.str + rdr.len, "://", 3); |
| 3125 | rdr.len += 3; |
| 3126 | |
| 3127 | /* add host */ |
| 3128 | memcpy(rdr.str + rdr.len, host, hostlen); |
| 3129 | rdr.len += hostlen; |
| 3130 | |
| 3131 | /* add path */ |
| 3132 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 3133 | rdr.len += pathlen; |
| 3134 | |
| 3135 | /* append a slash at the end of the location is needed and missing */ |
| 3136 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 3137 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3138 | if (rdr.len > rdr.size - 5) |
| 3139 | goto return_bad_req; |
| 3140 | rdr.str[rdr.len] = '/'; |
| 3141 | rdr.len++; |
| 3142 | } |
| 3143 | |
| 3144 | break; |
| 3145 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3146 | case REDIRECT_TYPE_PREFIX: { |
| 3147 | const char *path; |
| 3148 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3149 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3150 | path = http_get_path(txn); |
| 3151 | /* build message using path */ |
| 3152 | if (path) { |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 3153 | pathlen = txn->req.sl.rq.u_l + (req->buf.p + txn->req.sl.rq.u) - path; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3154 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3155 | int qs = 0; |
| 3156 | while (qs < pathlen) { |
| 3157 | if (path[qs] == '?') { |
| 3158 | pathlen = qs; |
| 3159 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3160 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3161 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3162 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3163 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3164 | } else { |
| 3165 | path = "/"; |
| 3166 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3167 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3168 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3169 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3170 | goto return_bad_req; |
| 3171 | |
| 3172 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3173 | * add anything, otherwise it makes it hard for the user to |
| 3174 | * configure a self-redirection. |
| 3175 | */ |
| 3176 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3177 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3178 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3179 | } |
| 3180 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3181 | /* add path */ |
| 3182 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 3183 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3184 | |
| 3185 | /* append a slash at the end of the location is needed and missing */ |
| 3186 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 3187 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3188 | if (rdr.len > rdr.size - 5) |
| 3189 | goto return_bad_req; |
| 3190 | rdr.str[rdr.len] = '/'; |
| 3191 | rdr.len++; |
| 3192 | } |
| 3193 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3194 | break; |
| 3195 | } |
| 3196 | case REDIRECT_TYPE_LOCATION: |
| 3197 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3198 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3199 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3200 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3201 | /* add location */ |
| 3202 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3203 | rdr.len += rule->rdr_len; |
| 3204 | break; |
| 3205 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3206 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3207 | if (rule->cookie_len) { |
| 3208 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 3209 | rdr.len += 14; |
| 3210 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 3211 | rdr.len += rule->cookie_len; |
| 3212 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 3213 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3214 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3215 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3216 | /* add end of headers and the keep-alive/close status. |
| 3217 | * We may choose to set keep-alive if the Location begins |
| 3218 | * with a slash, because the client will come back to the |
| 3219 | * same server. |
| 3220 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3221 | txn->status = rule->code; |
| 3222 | /* let's log the request time */ |
| 3223 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3224 | |
| 3225 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3226 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3227 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3228 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3229 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3230 | /* keep-alive possible */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3231 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3232 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3233 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3234 | rdr.len += 30; |
| 3235 | } else { |
| 3236 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24); |
| 3237 | rdr.len += 24; |
| 3238 | } |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3239 | } |
| 3240 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3241 | rdr.len += 4; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 3242 | bo_inject(req->prod->ob, rdr.str, rdr.len); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3243 | /* "eat" the request */ |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 3244 | bi_fast_delete(&req->buf, msg->sov); |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 3245 | msg->sov = 0; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3246 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 3247 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3248 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3249 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3250 | break; |
| 3251 | } else { |
| 3252 | /* keep-alive not possible */ |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3253 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3254 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3255 | rdr.len += 29; |
| 3256 | } else { |
| 3257 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3258 | rdr.len += 23; |
| 3259 | } |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 3260 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3261 | goto return_prx_cond; |
| 3262 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3263 | } |
| 3264 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3265 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3266 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3267 | * If this happens, then the data will not come immediately, so we must |
| 3268 | * send all what we have without waiting. Note that due to the small gain |
| 3269 | * 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] | 3270 | * 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] | 3271 | * itself once used. |
| 3272 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3273 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3274 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3275 | /* that's OK for us now, let's move on to next analysers */ |
| 3276 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3277 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3278 | return_bad_req: |
| 3279 | /* We centralize bad requests processing here */ |
| 3280 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3281 | /* we detected a parsing error. We want to archive this request |
| 3282 | * in the dedicated proxy area for later troubleshooting. |
| 3283 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3284 | 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] | 3285 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3286 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3287 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3288 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3289 | 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] | 3290 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3291 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3292 | if (s->listener->counters) |
| 3293 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3294 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3295 | return_prx_cond: |
| 3296 | if (!(s->flags & SN_ERR_MASK)) |
| 3297 | s->flags |= SN_ERR_PRXCOND; |
| 3298 | if (!(s->flags & SN_FINST_MASK)) |
| 3299 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3300 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3301 | req->analysers = 0; |
| 3302 | req->analyse_exp = TICK_ETERNITY; |
| 3303 | return 0; |
| 3304 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3305 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3306 | /* This function performs all the processing enabled for the current request. |
| 3307 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3308 | * needs more data, encounters an error, or wants to immediately abort the |
| 3309 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3310 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3311 | 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] | 3312 | { |
| 3313 | struct http_txn *txn = &s->txn; |
| 3314 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3315 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3316 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3317 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3318 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3319 | return 0; |
| 3320 | } |
| 3321 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3322 | 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] | 3323 | now_ms, __FUNCTION__, |
| 3324 | s, |
| 3325 | req, |
| 3326 | req->rex, req->wex, |
| 3327 | req->flags, |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 3328 | req->buf.i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3329 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3330 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3331 | /* |
| 3332 | * Right now, we know that we have processed the entire headers |
| 3333 | * and that unwanted requests have been filtered out. We can do |
| 3334 | * whatever we want with the remaining request. Also, now we |
| 3335 | * may have separate values for ->fe, ->be. |
| 3336 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3337 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3338 | /* |
| 3339 | * If HTTP PROXY is set we simply get remote server address |
| 3340 | * parsing incoming request. |
| 3341 | */ |
| 3342 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 3343 | url2sa(req->buf.p + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->conn.addr.to); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3344 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3345 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3346 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3347 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3348 | * Note that doing so might move headers in the request, but |
| 3349 | * the fields will stay coherent and the URI will not move. |
| 3350 | * This should only be performed in the backend. |
| 3351 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3352 | 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] | 3353 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3354 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3355 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3356 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3357 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3358 | * so let's do the same now. |
| 3359 | */ |
| 3360 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3361 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3362 | if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 3363 | 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] | 3364 | } |
| 3365 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3366 | /* add unique-id if "header-unique-id" is specified */ |
| 3367 | |
| 3368 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) |
| 3369 | build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id); |
| 3370 | |
| 3371 | if (s->fe->header_unique_id && s->unique_id) { |
| 3372 | int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 3373 | if (ret < 0 || ret > global.tune.bufsize) |
| 3374 | goto return_bad_req; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3375 | if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0)) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3376 | goto return_bad_req; |
| 3377 | } |
| 3378 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3379 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3380 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3381 | * asks for it. |
| 3382 | */ |
| 3383 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3384 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3385 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Cyril Bonté | a32d275 | 2012-05-29 23:27:41 +0200 | [diff] [blame] | 3386 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name, |
| 3387 | s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len, |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 3388 | req->buf.p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3389 | /* The header is set to be added only if none is present |
| 3390 | * and we found it, so don't do anything. |
| 3391 | */ |
| 3392 | } |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 3393 | else if (s->req->prod->conn.addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3394 | /* Add an X-Forwarded-For header unless the source IP is |
| 3395 | * in the 'except' network range. |
| 3396 | */ |
| 3397 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 3398 | (((struct sockaddr_in *)&s->req->prod->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] | 3399 | != s->fe->except_net.s_addr) && |
| 3400 | (!s->be->except_mask.s_addr || |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 3401 | (((struct sockaddr_in *)&s->req->prod->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] | 3402 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3403 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3404 | unsigned char *pn; |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 3405 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn.addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3406 | |
| 3407 | /* Note: we rely on the backend to get the header name to be used for |
| 3408 | * x-forwarded-for, because the header is really meant for the backends. |
| 3409 | * However, if the backend did not specify any option, we have to rely |
| 3410 | * on the frontend's header name. |
| 3411 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3412 | if (s->be->fwdfor_hdr_len) { |
| 3413 | len = s->be->fwdfor_hdr_len; |
| 3414 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3415 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3416 | len = s->fe->fwdfor_hdr_len; |
| 3417 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3418 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3419 | len += sprintf(trash + 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] | 3420 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3421 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3422 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3423 | } |
| 3424 | } |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 3425 | else if (s->req->prod->conn.addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3426 | /* FIXME: for the sake of completeness, we should also support |
| 3427 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3428 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3429 | int len; |
| 3430 | char pn[INET6_ADDRSTRLEN]; |
| 3431 | inet_ntop(AF_INET6, |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 3432 | (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn.addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3433 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3434 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3435 | /* Note: we rely on the backend to get the header name to be used for |
| 3436 | * x-forwarded-for, because the header is really meant for the backends. |
| 3437 | * However, if the backend did not specify any option, we have to rely |
| 3438 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3439 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3440 | if (s->be->fwdfor_hdr_len) { |
| 3441 | len = s->be->fwdfor_hdr_len; |
| 3442 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3443 | } else { |
| 3444 | len = s->fe->fwdfor_hdr_len; |
| 3445 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3446 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3447 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3448 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3449 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3450 | goto return_bad_req; |
| 3451 | } |
| 3452 | } |
| 3453 | |
| 3454 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3455 | * 10: add X-Original-To if either the frontend or the backend |
| 3456 | * asks for it. |
| 3457 | */ |
| 3458 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3459 | |
| 3460 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 3461 | if (s->req->prod->conn.addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3462 | /* Add an X-Original-To header unless the destination IP is |
| 3463 | * in the 'except' network range. |
| 3464 | */ |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 3465 | conn_get_to_addr(&s->req->prod->conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3466 | |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 3467 | if (s->req->prod->conn.addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3468 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 3469 | (((struct sockaddr_in *)&s->req->prod->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] | 3470 | != s->fe->except_to.s_addr) && |
| 3471 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 3472 | (((struct sockaddr_in *)&s->req->prod->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] | 3473 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3474 | int len; |
| 3475 | unsigned char *pn; |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 3476 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn.addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3477 | |
| 3478 | /* Note: we rely on the backend to get the header name to be used for |
| 3479 | * x-original-to, because the header is really meant for the backends. |
| 3480 | * However, if the backend did not specify any option, we have to rely |
| 3481 | * on the frontend's header name. |
| 3482 | */ |
| 3483 | if (s->be->orgto_hdr_len) { |
| 3484 | len = s->be->orgto_hdr_len; |
| 3485 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3486 | } else { |
| 3487 | len = s->fe->orgto_hdr_len; |
| 3488 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3489 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3490 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3491 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3492 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3493 | goto return_bad_req; |
| 3494 | } |
| 3495 | } |
| 3496 | } |
| 3497 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3498 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */ |
| 3499 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3500 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3501 | unsigned int want_flags = 0; |
| 3502 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3503 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3504 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3505 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) && |
| 3506 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3507 | want_flags |= TX_CON_CLO_SET; |
| 3508 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3509 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 3510 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) || |
| 3511 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3512 | want_flags |= TX_CON_KAL_SET; |
| 3513 | } |
| 3514 | |
| 3515 | 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] | 3516 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3517 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3518 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3519 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3520 | /* If we have no server assigned yet and we're balancing on url_param |
| 3521 | * with a POST request, we may be interested in checking the body for |
| 3522 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3523 | */ |
| 3524 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3525 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3526 | s->be->url_param_post_limit != 0 && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3527 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3528 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3529 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3530 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3531 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3532 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3533 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3534 | #ifdef TCP_QUICKACK |
| 3535 | /* We expect some data from the client. Unless we know for sure |
| 3536 | * we already have a full request, we have to re-enable quick-ack |
| 3537 | * in case we previously disabled it, otherwise we might cause |
| 3538 | * the client to delay further data. |
| 3539 | */ |
| 3540 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3541 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 3542 | (msg->body_len > req->buf.i - txn->req.eoh - 2))) |
Willy Tarreau | fb7508a | 2012-05-21 16:47:54 +0200 | [diff] [blame] | 3543 | setsockopt(si_fd(&s->si[0]), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3544 | #endif |
| 3545 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3546 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3547 | /************************************************************* |
| 3548 | * OK, that's finished for the headers. We have done what we * |
| 3549 | * could. Let's switch to the DATA state. * |
| 3550 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3551 | req->analyse_exp = TICK_ETERNITY; |
| 3552 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3553 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 3554 | /* if the server closes the connection, we want to immediately react |
| 3555 | * and close the socket to save packets and syscalls. |
| 3556 | */ |
| 3557 | req->cons->flags |= SI_FL_NOHALF; |
| 3558 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3559 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3560 | /* OK let's go on with the BODY now */ |
| 3561 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3562 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3563 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3564 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3565 | /* we detected a parsing error. We want to archive this request |
| 3566 | * in the dedicated proxy area for later troubleshooting. |
| 3567 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3568 | 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] | 3569 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3570 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3571 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3572 | txn->status = 400; |
| 3573 | req->analysers = 0; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3574 | 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] | 3575 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3576 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3577 | if (s->listener->counters) |
| 3578 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3579 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3580 | if (!(s->flags & SN_ERR_MASK)) |
| 3581 | s->flags |= SN_ERR_PRXCOND; |
| 3582 | if (!(s->flags & SN_FINST_MASK)) |
| 3583 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3584 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3585 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3586 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3587 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3588 | * returns zero, at the beginning because it prevents any other processing |
| 3589 | * from occurring, and at the end because it terminates the request. |
| 3590 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3591 | 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] | 3592 | { |
| 3593 | struct http_txn *txn = &s->txn; |
| 3594 | |
| 3595 | /* This connection is being tarpitted. The CLIENT side has |
| 3596 | * already set the connect expiration date to the right |
| 3597 | * timeout. We just have to check that the client is still |
| 3598 | * there and that the timeout has not expired. |
| 3599 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3600 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3601 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3602 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3603 | return 0; |
| 3604 | |
| 3605 | /* We will set the queue timer to the time spent, just for |
| 3606 | * logging purposes. We fake a 500 server error, so that the |
| 3607 | * attacker will not suspect his connection has been tarpitted. |
| 3608 | * It will not cause trouble to the logs because we can exclude |
| 3609 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3610 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3611 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3612 | |
| 3613 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3614 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3615 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3616 | |
| 3617 | req->analysers = 0; |
| 3618 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3619 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3620 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3621 | if (s->listener->counters) |
| 3622 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3623 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3624 | if (!(s->flags & SN_ERR_MASK)) |
| 3625 | s->flags |= SN_ERR_PRXCOND; |
| 3626 | if (!(s->flags & SN_FINST_MASK)) |
| 3627 | s->flags |= SN_FINST_T; |
| 3628 | return 0; |
| 3629 | } |
| 3630 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3631 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3632 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3633 | * must only be called after the standard HTTP request processing has occurred, |
| 3634 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3635 | * read more data, or 1 once it has completed its analysis. |
| 3636 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3637 | int http_process_request_body(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3638 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3639 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3640 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3641 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3642 | |
| 3643 | /* We have to parse the HTTP request body to find any required data. |
| 3644 | * "balance url_param check_post" should have been the only way to get |
| 3645 | * into this. We were brought here after HTTP header analysis, so all |
| 3646 | * related structures are ready. |
| 3647 | */ |
| 3648 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3649 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3650 | goto missing_data; |
| 3651 | |
| 3652 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3653 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3654 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3655 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3656 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3657 | struct hdr_ctx ctx; |
| 3658 | ctx.idx = 0; |
| 3659 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 3660 | if (http_find_header2("Expect", 6, req->buf.p, &txn->hdr_idx, &ctx) && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3661 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 3662 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3663 | } |
| 3664 | } |
| 3665 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3666 | } |
| 3667 | |
| 3668 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 3669 | /* we have msg->sov which points to the first byte of message body. |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 3670 | * req->buf.p still points to the beginning of the message and msg->sol |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 3671 | * is still null. We must save the body in msg->next because it |
| 3672 | * survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3673 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 3674 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3675 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3676 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3677 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3678 | else |
| 3679 | msg->msg_state = HTTP_MSG_DATA; |
| 3680 | } |
| 3681 | |
| 3682 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3683 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3684 | * 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] | 3685 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3686 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 3687 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3688 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3689 | if (!ret) |
| 3690 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3691 | else if (ret < 0) { |
| 3692 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3693 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3694 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3695 | } |
| 3696 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3697 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 3698 | * We have the first data byte is in msg->sov. We're waiting for at |
| 3699 | * least <url_param_post_limit> bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3700 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3701 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3702 | if (msg->body_len < limit) |
| 3703 | limit = msg->body_len; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3704 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 3705 | if (req->buf.i - msg->sov >= limit) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3706 | goto http_end; |
| 3707 | |
| 3708 | missing_data: |
| 3709 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 3710 | if (buffer_full(&req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3711 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3712 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3713 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3714 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3715 | 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] | 3716 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3717 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3718 | |
| 3719 | if (!(s->flags & SN_ERR_MASK)) |
| 3720 | s->flags |= SN_ERR_CLITO; |
| 3721 | if (!(s->flags & SN_FINST_MASK)) |
| 3722 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3723 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3724 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3725 | |
| 3726 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3727 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(&req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3728 | /* Not enough data. We'll re-use the http-request |
| 3729 | * timeout here. Ideally, we should set the timeout |
| 3730 | * relative to the accept() date. We just set the |
| 3731 | * request timeout once at the beginning of the |
| 3732 | * request. |
| 3733 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3734 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3735 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3736 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3737 | return 0; |
| 3738 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3739 | |
| 3740 | http_end: |
| 3741 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3742 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3743 | req->analysers &= ~an_bit; |
| 3744 | req->analyse_exp = TICK_ETERNITY; |
| 3745 | return 1; |
| 3746 | |
| 3747 | return_bad_req: /* let's centralize all bad requests */ |
| 3748 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3749 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3750 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3751 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3752 | if (!(s->flags & SN_ERR_MASK)) |
| 3753 | s->flags |= SN_ERR_PRXCOND; |
| 3754 | if (!(s->flags & SN_FINST_MASK)) |
| 3755 | s->flags |= SN_FINST_R; |
| 3756 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3757 | return_err_msg: |
| 3758 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3759 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3760 | if (s->listener->counters) |
| 3761 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3762 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3763 | } |
| 3764 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3765 | /* send a server's name with an outgoing request over an established connection. |
| 3766 | * Note: this function is designed to be called once the request has been scheduled |
| 3767 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 3768 | * proceeding. |
| 3769 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 3770 | 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] | 3771 | |
| 3772 | struct hdr_ctx ctx; |
| 3773 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3774 | char *hdr_name = be->server_id_hdr_name; |
| 3775 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 3776 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3777 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3778 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3779 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 3780 | ctx.idx = 0; |
| 3781 | |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 3782 | old_o = chn->buf.o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3783 | if (old_o) { |
| 3784 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 3785 | b_rew(&chn->buf, old_o); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3786 | } |
| 3787 | |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 3788 | old_i = chn->buf.i; |
| 3789 | 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] | 3790 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3791 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3792 | } |
| 3793 | |
| 3794 | /* Add the new header requested with the server value */ |
| 3795 | hdr_val = trash; |
| 3796 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 3797 | hdr_val += hdr_name_len; |
| 3798 | *hdr_val++ = ':'; |
| 3799 | *hdr_val++ = ' '; |
David du Colombier | 7af4605 | 2012-05-16 14:16:48 +0200 | [diff] [blame] | 3800 | hdr_val += strlcpy2(hdr_val, srv_name, trash + trashlen - hdr_val); |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3801 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3802 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3803 | if (old_o) { |
| 3804 | /* If this was a forwarded request, we must readjust the amount of |
| 3805 | * data to be forwarded in order to take into account the size |
| 3806 | * variations. |
| 3807 | */ |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 3808 | b_adv(&chn->buf, old_o + chn->buf.i - old_i); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3809 | } |
| 3810 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3811 | return 0; |
| 3812 | } |
| 3813 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3814 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3815 | * right now but it works. |
| 3816 | */ |
| 3817 | void http_end_txn_clean_session(struct session *s) |
| 3818 | { |
| 3819 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3820 | * server's connections. We need a safer way to reinitialize buffer |
| 3821 | * flags. We also need a more accurate method for computing per-request |
| 3822 | * data. |
| 3823 | */ |
| 3824 | http_silent_debug(__LINE__, s); |
| 3825 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 3826 | s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 3827 | si_shutr(s->req->cons); |
| 3828 | si_shutw(s->req->cons); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3829 | |
| 3830 | http_silent_debug(__LINE__, s); |
| 3831 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3832 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3833 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3834 | if (unlikely(s->srv_conn)) |
| 3835 | sess_change_server(s, NULL); |
| 3836 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3837 | |
| 3838 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3839 | session_process_counters(s); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 3840 | session_stop_backend_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3841 | |
| 3842 | if (s->txn.status) { |
| 3843 | int n; |
| 3844 | |
| 3845 | n = s->txn.status / 100; |
| 3846 | if (n < 1 || n > 5) |
| 3847 | n = 0; |
| 3848 | |
| 3849 | if (s->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3850 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3851 | |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3852 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3853 | (s->be->mode == PR_MODE_HTTP)) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3854 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3855 | } |
| 3856 | |
| 3857 | /* don't count other requests' data */ |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 3858 | s->logs.bytes_in -= s->req->buf.i; |
| 3859 | s->logs.bytes_out -= s->rep->buf.i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3860 | |
| 3861 | /* let's do a final log if we need it */ |
| 3862 | if (s->logs.logwait && |
| 3863 | !(s->flags & SN_MONITOR) && |
| 3864 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3865 | s->do_log(s); |
| 3866 | } |
| 3867 | |
| 3868 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3869 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3870 | tv_zero(&s->logs.tv_request); |
| 3871 | s->logs.t_queue = -1; |
| 3872 | s->logs.t_connect = -1; |
| 3873 | s->logs.t_data = -1; |
| 3874 | s->logs.t_close = 0; |
| 3875 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3876 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3877 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 3878 | s->logs.bytes_in = s->req->total = s->req->buf.i; |
| 3879 | s->logs.bytes_out = s->rep->total = s->rep->buf.i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3880 | |
| 3881 | if (s->pend_pos) |
| 3882 | pendconn_free(s->pend_pos); |
| 3883 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3884 | if (target_srv(&s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3885 | if (s->flags & SN_CURR_SESS) { |
| 3886 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3887 | target_srv(&s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3888 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3889 | if (may_dequeue_tasks(target_srv(&s->target), s->be)) |
| 3890 | process_srv_queue(target_srv(&s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3891 | } |
| 3892 | |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 3893 | clear_target(&s->target); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3894 | |
| 3895 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
Willy Tarreau | fb7508a | 2012-05-21 16:47:54 +0200 | [diff] [blame] | 3896 | s->req->cons->conn.t.sock.fd = -1; /* just to help with debugging */ |
Willy Tarreau | 505e34a | 2012-07-06 10:17:53 +0200 | [diff] [blame] | 3897 | s->req->cons->conn.flags = CO_FL_NONE; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3898 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 3899 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3900 | s->req->cons->err_loc = NULL; |
| 3901 | s->req->cons->exp = TICK_ETERNITY; |
| 3902 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3903 | s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT); |
| 3904 | 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] | 3905 | s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3906 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3907 | s->txn.meth = 0; |
| 3908 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 3909 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 3910 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3911 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 3912 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3913 | if (s->fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3914 | s->req->flags |= CF_NEVER_WAIT; |
| 3915 | s->rep->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3916 | } |
| 3917 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3918 | /* if the request buffer is not empty, it means we're |
| 3919 | * about to process another request, so send pending |
| 3920 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3921 | * Just don't do this if the buffer is close to be full, |
| 3922 | * because the request will wait for it to flush a little |
| 3923 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3924 | */ |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 3925 | if (s->req->buf.i) { |
| 3926 | if (s->rep->buf.o && |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 3927 | !buffer_full(&s->rep->buf, global.tune.maxrewrite) && |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 3928 | 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] | 3929 | s->rep->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3930 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3931 | |
| 3932 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3933 | channel_auto_read(s->req); |
| 3934 | channel_auto_close(s->req); |
| 3935 | channel_auto_read(s->rep); |
| 3936 | channel_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3937 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 3938 | s->req->analysers = s->listener->analysers; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3939 | s->rep->analysers = 0; |
| 3940 | |
| 3941 | http_silent_debug(__LINE__, s); |
| 3942 | } |
| 3943 | |
| 3944 | |
| 3945 | /* This function updates the request state machine according to the response |
| 3946 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3947 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3948 | * it is only used to find when a request/response couple is complete. Both |
| 3949 | * this function and its equivalent should loop until both return zero. It |
| 3950 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3951 | */ |
| 3952 | int http_sync_req_state(struct session *s) |
| 3953 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3954 | struct channel *chn = s->req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3955 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3956 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3957 | unsigned int old_state = txn->req.msg_state; |
| 3958 | |
| 3959 | http_silent_debug(__LINE__, s); |
| 3960 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 3961 | return 0; |
| 3962 | |
| 3963 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3964 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3965 | * We can shut the read side unless we want to abort_on_close, |
| 3966 | * or we have a POST request. The issue with POST requests is |
| 3967 | * that some browsers still send a CRLF after the request, and |
| 3968 | * this CRLF must be read so that it does not remain in the kernel |
| 3969 | * buffers, otherwise a close could cause an RST on some systems |
| 3970 | * (eg: Linux). |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3971 | */ |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3972 | if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3973 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3974 | |
| 3975 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3976 | goto wait_other_side; |
| 3977 | |
| 3978 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3979 | /* The server has not finished to respond, so we |
| 3980 | * don't want to move in order not to upset it. |
| 3981 | */ |
| 3982 | goto wait_other_side; |
| 3983 | } |
| 3984 | |
| 3985 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3986 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3987 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3988 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3989 | goto wait_other_side; |
| 3990 | } |
| 3991 | |
| 3992 | /* When we get here, it means that both the request and the |
| 3993 | * response have finished receiving. Depending on the connection |
| 3994 | * mode, we'll have to wait for the last bytes to leave in either |
| 3995 | * direction, and sometimes for a close to be effective. |
| 3996 | */ |
| 3997 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3998 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3999 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4000 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 4001 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4002 | } |
| 4003 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4004 | /* Option forceclose is set, or either side wants to close, |
| 4005 | * let's enforce it now that we're not expecting any new |
| 4006 | * data to come. The caller knows the session is complete |
| 4007 | * once both states are CLOSED. |
| 4008 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4009 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4010 | channel_shutr_now(chn); |
| 4011 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4012 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4013 | } |
| 4014 | else { |
| 4015 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4016 | * mode does not set the body analyser, we can't reach this place |
| 4017 | * in tunnel mode, so we're left with keep-alive only. |
| 4018 | * This mode is currently not implemented, we switch to tunnel mode. |
| 4019 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4020 | channel_auto_read(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4021 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4022 | } |
| 4023 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4024 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4025 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4026 | chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4027 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4028 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4029 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4030 | goto http_msg_closing; |
| 4031 | } |
| 4032 | else { |
| 4033 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4034 | goto http_msg_closed; |
| 4035 | } |
| 4036 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4037 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4038 | } |
| 4039 | |
| 4040 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4041 | http_msg_closing: |
| 4042 | /* nothing else to forward, just waiting for the output buffer |
| 4043 | * to be empty and for the shutw_now to take effect. |
| 4044 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4045 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4046 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4047 | goto http_msg_closed; |
| 4048 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4049 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4050 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4051 | goto wait_other_side; |
| 4052 | } |
| 4053 | } |
| 4054 | |
| 4055 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4056 | http_msg_closed: |
| 4057 | goto wait_other_side; |
| 4058 | } |
| 4059 | |
| 4060 | wait_other_side: |
| 4061 | http_silent_debug(__LINE__, s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4062 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4063 | } |
| 4064 | |
| 4065 | |
| 4066 | /* This function updates the response state machine according to the request |
| 4067 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4068 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4069 | * it is only used to find when a request/response couple is complete. Both |
| 4070 | * this function and its equivalent should loop until both return zero. It |
| 4071 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4072 | */ |
| 4073 | int http_sync_res_state(struct session *s) |
| 4074 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4075 | struct channel *chn = s->rep; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4076 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4077 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4078 | unsigned int old_state = txn->rsp.msg_state; |
| 4079 | |
| 4080 | http_silent_debug(__LINE__, s); |
| 4081 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4082 | return 0; |
| 4083 | |
| 4084 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4085 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4086 | * still monitor the server connection for a possible close |
| 4087 | * while the request is being uploaded, so we don't disable |
| 4088 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4089 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4090 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4091 | |
| 4092 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4093 | goto wait_other_side; |
| 4094 | |
| 4095 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4096 | /* The client seems to still be sending data, probably |
| 4097 | * because we got an error response during an upload. |
| 4098 | * We have the choice of either breaking the connection |
| 4099 | * or letting it pass through. Let's do the later. |
| 4100 | */ |
| 4101 | goto wait_other_side; |
| 4102 | } |
| 4103 | |
| 4104 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4105 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4106 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4107 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4108 | goto wait_other_side; |
| 4109 | } |
| 4110 | |
| 4111 | /* When we get here, it means that both the request and the |
| 4112 | * response have finished receiving. Depending on the connection |
| 4113 | * mode, we'll have to wait for the last bytes to leave in either |
| 4114 | * direction, and sometimes for a close to be effective. |
| 4115 | */ |
| 4116 | |
| 4117 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4118 | /* Server-close mode : shut read and wait for the request |
| 4119 | * side to close its output buffer. The caller will detect |
| 4120 | * when we're in DONE and the other is in CLOSED and will |
| 4121 | * catch that for the final cleanup. |
| 4122 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4123 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4124 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4125 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4126 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4127 | /* Option forceclose is set, or either side wants to close, |
| 4128 | * let's enforce it now that we're not expecting any new |
| 4129 | * data to come. The caller knows the session is complete |
| 4130 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4131 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4132 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4133 | channel_shutr_now(chn); |
| 4134 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4135 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4136 | } |
| 4137 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4138 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4139 | * mode does not set the body analyser, we can't reach this place |
| 4140 | * in tunnel mode, so we're left with keep-alive only. |
| 4141 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4142 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4143 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4144 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4145 | } |
| 4146 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4147 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4148 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4149 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4150 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4151 | goto http_msg_closing; |
| 4152 | } |
| 4153 | else { |
| 4154 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4155 | goto http_msg_closed; |
| 4156 | } |
| 4157 | } |
| 4158 | goto wait_other_side; |
| 4159 | } |
| 4160 | |
| 4161 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4162 | http_msg_closing: |
| 4163 | /* nothing else to forward, just waiting for the output buffer |
| 4164 | * to be empty and for the shutw_now to take effect. |
| 4165 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4166 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4167 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4168 | goto http_msg_closed; |
| 4169 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4170 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4171 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4172 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4173 | if (target_srv(&s->target)) |
| 4174 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4175 | goto wait_other_side; |
| 4176 | } |
| 4177 | } |
| 4178 | |
| 4179 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4180 | http_msg_closed: |
| 4181 | /* drop any pending data */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4182 | bi_erase(chn); |
| 4183 | channel_auto_close(chn); |
| 4184 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4185 | goto wait_other_side; |
| 4186 | } |
| 4187 | |
| 4188 | wait_other_side: |
| 4189 | http_silent_debug(__LINE__, s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4190 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | |
| 4194 | /* Resync the request and response state machines. Return 1 if either state |
| 4195 | * changes. |
| 4196 | */ |
| 4197 | int http_resync_states(struct session *s) |
| 4198 | { |
| 4199 | struct http_txn *txn = &s->txn; |
| 4200 | int old_req_state = txn->req.msg_state; |
| 4201 | int old_res_state = txn->rsp.msg_state; |
| 4202 | |
| 4203 | http_silent_debug(__LINE__, s); |
| 4204 | http_sync_req_state(s); |
| 4205 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4206 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4207 | if (!http_sync_res_state(s)) |
| 4208 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4209 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4210 | if (!http_sync_req_state(s)) |
| 4211 | break; |
| 4212 | } |
| 4213 | http_silent_debug(__LINE__, s); |
| 4214 | /* OK, both state machines agree on a compatible state. |
| 4215 | * There are a few cases we're interested in : |
| 4216 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4217 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4218 | * directions, so let's simply disable both analysers. |
| 4219 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4220 | * request. |
| 4221 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4222 | * with server-close mode means we've completed one request and we |
| 4223 | * must re-initialize the server connection. |
| 4224 | */ |
| 4225 | |
| 4226 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4227 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4228 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4229 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4230 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4231 | channel_auto_close(s->req); |
| 4232 | channel_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4233 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4234 | channel_auto_close(s->rep); |
| 4235 | channel_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4236 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4237 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4238 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4239 | txn->req.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4240 | (s->rep->flags & CF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4241 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4242 | channel_auto_close(s->rep); |
| 4243 | channel_auto_read(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4244 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4245 | channel_abort(s->req); |
| 4246 | channel_auto_close(s->req); |
| 4247 | channel_auto_read(s->req); |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4248 | bi_erase(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4249 | } |
| 4250 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4251 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4252 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4253 | /* server-close: terminate this server connection and |
| 4254 | * reinitialize a fresh-new transaction. |
| 4255 | */ |
| 4256 | http_end_txn_clean_session(s); |
| 4257 | } |
| 4258 | |
| 4259 | http_silent_debug(__LINE__, s); |
| 4260 | return txn->req.msg_state != old_req_state || |
| 4261 | txn->rsp.msg_state != old_res_state; |
| 4262 | } |
| 4263 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4264 | /* This function is an analyser which forwards request body (including chunk |
| 4265 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4266 | * zero byte. The only situation where it must not be called is when we're in |
| 4267 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4268 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4269 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4270 | * 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] | 4271 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4272 | * bytes of pending data + the headers if not already done (between sol and sov). |
| 4273 | * It eventually adjusts sol to match sov after the data in between have been sent. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4274 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4275 | 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] | 4276 | { |
| 4277 | struct http_txn *txn = &s->txn; |
| 4278 | struct http_msg *msg = &s->txn.req; |
| 4279 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4280 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4281 | return 0; |
| 4282 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4283 | if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
| 4284 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf.o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4285 | /* Output closed while we were sending data. We must abort and |
| 4286 | * wake the other side up. |
| 4287 | */ |
| 4288 | msg->msg_state = HTTP_MSG_ERROR; |
| 4289 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4290 | return 1; |
| 4291 | } |
| 4292 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4293 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4294 | channel_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4295 | |
| 4296 | /* Note that we don't have to send 100-continue back because we don't |
| 4297 | * need the data to complete our job, and it's up to the server to |
| 4298 | * decide whether to return 100, 417 or anything else in return of |
| 4299 | * an "Expect: 100-continue" header. |
| 4300 | */ |
| 4301 | |
| 4302 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4303 | /* we have msg->sov which points to the first byte of message body. |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4304 | * req->buf.p still points to the beginning of the message and msg->sol |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4305 | * is still null. We must save the body in msg->next because it |
| 4306 | * survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4307 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4308 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4309 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4310 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4311 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4312 | else { |
| 4313 | msg->msg_state = HTTP_MSG_DATA; |
| 4314 | } |
| 4315 | } |
| 4316 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4317 | while (1) { |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4318 | unsigned int bytes; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4319 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4320 | http_silent_debug(__LINE__, s); |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4321 | /* we may have some data pending between sol and sov */ |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4322 | bytes = msg->sov - msg->sol; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4323 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4324 | msg->sol = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4325 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4326 | msg->chunk_len += bytes; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4327 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4328 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4329 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4330 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4331 | /* must still forward */ |
| 4332 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4333 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4334 | |
| 4335 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4336 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4337 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4338 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4339 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4340 | } |
| 4341 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4342 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4343 | * 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] | 4344 | * TRAILERS state. |
| 4345 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4346 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4347 | |
| 4348 | if (!ret) |
| 4349 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4350 | else if (ret < 0) { |
| 4351 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4352 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4353 | 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] | 4354 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4355 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4356 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4357 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4358 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4359 | /* we want the CRLF after the data */ |
| 4360 | int ret; |
| 4361 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4362 | ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4363 | |
| 4364 | if (ret == 0) |
| 4365 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4366 | else if (ret < 0) { |
| 4367 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4368 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4369 | http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4370 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4371 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4372 | /* we're in MSG_CHUNK_SIZE now */ |
| 4373 | } |
| 4374 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4375 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4376 | |
| 4377 | if (ret == 0) |
| 4378 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4379 | else if (ret < 0) { |
| 4380 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4381 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4382 | 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] | 4383 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4384 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4385 | /* we're in HTTP_MSG_DONE now */ |
| 4386 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4387 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4388 | int old_state = msg->msg_state; |
| 4389 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4390 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4391 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4392 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4393 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4394 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4395 | if (http_resync_states(s)) { |
| 4396 | /* some state changes occurred, maybe the analyser |
| 4397 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4398 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4399 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4400 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4401 | /* request errors are most likely due to |
| 4402 | * the server aborting the transfer. |
| 4403 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4404 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4405 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4406 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4407 | 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] | 4408 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4409 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4410 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4411 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4412 | |
| 4413 | /* If "option abortonclose" is set on the backend, we |
| 4414 | * want to monitor the client's connection and forward |
| 4415 | * any shutdown notification to the server, which will |
| 4416 | * decide whether to close or to go on processing the |
| 4417 | * request. |
| 4418 | */ |
| 4419 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4420 | channel_auto_read(req); |
| 4421 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4422 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4423 | else if (s->txn.meth == HTTP_METH_POST) { |
| 4424 | /* POST requests may require to read extra CRLF |
| 4425 | * sent by broken browsers and which could cause |
| 4426 | * an RST to be sent upon close on some systems |
| 4427 | * (eg: Linux). |
| 4428 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4429 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4430 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4431 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4432 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4433 | } |
| 4434 | } |
| 4435 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4436 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4437 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4438 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4439 | if (!(s->flags & SN_ERR_MASK)) |
| 4440 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4441 | if (!(s->flags & SN_FINST_MASK)) { |
| 4442 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4443 | s->flags |= SN_FINST_H; |
| 4444 | else |
| 4445 | s->flags |= SN_FINST_D; |
| 4446 | } |
| 4447 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4448 | s->fe->fe_counters.cli_aborts++; |
| 4449 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4450 | if (target_srv(&s->target)) |
| 4451 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4452 | |
| 4453 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4454 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4455 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4456 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4457 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4458 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4459 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4460 | /* 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] | 4461 | * 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] | 4462 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4463 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4464 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4465 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4466 | /* 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] | 4467 | * 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] | 4468 | * 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] | 4469 | * modes are already handled by the stream sock layer. We must not do |
| 4470 | * this in content-length mode because it could present the MSG_MORE |
| 4471 | * flag with the last block of forwarded data, which would cause an |
| 4472 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4473 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4474 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4475 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4476 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4477 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4478 | return 0; |
| 4479 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4480 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4481 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4482 | if (s->listener->counters) |
| 4483 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4484 | return_bad_req_stats_ok: |
| 4485 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4486 | if (txn->status) { |
| 4487 | /* Note: we don't send any error if some data were already sent */ |
| 4488 | stream_int_retnclose(req->prod, NULL); |
| 4489 | } else { |
| 4490 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4491 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4492 | } |
| 4493 | req->analysers = 0; |
| 4494 | 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] | 4495 | |
| 4496 | if (!(s->flags & SN_ERR_MASK)) |
| 4497 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4498 | if (!(s->flags & SN_FINST_MASK)) { |
| 4499 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4500 | s->flags |= SN_FINST_H; |
| 4501 | else |
| 4502 | s->flags |= SN_FINST_D; |
| 4503 | } |
| 4504 | return 0; |
| 4505 | |
| 4506 | aborted_xfer: |
| 4507 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4508 | if (txn->status) { |
| 4509 | /* Note: we don't send any error if some data were already sent */ |
| 4510 | stream_int_retnclose(req->prod, NULL); |
| 4511 | } else { |
| 4512 | txn->status = 502; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4513 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4514 | } |
| 4515 | req->analysers = 0; |
| 4516 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 4517 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4518 | s->fe->fe_counters.srv_aborts++; |
| 4519 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4520 | if (target_srv(&s->target)) |
| 4521 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4522 | |
| 4523 | if (!(s->flags & SN_ERR_MASK)) |
| 4524 | s->flags |= SN_ERR_SRVCL; |
| 4525 | if (!(s->flags & SN_FINST_MASK)) { |
| 4526 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4527 | s->flags |= SN_FINST_H; |
| 4528 | else |
| 4529 | s->flags |= SN_FINST_D; |
| 4530 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4531 | return 0; |
| 4532 | } |
| 4533 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4534 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4535 | * processing can continue on next analysers, or zero if it either needs more |
| 4536 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4537 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4538 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4539 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4540 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4541 | 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] | 4542 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4543 | struct http_txn *txn = &s->txn; |
| 4544 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4545 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4546 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4547 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4548 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4549 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4550 | 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] | 4551 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4552 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4553 | rep, |
| 4554 | rep->rex, rep->wex, |
| 4555 | rep->flags, |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4556 | rep->buf.i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4557 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4558 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4559 | /* |
| 4560 | * Now parse the partial (or complete) lines. |
| 4561 | * We will check the response syntax, and also join multi-line |
| 4562 | * headers. An index of all the lines will be elaborated while |
| 4563 | * parsing. |
| 4564 | * |
| 4565 | * For the parsing, we use a 28 states FSM. |
| 4566 | * |
| 4567 | * Here is the information we currently have : |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4568 | * rep->buf.p = beginning of response |
| 4569 | * rep->buf.p + msg->eoh = end of processed headers / start of current one |
| 4570 | * rep->buf.p + rep->buf.i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4571 | * msg->eol = end of current header or line (LF or CRLF) |
| 4572 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4573 | */ |
| 4574 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4575 | /* There's a protected area at the end of the buffer for rewriting |
| 4576 | * purposes. We don't want to start to parse the request if the |
| 4577 | * protected area is affected, because we may have to move processed |
| 4578 | * data later, which is much more complicated. |
| 4579 | */ |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4580 | if (buffer_not_empty(&rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 4581 | if (unlikely(channel_full(rep) || |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4582 | bi_end(&rep->buf) < b_ptr(&rep->buf, msg->next) || |
| 4583 | bi_end(&rep->buf) > rep->buf.data + rep->buf.size - global.tune.maxrewrite)) { |
| 4584 | if (rep->buf.o) { |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4585 | /* some data has still not left the buffer, wake us once that's done */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4586 | if (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] | 4587 | goto abort_response; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4588 | channel_dont_close(rep); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4589 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4590 | return 0; |
| 4591 | } |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4592 | if (rep->buf.i <= rep->buf.size - global.tune.maxrewrite) |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4593 | buffer_slow_realign(&msg->chn->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4594 | } |
| 4595 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4596 | if (likely(msg->next < rep->buf.i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 4597 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4598 | } |
| 4599 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4600 | /* 1: we might have to print this header in debug mode */ |
| 4601 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4602 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4603 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4604 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4605 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4606 | sol = rep->buf.p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 4607 | 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] | 4608 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4609 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4610 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4611 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4612 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4613 | while (cur_idx) { |
| 4614 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4615 | debug_hdr("srvhdr", s, sol, eol); |
| 4616 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4617 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4618 | } |
| 4619 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4620 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4621 | /* |
| 4622 | * Now we quickly check if we have found a full valid response. |
| 4623 | * If not so, we check the FD and buffer states before leaving. |
| 4624 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4625 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4626 | * responses are checked first. |
| 4627 | * |
| 4628 | * Depending on whether the client is still there or not, we |
| 4629 | * may send an error response back or not. Note that normally |
| 4630 | * we should only check for HTTP status there, and check I/O |
| 4631 | * errors somewhere else. |
| 4632 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4633 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4634 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4635 | /* Invalid response */ |
| 4636 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4637 | /* we detected a parsing error. We want to archive this response |
| 4638 | * in the dedicated proxy area for later troubleshooting. |
| 4639 | */ |
| 4640 | hdr_response_bad: |
| 4641 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4642 | 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] | 4643 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4644 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4645 | if (target_srv(&s->target)) { |
| 4646 | target_srv(&s->target)->counters.failed_resp++; |
| 4647 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4648 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4649 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4650 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4651 | rep->analysers = 0; |
| 4652 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4653 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4654 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4655 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4656 | |
| 4657 | if (!(s->flags & SN_ERR_MASK)) |
| 4658 | s->flags |= SN_ERR_PRXCOND; |
| 4659 | if (!(s->flags & SN_FINST_MASK)) |
| 4660 | s->flags |= SN_FINST_H; |
| 4661 | |
| 4662 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4663 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4664 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4665 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 4666 | else if (buffer_full(&rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 4667 | if (msg->err_pos < 0) |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4668 | msg->err_pos = rep->buf.i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4669 | goto hdr_response_bad; |
| 4670 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4671 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4672 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4673 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4674 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4675 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4676 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4677 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4678 | if (target_srv(&s->target)) { |
| 4679 | target_srv(&s->target)->counters.failed_resp++; |
| 4680 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4681 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4682 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4683 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4684 | rep->analysers = 0; |
| 4685 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4686 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4687 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4688 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4689 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4690 | if (!(s->flags & SN_ERR_MASK)) |
| 4691 | s->flags |= SN_ERR_SRVCL; |
| 4692 | if (!(s->flags & SN_FINST_MASK)) |
| 4693 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4694 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4695 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4696 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4697 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4698 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4699 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4700 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4701 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4702 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4703 | if (target_srv(&s->target)) { |
| 4704 | target_srv(&s->target)->counters.failed_resp++; |
| 4705 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4706 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4707 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4708 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4709 | rep->analysers = 0; |
| 4710 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4711 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4712 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4713 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4714 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4715 | if (!(s->flags & SN_ERR_MASK)) |
| 4716 | s->flags |= SN_ERR_SRVTO; |
| 4717 | if (!(s->flags & SN_FINST_MASK)) |
| 4718 | s->flags |= SN_FINST_H; |
| 4719 | return 0; |
| 4720 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4721 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4722 | /* 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] | 4723 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4724 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4725 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4726 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4727 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4728 | if (target_srv(&s->target)) { |
| 4729 | target_srv(&s->target)->counters.failed_resp++; |
| 4730 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4731 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4732 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4733 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4734 | rep->analysers = 0; |
| 4735 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4736 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4737 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4738 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4739 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4740 | if (!(s->flags & SN_ERR_MASK)) |
| 4741 | s->flags |= SN_ERR_SRVCL; |
| 4742 | if (!(s->flags & SN_FINST_MASK)) |
| 4743 | s->flags |= SN_FINST_H; |
| 4744 | return 0; |
| 4745 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4746 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4747 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4748 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4749 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4750 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4751 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4752 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4753 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4754 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4755 | |
| 4756 | if (!(s->flags & SN_ERR_MASK)) |
| 4757 | s->flags |= SN_ERR_CLICL; |
| 4758 | if (!(s->flags & SN_FINST_MASK)) |
| 4759 | s->flags |= SN_FINST_H; |
| 4760 | |
| 4761 | /* process_session() will take care of the error */ |
| 4762 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4763 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4764 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4765 | channel_dont_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4766 | return 0; |
| 4767 | } |
| 4768 | |
| 4769 | /* More interesting part now : we know that we have a complete |
| 4770 | * response which at least looks like HTTP. We have an indicator |
| 4771 | * of each header's length, so we can parse them quickly. |
| 4772 | */ |
| 4773 | |
| 4774 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4775 | 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] | 4776 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4777 | /* |
| 4778 | * 1: get the status code |
| 4779 | */ |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4780 | n = rep->buf.p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4781 | if (n < 1 || n > 5) |
| 4782 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4783 | /* when the client triggers a 4xx from the server, it's most often due |
| 4784 | * to a missing object or permission. These events should be tracked |
| 4785 | * because if they happen often, it may indicate a brute force or a |
| 4786 | * vulnerability scan. |
| 4787 | */ |
| 4788 | if (n == 4) |
| 4789 | session_inc_http_err_ctr(s); |
| 4790 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4791 | if (target_srv(&s->target)) |
| 4792 | target_srv(&s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4793 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4794 | /* check if the response is HTTP/1.1 or above */ |
| 4795 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4796 | ((rep->buf.p[5] > '1') || |
| 4797 | ((rep->buf.p[5] == '1') && (rep->buf.p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4798 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4799 | |
| 4800 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4801 | txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_CON_CLO_SET|TX_CON_KAL_SET); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4802 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4803 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4804 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4805 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4806 | 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] | 4807 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4808 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4809 | * and 505 are triggered on demand by client request, so we must not |
| 4810 | * count them as server failures. |
| 4811 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4812 | if (target_srv(&s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4813 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4814 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4815 | else |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4816 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4817 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4818 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4819 | /* |
| 4820 | * 2: check for cacheability. |
| 4821 | */ |
| 4822 | |
| 4823 | switch (txn->status) { |
| 4824 | case 200: |
| 4825 | case 203: |
| 4826 | case 206: |
| 4827 | case 300: |
| 4828 | case 301: |
| 4829 | case 410: |
| 4830 | /* RFC2616 @13.4: |
| 4831 | * "A response received with a status code of |
| 4832 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4833 | * by a cache (...) unless a cache-control |
| 4834 | * directive prohibits caching." |
| 4835 | * |
| 4836 | * RFC2616 @9.5: POST method : |
| 4837 | * "Responses to this method are not cacheable, |
| 4838 | * unless the response includes appropriate |
| 4839 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4840 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4841 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 4842 | ((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] | 4843 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4844 | break; |
| 4845 | default: |
| 4846 | break; |
| 4847 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4848 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4849 | /* |
| 4850 | * 3: we may need to capture headers |
| 4851 | */ |
| 4852 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 4853 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4854 | capture_headers(rep->buf.p, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4855 | txn->rsp.cap, s->fe->rsp_cap); |
| 4856 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4857 | /* 4: determine the transfer-length. |
| 4858 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4859 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4860 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4861 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4862 | * All responses to the HEAD request method MUST NOT include a |
| 4863 | * message-body, even though the presence of entity-header fields |
| 4864 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4865 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4866 | * message-body. All other responses do include a message-body, |
| 4867 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4868 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4869 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4870 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4871 | * always terminated by the first empty line after the header fields, |
| 4872 | * regardless of the entity-header fields present in the message. |
| 4873 | * |
| 4874 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4875 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4876 | * transfer-length is defined by the use of this transfer-coding. |
| 4877 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4878 | * transfer-coding is not present, the transfer-length is defined by |
| 4879 | * the sender closing the connection. |
| 4880 | * |
| 4881 | * 3. If a Content-Length header field is present, its decimal value in |
| 4882 | * OCTETs represents both the entity-length and the transfer-length. |
| 4883 | * If a message is received with both a Transfer-Encoding header |
| 4884 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4885 | * |
| 4886 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4887 | * the transfer-length is not otherwise specified, then this self- |
| 4888 | * delimiting media type defines the transfer-length. This media |
| 4889 | * type MUST NOT be used unless the sender knows that the recipient |
| 4890 | * can parse it; the presence in a request of a Range header with |
| 4891 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4892 | * client can parse multipart/byteranges responses. |
| 4893 | * |
| 4894 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4895 | */ |
| 4896 | |
| 4897 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4898 | * 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] | 4899 | * 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] | 4900 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4901 | */ |
| 4902 | if (txn->meth == HTTP_METH_HEAD || |
| 4903 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4904 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4905 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4906 | goto skip_content_length; |
| 4907 | } |
| 4908 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4909 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4910 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4911 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4912 | 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] | 4913 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4914 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 4915 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4916 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4917 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4918 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4919 | break; |
| 4920 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4921 | } |
| 4922 | |
| 4923 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 4924 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4925 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4926 | 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] | 4927 | signed long long cl; |
| 4928 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4929 | if (!ctx.vlen) { |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4930 | msg->err_pos = ctx.line + ctx.val - rep->buf.p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4931 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4932 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4933 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4934 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4935 | msg->err_pos = ctx.line + ctx.val - rep->buf.p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4936 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4937 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4938 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4939 | if (cl < 0) { |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4940 | msg->err_pos = ctx.line + ctx.val - rep->buf.p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4941 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4942 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4943 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4944 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4945 | msg->err_pos = ctx.line + ctx.val - rep->buf.p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4946 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4947 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4948 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4949 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4950 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4951 | } |
| 4952 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4953 | /* FIXME: we should also implement the multipart/byterange method. |
| 4954 | * For now on, we resort to close mode in this case (unknown length). |
| 4955 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4956 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4957 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4958 | /* end of job, return OK */ |
| 4959 | rep->analysers &= ~an_bit; |
| 4960 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4961 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4962 | return 1; |
| 4963 | } |
| 4964 | |
| 4965 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4966 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 4967 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 4968 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4969 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4970 | int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4971 | { |
| 4972 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4973 | struct http_msg *msg = &txn->rsp; |
| 4974 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4975 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4976 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4977 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4978 | now_ms, __FUNCTION__, |
| 4979 | t, |
| 4980 | rep, |
| 4981 | rep->rex, rep->wex, |
| 4982 | rep->flags, |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 4983 | rep->buf.i, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4984 | rep->analysers); |
| 4985 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4986 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4987 | return 0; |
| 4988 | |
| 4989 | rep->analysers &= ~an_bit; |
| 4990 | rep->analyse_exp = TICK_ETERNITY; |
| 4991 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4992 | /* Now we have to check if we need to modify the Connection header. |
| 4993 | * This is more difficult on the response than it is on the request, |
| 4994 | * because we can have two different HTTP versions and we don't know |
| 4995 | * how the client will interprete a response. For instance, let's say |
| 4996 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4997 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4998 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4999 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5000 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5001 | * indicating how a request MAY be understood by the client. In case |
| 5002 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5003 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5004 | * will fall back to explicit close. Note that we won't take risks with |
| 5005 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5006 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5007 | */ |
| 5008 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5009 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5010 | txn->status == 101)) { |
| 5011 | /* Either we've established an explicit tunnel, or we're |
| 5012 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5013 | * to understand the next protocols. We have to switch to tunnel |
| 5014 | * mode, so that we transfer the request and responses then let |
| 5015 | * this protocol pass unmodified. When we later implement specific |
| 5016 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5017 | * header which contains information about that protocol for |
| 5018 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5019 | */ |
| 5020 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5021 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5022 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5023 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 5024 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5025 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5026 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5027 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5028 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5029 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5030 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5031 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5032 | /* now adjust header transformations depending on current state */ |
| 5033 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5034 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5035 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5036 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5037 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5038 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5039 | else { /* SCL / KAL */ |
| 5040 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5041 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5042 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5043 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5044 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5045 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5046 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5047 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5048 | /* Some keep-alive responses are converted to Server-close if |
| 5049 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5050 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5051 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5052 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5053 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5054 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5055 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5056 | } |
| 5057 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5058 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5059 | /* |
| 5060 | * 3: we will have to evaluate the filters. |
| 5061 | * As opposed to version 1.2, now they will be evaluated in the |
| 5062 | * filters order and not in the header order. This means that |
| 5063 | * each filter has to be validated among all headers. |
| 5064 | * |
| 5065 | * Filters are tried with ->be first, then with ->fe if it is |
| 5066 | * different from ->be. |
| 5067 | */ |
| 5068 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5069 | cur_proxy = t->be; |
| 5070 | while (1) { |
| 5071 | struct proxy *rule_set = cur_proxy; |
| 5072 | |
| 5073 | /* try headers filters */ |
| 5074 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5075 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5076 | return_bad_resp: |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5077 | if (target_srv(&t->target)) { |
| 5078 | target_srv(&t->target)->counters.failed_resp++; |
| 5079 | health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5080 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5081 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5082 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5083 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5084 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5085 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5086 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5087 | stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5088 | if (!(t->flags & SN_ERR_MASK)) |
| 5089 | t->flags |= SN_ERR_PRXCOND; |
| 5090 | if (!(t->flags & SN_FINST_MASK)) |
| 5091 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 5092 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5093 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5094 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5095 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5096 | /* has the response been denied ? */ |
| 5097 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5098 | if (target_srv(&t->target)) |
| 5099 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5100 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5101 | t->be->be_counters.denied_resp++; |
| 5102 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5103 | if (t->listener->counters) |
| 5104 | t->listener->counters->denied_resp++; |
| 5105 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5106 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 5107 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5108 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5109 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5110 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5111 | if (txn->status < 200) |
| 5112 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5113 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 5114 | int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5115 | ret = acl_pass(ret); |
| 5116 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5117 | ret = !ret; |
| 5118 | if (!ret) |
| 5119 | continue; |
| 5120 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5121 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5122 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5123 | } |
| 5124 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5125 | /* check whether we're already working on the frontend */ |
| 5126 | if (cur_proxy == t->fe) |
| 5127 | break; |
| 5128 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5129 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5130 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5131 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5132 | * We may be facing a 100-continue response, in which case this |
| 5133 | * is not the right response, and we're waiting for the next one. |
| 5134 | * Let's allow this response to go to the client and wait for the |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5135 | * next one. |
| 5136 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5137 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5138 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5139 | msg->next -= channel_forward(rep, msg->next); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5140 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5141 | txn->status = 0; |
| 5142 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5143 | return 1; |
| 5144 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5145 | else if (unlikely(txn->status < 200)) |
| 5146 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5147 | |
| 5148 | /* we don't have any 1xx status code now */ |
| 5149 | |
| 5150 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5151 | * 4: check for server cookie. |
| 5152 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5153 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5154 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5155 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5156 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5157 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5158 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5159 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5160 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5161 | if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5162 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5163 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5164 | /* |
| 5165 | * 6: add server cookie in the response if needed |
| 5166 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5167 | if (target_srv(&t->target) && (t->be->ck_opts & PR_CK_INS) && |
| 5168 | !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5169 | (!(t->flags & SN_DIRECT) || |
| 5170 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5171 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5172 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5173 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5174 | (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5175 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5176 | int len; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5177 | /* the server is known, it's not the one the client requested, or the |
| 5178 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5179 | * insert a set-cookie here, except if we want to insert only on POST |
| 5180 | * requests and this one isn't. Note that servers which don't have cookies |
| 5181 | * (eg: some backup servers) will return a full cookie removal request. |
| 5182 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5183 | if (!target_srv(&t->target)->cookie) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5184 | len = sprintf(trash, |
| 5185 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5186 | t->be->cookie_name); |
| 5187 | } |
| 5188 | else { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5189 | len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5190 | |
| 5191 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5192 | /* emit last_date, which is mandatory */ |
| 5193 | trash[len++] = COOKIE_DELIM_DATE; |
| 5194 | s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5; |
| 5195 | if (t->be->cookie_maxlife) { |
| 5196 | /* emit first_date, which is either the original one or |
| 5197 | * the current date. |
| 5198 | */ |
| 5199 | trash[len++] = COOKIE_DELIM_DATE; |
| 5200 | s30tob64(txn->cookie_first_date ? |
| 5201 | txn->cookie_first_date >> 2 : |
| 5202 | (date.tv_sec+3) >> 2, trash + len); |
| 5203 | len += 5; |
| 5204 | } |
| 5205 | } |
| 5206 | len += sprintf(trash + len, "; path=/"); |
| 5207 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5208 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5209 | if (t->be->cookie_domain) |
| 5210 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5211 | |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5212 | if (t->be->ck_opts & PR_CK_HTTPONLY) |
| 5213 | len += sprintf(trash+len, "; HttpOnly"); |
| 5214 | |
| 5215 | if (t->be->ck_opts & PR_CK_SECURE) |
| 5216 | len += sprintf(trash+len, "; Secure"); |
| 5217 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5218 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5219 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5220 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5221 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5222 | if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT)) |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5223 | /* the server did not change, only the date was updated */ |
| 5224 | txn->flags |= TX_SCK_UPDATED; |
| 5225 | else |
| 5226 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5227 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5228 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5229 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5230 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5231 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5232 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5233 | if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5234 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5235 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5236 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5237 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5238 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5239 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5240 | } |
| 5241 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5242 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5243 | /* |
| 5244 | * 7: check if result will be cacheable with a cookie. |
| 5245 | * We'll block the response if security checks have caught |
| 5246 | * nasty things such as a cacheable cookie. |
| 5247 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5248 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5249 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5250 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5251 | |
| 5252 | /* we're in presence of a cacheable response containing |
| 5253 | * a set-cookie header. We'll block it as requested by |
| 5254 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5255 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5256 | if (target_srv(&t->target)) |
| 5257 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5258 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5259 | t->be->be_counters.denied_resp++; |
| 5260 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5261 | if (t->listener->counters) |
| 5262 | t->listener->counters->denied_resp++; |
| 5263 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5264 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5265 | t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>"); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5266 | send_log(t->be, LOG_ALERT, |
| 5267 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5268 | t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>"); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5269 | goto return_srv_prx_502; |
| 5270 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5271 | |
| 5272 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5273 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5274 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5275 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 5276 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) { |
| 5277 | unsigned int want_flags = 0; |
| 5278 | |
| 5279 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5280 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5281 | /* we want a keep-alive response here. Keep-alive header |
| 5282 | * required if either side is not 1.1. |
| 5283 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5284 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5285 | want_flags |= TX_CON_KAL_SET; |
| 5286 | } |
| 5287 | else { |
| 5288 | /* we want a close response here. Close header required if |
| 5289 | * the server is 1.1, regardless of the client. |
| 5290 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5291 | if (msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5292 | want_flags |= TX_CON_CLO_SET; |
| 5293 | } |
| 5294 | |
| 5295 | 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] | 5296 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5297 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5298 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5299 | skip_header_mangling: |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5300 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5301 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5302 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5303 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5304 | /************************************************************* |
| 5305 | * OK, that's finished for the headers. We have done what we * |
| 5306 | * could. Let's switch to the DATA state. * |
| 5307 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5308 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5309 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5310 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5311 | /* if the user wants to log as soon as possible, without counting |
| 5312 | * bytes from the server, then this is the right moment. We have |
| 5313 | * to temporarily assign bytes_out to log what we currently have. |
| 5314 | */ |
| 5315 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 5316 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5317 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 5318 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5319 | t->logs.bytes_out = 0; |
| 5320 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5321 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5322 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 5323 | * otherwise we would not let the client side wake up. |
| 5324 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5325 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5326 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5327 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5328 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5329 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5330 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5331 | /* This function is an analyser which forwards response body (including chunk |
| 5332 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5333 | * zero byte. The only situation where it must not be called is when we're in |
| 5334 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5335 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5336 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5337 | * 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] | 5338 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5339 | * bytes of pending data + the headers if not already done (between sol and sov). |
| 5340 | * It eventually adjusts sol to match sov after the data in between have been sent. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5341 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5342 | 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] | 5343 | { |
| 5344 | struct http_txn *txn = &s->txn; |
| 5345 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5346 | unsigned int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5347 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5348 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5349 | return 0; |
| 5350 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5351 | if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
| 5352 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf.o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5353 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5354 | /* Output closed while we were sending data. We must abort and |
| 5355 | * wake the other side up. |
| 5356 | */ |
| 5357 | msg->msg_state = HTTP_MSG_ERROR; |
| 5358 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5359 | return 1; |
| 5360 | } |
| 5361 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5362 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5363 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5364 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5365 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 5366 | /* we have msg->sov which points to the first byte of message body. |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 5367 | * rep->buf.p still points to the beginning of the message and msg->sol |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5368 | * is still null. We must save the body in msg->next because it |
| 5369 | * survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5370 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 5371 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5372 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5373 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5374 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5375 | else { |
| 5376 | msg->msg_state = HTTP_MSG_DATA; |
| 5377 | } |
| 5378 | } |
| 5379 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5380 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5381 | http_silent_debug(__LINE__, s); |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5382 | /* we may have some data pending between sol and sov */ |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5383 | bytes = msg->sov - msg->sol; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5384 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5385 | msg->sol = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5386 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5387 | msg->chunk_len += bytes; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5388 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5389 | } |
| 5390 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5391 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5392 | /* must still forward */ |
| 5393 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5394 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5395 | |
| 5396 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5397 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5398 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 5399 | else |
| 5400 | msg->msg_state = HTTP_MSG_DONE; |
| 5401 | } |
| 5402 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5403 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5404 | * set ->sov and ->next to point to the body and switch to DATA or |
| 5405 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5406 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5407 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5408 | |
| 5409 | if (!ret) |
| 5410 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5411 | else if (ret < 0) { |
| 5412 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5413 | 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] | 5414 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5415 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5416 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5417 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5418 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 5419 | /* we want the CRLF after the data */ |
| 5420 | int ret; |
| 5421 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5422 | ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5423 | |
| 5424 | if (!ret) |
| 5425 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5426 | else if (ret < 0) { |
| 5427 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5428 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5429 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5430 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5431 | /* we're in MSG_CHUNK_SIZE now */ |
| 5432 | } |
| 5433 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5434 | int ret = http_forward_trailers(msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5435 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5436 | if (ret == 0) |
| 5437 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5438 | else if (ret < 0) { |
| 5439 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5440 | 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] | 5441 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5442 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5443 | /* we're in HTTP_MSG_DONE now */ |
| 5444 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5445 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5446 | int old_state = msg->msg_state; |
| 5447 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5448 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5449 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5450 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5451 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5452 | channel_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5453 | if (http_resync_states(s)) { |
| 5454 | http_silent_debug(__LINE__, s); |
| 5455 | /* some state changes occurred, maybe the analyser |
| 5456 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5457 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5458 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5459 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5460 | /* response errors are most likely due to |
| 5461 | * the client aborting the transfer. |
| 5462 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5463 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5464 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5465 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5466 | http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5467 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5468 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5469 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5470 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5471 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5472 | } |
| 5473 | } |
| 5474 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5475 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5476 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5477 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5478 | if (!(s->flags & SN_ERR_MASK)) |
| 5479 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5480 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5481 | if (target_srv(&s->target)) |
| 5482 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5483 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5484 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5485 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5486 | if (res->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5487 | goto aborted_xfer; |
| 5488 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5489 | /* 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] | 5490 | if (!s->req->analysers) |
| 5491 | goto return_bad_res; |
| 5492 | |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5493 | /* forward any data pending between sol and sov */ |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5494 | bytes = msg->sov - msg->sol; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5495 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5496 | msg->sol = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5497 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5498 | msg->chunk_len += bytes; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5499 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5500 | } |
| 5501 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5502 | /* 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] | 5503 | * 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] | 5504 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 5505 | * close. |
| 5506 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5507 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5508 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5509 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5510 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5511 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5512 | /* 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] | 5513 | * 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] | 5514 | * 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] | 5515 | * modes are already handled by the stream sock layer. We must not do |
| 5516 | * this in content-length mode because it could present the MSG_MORE |
| 5517 | * flag with the last block of forwarded data, which would cause an |
| 5518 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5519 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5520 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5521 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5522 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5523 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5524 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5525 | return 0; |
| 5526 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5527 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5528 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5529 | if (target_srv(&s->target)) |
| 5530 | target_srv(&s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5531 | |
| 5532 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5533 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5534 | /* don't send any error message as we're in the body */ |
| 5535 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5536 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5537 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5538 | if (target_srv(&s->target)) |
| 5539 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5540 | |
| 5541 | if (!(s->flags & SN_ERR_MASK)) |
| 5542 | s->flags |= SN_ERR_PRXCOND; |
| 5543 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5544 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5545 | return 0; |
| 5546 | |
| 5547 | aborted_xfer: |
| 5548 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5549 | /* don't send any error message as we're in the body */ |
| 5550 | stream_int_retnclose(res->cons, NULL); |
| 5551 | res->analysers = 0; |
| 5552 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5553 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5554 | s->fe->fe_counters.cli_aborts++; |
| 5555 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5556 | if (target_srv(&s->target)) |
| 5557 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5558 | |
| 5559 | if (!(s->flags & SN_ERR_MASK)) |
| 5560 | s->flags |= SN_ERR_CLICL; |
| 5561 | if (!(s->flags & SN_FINST_MASK)) |
| 5562 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5563 | return 0; |
| 5564 | } |
| 5565 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5566 | /* Iterate the same filter through all request headers. |
| 5567 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5568 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5569 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5570 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5571 | int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5572 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5573 | char term; |
| 5574 | char *cur_ptr, *cur_end, *cur_next; |
| 5575 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5576 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5577 | struct hdr_idx_elem *cur_hdr; |
| 5578 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5579 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5580 | last_hdr = 0; |
| 5581 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 5582 | cur_next = req->buf.p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5583 | old_idx = 0; |
| 5584 | |
| 5585 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5586 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5587 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5588 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5589 | (exp->action == ACT_ALLOW || |
| 5590 | exp->action == ACT_DENY || |
| 5591 | exp->action == ACT_TARPIT)) |
| 5592 | return 0; |
| 5593 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5594 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5595 | if (!cur_idx) |
| 5596 | break; |
| 5597 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5598 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5599 | cur_ptr = cur_next; |
| 5600 | cur_end = cur_ptr + cur_hdr->len; |
| 5601 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5602 | |
| 5603 | /* Now we have one header between cur_ptr and cur_end, |
| 5604 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5605 | */ |
| 5606 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5607 | /* The annoying part is that pattern matching needs |
| 5608 | * that we modify the contents to null-terminate all |
| 5609 | * strings before testing them. |
| 5610 | */ |
| 5611 | |
| 5612 | term = *cur_end; |
| 5613 | *cur_end = '\0'; |
| 5614 | |
| 5615 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5616 | switch (exp->action) { |
| 5617 | case ACT_SETBE: |
| 5618 | /* It is not possible to jump a second time. |
| 5619 | * FIXME: should we return an HTTP/500 here so that |
| 5620 | * the admin knows there's a problem ? |
| 5621 | */ |
| 5622 | if (t->be != t->fe) |
| 5623 | break; |
| 5624 | |
| 5625 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5626 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5627 | last_hdr = 1; |
| 5628 | break; |
| 5629 | |
| 5630 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5631 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5632 | last_hdr = 1; |
| 5633 | break; |
| 5634 | |
| 5635 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5636 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5637 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5638 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5639 | t->fe->fe_counters.denied_req++; |
| 5640 | if (t->fe != t->be) |
| 5641 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5642 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5643 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5644 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5645 | break; |
| 5646 | |
| 5647 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5648 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5649 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5650 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5651 | t->fe->fe_counters.denied_req++; |
| 5652 | if (t->fe != t->be) |
| 5653 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5654 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5655 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5656 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5657 | break; |
| 5658 | |
| 5659 | case ACT_REPLACE: |
| 5660 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 5661 | delta = buffer_replace2(&req->buf, cur_ptr, cur_end, trash, len); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5662 | /* FIXME: if the user adds a newline in the replacement, the |
| 5663 | * index will not be recalculated for now, and the new line |
| 5664 | * will not be counted as a new header. |
| 5665 | */ |
| 5666 | |
| 5667 | cur_end += delta; |
| 5668 | cur_next += delta; |
| 5669 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5670 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5671 | break; |
| 5672 | |
| 5673 | case ACT_REMOVE: |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 5674 | delta = buffer_replace2(&req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5675 | cur_next += delta; |
| 5676 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5677 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5678 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5679 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5680 | cur_hdr->len = 0; |
| 5681 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 5682 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5683 | break; |
| 5684 | |
| 5685 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5686 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5687 | if (cur_end) |
| 5688 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5689 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5690 | /* keep the link from this header to next one in case of later |
| 5691 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5692 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5693 | old_idx = cur_idx; |
| 5694 | } |
| 5695 | return 0; |
| 5696 | } |
| 5697 | |
| 5698 | |
| 5699 | /* Apply the filter to the request line. |
| 5700 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5701 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5702 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5703 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5704 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5705 | int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5706 | { |
| 5707 | char term; |
| 5708 | char *cur_ptr, *cur_end; |
| 5709 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5710 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5711 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5712 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5713 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5714 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5715 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5716 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5717 | (exp->action == ACT_ALLOW || |
| 5718 | exp->action == ACT_DENY || |
| 5719 | exp->action == ACT_TARPIT)) |
| 5720 | return 0; |
| 5721 | else if (exp->action == ACT_REMOVE) |
| 5722 | return 0; |
| 5723 | |
| 5724 | done = 0; |
| 5725 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 5726 | cur_ptr = req->buf.p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5727 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5728 | |
| 5729 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5730 | |
| 5731 | /* The annoying part is that pattern matching needs |
| 5732 | * that we modify the contents to null-terminate all |
| 5733 | * strings before testing them. |
| 5734 | */ |
| 5735 | |
| 5736 | term = *cur_end; |
| 5737 | *cur_end = '\0'; |
| 5738 | |
| 5739 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5740 | switch (exp->action) { |
| 5741 | case ACT_SETBE: |
| 5742 | /* It is not possible to jump a second time. |
| 5743 | * FIXME: should we return an HTTP/500 here so that |
| 5744 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5745 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5746 | if (t->be != t->fe) |
| 5747 | break; |
| 5748 | |
| 5749 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5750 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5751 | done = 1; |
| 5752 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5753 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5754 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5755 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5756 | done = 1; |
| 5757 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5758 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5759 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5760 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5761 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5762 | t->fe->fe_counters.denied_req++; |
| 5763 | if (t->fe != t->be) |
| 5764 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5765 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5766 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5767 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5768 | done = 1; |
| 5769 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5770 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5771 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5772 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5773 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5774 | t->fe->fe_counters.denied_req++; |
| 5775 | if (t->fe != t->be) |
| 5776 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5777 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5778 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5779 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5780 | done = 1; |
| 5781 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5782 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5783 | case ACT_REPLACE: |
| 5784 | *cur_end = term; /* restore the string terminator */ |
| 5785 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 5786 | delta = buffer_replace2(&req->buf, cur_ptr, cur_end, trash, len); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5787 | /* FIXME: if the user adds a newline in the replacement, the |
| 5788 | * index will not be recalculated for now, and the new line |
| 5789 | * will not be counted as a new header. |
| 5790 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5791 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5792 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5793 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 5794 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5795 | HTTP_MSG_RQMETH, |
| 5796 | cur_ptr, cur_end + 1, |
| 5797 | NULL, NULL); |
| 5798 | if (unlikely(!cur_end)) |
| 5799 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5800 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5801 | /* we have a full request and we know that we have either a CR |
| 5802 | * or an LF at <ptr>. |
| 5803 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5804 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5805 | 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] | 5806 | /* there is no point trying this regex on headers */ |
| 5807 | return 1; |
| 5808 | } |
| 5809 | } |
| 5810 | *cur_end = term; /* restore the string terminator */ |
| 5811 | return done; |
| 5812 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5813 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5814 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5815 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5816 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5817 | * 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] | 5818 | * 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] | 5819 | * unparsable request. Since it can manage the switch to another backend, it |
| 5820 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5821 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5822 | 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] | 5823 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5824 | struct http_txn *txn = &s->txn; |
| 5825 | struct hdr_exp *exp; |
| 5826 | |
| 5827 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5828 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5829 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5830 | /* |
| 5831 | * The interleaving of transformations and verdicts |
| 5832 | * makes it difficult to decide to continue or stop |
| 5833 | * the evaluation. |
| 5834 | */ |
| 5835 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5836 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 5837 | break; |
| 5838 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5839 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5840 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5841 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5842 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5843 | |
| 5844 | /* if this filter had a condition, evaluate it now and skip to |
| 5845 | * next filter if the condition does not match. |
| 5846 | */ |
| 5847 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 5848 | 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] | 5849 | ret = acl_pass(ret); |
| 5850 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 5851 | ret = !ret; |
| 5852 | |
| 5853 | if (!ret) |
| 5854 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5855 | } |
| 5856 | |
| 5857 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5858 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5859 | if (unlikely(ret < 0)) |
| 5860 | return -1; |
| 5861 | |
| 5862 | if (likely(ret == 0)) { |
| 5863 | /* The filter did not match the request, it can be |
| 5864 | * iterated through all headers. |
| 5865 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5866 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5867 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5868 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5869 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5870 | } |
| 5871 | |
| 5872 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5873 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5874 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5875 | * Try to retrieve the server associated to the appsession. |
| 5876 | * If the server is found, it's assigned to the session. |
| 5877 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5878 | void manage_client_side_appsession(struct session *t, const char *buf, int len) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5879 | struct http_txn *txn = &t->txn; |
| 5880 | appsess *asession = NULL; |
| 5881 | char *sessid_temp = NULL; |
| 5882 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5883 | if (len > t->be->appsession_len) { |
| 5884 | len = t->be->appsession_len; |
| 5885 | } |
| 5886 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5887 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5888 | /* 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] | 5889 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5890 | /* 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] | 5891 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5892 | } |
| 5893 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5894 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5895 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5896 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5897 | return; |
| 5898 | } |
| 5899 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5900 | memcpy(txn->sessid, buf, len); |
| 5901 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5902 | } |
| 5903 | |
| 5904 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 5905 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5906 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5907 | return; |
| 5908 | } |
| 5909 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5910 | memcpy(sessid_temp, buf, len); |
| 5911 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5912 | |
| 5913 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 5914 | /* free previously allocated memory */ |
| 5915 | pool_free2(apools.sessid, sessid_temp); |
| 5916 | |
| 5917 | if (asession != NULL) { |
| 5918 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5919 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 5920 | asession->request_count++; |
| 5921 | |
| 5922 | if (asession->serverid != NULL) { |
| 5923 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5924 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5925 | while (srv) { |
| 5926 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 5927 | if ((srv->state & SRV_RUNNING) || |
| 5928 | (t->be->options & PR_O_PERSIST) || |
| 5929 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5930 | /* we found the server and it's usable */ |
| 5931 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 5932 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5933 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 5934 | set_target_server(&t->target, srv); |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 5935 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5936 | break; |
| 5937 | } else { |
| 5938 | txn->flags &= ~TX_CK_MASK; |
| 5939 | txn->flags |= TX_CK_DOWN; |
| 5940 | } |
| 5941 | } |
| 5942 | srv = srv->next; |
| 5943 | } |
| 5944 | } |
| 5945 | } |
| 5946 | } |
| 5947 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5948 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 5949 | * same way as with headers above except that the semi-colon also ends a token. |
| 5950 | * See RFC2965 for more information. Note that it requires a valid header to |
| 5951 | * return a valid result. |
| 5952 | */ |
| 5953 | char *find_cookie_value_end(char *s, const char *e) |
| 5954 | { |
| 5955 | int quoted, qdpair; |
| 5956 | |
| 5957 | quoted = qdpair = 0; |
| 5958 | for (; s < e; s++) { |
| 5959 | if (qdpair) qdpair = 0; |
| 5960 | else if (quoted) { |
| 5961 | if (*s == '\\') qdpair = 1; |
| 5962 | else if (*s == '"') quoted = 0; |
| 5963 | } |
| 5964 | else if (*s == '"') quoted = 1; |
| 5965 | else if (*s == ',' || *s == ';') return s; |
| 5966 | } |
| 5967 | return s; |
| 5968 | } |
| 5969 | |
| 5970 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 5971 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 5972 | * the first delimiter is updated if required. The function tries as much as |
| 5973 | * possible to respect the following principles : |
| 5974 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 5975 | * colon, in which case <next> is simply removed |
| 5976 | * - set exactly one space character after the new first delimiter, unless |
| 5977 | * there are not enough characters in the block being moved to do so. |
| 5978 | * - remove unneeded spaces before the previous delimiter and after the new |
| 5979 | * one. |
| 5980 | * |
| 5981 | * It is the caller's responsibility to ensure that : |
| 5982 | * - <from> points to a valid delimiter or the colon ; |
| 5983 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 5984 | * - there are non-space chars before <from> ; |
| 5985 | * - there is a CR/LF at or after <next>. |
| 5986 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 5987 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5988 | { |
| 5989 | char *prev = *from; |
| 5990 | |
| 5991 | if (*prev == ':') { |
| 5992 | /* We're removing the first value, preserve the colon and add a |
| 5993 | * space if possible. |
| 5994 | */ |
| 5995 | if (!http_is_crlf[(unsigned char)*next]) |
| 5996 | next++; |
| 5997 | prev++; |
| 5998 | if (prev < next) |
| 5999 | *prev++ = ' '; |
| 6000 | |
| 6001 | while (http_is_spht[(unsigned char)*next]) |
| 6002 | next++; |
| 6003 | } else { |
| 6004 | /* Remove useless spaces before the old delimiter. */ |
| 6005 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 6006 | prev--; |
| 6007 | *from = prev; |
| 6008 | |
| 6009 | /* copy the delimiter and if possible a space if we're |
| 6010 | * not at the end of the line. |
| 6011 | */ |
| 6012 | if (!http_is_crlf[(unsigned char)*next]) { |
| 6013 | *prev++ = *next++; |
| 6014 | if (prev + 1 < next) |
| 6015 | *prev++ = ' '; |
| 6016 | while (http_is_spht[(unsigned char)*next]) |
| 6017 | next++; |
| 6018 | } |
| 6019 | } |
| 6020 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6021 | } |
| 6022 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6023 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6024 | * 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] | 6025 | * desirable to call it only when needed. This code is quite complex because |
| 6026 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6027 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6028 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6029 | void manage_client_side_cookies(struct session *t, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6030 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6031 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6032 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6033 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6034 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6035 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6036 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6037 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6038 | old_idx = 0; |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 6039 | hdr_next = req->buf.p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6040 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6041 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6042 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6043 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6044 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6045 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6046 | hdr_beg = hdr_next; |
| 6047 | hdr_end = hdr_beg + cur_hdr->len; |
| 6048 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6049 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6050 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6051 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6052 | * "Cookie:" headers. |
| 6053 | */ |
| 6054 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6055 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6056 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6057 | old_idx = cur_idx; |
| 6058 | continue; |
| 6059 | } |
| 6060 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6061 | del_from = NULL; /* nothing to be deleted */ |
| 6062 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6063 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6064 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6065 | * attributes whose name begin with a '$', and associate them with |
| 6066 | * the right cookie, if we want to delete this cookie. |
| 6067 | * So there are 3 cases for each cookie read : |
| 6068 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6069 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6070 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6071 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6072 | * "special" cookie. |
| 6073 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6074 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6075 | * *MUST* delete it. |
| 6076 | * |
| 6077 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6078 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6079 | * the RFC explicitly allows spaces before it, and not within the |
| 6080 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6081 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6082 | * after the equal sign too, resulting in some (rare) buggy |
| 6083 | * implementations trying to do that. So let's do what servers do. |
| 6084 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6085 | * allowed quoted strings in values, with any possible character |
| 6086 | * after a backslash, including control chars and delimitors, which |
| 6087 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6088 | * within values even without quotes. |
| 6089 | * |
| 6090 | * We have to keep multiple pointers in order to support cookie |
| 6091 | * removal at the beginning, middle or end of header without |
| 6092 | * corrupting the header. All of these headers are valid : |
| 6093 | * |
| 6094 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6095 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6096 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6097 | * | | | | | | | | | |
| 6098 | * | | | | | | | | hdr_end <--+ |
| 6099 | * | | | | | | | +--> next |
| 6100 | * | | | | | | +----> val_end |
| 6101 | * | | | | | +-----------> val_beg |
| 6102 | * | | | | +--------------> equal |
| 6103 | * | | | +----------------> att_end |
| 6104 | * | | +---------------------> att_beg |
| 6105 | * | +--------------------------> prev |
| 6106 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6107 | */ |
| 6108 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6109 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6110 | /* Iterate through all cookies on this line */ |
| 6111 | |
| 6112 | /* find att_beg */ |
| 6113 | att_beg = prev + 1; |
| 6114 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6115 | att_beg++; |
| 6116 | |
| 6117 | /* find att_end : this is the first character after the last non |
| 6118 | * space before the equal. It may be equal to hdr_end. |
| 6119 | */ |
| 6120 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6121 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6122 | while (equal < hdr_end) { |
| 6123 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6124 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6125 | if (http_is_spht[(unsigned char)*equal++]) |
| 6126 | continue; |
| 6127 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6128 | } |
| 6129 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6130 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6131 | * is between <att_beg> and <equal>, both may be identical. |
| 6132 | */ |
| 6133 | |
| 6134 | /* look for end of cookie if there is an equal sign */ |
| 6135 | if (equal < hdr_end && *equal == '=') { |
| 6136 | /* look for the beginning of the value */ |
| 6137 | val_beg = equal + 1; |
| 6138 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6139 | val_beg++; |
| 6140 | |
| 6141 | /* find the end of the value, respecting quotes */ |
| 6142 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6143 | |
| 6144 | /* make val_end point to the first white space or delimitor after the value */ |
| 6145 | val_end = next; |
| 6146 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6147 | val_end--; |
| 6148 | } else { |
| 6149 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6150 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6151 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6152 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6153 | * they will automatically be removed if a header before them is removed, |
| 6154 | * since they're supposed to be linked together. |
| 6155 | */ |
| 6156 | if (*att_beg == '$') |
| 6157 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6158 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6159 | /* Ignore cookies with no equal sign */ |
| 6160 | if (equal == next) { |
| 6161 | /* This is not our cookie, so we must preserve it. But if we already |
| 6162 | * scheduled another cookie for removal, we cannot remove the |
| 6163 | * complete header, but we can remove the previous block itself. |
| 6164 | */ |
| 6165 | preserve_hdr = 1; |
| 6166 | if (del_from != NULL) { |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6167 | int delta = del_hdr_value(&req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6168 | val_end += delta; |
| 6169 | next += delta; |
| 6170 | hdr_end += delta; |
| 6171 | hdr_next += delta; |
| 6172 | cur_hdr->len += delta; |
| 6173 | http_msg_move_end(&txn->req, delta); |
| 6174 | prev = del_from; |
| 6175 | del_from = NULL; |
| 6176 | } |
| 6177 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6178 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6179 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6180 | /* if there are spaces around the equal sign, we need to |
| 6181 | * strip them otherwise we'll get trouble for cookie captures, |
| 6182 | * or even for rewrites. Since this happens extremely rarely, |
| 6183 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6184 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6185 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6186 | int stripped_before = 0; |
| 6187 | int stripped_after = 0; |
| 6188 | |
| 6189 | if (att_end != equal) { |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6190 | stripped_before = buffer_replace2(&req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6191 | equal += stripped_before; |
| 6192 | val_beg += stripped_before; |
| 6193 | } |
| 6194 | |
| 6195 | if (val_beg > equal + 1) { |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6196 | stripped_after = buffer_replace2(&req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6197 | val_beg += stripped_after; |
| 6198 | stripped_before += stripped_after; |
| 6199 | } |
| 6200 | |
| 6201 | val_end += stripped_before; |
| 6202 | next += stripped_before; |
| 6203 | hdr_end += stripped_before; |
| 6204 | hdr_next += stripped_before; |
| 6205 | cur_hdr->len += stripped_before; |
| 6206 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6207 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6208 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6209 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6210 | /* First, let's see if we want to capture this cookie. We check |
| 6211 | * that we don't already have a client side cookie, because we |
| 6212 | * can only capture one. Also as an optimisation, we ignore |
| 6213 | * cookies shorter than the declared name. |
| 6214 | */ |
| 6215 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6216 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6217 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6218 | int log_len = val_end - att_beg; |
| 6219 | |
| 6220 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6221 | Alert("HTTP logging : out of memory.\n"); |
| 6222 | } else { |
| 6223 | if (log_len > t->fe->capture_len) |
| 6224 | log_len = t->fe->capture_len; |
| 6225 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6226 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6227 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6228 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6229 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6230 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6231 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6232 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6233 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6234 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6235 | * For cookies in prefix mode, the form is : |
| 6236 | * |
| 6237 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6238 | */ |
| 6239 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6240 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6241 | struct server *srv = t->be->srv; |
| 6242 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6243 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6244 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6245 | * have the server ID between val_beg and delim, and the original cookie between |
| 6246 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6247 | * |
| 6248 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6249 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6250 | * | || || | |+-> next |
| 6251 | * | || || | +--> val_end |
| 6252 | * | || || +---------> delim |
| 6253 | * | || |+------------> val_beg |
| 6254 | * | || +-------------> att_end = equal |
| 6255 | * | |+-----------------> att_beg |
| 6256 | * | +------------------> prev |
| 6257 | * +-------------------------> hdr_beg |
| 6258 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6259 | |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6260 | if (t->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6261 | for (delim = val_beg; delim < val_end; delim++) |
| 6262 | if (*delim == COOKIE_DELIM) |
| 6263 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6264 | } else { |
| 6265 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6266 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6267 | /* Now check if the cookie contains a date field, which would |
| 6268 | * appear after a vertical bar ('|') just after the server name |
| 6269 | * and before the delimiter. |
| 6270 | */ |
| 6271 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6272 | if (vbar1) { |
| 6273 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6274 | * right is the last seen date. It is a base64 encoded |
| 6275 | * 30-bit value representing the UNIX date since the |
| 6276 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6277 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6278 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6279 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6280 | if (val_end - vbar1 >= 5) { |
| 6281 | val = b64tos30(vbar1); |
| 6282 | if (val > 0) |
| 6283 | txn->cookie_last_date = val << 2; |
| 6284 | } |
| 6285 | /* look for a second vertical bar */ |
| 6286 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6287 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6288 | val = b64tos30(vbar1 + 1); |
| 6289 | if (val > 0) |
| 6290 | txn->cookie_first_date = val << 2; |
| 6291 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6292 | } |
| 6293 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6294 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6295 | /* if the cookie has an expiration date and the proxy wants to check |
| 6296 | * it, then we do that now. We first check if the cookie is too old, |
| 6297 | * then only if it has expired. We detect strict overflow because the |
| 6298 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6299 | * in the future are ignored if their offset is beyond one day. This |
| 6300 | * allows an admin to fix timezone issues without expiring everyone |
| 6301 | * and at the same time avoids keeping unwanted side effects for too |
| 6302 | * long. |
| 6303 | */ |
| 6304 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6305 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 6306 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6307 | txn->flags &= ~TX_CK_MASK; |
| 6308 | txn->flags |= TX_CK_OLD; |
| 6309 | delim = val_beg; // let's pretend we have not found the cookie |
| 6310 | txn->cookie_first_date = 0; |
| 6311 | txn->cookie_last_date = 0; |
| 6312 | } |
| 6313 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6314 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 6315 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6316 | txn->flags &= ~TX_CK_MASK; |
| 6317 | txn->flags |= TX_CK_EXPIRED; |
| 6318 | delim = val_beg; // let's pretend we have not found the cookie |
| 6319 | txn->cookie_first_date = 0; |
| 6320 | txn->cookie_last_date = 0; |
| 6321 | } |
| 6322 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6323 | /* Here, we'll look for the first running server which supports the cookie. |
| 6324 | * This allows to share a same cookie between several servers, for example |
| 6325 | * to dedicate backup servers to specific servers only. |
| 6326 | * However, to prevent clients from sticking to cookie-less backup server |
| 6327 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6328 | * empty cookies and mark them as invalid. |
| 6329 | * The same behaviour is applied when persistence must be ignored. |
| 6330 | */ |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 6331 | if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6332 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6333 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6334 | while (srv) { |
| 6335 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6336 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 6337 | if ((srv->state & SRV_RUNNING) || |
| 6338 | (t->be->options & PR_O_PERSIST) || |
| 6339 | (t->flags & SN_FORCE_PRST)) { |
| 6340 | /* we found the server and we can use it */ |
| 6341 | txn->flags &= ~TX_CK_MASK; |
| 6342 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 6343 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 6344 | set_target_server(&t->target, srv); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6345 | break; |
| 6346 | } else { |
| 6347 | /* we found a server, but it's down, |
| 6348 | * mark it as such and go on in case |
| 6349 | * another one is available. |
| 6350 | */ |
| 6351 | txn->flags &= ~TX_CK_MASK; |
| 6352 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6353 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6354 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6355 | srv = srv->next; |
| 6356 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6357 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6358 | 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] | 6359 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6360 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6361 | if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
| 6362 | txn->flags |= TX_CK_UNUSED; |
| 6363 | else |
| 6364 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6365 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6366 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6367 | /* depending on the cookie mode, we may have to either : |
| 6368 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6369 | * the server never sees it ; |
| 6370 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6371 | * application cookie so that it does not get accidentely removed later, |
| 6372 | * if we're in cookie prefix mode |
| 6373 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6374 | if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6375 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6376 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6377 | delta = buffer_replace2(&req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6378 | val_end += delta; |
| 6379 | next += delta; |
| 6380 | hdr_end += delta; |
| 6381 | hdr_next += delta; |
| 6382 | cur_hdr->len += delta; |
| 6383 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6384 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6385 | del_from = NULL; |
| 6386 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6387 | } |
| 6388 | else if (del_from == NULL && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6389 | (t->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] | 6390 | del_from = prev; |
| 6391 | } |
| 6392 | } else { |
| 6393 | /* This is not our cookie, so we must preserve it. But if we already |
| 6394 | * scheduled another cookie for removal, we cannot remove the |
| 6395 | * complete header, but we can remove the previous block itself. |
| 6396 | */ |
| 6397 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6398 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6399 | if (del_from != NULL) { |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6400 | int delta = del_hdr_value(&req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6401 | if (att_beg >= del_from) |
| 6402 | att_beg += delta; |
| 6403 | if (att_end >= del_from) |
| 6404 | att_end += delta; |
| 6405 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6406 | val_end += delta; |
| 6407 | next += delta; |
| 6408 | hdr_end += delta; |
| 6409 | hdr_next += delta; |
| 6410 | cur_hdr->len += delta; |
| 6411 | http_msg_move_end(&txn->req, delta); |
| 6412 | prev = del_from; |
| 6413 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6414 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6415 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6416 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6417 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 6418 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 6419 | int cmp_len, value_len; |
| 6420 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6421 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6422 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6423 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6424 | value_begin = att_beg + t->be->appsession_name_len; |
| 6425 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 6426 | } else { |
| 6427 | cmp_len = att_end - att_beg; |
| 6428 | value_begin = val_beg; |
| 6429 | value_len = val_end - val_beg; |
| 6430 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6431 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6432 | /* let's see if the cookie is our appcookie */ |
| 6433 | if (cmp_len == t->be->appsession_name_len && |
| 6434 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 6435 | manage_client_side_appsession(t, value_begin, value_len); |
| 6436 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6437 | } |
| 6438 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6439 | /* continue with next cookie on this header line */ |
| 6440 | att_beg = next; |
| 6441 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6442 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6443 | /* There are no more cookies on this line. |
| 6444 | * We may still have one (or several) marked for deletion at the |
| 6445 | * end of the line. We must do this now in two ways : |
| 6446 | * - if some cookies must be preserved, we only delete from the |
| 6447 | * mark to the end of line ; |
| 6448 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6449 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6450 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6451 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6452 | if (preserve_hdr) { |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6453 | delta = del_hdr_value(&req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6454 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6455 | cur_hdr->len += delta; |
| 6456 | } else { |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6457 | delta = buffer_replace2(&req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6458 | |
| 6459 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6460 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6461 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6462 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6463 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6464 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6465 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6466 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6467 | } |
| 6468 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6469 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6470 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6471 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6472 | } |
| 6473 | |
| 6474 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6475 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6476 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6477 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6478 | int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6479 | { |
| 6480 | char term; |
| 6481 | char *cur_ptr, *cur_end, *cur_next; |
| 6482 | int cur_idx, old_idx, last_hdr; |
| 6483 | struct http_txn *txn = &t->txn; |
| 6484 | struct hdr_idx_elem *cur_hdr; |
| 6485 | int len, delta; |
| 6486 | |
| 6487 | last_hdr = 0; |
| 6488 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 6489 | cur_next = rtr->buf.p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6490 | old_idx = 0; |
| 6491 | |
| 6492 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6493 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6494 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6495 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6496 | (exp->action == ACT_ALLOW || |
| 6497 | exp->action == ACT_DENY)) |
| 6498 | return 0; |
| 6499 | |
| 6500 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6501 | if (!cur_idx) |
| 6502 | break; |
| 6503 | |
| 6504 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6505 | cur_ptr = cur_next; |
| 6506 | cur_end = cur_ptr + cur_hdr->len; |
| 6507 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6508 | |
| 6509 | /* Now we have one header between cur_ptr and cur_end, |
| 6510 | * and the next header starts at cur_next. |
| 6511 | */ |
| 6512 | |
| 6513 | /* The annoying part is that pattern matching needs |
| 6514 | * that we modify the contents to null-terminate all |
| 6515 | * strings before testing them. |
| 6516 | */ |
| 6517 | |
| 6518 | term = *cur_end; |
| 6519 | *cur_end = '\0'; |
| 6520 | |
| 6521 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6522 | switch (exp->action) { |
| 6523 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6524 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6525 | last_hdr = 1; |
| 6526 | break; |
| 6527 | |
| 6528 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6529 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6530 | last_hdr = 1; |
| 6531 | break; |
| 6532 | |
| 6533 | case ACT_REPLACE: |
| 6534 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6535 | delta = buffer_replace2(&rtr->buf, cur_ptr, cur_end, trash, len); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6536 | /* FIXME: if the user adds a newline in the replacement, the |
| 6537 | * index will not be recalculated for now, and the new line |
| 6538 | * will not be counted as a new header. |
| 6539 | */ |
| 6540 | |
| 6541 | cur_end += delta; |
| 6542 | cur_next += delta; |
| 6543 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6544 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6545 | break; |
| 6546 | |
| 6547 | case ACT_REMOVE: |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6548 | delta = buffer_replace2(&rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6549 | cur_next += delta; |
| 6550 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6551 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6552 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6553 | txn->hdr_idx.used--; |
| 6554 | cur_hdr->len = 0; |
| 6555 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6556 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6557 | break; |
| 6558 | |
| 6559 | } |
| 6560 | } |
| 6561 | if (cur_end) |
| 6562 | *cur_end = term; /* restore the string terminator */ |
| 6563 | |
| 6564 | /* keep the link from this header to next one in case of later |
| 6565 | * removal of next header. |
| 6566 | */ |
| 6567 | old_idx = cur_idx; |
| 6568 | } |
| 6569 | return 0; |
| 6570 | } |
| 6571 | |
| 6572 | |
| 6573 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6574 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6575 | * or -1 if a replacement resulted in an invalid status line. |
| 6576 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6577 | int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6578 | { |
| 6579 | char term; |
| 6580 | char *cur_ptr, *cur_end; |
| 6581 | int done; |
| 6582 | struct http_txn *txn = &t->txn; |
| 6583 | int len, delta; |
| 6584 | |
| 6585 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6586 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6587 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6588 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6589 | (exp->action == ACT_ALLOW || |
| 6590 | exp->action == ACT_DENY)) |
| 6591 | return 0; |
| 6592 | else if (exp->action == ACT_REMOVE) |
| 6593 | return 0; |
| 6594 | |
| 6595 | done = 0; |
| 6596 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 6597 | cur_ptr = rtr->buf.p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6598 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6599 | |
| 6600 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6601 | |
| 6602 | /* The annoying part is that pattern matching needs |
| 6603 | * that we modify the contents to null-terminate all |
| 6604 | * strings before testing them. |
| 6605 | */ |
| 6606 | |
| 6607 | term = *cur_end; |
| 6608 | *cur_end = '\0'; |
| 6609 | |
| 6610 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6611 | switch (exp->action) { |
| 6612 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6613 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6614 | done = 1; |
| 6615 | break; |
| 6616 | |
| 6617 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6618 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6619 | done = 1; |
| 6620 | break; |
| 6621 | |
| 6622 | case ACT_REPLACE: |
| 6623 | *cur_end = term; /* restore the string terminator */ |
| 6624 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6625 | delta = buffer_replace2(&rtr->buf, cur_ptr, cur_end, trash, len); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6626 | /* FIXME: if the user adds a newline in the replacement, the |
| 6627 | * index will not be recalculated for now, and the new line |
| 6628 | * will not be counted as a new header. |
| 6629 | */ |
| 6630 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6631 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6632 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6633 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6634 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6635 | cur_ptr, cur_end + 1, |
| 6636 | NULL, NULL); |
| 6637 | if (unlikely(!cur_end)) |
| 6638 | return -1; |
| 6639 | |
| 6640 | /* we have a full respnse and we know that we have either a CR |
| 6641 | * or an LF at <ptr>. |
| 6642 | */ |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 6643 | 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] | 6644 | 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] | 6645 | /* there is no point trying this regex on headers */ |
| 6646 | return 1; |
| 6647 | } |
| 6648 | } |
| 6649 | *cur_end = term; /* restore the string terminator */ |
| 6650 | return done; |
| 6651 | } |
| 6652 | |
| 6653 | |
| 6654 | |
| 6655 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6656 | * 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] | 6657 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6658 | * unparsable response. |
| 6659 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6660 | 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] | 6661 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6662 | struct http_txn *txn = &s->txn; |
| 6663 | struct hdr_exp *exp; |
| 6664 | |
| 6665 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6666 | int ret; |
| 6667 | |
| 6668 | /* |
| 6669 | * The interleaving of transformations and verdicts |
| 6670 | * makes it difficult to decide to continue or stop |
| 6671 | * the evaluation. |
| 6672 | */ |
| 6673 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6674 | if (txn->flags & TX_SVDENY) |
| 6675 | break; |
| 6676 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6677 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6678 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6679 | exp->action == ACT_PASS)) { |
| 6680 | exp = exp->next; |
| 6681 | continue; |
| 6682 | } |
| 6683 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6684 | /* if this filter had a condition, evaluate it now and skip to |
| 6685 | * next filter if the condition does not match. |
| 6686 | */ |
| 6687 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 6688 | 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] | 6689 | ret = acl_pass(ret); |
| 6690 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6691 | ret = !ret; |
| 6692 | if (!ret) |
| 6693 | continue; |
| 6694 | } |
| 6695 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6696 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6697 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6698 | if (unlikely(ret < 0)) |
| 6699 | return -1; |
| 6700 | |
| 6701 | if (likely(ret == 0)) { |
| 6702 | /* The filter did not match the response, it can be |
| 6703 | * iterated through all headers. |
| 6704 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6705 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6706 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6707 | } |
| 6708 | return 0; |
| 6709 | } |
| 6710 | |
| 6711 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6712 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6713 | * 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] | 6714 | * desirable to call it only when needed. This function is also used when we |
| 6715 | * 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] | 6716 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6717 | void manage_server_side_cookies(struct session *t, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6718 | { |
| 6719 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6720 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6721 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6722 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6723 | char *hdr_beg, *hdr_end, *hdr_next; |
| 6724 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6725 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6726 | /* Iterate through the headers. |
| 6727 | * we start with the start line. |
| 6728 | */ |
| 6729 | old_idx = 0; |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 6730 | hdr_next = res->buf.p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6731 | |
| 6732 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6733 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6734 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6735 | |
| 6736 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6737 | hdr_beg = hdr_next; |
| 6738 | hdr_end = hdr_beg + cur_hdr->len; |
| 6739 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6740 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6741 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6742 | * next header starts at hdr_next. We're only interested in |
| 6743 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6744 | */ |
| 6745 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6746 | is_cookie2 = 0; |
| 6747 | prev = hdr_beg + 10; |
| 6748 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6749 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6750 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 6751 | if (!val) { |
| 6752 | old_idx = cur_idx; |
| 6753 | continue; |
| 6754 | } |
| 6755 | is_cookie2 = 1; |
| 6756 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6757 | } |
| 6758 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6759 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 6760 | * <prev> points to the colon. |
| 6761 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6762 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6763 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6764 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 6765 | * check-cache is enabled) and we are not interested in checking |
| 6766 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6767 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6768 | if (t->be->cookie_name == NULL && |
| 6769 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6770 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6771 | return; |
| 6772 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6773 | /* OK so now we know we have to process this response cookie. |
| 6774 | * The format of the Set-Cookie header is slightly different |
| 6775 | * from the format of the Cookie header in that it does not |
| 6776 | * support the comma as a cookie delimiter (thus the header |
| 6777 | * cannot be folded) because the Expires attribute described in |
| 6778 | * the original Netscape's spec may contain an unquoted date |
| 6779 | * with a comma inside. We have to live with this because |
| 6780 | * many browsers don't support Max-Age and some browsers don't |
| 6781 | * support quoted strings. However the Set-Cookie2 header is |
| 6782 | * clean. |
| 6783 | * |
| 6784 | * We have to keep multiple pointers in order to support cookie |
| 6785 | * removal at the beginning, middle or end of header without |
| 6786 | * corrupting the header (in case of set-cookie2). A special |
| 6787 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 6788 | * fields after the first semi-colon. The <next> pointer points |
| 6789 | * either to the end of line (set-cookie) or next unquoted comma |
| 6790 | * (set-cookie2). All of these headers are valid : |
| 6791 | * |
| 6792 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 6793 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6794 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6795 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 6796 | * | | | | | | | | | | |
| 6797 | * | | | | | | | | +-> next hdr_end <--+ |
| 6798 | * | | | | | | | +------------> scav |
| 6799 | * | | | | | | +--------------> val_end |
| 6800 | * | | | | | +--------------------> val_beg |
| 6801 | * | | | | +----------------------> equal |
| 6802 | * | | | +------------------------> att_end |
| 6803 | * | | +----------------------------> att_beg |
| 6804 | * | +------------------------------> prev |
| 6805 | * +-----------------------------------------> hdr_beg |
| 6806 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6807 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6808 | for (; prev < hdr_end; prev = next) { |
| 6809 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6810 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6811 | /* find att_beg */ |
| 6812 | att_beg = prev + 1; |
| 6813 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6814 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6815 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6816 | /* find att_end : this is the first character after the last non |
| 6817 | * space before the equal. It may be equal to hdr_end. |
| 6818 | */ |
| 6819 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6820 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6821 | while (equal < hdr_end) { |
| 6822 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 6823 | break; |
| 6824 | if (http_is_spht[(unsigned char)*equal++]) |
| 6825 | continue; |
| 6826 | att_end = equal; |
| 6827 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6828 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6829 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6830 | * is between <att_beg> and <equal>, both may be identical. |
| 6831 | */ |
| 6832 | |
| 6833 | /* look for end of cookie if there is an equal sign */ |
| 6834 | if (equal < hdr_end && *equal == '=') { |
| 6835 | /* look for the beginning of the value */ |
| 6836 | val_beg = equal + 1; |
| 6837 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6838 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6839 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6840 | /* find the end of the value, respecting quotes */ |
| 6841 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6842 | |
| 6843 | /* make val_end point to the first white space or delimitor after the value */ |
| 6844 | val_end = next; |
| 6845 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6846 | val_end--; |
| 6847 | } else { |
| 6848 | /* <equal> points to next comma, semi-colon or EOL */ |
| 6849 | val_beg = val_end = next = equal; |
| 6850 | } |
| 6851 | |
| 6852 | if (next < hdr_end) { |
| 6853 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 6854 | * a colon or semi-colon before the end. So skip all attr-value |
| 6855 | * pairs and look for the next comma. For Set-Cookie, since |
| 6856 | * commas are permitted in values, skip to the end. |
| 6857 | */ |
| 6858 | if (is_cookie2) |
| 6859 | next = find_hdr_value_end(next, hdr_end); |
| 6860 | else |
| 6861 | next = hdr_end; |
| 6862 | } |
| 6863 | |
| 6864 | /* Now everything is as on the diagram above */ |
| 6865 | |
| 6866 | /* Ignore cookies with no equal sign */ |
| 6867 | if (equal == val_end) |
| 6868 | continue; |
| 6869 | |
| 6870 | /* If there are spaces around the equal sign, we need to |
| 6871 | * strip them otherwise we'll get trouble for cookie captures, |
| 6872 | * or even for rewrites. Since this happens extremely rarely, |
| 6873 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6874 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6875 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6876 | int stripped_before = 0; |
| 6877 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6878 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6879 | if (att_end != equal) { |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6880 | stripped_before = buffer_replace2(&res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6881 | equal += stripped_before; |
| 6882 | val_beg += stripped_before; |
| 6883 | } |
| 6884 | |
| 6885 | if (val_beg > equal + 1) { |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6886 | stripped_after = buffer_replace2(&res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6887 | val_beg += stripped_after; |
| 6888 | stripped_before += stripped_after; |
| 6889 | } |
| 6890 | |
| 6891 | val_end += stripped_before; |
| 6892 | next += stripped_before; |
| 6893 | hdr_end += stripped_before; |
| 6894 | hdr_next += stripped_before; |
| 6895 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 6896 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6897 | } |
| 6898 | |
| 6899 | /* First, let's see if we want to capture this cookie. We check |
| 6900 | * that we don't already have a server side cookie, because we |
| 6901 | * can only capture one. Also as an optimisation, we ignore |
| 6902 | * cookies shorter than the declared name. |
| 6903 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6904 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6905 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6906 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6907 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6908 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 6909 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6910 | Alert("HTTP logging : out of memory.\n"); |
| 6911 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 6912 | else { |
| 6913 | if (log_len > t->fe->capture_len) |
| 6914 | log_len = t->fe->capture_len; |
| 6915 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 6916 | txn->srv_cookie[log_len] = 0; |
| 6917 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6918 | } |
| 6919 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6920 | srv = target_srv(&t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6921 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6922 | if (!(t->flags & SN_IGNORE_PRST) && |
| 6923 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6924 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6925 | /* assume passive cookie by default */ |
| 6926 | txn->flags &= ~TX_SCK_MASK; |
| 6927 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6928 | |
| 6929 | /* If the cookie is in insert mode on a known server, we'll delete |
| 6930 | * this occurrence because we'll insert another one later. |
| 6931 | * 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] | 6932 | * a direct access. |
| 6933 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6934 | if (t->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 6935 | /* The "preserve" flag was set, we don't want to touch the |
| 6936 | * server's cookie. |
| 6937 | */ |
| 6938 | } |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6939 | else if ((srv && (t->be->ck_opts & PR_CK_INS)) || |
| 6940 | ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6941 | /* this cookie must be deleted */ |
| 6942 | if (*prev == ':' && next == hdr_end) { |
| 6943 | /* whole header */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6944 | delta = buffer_replace2(&res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6945 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6946 | txn->hdr_idx.used--; |
| 6947 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6948 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6949 | hdr_next += delta; |
| 6950 | http_msg_move_end(&txn->rsp, delta); |
| 6951 | /* note: while both invalid now, <next> and <hdr_end> |
| 6952 | * are still equal, so the for() will stop as expected. |
| 6953 | */ |
| 6954 | } else { |
| 6955 | /* just remove the value */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6956 | int delta = del_hdr_value(&res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6957 | next = prev; |
| 6958 | hdr_end += delta; |
| 6959 | hdr_next += delta; |
| 6960 | cur_hdr->len += delta; |
| 6961 | http_msg_move_end(&txn->rsp, delta); |
| 6962 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6963 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6964 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6965 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6966 | } |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6967 | else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6968 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6969 | * with this server since we know it. |
| 6970 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6971 | 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] | 6972 | next += delta; |
| 6973 | hdr_end += delta; |
| 6974 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6975 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6976 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6977 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6978 | txn->flags &= ~TX_SCK_MASK; |
| 6979 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6980 | } |
Willy Tarreau | a059031 | 2012-06-06 16:07:00 +0200 | [diff] [blame] | 6981 | else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6982 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6983 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6984 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6985 | 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] | 6986 | next += delta; |
| 6987 | hdr_end += delta; |
| 6988 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6989 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6990 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6991 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6992 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6993 | txn->flags &= ~TX_SCK_MASK; |
| 6994 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6995 | } |
| 6996 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6997 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 6998 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6999 | int cmp_len, value_len; |
| 7000 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7001 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7002 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7003 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 7004 | value_begin = att_beg + t->be->appsession_name_len; |
| 7005 | value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7006 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7007 | cmp_len = att_end - att_beg; |
| 7008 | value_begin = val_beg; |
| 7009 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7010 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7011 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7012 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7013 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7014 | /* free a possibly previously allocated memory */ |
| 7015 | pool_free2(apools.sessid, txn->sessid); |
| 7016 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7017 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7018 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7019 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7020 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7021 | return; |
| 7022 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7023 | memcpy(txn->sessid, value_begin, value_len); |
| 7024 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7025 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7026 | } |
| 7027 | /* that's done for this cookie, check the next one on the same |
| 7028 | * line when next != hdr_end (only if is_cookie2). |
| 7029 | */ |
| 7030 | } |
| 7031 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7032 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7033 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7034 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7035 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7036 | appsess *asession = NULL; |
| 7037 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7038 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7039 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 7040 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7041 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 7042 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 7043 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 7044 | return; |
| 7045 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7046 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 7047 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7048 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 7049 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7050 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Cyril Bonté | 41689c2 | 2010-01-10 00:30:14 +0100 | [diff] [blame] | 7051 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7052 | return; |
| 7053 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7054 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7055 | asession->sessid[t->be->appsession_len] = 0; |
| 7056 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7057 | server_id_len = strlen(target_srv(&t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7058 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7059 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7060 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Cyril Bonté | 41689c2 | 2010-01-10 00:30:14 +0100 | [diff] [blame] | 7061 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7062 | return; |
| 7063 | } |
| 7064 | asession->serverid[0] = '\0'; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7065 | memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7066 | |
| 7067 | asession->request_count = 0; |
| 7068 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 7069 | } |
| 7070 | |
| 7071 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 7072 | asession->request_count++; |
| 7073 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7074 | } |
| 7075 | |
| 7076 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7077 | /* |
| 7078 | * Check if response is cacheable or not. Updates t->flags. |
| 7079 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7080 | void check_response_for_cacheability(struct session *t, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7081 | { |
| 7082 | struct http_txn *txn = &t->txn; |
| 7083 | char *p1, *p2; |
| 7084 | |
| 7085 | char *cur_ptr, *cur_end, *cur_next; |
| 7086 | int cur_idx; |
| 7087 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 7088 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7089 | return; |
| 7090 | |
| 7091 | /* Iterate through the headers. |
| 7092 | * we start with the start line. |
| 7093 | */ |
| 7094 | cur_idx = 0; |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 7095 | cur_next = rtr->buf.p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7096 | |
| 7097 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7098 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7099 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7100 | |
| 7101 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7102 | cur_ptr = cur_next; |
| 7103 | cur_end = cur_ptr + cur_hdr->len; |
| 7104 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7105 | |
| 7106 | /* We have one full header between cur_ptr and cur_end, and the |
| 7107 | * next header starts at cur_next. We're only interested in |
| 7108 | * "Cookie:" headers. |
| 7109 | */ |
| 7110 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7111 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7112 | if (val) { |
| 7113 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7114 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7115 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7116 | return; |
| 7117 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7118 | } |
| 7119 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7120 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7121 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7122 | continue; |
| 7123 | |
| 7124 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7125 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7126 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7127 | |
| 7128 | if (p1 >= cur_end) /* no more info */ |
| 7129 | continue; |
| 7130 | |
| 7131 | /* p1 is at the beginning of the value */ |
| 7132 | p2 = p1; |
| 7133 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7134 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7135 | p2++; |
| 7136 | |
| 7137 | /* we have a complete value between p1 and p2 */ |
| 7138 | if (p2 < cur_end && *p2 == '=') { |
| 7139 | /* we have something of the form no-cache="set-cookie" */ |
| 7140 | if ((cur_end - p1 >= 21) && |
| 7141 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7142 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7143 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7144 | continue; |
| 7145 | } |
| 7146 | |
| 7147 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7148 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 7149 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7150 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7151 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7152 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7153 | return; |
| 7154 | } |
| 7155 | |
| 7156 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7157 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7158 | continue; |
| 7159 | } |
| 7160 | } |
| 7161 | } |
| 7162 | |
| 7163 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7164 | /* |
| 7165 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7166 | * If the server is found, it's assigned to the session. |
| 7167 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7168 | void get_srv_from_appsession(struct session *t, const char *begin, int len) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7169 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7170 | char *end_params, *first_param, *cur_param, *next_param; |
| 7171 | char separator; |
| 7172 | int value_len; |
| 7173 | |
| 7174 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7175 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7176 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7177 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST && t->txn.meth != HTTP_METH_HEAD)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7178 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7179 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7180 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7181 | first_param = NULL; |
| 7182 | switch (mode) { |
| 7183 | case PR_O2_AS_M_PP: |
| 7184 | first_param = memchr(begin, ';', len); |
| 7185 | break; |
| 7186 | case PR_O2_AS_M_QS: |
| 7187 | first_param = memchr(begin, '?', len); |
| 7188 | break; |
| 7189 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7190 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7191 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7192 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7193 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7194 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7195 | switch (mode) { |
| 7196 | case PR_O2_AS_M_PP: |
| 7197 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7198 | end_params = (char *) begin + len; |
| 7199 | } |
| 7200 | separator = ';'; |
| 7201 | break; |
| 7202 | case PR_O2_AS_M_QS: |
| 7203 | end_params = (char *) begin + len; |
| 7204 | separator = '&'; |
| 7205 | break; |
| 7206 | default: |
| 7207 | /* unknown mode, shouldn't happen */ |
| 7208 | return; |
| 7209 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7210 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7211 | cur_param = next_param = end_params; |
| 7212 | while (cur_param > first_param) { |
| 7213 | cur_param--; |
| 7214 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7215 | /* let's see if this is the appsession parameter */ |
| 7216 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7217 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7218 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7219 | /* Cool... it's the right one */ |
| 7220 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7221 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7222 | if (value_len > 0) { |
| 7223 | manage_client_side_appsession(t, cur_param, value_len); |
| 7224 | } |
| 7225 | break; |
| 7226 | } |
| 7227 | next_param = cur_param; |
| 7228 | } |
| 7229 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7230 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7231 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7232 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7233 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7234 | } |
| 7235 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7236 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7237 | * 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] | 7238 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7239 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7240 | * 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] | 7241 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7242 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7243 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7244 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7245 | 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] | 7246 | { |
| 7247 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7248 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 7249 | const char *uri = msg->chn->buf.p+ msg->sl.rq.u; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7250 | const char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7251 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7252 | if (!uri_auth) |
| 7253 | return 0; |
| 7254 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7255 | 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] | 7256 | return 0; |
| 7257 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7258 | memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats)); |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7259 | si->applet.ctx.stats.st_code = STAT_STATUS_INIT; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7260 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7261 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7262 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7263 | return 0; |
| 7264 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7265 | h = uri; |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 7266 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7267 | return 0; |
| 7268 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7269 | h += uri_auth->uri_len; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7270 | while (h <= uri + msg->sl.rq.u_l - 3) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7271 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7272 | si->applet.ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7273 | break; |
| 7274 | } |
| 7275 | h++; |
| 7276 | } |
| 7277 | |
| 7278 | if (uri_auth->refresh) { |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7279 | h = uri + uri_auth->uri_len; |
| 7280 | while (h <= uri + msg->sl.rq.u_l - 10) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7281 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7282 | si->applet.ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7283 | break; |
| 7284 | } |
| 7285 | h++; |
| 7286 | } |
| 7287 | } |
| 7288 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7289 | h = uri + uri_auth->uri_len; |
| 7290 | while (h <= uri + msg->sl.rq.u_l - 4) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7291 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7292 | si->applet.ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7293 | break; |
| 7294 | } |
| 7295 | h++; |
| 7296 | } |
| 7297 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7298 | h = uri + uri_auth->uri_len; |
| 7299 | while (h <= uri + msg->sl.rq.u_l - 8) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7300 | if (memcmp(h, ";st=", 4) == 0) { |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7301 | int i; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7302 | h += 4; |
Cyril Bonté | 20a804a | 2012-05-10 19:42:52 +0200 | [diff] [blame] | 7303 | si->applet.ctx.stats.st_code = STAT_STATUS_UNKN; |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7304 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 7305 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
| 7306 | si->applet.ctx.stats.st_code = i; |
| 7307 | break; |
| 7308 | } |
| 7309 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7310 | break; |
| 7311 | } |
| 7312 | h++; |
| 7313 | } |
| 7314 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7315 | si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7316 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7317 | return 1; |
| 7318 | } |
| 7319 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7320 | /* |
| 7321 | * 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] | 7322 | * By default it tries to report the error position as msg->err_pos. However if |
| 7323 | * this one is not set, it will then report msg->next, which is the last known |
| 7324 | * 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] | 7325 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7326 | */ |
| 7327 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7328 | struct http_msg *msg, |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7329 | int state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7330 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7331 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7332 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7333 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7334 | es->len = MIN(chn->buf.i, sizeof(es->buf)); |
| 7335 | len1 = chn->buf.data + chn->buf.size - chn->buf.p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7336 | len1 = MIN(len1, es->len); |
| 7337 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 7338 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7339 | memcpy(es->buf, chn->buf.p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7340 | if (len2) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7341 | memcpy(es->buf + len1, chn->buf.data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7342 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7343 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7344 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7345 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7346 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7347 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7348 | es->when = date; // user-visible date |
| 7349 | es->sid = s->uniq_id; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7350 | es->srv = target_srv(&s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7351 | es->oe = other_end; |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 7352 | es->src = s->req->prod->conn.addr.from; |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7353 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7354 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7355 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7356 | es->s_flags = s->flags; |
| 7357 | es->t_flags = s->txn.flags; |
| 7358 | es->m_flags = msg->flags; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7359 | es->b_out = chn->buf.o; |
| 7360 | es->b_wrap = chn->buf.data + chn->buf.size - chn->buf.p; |
| 7361 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7362 | es->m_clen = msg->chunk_len; |
| 7363 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7364 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7365 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7366 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7367 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7368 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7369 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7370 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7371 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7372 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 7373 | * -1. |
| 7374 | * 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] | 7375 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7376 | 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] | 7377 | struct hdr_idx *idx, int occ, |
| 7378 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7379 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7380 | struct hdr_ctx local_ctx; |
| 7381 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7382 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7383 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7384 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7385 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7386 | if (!ctx) { |
| 7387 | local_ctx.idx = 0; |
| 7388 | ctx = &local_ctx; |
| 7389 | } |
| 7390 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7391 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7392 | /* search from the beginning */ |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 7393 | while (http_find_header2(hname, hlen, msg->chn->buf.p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7394 | occ--; |
| 7395 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7396 | *vptr = ctx->line + ctx->val; |
| 7397 | *vlen = ctx->vlen; |
| 7398 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7399 | } |
| 7400 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7401 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7402 | } |
| 7403 | |
| 7404 | /* negative occurrence, we scan all the list then walk back */ |
| 7405 | if (-occ > MAX_HDR_HISTORY) |
| 7406 | return 0; |
| 7407 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7408 | found = hist_ptr = 0; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 7409 | while (http_find_header2(hname, hlen, msg->chn->buf.p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7410 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7411 | len_hist[hist_ptr] = ctx->vlen; |
| 7412 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7413 | hist_ptr = 0; |
| 7414 | found++; |
| 7415 | } |
| 7416 | if (-occ > found) |
| 7417 | return 0; |
| 7418 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 7419 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 7420 | */ |
| 7421 | hist_ptr += occ; |
| 7422 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7423 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7424 | *vptr = ptr_hist[hist_ptr]; |
| 7425 | *vlen = len_hist[hist_ptr]; |
| 7426 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7427 | } |
| 7428 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7429 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7430 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 7431 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 7432 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7433 | */ |
| 7434 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 7435 | { |
| 7436 | int len, max; |
| 7437 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
Willy Tarreau | fb7508a | 2012-05-21 16:47:54 +0200 | [diff] [blame] | 7438 | dir, (unsigned short)si_fd(t->req->prod), (unsigned short)si_fd(t->req->cons)); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7439 | |
| 7440 | for (max = 0; start + max < end; max++) |
| 7441 | if (start[max] == '\r' || start[max] == '\n') |
| 7442 | break; |
| 7443 | |
| 7444 | UBOUND(max, trashlen - len - 3); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7445 | len += strlcpy2(trash + len, start, max + 1); |
| 7446 | trash[len++] = '\n'; |
Willy Tarreau | 2133782 | 2012-04-29 14:11:38 +0200 | [diff] [blame] | 7447 | if (write(1, trash, len) < 0) /* shut gcc warning */; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7448 | } |
| 7449 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7450 | /* |
| 7451 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 7452 | * the required fields are properly allocated and that we only need to (re)init |
| 7453 | * them. This should be used before processing any new request. |
| 7454 | */ |
| 7455 | void http_init_txn(struct session *s) |
| 7456 | { |
| 7457 | struct http_txn *txn = &s->txn; |
| 7458 | struct proxy *fe = s->fe; |
| 7459 | |
| 7460 | txn->flags = 0; |
| 7461 | txn->status = -1; |
| 7462 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 7463 | global.req_count++; |
| 7464 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7465 | txn->cookie_first_date = 0; |
| 7466 | txn->cookie_last_date = 0; |
| 7467 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7468 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 7469 | 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] | 7470 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7471 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 7472 | 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] | 7473 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 7474 | txn->req.chunk_len = 0LL; |
| 7475 | txn->req.body_len = 0LL; |
| 7476 | txn->rsp.chunk_len = 0LL; |
| 7477 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7478 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7479 | 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] | 7480 | txn->req.chn = s->req; |
| 7481 | txn->rsp.chn = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7482 | |
| 7483 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7484 | |
| 7485 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7486 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7487 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7488 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7489 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7490 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 7491 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7492 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7493 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 7494 | |
| 7495 | if (txn->hdr_idx.v) |
| 7496 | hdr_idx_init(&txn->hdr_idx); |
| 7497 | } |
| 7498 | |
| 7499 | /* to be used at the end of a transaction */ |
| 7500 | void http_end_txn(struct session *s) |
| 7501 | { |
| 7502 | struct http_txn *txn = &s->txn; |
| 7503 | |
| 7504 | /* these ones will have been dynamically allocated */ |
| 7505 | pool_free2(pool2_requri, txn->uri); |
| 7506 | pool_free2(pool2_capture, txn->cli_cookie); |
| 7507 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7508 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7509 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7510 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7511 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7512 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7513 | txn->uri = NULL; |
| 7514 | txn->srv_cookie = NULL; |
| 7515 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7516 | |
| 7517 | if (txn->req.cap) { |
| 7518 | struct cap_hdr *h; |
| 7519 | for (h = s->fe->req_cap; h; h = h->next) |
| 7520 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 7521 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 7522 | } |
| 7523 | |
| 7524 | if (txn->rsp.cap) { |
| 7525 | struct cap_hdr *h; |
| 7526 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 7527 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 7528 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 7529 | } |
| 7530 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7531 | } |
| 7532 | |
| 7533 | /* to be used at the end of a transaction to prepare a new one */ |
| 7534 | void http_reset_txn(struct session *s) |
| 7535 | { |
| 7536 | http_end_txn(s); |
| 7537 | http_init_txn(s); |
| 7538 | |
| 7539 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7540 | s->logs.logwait = s->fe->to_log; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 7541 | session_del_srv_conn(s); |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 7542 | clear_target(&s->target); |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7543 | /* re-init store persistence */ |
| 7544 | s->store_count = 0; |
| 7545 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7546 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7547 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7548 | s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7549 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7550 | /* We must trim any excess data from the response buffer, because we |
| 7551 | * may have blocked an invalid response from a server that we don't |
| 7552 | * want to accidentely forward once we disable the analysers, nor do |
| 7553 | * we want those data to come along with next response. A typical |
| 7554 | * example of such data would be from a buggy server responding to |
| 7555 | * a HEAD with some data, or sending more than the advertised |
| 7556 | * content-length. |
| 7557 | */ |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 7558 | if (unlikely(s->rep->buf.i)) |
| 7559 | s->rep->buf.i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7560 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7561 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7562 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7563 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7564 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7565 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7566 | |
| 7567 | s->req->rex = TICK_ETERNITY; |
| 7568 | s->req->wex = TICK_ETERNITY; |
| 7569 | s->req->analyse_exp = TICK_ETERNITY; |
| 7570 | s->rep->rex = TICK_ETERNITY; |
| 7571 | s->rep->wex = TICK_ETERNITY; |
| 7572 | s->rep->analyse_exp = TICK_ETERNITY; |
| 7573 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7574 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7575 | void free_http_req_rules(struct list *r) { |
| 7576 | struct http_req_rule *tr, *pr; |
| 7577 | |
| 7578 | list_for_each_entry_safe(pr, tr, r, list) { |
| 7579 | LIST_DEL(&pr->list); |
| 7580 | if (pr->action == HTTP_REQ_ACT_HTTP_AUTH) |
| 7581 | free(pr->http_auth.realm); |
| 7582 | |
| 7583 | free(pr); |
| 7584 | } |
| 7585 | } |
| 7586 | |
| 7587 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 7588 | { |
| 7589 | struct http_req_rule *rule; |
| 7590 | int cur_arg; |
| 7591 | |
| 7592 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 7593 | if (!rule) { |
| 7594 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 7595 | return NULL; |
| 7596 | } |
| 7597 | |
| 7598 | if (!*args[0]) { |
| 7599 | goto req_error_parsing; |
| 7600 | } else if (!strcmp(args[0], "allow")) { |
| 7601 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 7602 | cur_arg = 1; |
| 7603 | } else if (!strcmp(args[0], "deny")) { |
| 7604 | rule->action = HTTP_REQ_ACT_DENY; |
| 7605 | cur_arg = 1; |
| 7606 | } else if (!strcmp(args[0], "auth")) { |
| 7607 | rule->action = HTTP_REQ_ACT_HTTP_AUTH; |
| 7608 | cur_arg = 1; |
| 7609 | |
| 7610 | while(*args[cur_arg]) { |
| 7611 | if (!strcmp(args[cur_arg], "realm")) { |
| 7612 | rule->http_auth.realm = strdup(args[cur_arg + 1]); |
| 7613 | cur_arg+=2; |
| 7614 | continue; |
| 7615 | } else |
| 7616 | break; |
| 7617 | } |
| 7618 | } else { |
| 7619 | req_error_parsing: |
| 7620 | Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n", |
| 7621 | file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]); |
| 7622 | return NULL; |
| 7623 | } |
| 7624 | |
| 7625 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 7626 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 7627 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7628 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 7629 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 7630 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 7631 | file, linenum, args[0], errmsg); |
| 7632 | free(errmsg); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7633 | return NULL; |
| 7634 | } |
| 7635 | rule->cond = cond; |
| 7636 | } |
| 7637 | else if (*args[cur_arg]) { |
| 7638 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 7639 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 7640 | file, linenum, args[0], args[cur_arg]); |
| 7641 | return NULL; |
| 7642 | } |
| 7643 | |
| 7644 | return rule; |
| 7645 | } |
| 7646 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7647 | /************************************************************************/ |
| 7648 | /* The code below is dedicated to ACL parsing and matching */ |
| 7649 | /************************************************************************/ |
| 7650 | |
| 7651 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7652 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 7653 | * which means that a request or response is ready. If some data is missing, |
| 7654 | * 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] | 7655 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 7656 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7657 | * |
| 7658 | * The function returns : |
| 7659 | * 0 if some data is missing or if the requested data cannot be fetched |
| 7660 | * -1 if it is certain that we'll never have any HTTP message there |
| 7661 | * 1 if an HTTP message is ready |
| 7662 | */ |
| 7663 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7664 | acl_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] | 7665 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7666 | { |
| 7667 | struct http_txn *txn = l7; |
| 7668 | struct http_msg *msg = &txn->req; |
| 7669 | |
| 7670 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 7671 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 7672 | */ |
| 7673 | |
| 7674 | if (unlikely(!s || !txn)) |
| 7675 | return 0; |
| 7676 | |
| 7677 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7678 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7679 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7680 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7681 | if (unlikely(!s->req)) |
| 7682 | return 0; |
| 7683 | |
| 7684 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 7685 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
| 7686 | buffer_full(&s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 7687 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7688 | return -1; |
| 7689 | } |
| 7690 | |
| 7691 | /* Try to decode HTTP request */ |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 7692 | if (likely(msg->next < s->req->buf.i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7693 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 7694 | |
| 7695 | /* Still no valid request ? */ |
| 7696 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 7697 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
| 7698 | buffer_full(&s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 7699 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7700 | return -1; |
| 7701 | } |
| 7702 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7703 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7704 | return 0; |
| 7705 | } |
| 7706 | |
| 7707 | /* OK we just got a valid HTTP request. We have some minor |
| 7708 | * preparation to perform so that further checks can rely |
| 7709 | * on HTTP tests. |
| 7710 | */ |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 7711 | 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] | 7712 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 7713 | s->flags |= SN_REDIRECTABLE; |
| 7714 | |
| 7715 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) { |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 7716 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7717 | return -1; |
| 7718 | } |
| 7719 | } |
| 7720 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7721 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7722 | return 0; /* data might have moved and indexes changed */ |
| 7723 | |
| 7724 | /* otherwise everything's ready for the request */ |
| 7725 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7726 | else { |
| 7727 | /* Check for a dependency on a response */ |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7728 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 7729 | return 0; |
| 7730 | } |
| 7731 | |
| 7732 | /* everything's OK */ |
| 7733 | return 1; |
| 7734 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7735 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7736 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7737 | do { int r = acl_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] | 7738 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7739 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7740 | do { int r = acl_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] | 7741 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7742 | |
| 7743 | /* 1. Check on METHOD |
| 7744 | * We use the pre-parsed method if it is known, and store its number as an |
| 7745 | * integer. If it is unknown, we use the pointer and the length. |
| 7746 | */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7747 | static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7748 | { |
| 7749 | int len, meth; |
| 7750 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7751 | len = strlen(*text); |
| 7752 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7753 | |
| 7754 | pattern->val.i = meth; |
| 7755 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7756 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7757 | if (!pattern->ptr.str) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 7758 | memprintf(err, "out of memory while loading pattern"); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7759 | return 0; |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7760 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7761 | pattern->len = len; |
| 7762 | } |
| 7763 | return 1; |
| 7764 | } |
| 7765 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7766 | /* This function fetches the method of current HTTP request and stores |
| 7767 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 7768 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 7769 | * in <len> and <ptr> is NULL ; |
| 7770 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 7771 | * <len> to its length. |
| 7772 | * This is intended to be used with acl_match_meth() only. |
| 7773 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7774 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7775 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7776 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7777 | { |
| 7778 | int meth; |
| 7779 | struct http_txn *txn = l7; |
| 7780 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7781 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7782 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7783 | meth = txn->meth; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7784 | smp->type = SMP_T_UINT; |
| 7785 | smp->data.uint = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7786 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7787 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7788 | /* ensure the indexes are not affected */ |
| 7789 | return 0; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7790 | smp->type = SMP_T_CSTR; |
| 7791 | smp->data.str.len = txn->req.sl.rq.m_l; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 7792 | smp->data.str.str = txn->req.chn->buf.p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7793 | } |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 7794 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7795 | return 1; |
| 7796 | } |
| 7797 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7798 | /* See above how the method is stored in the global pattern */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7799 | static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7800 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7801 | int icase; |
| 7802 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7803 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7804 | if (smp->type == SMP_T_UINT) { |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7805 | /* well-known method */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7806 | if (smp->data.uint == pattern->val.i) |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7807 | return ACL_PAT_PASS; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7808 | return ACL_PAT_FAIL; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7809 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7810 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7811 | /* Uncommon method, only HTTP_METH_OTHER is accepted now */ |
| 7812 | if (pattern->val.i != HTTP_METH_OTHER) |
| 7813 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7814 | |
| 7815 | /* Other method, we must compare the strings */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7816 | if (pattern->len != smp->data.str.len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7817 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7818 | |
| 7819 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7820 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) || |
| 7821 | (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7822 | return ACL_PAT_FAIL; |
| 7823 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7824 | } |
| 7825 | |
| 7826 | /* 2. Check on Request/Status Version |
| 7827 | * We simply compare strings here. |
| 7828 | */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7829 | static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7830 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7831 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7832 | if (!pattern->ptr.str) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 7833 | memprintf(err, "out of memory while loading pattern"); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7834 | return 0; |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7835 | } |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7836 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7837 | return 1; |
| 7838 | } |
| 7839 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7840 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7841 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7842 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7843 | { |
| 7844 | struct http_txn *txn = l7; |
| 7845 | char *ptr; |
| 7846 | int len; |
| 7847 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7848 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7849 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7850 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 7851 | ptr = txn->req.chn->buf.p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7852 | |
| 7853 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7854 | if (len <= 0) |
| 7855 | return 0; |
| 7856 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7857 | smp->type = SMP_T_CSTR; |
| 7858 | smp->data.str.str = ptr; |
| 7859 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7860 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 7861 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7862 | return 1; |
| 7863 | } |
| 7864 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7865 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7866 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7867 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7868 | { |
| 7869 | struct http_txn *txn = l7; |
| 7870 | char *ptr; |
| 7871 | int len; |
| 7872 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7873 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7874 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7875 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 7876 | ptr = txn->rsp.chn->buf.p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7877 | |
| 7878 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7879 | if (len <= 0) |
| 7880 | return 0; |
| 7881 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7882 | smp->type = SMP_T_CSTR; |
| 7883 | smp->data.str.str = ptr; |
| 7884 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7885 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 7886 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7887 | return 1; |
| 7888 | } |
| 7889 | |
| 7890 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7891 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7892 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7893 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7894 | { |
| 7895 | struct http_txn *txn = l7; |
| 7896 | char *ptr; |
| 7897 | int len; |
| 7898 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7899 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7900 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7901 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 7902 | ptr = txn->rsp.chn->buf.p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7903 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7904 | smp->type = SMP_T_UINT; |
| 7905 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7906 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7907 | return 1; |
| 7908 | } |
| 7909 | |
| 7910 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7911 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 7912 | smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7913 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7914 | { |
| 7915 | struct http_txn *txn = l7; |
| 7916 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7917 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7918 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7919 | smp->type = SMP_T_CSTR; |
| 7920 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 7921 | smp->data.str.str = txn->req.chn->buf.p + txn->req.sl.rq.u; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7922 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7923 | return 1; |
| 7924 | } |
| 7925 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7926 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 7927 | smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7928 | const struct arg *args, struct sample *smp) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7929 | { |
| 7930 | struct http_txn *txn = l7; |
| 7931 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7932 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7933 | |
| 7934 | /* Parse HTTP request */ |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 7935 | url2sa(txn->req.chn->buf.p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn.addr.to); |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 7936 | if (((struct sockaddr_in *)&l4->req->cons->conn.addr.to)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 7937 | return 0; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7938 | smp->type = SMP_T_IPV4; |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 7939 | smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn.addr.to)->sin_addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7940 | |
| 7941 | /* |
| 7942 | * If we are parsing url in frontend space, we prepare backend stage |
| 7943 | * to not parse again the same url ! optimization lazyness... |
| 7944 | */ |
| 7945 | if (px->options & PR_O_HTTP_PROXY) |
| 7946 | l4->flags |= SN_ADDR_SET; |
| 7947 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7948 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7949 | return 1; |
| 7950 | } |
| 7951 | |
| 7952 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 7953 | smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7954 | const struct arg *args, struct sample *smp) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7955 | { |
| 7956 | struct http_txn *txn = l7; |
| 7957 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7958 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7959 | |
| 7960 | /* Same optimization as url_ip */ |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 7961 | url2sa(txn->req.chn->buf.p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn.addr.to); |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7962 | smp->type = SMP_T_UINT; |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 7963 | smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn.addr.to)->sin_port); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7964 | |
| 7965 | if (px->options & PR_O_HTTP_PROXY) |
| 7966 | l4->flags |= SN_ADDR_SET; |
| 7967 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 7968 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7969 | return 1; |
| 7970 | } |
| 7971 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7972 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 7973 | * Accepts an optional argument of type string containing the header field name, |
| 7974 | * and an optional argument of type signed or unsigned integer to request an |
| 7975 | * explicit occurrence of the header. Note that in the event of a missing name, |
| 7976 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7977 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7978 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7979 | smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7980 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7981 | { |
| 7982 | struct http_txn *txn = l7; |
| 7983 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7984 | struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7985 | 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] | 7986 | int occ = 0; |
| 7987 | const char *name_str = NULL; |
| 7988 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 7989 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7990 | if (args) { |
| 7991 | if (args[0].type != ARGT_STR) |
| 7992 | return 0; |
| 7993 | name_str = args[0].data.str.str; |
| 7994 | name_len = args[0].data.str.len; |
| 7995 | |
| 7996 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 7997 | occ = args[1].data.uint; |
| 7998 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 7999 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8000 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8001 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8002 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8003 | /* search for header from the beginning */ |
| 8004 | ctx->idx = 0; |
| 8005 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8006 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 8007 | /* no explicit occurrence and single fetch => last header by default */ |
| 8008 | occ = -1; |
| 8009 | |
| 8010 | if (!occ) |
| 8011 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8012 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 8013 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8014 | smp->type = SMP_T_CSTR; |
| 8015 | smp->flags |= SMP_F_VOL_HDR; |
| 8016 | 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] | 8017 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8018 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8019 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8020 | return 0; |
| 8021 | } |
| 8022 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8023 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8024 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8025 | */ |
| 8026 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8027 | smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8028 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8029 | { |
| 8030 | struct http_txn *txn = l7; |
| 8031 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8032 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8033 | 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] | 8034 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8035 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8036 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8037 | return 0; |
| 8038 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8039 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8040 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8041 | ctx.idx = 0; |
| 8042 | cnt = 0; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 8043 | 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] | 8044 | cnt++; |
| 8045 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8046 | smp->type = SMP_T_UINT; |
| 8047 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8048 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8049 | return 1; |
| 8050 | } |
| 8051 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8052 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 8053 | * takes a mandatory argument of type string and an optional one of type int |
| 8054 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 8055 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8056 | */ |
| 8057 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8058 | smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8059 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8060 | { |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8061 | int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8062 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8063 | if (ret > 0) { |
| 8064 | smp->type = SMP_T_UINT; |
| 8065 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 8066 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8067 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8068 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8069 | } |
| 8070 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8071 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 8072 | * takes a mandatory argument of type string and an optional one of type int |
| 8073 | * to designate a specific occurrence. It returns an IPv4 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8074 | */ |
| 8075 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8076 | smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8077 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8078 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8079 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8080 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8081 | while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8082 | smp->type = SMP_T_IPV4; |
| 8083 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8084 | break; |
| 8085 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8086 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 8087 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8088 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8089 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8090 | } |
| 8091 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8092 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 8093 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 8094 | */ |
| 8095 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 8096 | smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8097 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8098 | { |
| 8099 | struct http_txn *txn = l7; |
| 8100 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8101 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8102 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8103 | |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 8104 | 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] | 8105 | ptr = http_get_path(txn); |
| 8106 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8107 | return 0; |
| 8108 | |
| 8109 | /* OK, we got the '/' ! */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8110 | smp->type = SMP_T_CSTR; |
| 8111 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8112 | |
| 8113 | while (ptr < end && *ptr != '?') |
| 8114 | ptr++; |
| 8115 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8116 | smp->data.str.len = ptr - smp->data.str.str; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8117 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8118 | return 1; |
| 8119 | } |
| 8120 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 8121 | /* This produces a concatenation of the first occurrence of the Host header |
| 8122 | * followed by the path component if it begins with a slash ('/'). This means |
| 8123 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 8124 | * If no Host header is found, then the path is returned as-is. The returned |
| 8125 | * value is stored in the trash so it does not need to be marked constant. |
| 8126 | */ |
| 8127 | static int |
| 8128 | smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8129 | const struct arg *args, struct sample *smp) |
| 8130 | { |
| 8131 | struct http_txn *txn = l7; |
| 8132 | char *ptr, *end, *beg; |
| 8133 | struct hdr_ctx ctx; |
| 8134 | |
| 8135 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8136 | |
| 8137 | ctx.idx = 0; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 8138 | if (!http_find_header2("Host", 4, txn->req.chn->buf.p + txn->req.sol, &txn->hdr_idx, &ctx) || |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 8139 | !ctx.vlen) |
| 8140 | return smp_fetch_path(px, l4, l7, opt, args, smp); |
| 8141 | |
| 8142 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 8143 | memcpy(trash, ctx.line + ctx.val, ctx.vlen); |
| 8144 | smp->type = SMP_T_STR; |
| 8145 | smp->data.str.str = trash; |
| 8146 | smp->data.str.len = ctx.vlen; |
| 8147 | |
| 8148 | /* now retrieve the path */ |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 8149 | end = txn->req.chn->buf.p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 8150 | beg = http_get_path(txn); |
| 8151 | if (!beg) |
| 8152 | beg = end; |
| 8153 | |
| 8154 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 8155 | |
| 8156 | if (beg < ptr && *beg == '/') { |
| 8157 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 8158 | smp->data.str.len += ptr - beg; |
| 8159 | } |
| 8160 | |
| 8161 | smp->flags = SMP_F_VOL_1ST; |
| 8162 | return 1; |
| 8163 | } |
| 8164 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8165 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8166 | acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8167 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8168 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8169 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8170 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8171 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8172 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8173 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8174 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8175 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 8176 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8177 | return 1; |
| 8178 | } |
| 8179 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8180 | /* return a valid test if the current request is the first one on the connection */ |
| 8181 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8182 | acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8183 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8184 | { |
| 8185 | if (!s) |
| 8186 | return 0; |
| 8187 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8188 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 8189 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8190 | return 1; |
| 8191 | } |
| 8192 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8193 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8194 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8195 | acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8196 | const struct arg *args, struct sample *smp) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8197 | { |
| 8198 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8199 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8200 | return 0; |
| 8201 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8202 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8203 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8204 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8205 | return 0; |
| 8206 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8207 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8208 | smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8209 | return 1; |
| 8210 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8211 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 8212 | /* Accepts exactly 1 argument of type userlist */ |
| 8213 | static int |
| 8214 | acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8215 | const struct arg *args, struct sample *smp) |
| 8216 | { |
| 8217 | |
| 8218 | if (!args || args->type != ARGT_USR) |
| 8219 | return 0; |
| 8220 | |
| 8221 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8222 | |
| 8223 | if (!get_http_auth(l4)) |
| 8224 | return 0; |
| 8225 | |
| 8226 | /* acl_match_auth() will need several information at once */ |
| 8227 | smp->ctx.a[0] = args->data.usr; /* user list */ |
| 8228 | smp->ctx.a[1] = l4->txn.auth.user; /* user name */ |
| 8229 | smp->ctx.a[2] = l4->txn.auth.pass; /* password */ |
| 8230 | |
| 8231 | /* if the user does not belong to the userlist or has a wrong password, |
| 8232 | * report that it unconditionally does not match. Otherwise we return |
| 8233 | * a non-zero integer which will be ignored anyway since all the params |
| 8234 | * that acl_match_auth() will use are in test->ctx.a[0,1,2]. |
| 8235 | */ |
| 8236 | smp->type = SMP_T_BOOL; |
| 8237 | smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass); |
| 8238 | if (smp->data.uint) |
| 8239 | smp->type = SMP_T_UINT; |
| 8240 | |
| 8241 | return 1; |
| 8242 | } |
| 8243 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8244 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 8245 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 8246 | * the cookie value is returned into *value and *value_l, and the function |
| 8247 | * returns a pointer to the next pointer to search from if the value was found. |
| 8248 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 8249 | * nor value_l are touched. The input <hdr> string should first point to the |
| 8250 | * header's value, and the <hdr_end> pointer must point to the first character |
| 8251 | * not part of the value. <list> must be non-zero if value may represent a list |
| 8252 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 8253 | * list is expected. |
| 8254 | */ |
| 8255 | static char * |
| 8256 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 8257 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 8258 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8259 | { |
| 8260 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 8261 | char *next; |
| 8262 | |
| 8263 | /* we search at least a cookie name followed by an equal, and more |
| 8264 | * generally something like this : |
| 8265 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 8266 | */ |
| 8267 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 8268 | /* Iterate through all cookies on this line */ |
| 8269 | |
| 8270 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8271 | att_beg++; |
| 8272 | |
| 8273 | /* find att_end : this is the first character after the last non |
| 8274 | * space before the equal. It may be equal to hdr_end. |
| 8275 | */ |
| 8276 | equal = att_end = att_beg; |
| 8277 | |
| 8278 | while (equal < hdr_end) { |
| 8279 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 8280 | break; |
| 8281 | if (http_is_spht[(unsigned char)*equal++]) |
| 8282 | continue; |
| 8283 | att_end = equal; |
| 8284 | } |
| 8285 | |
| 8286 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8287 | * is between <att_beg> and <equal>, both may be identical. |
| 8288 | */ |
| 8289 | |
| 8290 | /* look for end of cookie if there is an equal sign */ |
| 8291 | if (equal < hdr_end && *equal == '=') { |
| 8292 | /* look for the beginning of the value */ |
| 8293 | val_beg = equal + 1; |
| 8294 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8295 | val_beg++; |
| 8296 | |
| 8297 | /* find the end of the value, respecting quotes */ |
| 8298 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8299 | |
| 8300 | /* make val_end point to the first white space or delimitor after the value */ |
| 8301 | val_end = next; |
| 8302 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8303 | val_end--; |
| 8304 | } else { |
| 8305 | val_beg = val_end = next = equal; |
| 8306 | } |
| 8307 | |
| 8308 | /* We have nothing to do with attributes beginning with '$'. However, |
| 8309 | * they will automatically be removed if a header before them is removed, |
| 8310 | * since they're supposed to be linked together. |
| 8311 | */ |
| 8312 | if (*att_beg == '$') |
| 8313 | continue; |
| 8314 | |
| 8315 | /* Ignore cookies with no equal sign */ |
| 8316 | if (equal == next) |
| 8317 | continue; |
| 8318 | |
| 8319 | /* Now we have the cookie name between att_beg and att_end, and |
| 8320 | * its value between val_beg and val_end. |
| 8321 | */ |
| 8322 | |
| 8323 | if (att_end - att_beg == cookie_name_l && |
| 8324 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 8325 | /* let's return this value and indicate where to go on from */ |
| 8326 | *value = val_beg; |
| 8327 | *value_l = val_end - val_beg; |
| 8328 | return next + 1; |
| 8329 | } |
| 8330 | |
| 8331 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 8332 | if (!list) |
| 8333 | break; |
| 8334 | } |
| 8335 | |
| 8336 | return NULL; |
| 8337 | } |
| 8338 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8339 | /* 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] | 8340 | * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the |
| 8341 | * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8342 | * 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] | 8343 | * 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] | 8344 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 8345 | * that no iterating is desired, then only last value is fetched if any. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8346 | */ |
| 8347 | static int |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 8348 | smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8349 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8350 | { |
| 8351 | struct http_txn *txn = l7; |
| 8352 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8353 | struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8354 | const struct http_msg *msg; |
| 8355 | const char *hdr_name; |
| 8356 | int hdr_name_len; |
| 8357 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8358 | int occ = 0; |
| 8359 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8360 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8361 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8362 | return 0; |
| 8363 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8364 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8365 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8366 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8367 | msg = &txn->req; |
| 8368 | hdr_name = "Cookie"; |
| 8369 | hdr_name_len = 6; |
| 8370 | } else { |
| 8371 | msg = &txn->rsp; |
| 8372 | hdr_name = "Set-Cookie"; |
| 8373 | hdr_name_len = 10; |
| 8374 | } |
| 8375 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8376 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 8377 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 8378 | occ = -1; |
| 8379 | |
| 8380 | /* OK so basically here, either we want only one value and it's the |
| 8381 | * last one, or we want to iterate over all of them and we fetch the |
| 8382 | * next one. |
| 8383 | */ |
| 8384 | |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 8385 | sol = msg->chn->buf.p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8386 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8387 | /* search for the header from the beginning, we must first initialize |
| 8388 | * the search parameters. |
| 8389 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8390 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8391 | ctx->idx = 0; |
| 8392 | } |
| 8393 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8394 | smp->flags |= SMP_F_VOL_HDR; |
| 8395 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8396 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8397 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 8398 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8399 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 8400 | goto out; |
| 8401 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8402 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8403 | continue; |
| 8404 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8405 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 8406 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8407 | } |
| 8408 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8409 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8410 | 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] | 8411 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8412 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8413 | &smp->data.str.str, |
| 8414 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8415 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8416 | found = 1; |
| 8417 | if (occ >= 0) { |
| 8418 | /* one value was returned into smp->data.str.{str,len} */ |
| 8419 | smp->flags |= SMP_F_NOT_LAST; |
| 8420 | return 1; |
| 8421 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8422 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8423 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8424 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8425 | /* all cookie headers and values were scanned. If we're looking for the |
| 8426 | * last occurrence, we may return it now. |
| 8427 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8428 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8429 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8430 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8431 | } |
| 8432 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8433 | /* 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] | 8434 | * match the name in args and args->data.str.len. If <multi> is non-null, then |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8435 | * multiple cookies may be parsed on the same line. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8436 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8437 | */ |
| 8438 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8439 | acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8440 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8441 | { |
| 8442 | struct http_txn *txn = l7; |
| 8443 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8444 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8445 | const struct http_msg *msg; |
| 8446 | const char *hdr_name; |
| 8447 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8448 | int cnt; |
| 8449 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8450 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8451 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8452 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8453 | return 0; |
| 8454 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8455 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8456 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8457 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8458 | msg = &txn->req; |
| 8459 | hdr_name = "Cookie"; |
| 8460 | hdr_name_len = 6; |
| 8461 | } else { |
| 8462 | msg = &txn->rsp; |
| 8463 | hdr_name = "Set-Cookie"; |
| 8464 | hdr_name_len = 10; |
| 8465 | } |
| 8466 | |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 8467 | sol = msg->chn->buf.p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 8468 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8469 | ctx.idx = 0; |
| 8470 | cnt = 0; |
| 8471 | |
| 8472 | while (1) { |
| 8473 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 8474 | if (!val_beg) { |
| 8475 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 8476 | break; |
| 8477 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8478 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8479 | continue; |
| 8480 | |
| 8481 | val_beg = ctx.line + ctx.val; |
| 8482 | val_end = val_beg + ctx.vlen; |
| 8483 | } |
| 8484 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8485 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8486 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8487 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8488 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8489 | &smp->data.str.str, |
| 8490 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8491 | cnt++; |
| 8492 | } |
| 8493 | } |
| 8494 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8495 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8496 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8497 | return 1; |
| 8498 | } |
| 8499 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 8500 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 8501 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 8502 | */ |
| 8503 | static int |
| 8504 | smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8505 | const struct arg *args, struct sample *smp) |
| 8506 | { |
| 8507 | int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp); |
| 8508 | |
| 8509 | if (ret > 0) { |
| 8510 | smp->type = SMP_T_UINT; |
| 8511 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 8512 | } |
| 8513 | |
| 8514 | return ret; |
| 8515 | } |
| 8516 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8517 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 8518 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8519 | /************************************************************************/ |
| 8520 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8521 | /* |
| 8522 | * Given a path string and its length, find the position of beginning of the |
| 8523 | * query string. Returns NULL if no query string is found in the path. |
| 8524 | * |
| 8525 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 8526 | * |
| 8527 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 8528 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8529 | 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] | 8530 | { |
| 8531 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8532 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8533 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8534 | return p ? p + 1 : NULL; |
| 8535 | } |
| 8536 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8537 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8538 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8539 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8540 | } |
| 8541 | |
| 8542 | /* |
| 8543 | * Given a url parameter, find the starting position of the first occurence, |
| 8544 | * or NULL if the parameter is not found. |
| 8545 | * |
| 8546 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 8547 | * the function will return query_string+8. |
| 8548 | */ |
| 8549 | static char* |
| 8550 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8551 | char* url_param_name, size_t url_param_name_l, |
| 8552 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8553 | { |
| 8554 | char *pos, *last; |
| 8555 | |
| 8556 | pos = query_string; |
| 8557 | last = query_string + query_string_l - url_param_name_l - 1; |
| 8558 | |
| 8559 | while (pos <= last) { |
| 8560 | if (pos[url_param_name_l] == '=') { |
| 8561 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 8562 | return pos; |
| 8563 | pos += url_param_name_l + 1; |
| 8564 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8565 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8566 | pos++; |
| 8567 | pos++; |
| 8568 | } |
| 8569 | return NULL; |
| 8570 | } |
| 8571 | |
| 8572 | /* |
| 8573 | * Given a url parameter name, returns its value and size into *value and |
| 8574 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 8575 | * zero is returned and value/value_l are not touched. |
| 8576 | */ |
| 8577 | static int |
| 8578 | find_url_param_value(char* path, size_t path_l, |
| 8579 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8580 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8581 | { |
| 8582 | char *query_string, *qs_end; |
| 8583 | char *arg_start; |
| 8584 | char *value_start, *value_end; |
| 8585 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8586 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8587 | if (!query_string) |
| 8588 | return 0; |
| 8589 | |
| 8590 | qs_end = path + path_l; |
| 8591 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8592 | url_param_name, url_param_name_l, |
| 8593 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8594 | if (!arg_start) |
| 8595 | return 0; |
| 8596 | |
| 8597 | value_start = arg_start + url_param_name_l + 1; |
| 8598 | value_end = value_start; |
| 8599 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8600 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8601 | value_end++; |
| 8602 | |
| 8603 | *value = value_start; |
| 8604 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 8605 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8606 | } |
| 8607 | |
| 8608 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8609 | smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8610 | const struct arg *args, struct sample *smp) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8611 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8612 | char delim = '?'; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8613 | struct http_txn *txn = l7; |
| 8614 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8615 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8616 | if (!args || args[0].type != ARGT_STR || |
| 8617 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8618 | return 0; |
| 8619 | |
| 8620 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8621 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8622 | if (args[1].type) |
| 8623 | delim = *args[1].data.str.str; |
| 8624 | |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 8625 | 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] | 8626 | args->data.str.str, args->data.str.len, |
| 8627 | &smp->data.str.str, &smp->data.str.len, |
| 8628 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8629 | return 0; |
| 8630 | |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 8631 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8632 | smp->flags = SMP_F_VOL_1ST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8633 | return 1; |
| 8634 | } |
| 8635 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 8636 | /* Return the signed integer value for the specified url parameter (see url_param |
| 8637 | * above). |
| 8638 | */ |
| 8639 | static int |
| 8640 | smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8641 | const struct arg *args, struct sample *smp) |
| 8642 | { |
| 8643 | int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp); |
| 8644 | |
| 8645 | if (ret > 0) { |
| 8646 | smp->type = SMP_T_UINT; |
| 8647 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 8648 | } |
| 8649 | |
| 8650 | return ret; |
| 8651 | } |
| 8652 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8653 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 8654 | * keyword. These keywords support an optional positive or negative occurrence |
| 8655 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 8656 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 8657 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 8658 | * error message in case of error, that the caller is responsible for freeing. |
| 8659 | * The initial location must either be freeable or NULL. |
| 8660 | */ |
| 8661 | static int val_hdr(struct arg *arg, char **err_msg) |
| 8662 | { |
| 8663 | 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] | 8664 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8665 | return 0; |
| 8666 | } |
| 8667 | return 1; |
| 8668 | } |
| 8669 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8670 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8671 | /* All supported ACL keywords must be declared here. */ |
| 8672 | /************************************************************************/ |
| 8673 | |
| 8674 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8675 | * Please take care of keeping this list alphabetically sorted. |
| 8676 | */ |
| 8677 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 8678 | { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 8679 | { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8680 | { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8681 | { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8682 | { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8683 | { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8684 | { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8685 | { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8686 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 8687 | { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) }, |
| 8688 | { "cook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8689 | { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 8690 | { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8691 | { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8692 | { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8693 | { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8694 | { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8695 | { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8696 | { "cook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8697 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8698 | { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr }, |
| 8699 | { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8700 | { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8701 | { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8702 | { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8703 | { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8704 | { "hdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr }, |
| 8705 | { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8706 | { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8707 | { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8708 | { "hdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8709 | |
| 8710 | { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_nothing, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) }, |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 8711 | { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth_grp, acl_match_auth, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8712 | { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 }, |
| 8713 | |
| 8714 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 }, |
| 8715 | |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 8716 | { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 8717 | { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8718 | { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8719 | { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8720 | { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8721 | { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8722 | { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8723 | { "path_sub", acl_parse_str, smp_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8724 | |
| 8725 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 }, |
| 8726 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 8727 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 8728 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 8729 | { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) }, |
| 8730 | { "scook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8731 | { "scook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 8732 | { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8733 | { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8734 | { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8735 | { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8736 | { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8737 | { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8738 | { "scook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8739 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8740 | { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr }, |
| 8741 | { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8742 | { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8743 | { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8744 | { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8745 | { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8746 | { "shdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr }, |
| 8747 | { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8748 | { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8749 | { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8750 | { "shdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8751 | |
| 8752 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 }, |
| 8753 | |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 8754 | { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 8755 | { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8756 | { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8757 | { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8758 | { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8759 | { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 8760 | { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8761 | { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8762 | { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8763 | { "url_sub", acl_parse_str, smp_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8764 | |
| 8765 | { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) }, |
| 8766 | { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8767 | { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8768 | { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8769 | { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8770 | { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) }, |
| 8771 | { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8772 | { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8773 | { "urlp_sub", acl_parse_str, smp_fetch_url_param, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 8774 | { "urlp_val", acl_parse_int, smp_fetch_url_param_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8775 | |
| 8776 | { NULL, NULL, NULL, NULL }, |
| 8777 | }}; |
| 8778 | |
| 8779 | /************************************************************************/ |
| 8780 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8781 | /************************************************************************/ |
| 8782 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 8783 | static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{ |
Willy Tarreau | 1b6c00c | 2012-10-05 22:41:26 +0200 | [diff] [blame] | 8784 | { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ }, |
| 8785 | { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ }, |
| 8786 | { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ }, |
| 8787 | { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ }, |
| 8788 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_CAP_L7|SMP_CAP_REQ }, |
| 8789 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ }, |
| 8790 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ }, |
| 8791 | { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ|SMP_CAP_RES }, |
| 8792 | { "set-cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_RES }, /* deprecated */ |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 8793 | { NULL, NULL, 0, 0, 0 }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8794 | }}; |
| 8795 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8796 | |
| 8797 | __attribute__((constructor)) |
| 8798 | static void __http_protocol_init(void) |
| 8799 | { |
| 8800 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 8801 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8802 | } |
| 8803 | |
| 8804 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8805 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8806 | * Local variables: |
| 8807 | * c-indent-level: 8 |
| 8808 | * c-basic-offset: 8 |
| 8809 | * End: |
| 8810 | */ |