Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4 | * Copyright 2000-2011 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <syslog.h> |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 20 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | b05405a | 2012-01-23 15:35:52 +0100 | [diff] [blame] | 26 | #include <netinet/tcp.h> |
| 27 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/appsession.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 29 | #include <common/base64.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 30 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 31 | #include <common/compat.h> |
| 32 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 33 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/memory.h> |
| 35 | #include <common/mini-clist.h> |
| 36 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 37 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 38 | #include <common/time.h> |
| 39 | #include <common/uri_auth.h> |
| 40 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
| 42 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 45 | #include <proto/acl.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 46 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 47 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 49 | #include <proto/channel.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 50 | #include <proto/checks.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 51 | #include <proto/compression.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 52 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 54 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 56 | #include <proto/hdr_idx.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 57 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 59 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 61 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 62 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 64 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | #include <proto/task.h> |
| 66 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 67 | const char HTTP_100[] = |
| 68 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 69 | |
| 70 | const struct chunk http_100_chunk = { |
| 71 | .str = (char *)&HTTP_100, |
| 72 | .len = sizeof(HTTP_100)-1 |
| 73 | }; |
| 74 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 75 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 76 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 77 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 78 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 79 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 80 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 81 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 82 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 83 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 84 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 85 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 86 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 87 | |
| 88 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 89 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 90 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 91 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 92 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 93 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 94 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 95 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 96 | const char *HTTP_401_fmt = |
| 97 | "HTTP/1.0 401 Unauthorized\r\n" |
| 98 | "Cache-Control: no-cache\r\n" |
| 99 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 100 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 101 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 102 | "\r\n" |
| 103 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 104 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 105 | const char *HTTP_407_fmt = |
| 106 | "HTTP/1.0 407 Unauthorized\r\n" |
| 107 | "Cache-Control: no-cache\r\n" |
| 108 | "Connection: close\r\n" |
| 109 | "Content-Type: text/html\r\n" |
| 110 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 111 | "\r\n" |
| 112 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 113 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 114 | |
| 115 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 116 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 117 | [HTTP_ERR_400] = 400, |
| 118 | [HTTP_ERR_403] = 403, |
| 119 | [HTTP_ERR_408] = 408, |
| 120 | [HTTP_ERR_500] = 500, |
| 121 | [HTTP_ERR_502] = 502, |
| 122 | [HTTP_ERR_503] = 503, |
| 123 | [HTTP_ERR_504] = 504, |
| 124 | }; |
| 125 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 126 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 127 | [HTTP_ERR_200] = |
| 128 | "HTTP/1.0 200 OK\r\n" |
| 129 | "Cache-Control: no-cache\r\n" |
| 130 | "Connection: close\r\n" |
| 131 | "Content-Type: text/html\r\n" |
| 132 | "\r\n" |
| 133 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 134 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 135 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 136 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 137 | "Cache-Control: no-cache\r\n" |
| 138 | "Connection: close\r\n" |
| 139 | "Content-Type: text/html\r\n" |
| 140 | "\r\n" |
| 141 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 142 | |
| 143 | [HTTP_ERR_403] = |
| 144 | "HTTP/1.0 403 Forbidden\r\n" |
| 145 | "Cache-Control: no-cache\r\n" |
| 146 | "Connection: close\r\n" |
| 147 | "Content-Type: text/html\r\n" |
| 148 | "\r\n" |
| 149 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 150 | |
| 151 | [HTTP_ERR_408] = |
| 152 | "HTTP/1.0 408 Request Time-out\r\n" |
| 153 | "Cache-Control: no-cache\r\n" |
| 154 | "Connection: close\r\n" |
| 155 | "Content-Type: text/html\r\n" |
| 156 | "\r\n" |
| 157 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 158 | |
| 159 | [HTTP_ERR_500] = |
| 160 | "HTTP/1.0 500 Server Error\r\n" |
| 161 | "Cache-Control: no-cache\r\n" |
| 162 | "Connection: close\r\n" |
| 163 | "Content-Type: text/html\r\n" |
| 164 | "\r\n" |
| 165 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 166 | |
| 167 | [HTTP_ERR_502] = |
| 168 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 169 | "Cache-Control: no-cache\r\n" |
| 170 | "Connection: close\r\n" |
| 171 | "Content-Type: text/html\r\n" |
| 172 | "\r\n" |
| 173 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 174 | |
| 175 | [HTTP_ERR_503] = |
| 176 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 177 | "Cache-Control: no-cache\r\n" |
| 178 | "Connection: close\r\n" |
| 179 | "Content-Type: text/html\r\n" |
| 180 | "\r\n" |
| 181 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 182 | |
| 183 | [HTTP_ERR_504] = |
| 184 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 185 | "Cache-Control: no-cache\r\n" |
| 186 | "Connection: close\r\n" |
| 187 | "Content-Type: text/html\r\n" |
| 188 | "\r\n" |
| 189 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 190 | |
| 191 | }; |
| 192 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 193 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 194 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 195 | [STAT_STATUS_DENY] = "DENY", |
| 196 | [STAT_STATUS_DONE] = "DONE", |
| 197 | [STAT_STATUS_ERRP] = "ERRP", |
| 198 | [STAT_STATUS_EXCD] = "EXCD", |
| 199 | [STAT_STATUS_NONE] = "NONE", |
| 200 | [STAT_STATUS_PART] = "PART", |
| 201 | [STAT_STATUS_UNKN] = "UNKN", |
| 202 | }; |
| 203 | |
| 204 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 205 | /* We must put the messages here since GCC cannot initialize consts depending |
| 206 | * on strlen(). |
| 207 | */ |
| 208 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 209 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 210 | #define FD_SETS_ARE_BITFIELDS |
| 211 | #ifdef FD_SETS_ARE_BITFIELDS |
| 212 | /* |
| 213 | * This map is used with all the FD_* macros to check whether a particular bit |
| 214 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 215 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 216 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 217 | * exclusively to the macros. |
| 218 | */ |
| 219 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 220 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 221 | |
| 222 | #else |
| 223 | #error "Check if your OS uses bitfields for fd_sets" |
| 224 | #endif |
| 225 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 226 | void init_proto_http() |
| 227 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 228 | int i; |
| 229 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 230 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 231 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 232 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 233 | if (!http_err_msgs[msg]) { |
| 234 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 235 | abort(); |
| 236 | } |
| 237 | |
| 238 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 239 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 240 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 241 | |
| 242 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 243 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 244 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 245 | * printable characters above. |
| 246 | */ |
| 247 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 248 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 249 | for (i = 0; i < 32; i++) { |
| 250 | FD_SET(i, hdr_encode_map); |
| 251 | FD_SET(i, url_encode_map); |
| 252 | } |
| 253 | for (i = 127; i < 256; i++) { |
| 254 | FD_SET(i, hdr_encode_map); |
| 255 | FD_SET(i, url_encode_map); |
| 256 | } |
| 257 | |
| 258 | tmp = "\"#{|}"; |
| 259 | while (*tmp) { |
| 260 | FD_SET(*tmp, hdr_encode_map); |
| 261 | tmp++; |
| 262 | } |
| 263 | |
| 264 | tmp = "\"#"; |
| 265 | while (*tmp) { |
| 266 | FD_SET(*tmp, url_encode_map); |
| 267 | tmp++; |
| 268 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 269 | |
| 270 | /* memory allocations */ |
| 271 | pool2_requri = create_pool("requri", REQURI_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 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 392 | chunk_printf(&trash, |
| 393 | "[%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", |
| 394 | line, |
| 395 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 396 | 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); |
| 397 | write(-1, trash.str, trash.len); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 398 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 399 | chunk_printf(&trash, |
| 400 | " %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", |
| 401 | line, |
| 402 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 403 | 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); |
| 404 | write(-1, trash.str, trash.len); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 405 | } |
| 406 | #else |
| 407 | #define http_silent_debug(l,s) do { } while (0) |
| 408 | #endif |
| 409 | |
| 410 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 411 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 412 | * 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] | 413 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 414 | * of headers is automatically adjusted. The number of bytes added is returned |
| 415 | * on success, otherwise <0 is returned indicating an error. |
| 416 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 417 | 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] | 418 | { |
| 419 | int bytes, len; |
| 420 | |
| 421 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 422 | 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] | 423 | if (!bytes) |
| 424 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 425 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 426 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 427 | } |
| 428 | |
| 429 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 430 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 431 | * 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] | 432 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 433 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 434 | * of headers is automatically adjusted. The number of bytes added is returned |
| 435 | * on success, otherwise <0 is returned indicating an error. |
| 436 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 437 | int http_header_add_tail2(struct http_msg *msg, |
| 438 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 439 | { |
| 440 | int bytes; |
| 441 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 442 | 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] | 443 | if (!bytes) |
| 444 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 445 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 446 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 447 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 448 | |
| 449 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 450 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 451 | * If so, returns the position of the first non-space character relative to |
| 452 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 453 | * to return a pointer to the place after the first space. Returns 0 if the |
| 454 | * header name does not match. Checks are case-insensitive. |
| 455 | */ |
| 456 | int http_header_match2(const char *hdr, const char *end, |
| 457 | const char *name, int len) |
| 458 | { |
| 459 | const char *val; |
| 460 | |
| 461 | if (hdr + len >= end) |
| 462 | return 0; |
| 463 | if (hdr[len] != ':') |
| 464 | return 0; |
| 465 | if (strncasecmp(hdr, name, len) != 0) |
| 466 | return 0; |
| 467 | val = hdr + len + 1; |
| 468 | while (val < end && HTTP_IS_SPHT(*val)) |
| 469 | val++; |
| 470 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 471 | return len + 2; /* we may replace starting from second space */ |
| 472 | return val - hdr; |
| 473 | } |
| 474 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 475 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 476 | * par 2.2 for more information. Note that it requires a valid header to return |
| 477 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 478 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 479 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 480 | { |
| 481 | int quoted, qdpair; |
| 482 | |
| 483 | quoted = qdpair = 0; |
| 484 | for (; s < e; s++) { |
| 485 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 486 | else if (quoted) { |
| 487 | if (*s == '\\') qdpair = 1; |
| 488 | else if (*s == '"') quoted = 0; |
| 489 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 490 | else if (*s == '"') quoted = 1; |
| 491 | else if (*s == ',') return s; |
| 492 | } |
| 493 | return s; |
| 494 | } |
| 495 | |
| 496 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 497 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 498 | * structure holds everything necessary to use the header and find next |
| 499 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 500 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 501 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 502 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 503 | * is NULL when searching for a new values of a header, the current header is |
| 504 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 505 | */ |
| 506 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 507 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 508 | struct hdr_ctx *ctx) |
| 509 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 510 | char *eol, *sov; |
| 511 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 512 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 513 | cur_idx = ctx->idx; |
| 514 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 515 | /* We have previously returned a value, let's search |
| 516 | * another one on the same line. |
| 517 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 518 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 519 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 520 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 521 | eol = sol + idx->v[cur_idx].len; |
| 522 | |
| 523 | if (sov >= eol) |
| 524 | /* no more values in this header */ |
| 525 | goto next_hdr; |
| 526 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 527 | /* values remaining for this header, skip the comma but save it |
| 528 | * for later use (eg: for header deletion). |
| 529 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 530 | sov++; |
| 531 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 532 | sov++; |
| 533 | |
| 534 | goto return_hdr; |
| 535 | } |
| 536 | |
| 537 | /* first request for this header */ |
| 538 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 539 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 540 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 541 | while (cur_idx) { |
| 542 | eol = sol + idx->v[cur_idx].len; |
| 543 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 544 | if (len == 0) { |
| 545 | /* No argument was passed, we want any header. |
| 546 | * To achieve this, we simply build a fake request. */ |
| 547 | while (sol + len < eol && sol[len] != ':') |
| 548 | len++; |
| 549 | name = sol; |
| 550 | } |
| 551 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 552 | if ((len < eol - sol) && |
| 553 | (sol[len] == ':') && |
| 554 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 555 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 556 | sov = sol + len + 1; |
| 557 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 558 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 559 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 560 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 561 | ctx->prev = old_idx; |
| 562 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 563 | ctx->idx = cur_idx; |
| 564 | ctx->val = sov - sol; |
| 565 | |
| 566 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 567 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 568 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 569 | eol--; |
| 570 | ctx->tws++; |
| 571 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 572 | ctx->vlen = eol - sov; |
| 573 | return 1; |
| 574 | } |
| 575 | next_hdr: |
| 576 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 577 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 578 | cur_idx = idx->v[cur_idx].next; |
| 579 | } |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 584 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 585 | struct hdr_ctx *ctx) |
| 586 | { |
| 587 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 588 | } |
| 589 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 590 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 591 | * the http_find_header functions. The value is removed, as well as surrounding |
| 592 | * 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] | 593 | * 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] | 594 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 595 | * no more header, so any processing may stop. The ctx is always left in a form |
| 596 | * that can be handled by http_find_header2() to find next occurrence. |
| 597 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 598 | 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] | 599 | { |
| 600 | int cur_idx = ctx->idx; |
| 601 | char *sol = ctx->line; |
| 602 | struct hdr_idx_elem *hdr; |
| 603 | int delta, skip_comma; |
| 604 | |
| 605 | if (!cur_idx) |
| 606 | return 0; |
| 607 | |
| 608 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 609 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 610 | /* This was the only value of the header, we must now remove it entirely. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 611 | 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] | 612 | http_msg_move_end(msg, delta); |
| 613 | idx->used--; |
| 614 | hdr->len = 0; /* unused entry */ |
| 615 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 616 | if (idx->tail == ctx->idx) |
| 617 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 618 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 619 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 620 | 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] | 621 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 622 | return ctx->idx; |
| 623 | } |
| 624 | |
| 625 | /* 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] | 626 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 627 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 628 | */ |
| 629 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 630 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 631 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 632 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 633 | NULL, 0); |
| 634 | hdr->len += delta; |
| 635 | http_msg_move_end(msg, delta); |
| 636 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 637 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 638 | return ctx->idx; |
| 639 | } |
| 640 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 641 | /* This function handles a server error at the stream interface level. The |
| 642 | * stream interface is assumed to be already in a closed state. An optional |
| 643 | * message is copied into the input buffer, and an HTTP status code stored. |
| 644 | * 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] | 645 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 646 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 647 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 648 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 649 | { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 650 | channel_auto_read(si->ob); |
| 651 | channel_abort(si->ob); |
| 652 | channel_auto_close(si->ob); |
| 653 | channel_erase(si->ob); |
| 654 | channel_auto_close(si->ib); |
| 655 | channel_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 656 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 657 | t->txn.status = status; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 658 | bo_inject(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 659 | } |
| 660 | if (!(t->flags & SN_ERR_MASK)) |
| 661 | t->flags |= err; |
| 662 | if (!(t->flags & SN_FINST_MASK)) |
| 663 | t->flags |= finst; |
| 664 | } |
| 665 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 666 | /* This function returns the appropriate error location for the given session |
| 667 | * and message. |
| 668 | */ |
| 669 | |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 670 | struct chunk *http_error_message(struct session *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 671 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 672 | if (s->be->errmsg[msgnum].str) |
| 673 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 674 | else if (s->fe->errmsg[msgnum].str) |
| 675 | return &s->fe->errmsg[msgnum]; |
| 676 | else |
| 677 | return &http_err_chunks[msgnum]; |
| 678 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 679 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 680 | /* |
| 681 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 682 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 683 | */ |
| 684 | static http_meth_t find_http_meth(const char *str, const int len) |
| 685 | { |
| 686 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 687 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 688 | |
| 689 | m = ((unsigned)*str - 'A'); |
| 690 | |
| 691 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 692 | for (h = http_methods[m]; h->len > 0; h++) { |
| 693 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 694 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 695 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 696 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 697 | }; |
| 698 | return HTTP_METH_OTHER; |
| 699 | } |
| 700 | return HTTP_METH_NONE; |
| 701 | |
| 702 | } |
| 703 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 704 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 705 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 706 | * It is returned otherwise. |
| 707 | */ |
| 708 | static char * |
| 709 | http_get_path(struct http_txn *txn) |
| 710 | { |
| 711 | char *ptr, *end; |
| 712 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 713 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 714 | end = ptr + txn->req.sl.rq.u_l; |
| 715 | |
| 716 | if (ptr >= end) |
| 717 | return NULL; |
| 718 | |
| 719 | /* RFC2616, par. 5.1.2 : |
| 720 | * Request-URI = "*" | absuri | abspath | authority |
| 721 | */ |
| 722 | |
| 723 | if (*ptr == '*') |
| 724 | return NULL; |
| 725 | |
| 726 | if (isalpha((unsigned char)*ptr)) { |
| 727 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 728 | ptr++; |
| 729 | while (ptr < end && |
| 730 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 731 | ptr++; |
| 732 | /* skip '://' */ |
| 733 | if (ptr == end || *ptr++ != ':') |
| 734 | return NULL; |
| 735 | if (ptr == end || *ptr++ != '/') |
| 736 | return NULL; |
| 737 | if (ptr == end || *ptr++ != '/') |
| 738 | return NULL; |
| 739 | } |
| 740 | /* skip [user[:passwd]@]host[:[port]] */ |
| 741 | |
| 742 | while (ptr < end && *ptr != '/') |
| 743 | ptr++; |
| 744 | |
| 745 | if (ptr == end) |
| 746 | return NULL; |
| 747 | |
| 748 | /* OK, we got the '/' ! */ |
| 749 | return ptr; |
| 750 | } |
| 751 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 752 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 753 | * in redirect mode. This may only be called just after the stream interface |
| 754 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 755 | * follow normal proxy processing. NOTE: this function is designed to support |
| 756 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 757 | */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 758 | void http_perform_server_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 759 | { |
| 760 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 761 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 762 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 763 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 764 | |
| 765 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 766 | trash.len = strlen(HTTP_302); |
| 767 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 768 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 769 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 770 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 771 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 772 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 773 | return; |
| 774 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 775 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 776 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 777 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 778 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 779 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 780 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 781 | /* 3: add the request URI. Since it was already forwarded, we need |
| 782 | * to temporarily rewind the buffer. |
| 783 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 784 | txn = &s->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 785 | b_rew(s->req->buf, rewind = s->req->buf->o); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 786 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 787 | path = http_get_path(txn); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 788 | 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] | 789 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 790 | b_adv(s->req->buf, rewind); |
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 | if (!path) |
| 793 | return; |
| 794 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 795 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 796 | return; |
| 797 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 798 | memcpy(trash.str + trash.len, path, len); |
| 799 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 800 | |
| 801 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 802 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 803 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 804 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 805 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 806 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 807 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 808 | |
| 809 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 810 | si_shutr(si); |
| 811 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 812 | si->err_type = SI_ET_NONE; |
| 813 | si->err_loc = NULL; |
| 814 | si->state = SI_ST_CLO; |
| 815 | |
| 816 | /* send the message */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 817 | http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 818 | |
| 819 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
Willy Tarreau | 4521ba6 | 2013-01-24 01:25:25 +0100 | [diff] [blame] | 820 | srv_inc_sess_ctr(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 821 | } |
| 822 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 823 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 824 | * that the server side is closed. Note that err_type is actually a |
| 825 | * bitmask, where almost only aborts may be cumulated with other |
| 826 | * values. We consider that aborted operations are more important |
| 827 | * than timeouts or errors due to the fact that nobody else in the |
| 828 | * logs might explain incomplete retries. All others should avoid |
| 829 | * being cumulated. It should normally not be possible to have multiple |
| 830 | * aborts at once, but just in case, the first one in sequence is reported. |
| 831 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 832 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 833 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 834 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 835 | |
| 836 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 837 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 838 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 839 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 840 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 841 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 842 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 843 | http_server_error(s, si, SN_ERR_SRVTO, 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_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 846 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
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_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 849 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
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_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 852 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
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 /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 855 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 856 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 857 | } |
| 858 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 859 | extern const char sess_term_cond[8]; |
| 860 | extern const char sess_fin_state[8]; |
| 861 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 862 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 863 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 864 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 865 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 866 | /* |
| 867 | * Capture headers from message starting at <som> according to header list |
| 868 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 869 | */ |
| 870 | void capture_headers(char *som, struct hdr_idx *idx, |
| 871 | char **cap, struct cap_hdr *cap_hdr) |
| 872 | { |
| 873 | char *eol, *sol, *col, *sov; |
| 874 | int cur_idx; |
| 875 | struct cap_hdr *h; |
| 876 | int len; |
| 877 | |
| 878 | sol = som + hdr_idx_first_pos(idx); |
| 879 | cur_idx = hdr_idx_first_idx(idx); |
| 880 | |
| 881 | while (cur_idx) { |
| 882 | eol = sol + idx->v[cur_idx].len; |
| 883 | |
| 884 | col = sol; |
| 885 | while (col < eol && *col != ':') |
| 886 | col++; |
| 887 | |
| 888 | sov = col + 1; |
| 889 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 890 | sov++; |
| 891 | |
| 892 | for (h = cap_hdr; h; h = h->next) { |
| 893 | if ((h->namelen == col - sol) && |
| 894 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 895 | if (cap[h->index] == NULL) |
| 896 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 897 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 898 | |
| 899 | if (cap[h->index] == NULL) { |
| 900 | Alert("HTTP capture : out of memory.\n"); |
| 901 | continue; |
| 902 | } |
| 903 | |
| 904 | len = eol - sov; |
| 905 | if (len > h->len) |
| 906 | len = h->len; |
| 907 | |
| 908 | memcpy(cap[h->index], sov, len); |
| 909 | cap[h->index][len]=0; |
| 910 | } |
| 911 | } |
| 912 | sol = eol + idx->v[cur_idx].cr + 1; |
| 913 | cur_idx = idx->v[cur_idx].next; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 918 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 919 | */ |
| 920 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 921 | |
| 922 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 923 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 924 | */ |
| 925 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 926 | ptr++; \ |
| 927 | if (likely(ptr < end)) \ |
| 928 | goto good; \ |
| 929 | else { \ |
| 930 | state = (st); \ |
| 931 | goto http_msg_ood; \ |
| 932 | } \ |
| 933 | } while (0) |
| 934 | |
| 935 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 936 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 937 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 938 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 939 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 940 | * will give undefined results. |
| 941 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 942 | * and that msg->sol points to the beginning of the response. |
| 943 | * If a complete line is found (which implies that at least one CR or LF is |
| 944 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 945 | * returned indicating an incomplete line (which does not mean that parts have |
| 946 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 947 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 948 | * upon next call. |
| 949 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 950 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 951 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 952 | * 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] | 953 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 954 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 955 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 956 | unsigned int state, const char *ptr, const char *end, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 957 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 958 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 959 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 960 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 961 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 962 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 963 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 964 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 965 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 966 | |
| 967 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 968 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 969 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 970 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 971 | state = HTTP_MSG_ERROR; |
| 972 | break; |
| 973 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 974 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 975 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 976 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 977 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 978 | goto http_msg_rpcode; |
| 979 | } |
| 980 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 981 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 982 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 983 | state = HTTP_MSG_ERROR; |
| 984 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 985 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 986 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 987 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 988 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 989 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 990 | |
| 991 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 992 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 993 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 994 | } |
| 995 | |
| 996 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 997 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 998 | http_msg_rsp_reason: |
| 999 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1000 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1001 | msg->sl.st.r_l = 0; |
| 1002 | goto http_msg_rpline_eol; |
| 1003 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1004 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1005 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1006 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1007 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1008 | goto http_msg_rpreason; |
| 1009 | } |
| 1010 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1011 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1012 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1013 | goto http_msg_rsp_reason; |
| 1014 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1015 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1016 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1017 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1018 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1019 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1020 | http_msg_rpline_eol: |
| 1021 | /* We have seen the end of line. Note that we do not |
| 1022 | * necessarily have the \n yet, but at least we know that we |
| 1023 | * have EITHER \r OR \n, otherwise the response would not be |
| 1024 | * complete. We can then record the response length and return |
| 1025 | * to the caller which will be able to register it. |
| 1026 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1027 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1028 | return ptr; |
| 1029 | |
| 1030 | #ifdef DEBUG_FULL |
| 1031 | default: |
| 1032 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1033 | exit(1); |
| 1034 | #endif |
| 1035 | } |
| 1036 | |
| 1037 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1038 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1039 | if (ret_state) |
| 1040 | *ret_state = state; |
| 1041 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1042 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1043 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1044 | } |
| 1045 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1046 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1047 | * This function parses a request line between <ptr> and <end>, starting with |
| 1048 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1049 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1050 | * will give undefined results. |
| 1051 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1052 | * and that msg->sol points to the beginning of the request. |
| 1053 | * If a complete line is found (which implies that at least one CR or LF is |
| 1054 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1055 | * returned indicating an incomplete line (which does not mean that parts have |
| 1056 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1057 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1058 | * upon next call. |
| 1059 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1060 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1061 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1062 | * 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] | 1063 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1064 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1065 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1066 | unsigned int state, const char *ptr, const char *end, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1067 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1068 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1069 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1070 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1071 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1072 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1073 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1074 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1075 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1076 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1077 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1078 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1079 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1080 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1081 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1082 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1083 | /* HTTP 0.9 request */ |
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 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1086 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1087 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1088 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1089 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1090 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1091 | msg->sl.rq.v_l = 0; |
| 1092 | goto http_msg_rqline_eol; |
| 1093 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1094 | state = HTTP_MSG_ERROR; |
| 1095 | break; |
| 1096 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1097 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1098 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1099 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1100 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1101 | goto http_msg_rquri; |
| 1102 | } |
| 1103 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1104 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1105 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1106 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1107 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1108 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1109 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1110 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1111 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1112 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1113 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1114 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1115 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1116 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1117 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1118 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1119 | /* non-ASCII chars are forbidden unless option |
| 1120 | * accept-invalid-http-request is enabled in the frontend. |
| 1121 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1122 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1123 | if (msg->err_pos < -1) |
| 1124 | goto invalid_char; |
| 1125 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1126 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1127 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1128 | } |
| 1129 | |
| 1130 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1131 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1132 | goto http_msg_req09_uri_e; |
| 1133 | } |
| 1134 | |
| 1135 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1136 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1137 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1138 | state = HTTP_MSG_ERROR; |
| 1139 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1140 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1141 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1142 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1143 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1144 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1145 | goto http_msg_rqver; |
| 1146 | } |
| 1147 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1148 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1149 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1150 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1151 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1152 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1153 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1154 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1155 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1156 | |
| 1157 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1158 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1159 | http_msg_rqline_eol: |
| 1160 | /* We have seen the end of line. Note that we do not |
| 1161 | * necessarily have the \n yet, but at least we know that we |
| 1162 | * have EITHER \r OR \n, otherwise the request would not be |
| 1163 | * complete. We can then record the request length and return |
| 1164 | * to the caller which will be able to register it. |
| 1165 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1166 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1167 | return ptr; |
| 1168 | } |
| 1169 | |
| 1170 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1171 | state = HTTP_MSG_ERROR; |
| 1172 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1173 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1174 | #ifdef DEBUG_FULL |
| 1175 | default: |
| 1176 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1177 | exit(1); |
| 1178 | #endif |
| 1179 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1180 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1181 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1182 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1183 | if (ret_state) |
| 1184 | *ret_state = state; |
| 1185 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1186 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1187 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1188 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1189 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1190 | /* |
| 1191 | * Returns the data from Authorization header. Function may be called more |
| 1192 | * than once so data is stored in txn->auth_data. When no header is found |
| 1193 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1194 | * searching again for something we are unable to find anyway. |
| 1195 | */ |
| 1196 | |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1197 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1198 | |
| 1199 | int |
| 1200 | get_http_auth(struct session *s) |
| 1201 | { |
| 1202 | |
| 1203 | struct http_txn *txn = &s->txn; |
| 1204 | struct chunk auth_method; |
| 1205 | struct hdr_ctx ctx; |
| 1206 | char *h, *p; |
| 1207 | int len; |
| 1208 | |
| 1209 | #ifdef DEBUG_AUTH |
| 1210 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1211 | #endif |
| 1212 | |
| 1213 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1214 | return 0; |
| 1215 | |
| 1216 | if (txn->auth.method) |
| 1217 | return 1; |
| 1218 | |
| 1219 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1220 | |
| 1221 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1222 | |
| 1223 | if (txn->flags & TX_USE_PX_CONN) { |
| 1224 | h = "Proxy-Authorization"; |
| 1225 | len = strlen(h); |
| 1226 | } else { |
| 1227 | h = "Authorization"; |
| 1228 | len = strlen(h); |
| 1229 | } |
| 1230 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1231 | 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] | 1232 | return 0; |
| 1233 | |
| 1234 | h = ctx.line + ctx.val; |
| 1235 | |
| 1236 | p = memchr(h, ' ', ctx.vlen); |
| 1237 | if (!p || p == h) |
| 1238 | return 0; |
| 1239 | |
| 1240 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1241 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1242 | |
| 1243 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1244 | |
| 1245 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1246 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1247 | |
| 1248 | if (len < 0) |
| 1249 | return 0; |
| 1250 | |
| 1251 | |
| 1252 | get_http_auth_buff[len] = '\0'; |
| 1253 | |
| 1254 | p = strchr(get_http_auth_buff, ':'); |
| 1255 | |
| 1256 | if (!p) |
| 1257 | return 0; |
| 1258 | |
| 1259 | txn->auth.user = get_http_auth_buff; |
| 1260 | *p = '\0'; |
| 1261 | txn->auth.pass = p+1; |
| 1262 | |
| 1263 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1264 | return 1; |
| 1265 | } |
| 1266 | |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1270 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1271 | /* |
| 1272 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1273 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1274 | * ensuring that the message does not wrap. The function can be preempted |
| 1275 | * everywhere when data are missing and recalled at the exact same location |
| 1276 | * with no information loss. The message may even be realigned between two |
| 1277 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1278 | * 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] | 1279 | * fields. Note that msg->sol will be initialized after completing the first |
| 1280 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1281 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1282 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1283 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1284 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1285 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1286 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1287 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1288 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1289 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1290 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1291 | ptr = buf->p + msg->next; |
| 1292 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1293 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1294 | if (unlikely(ptr >= end)) |
| 1295 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1296 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1297 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1298 | /* |
| 1299 | * First, states that are specific to the response only. |
| 1300 | * We check them first so that request and headers are |
| 1301 | * closer to each other (accessed more often). |
| 1302 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1303 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1304 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1305 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1306 | /* we have a start of message, but we have to check |
| 1307 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1308 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1309 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1310 | if (unlikely(ptr != buf->p)) { |
| 1311 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1312 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1313 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1314 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1315 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1316 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1317 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1318 | hdr_idx_init(idx); |
| 1319 | state = HTTP_MSG_RPVER; |
| 1320 | goto http_msg_rpver; |
| 1321 | } |
| 1322 | |
| 1323 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1324 | goto http_msg_invalid; |
| 1325 | |
| 1326 | if (unlikely(*ptr == '\n')) |
| 1327 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1328 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1329 | /* stop here */ |
| 1330 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1331 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1332 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1333 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1334 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1335 | /* stop here */ |
| 1336 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1337 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1338 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1339 | case HTTP_MSG_RPVER_SP: |
| 1340 | case HTTP_MSG_RPCODE: |
| 1341 | case HTTP_MSG_RPCODE_SP: |
| 1342 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1343 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1344 | state, ptr, end, |
| 1345 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1346 | if (unlikely(!ptr)) |
| 1347 | return; |
| 1348 | |
| 1349 | /* we have a full response and we know that we have either a CR |
| 1350 | * or an LF at <ptr>. |
| 1351 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1352 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1353 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1354 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1355 | if (likely(*ptr == '\r')) |
| 1356 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1357 | goto http_msg_rpline_end; |
| 1358 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1359 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1360 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1361 | /* msg->sol must point to the first of CR or LF. */ |
| 1362 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1363 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1364 | /* stop here */ |
| 1365 | |
| 1366 | /* |
| 1367 | * Second, states that are specific to the request only |
| 1368 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1369 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1370 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1371 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1372 | /* we have a start of message, but we have to check |
| 1373 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1374 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1375 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1376 | if (likely(ptr != buf->p)) { |
| 1377 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1378 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1379 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1380 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1381 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1382 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1383 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1384 | state = HTTP_MSG_RQMETH; |
| 1385 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1386 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1387 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1388 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1389 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1390 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1391 | if (unlikely(*ptr == '\n')) |
| 1392 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1393 | 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] | 1394 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1395 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1396 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1397 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1398 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1399 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
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_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1403 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1404 | case HTTP_MSG_RQMETH_SP: |
| 1405 | case HTTP_MSG_RQURI: |
| 1406 | case HTTP_MSG_RQURI_SP: |
| 1407 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1408 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1409 | state, ptr, end, |
| 1410 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1411 | if (unlikely(!ptr)) |
| 1412 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1413 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1414 | /* we have a full request and we know that we have either a CR |
| 1415 | * or an LF at <ptr>. |
| 1416 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1417 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1418 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1419 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1420 | if (likely(*ptr == '\r')) |
| 1421 | 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] | 1422 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1423 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1424 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1425 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1426 | /* check for HTTP/0.9 request : no version information available. |
| 1427 | * msg->sol must point to the first of CR or LF. |
| 1428 | */ |
| 1429 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1430 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1431 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1432 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1433 | 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] | 1434 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1435 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1436 | /* |
| 1437 | * Common states below |
| 1438 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1439 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1440 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1441 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1442 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1443 | goto http_msg_hdr_name; |
| 1444 | } |
| 1445 | |
| 1446 | if (likely(*ptr == '\r')) |
| 1447 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1448 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1449 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1450 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1451 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1452 | /* assumes msg->sol points to the first char */ |
| 1453 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1454 | 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] | 1455 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1456 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1457 | 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] | 1458 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1459 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1460 | goto http_msg_invalid; |
| 1461 | |
| 1462 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1463 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1464 | |
| 1465 | /* and we still accept this non-token character */ |
| 1466 | 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] | 1467 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1468 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1469 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1470 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1471 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1472 | 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] | 1473 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1474 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1475 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1476 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1477 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1478 | goto http_msg_hdr_val; |
| 1479 | } |
| 1480 | |
| 1481 | if (likely(*ptr == '\r')) |
| 1482 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1483 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1484 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1485 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1486 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1487 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1488 | 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] | 1489 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1490 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1491 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1492 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1493 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1494 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1495 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1496 | goto http_msg_hdr_l1_sp; |
| 1497 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1498 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1499 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1500 | goto http_msg_complete_header; |
| 1501 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1502 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1503 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1504 | /* assumes msg->sol points to the first char, and msg->sov |
| 1505 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1506 | */ |
| 1507 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1508 | 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] | 1509 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1510 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1511 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1512 | * real header end in case it ends with lots of LWS, but is this |
| 1513 | * really needed ? |
| 1514 | */ |
| 1515 | if (likely(*ptr == '\r')) |
| 1516 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1517 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1518 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1519 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1520 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1521 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1522 | 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] | 1523 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1524 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1525 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1526 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1527 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1528 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1529 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1530 | goto http_msg_hdr_val; |
| 1531 | } |
| 1532 | http_msg_complete_header: |
| 1533 | /* |
| 1534 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1535 | * Assumes msg->sol points to the first char, msg->sov points |
| 1536 | * to the first character of the value and msg->eol to the |
| 1537 | * first CR or LF so we know how the line ends. We insert last |
| 1538 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1539 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1540 | 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] | 1541 | idx, idx->tail) < 0)) |
| 1542 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1543 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1544 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1545 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1546 | goto http_msg_hdr_name; |
| 1547 | } |
| 1548 | |
| 1549 | if (likely(*ptr == '\r')) |
| 1550 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1551 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1552 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1553 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1554 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1555 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1556 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1557 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1558 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1559 | msg->eoh = msg->sol; |
| 1560 | msg->sol = 0; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1561 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1562 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1563 | |
| 1564 | case HTTP_MSG_ERROR: |
| 1565 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1566 | break; |
| 1567 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1568 | #ifdef DEBUG_FULL |
| 1569 | default: |
| 1570 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1571 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1572 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1573 | } |
| 1574 | http_msg_ood: |
| 1575 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1576 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1577 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1578 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1579 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1580 | http_msg_invalid: |
| 1581 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1582 | msg->msg_state = HTTP_MSG_ERROR; |
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; |
| 1585 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1586 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1587 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1588 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1589 | * nothing is done and 1 is returned. |
| 1590 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1591 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1592 | { |
| 1593 | int delta; |
| 1594 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1595 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1596 | |
| 1597 | if (msg->sl.rq.v_l != 0) |
| 1598 | return 1; |
| 1599 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1600 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1601 | delta = 0; |
| 1602 | |
| 1603 | if (msg->sl.rq.u_l == 0) { |
| 1604 | /* if no URI was set, add "/" */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1605 | delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1606 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1607 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1608 | } |
| 1609 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1610 | 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] | 1611 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1612 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1613 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1614 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1615 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1616 | NULL, NULL); |
| 1617 | if (unlikely(!cur_end)) |
| 1618 | return 0; |
| 1619 | |
| 1620 | /* we have a full HTTP/1.0 request now and we know that |
| 1621 | * we have either a CR or an LF at <ptr>. |
| 1622 | */ |
| 1623 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1624 | return 1; |
| 1625 | } |
| 1626 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1627 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1628 | * and "keep-alive" values. If we already know that some headers may safely |
| 1629 | * 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] | 1630 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1631 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1632 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1633 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1634 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1635 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1636 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1637 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1638 | 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] | 1639 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1640 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1641 | const char *hdr_val = "Connection"; |
| 1642 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1643 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1644 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1645 | return; |
| 1646 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1647 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1648 | hdr_val = "Proxy-Connection"; |
| 1649 | hdr_len = 16; |
| 1650 | } |
| 1651 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1652 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1653 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1654 | 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] | 1655 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1656 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1657 | if (to_del & 2) |
| 1658 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1659 | else |
| 1660 | txn->flags |= TX_CON_KAL_SET; |
| 1661 | } |
| 1662 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1663 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1664 | if (to_del & 1) |
| 1665 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1666 | else |
| 1667 | txn->flags |= TX_CON_CLO_SET; |
| 1668 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1669 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1670 | txn->flags |= TX_HDR_CONN_UPG; |
| 1671 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1672 | } |
| 1673 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1674 | txn->flags |= TX_HDR_CONN_PRS; |
| 1675 | return; |
| 1676 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1677 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1678 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1679 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1680 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1681 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1682 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1683 | 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] | 1684 | { |
| 1685 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1686 | const char *hdr_val = "Connection"; |
| 1687 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1688 | |
| 1689 | ctx.idx = 0; |
| 1690 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1691 | |
| 1692 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1693 | hdr_val = "Proxy-Connection"; |
| 1694 | hdr_len = 16; |
| 1695 | } |
| 1696 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1697 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1698 | 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] | 1699 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1700 | if (wanted & TX_CON_KAL_SET) |
| 1701 | txn->flags |= TX_CON_KAL_SET; |
| 1702 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1703 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1704 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1705 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1706 | if (wanted & TX_CON_CLO_SET) |
| 1707 | txn->flags |= TX_CON_CLO_SET; |
| 1708 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1709 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1710 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1711 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1712 | |
| 1713 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1714 | return; |
| 1715 | |
| 1716 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1717 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1718 | hdr_val = "Connection: close"; |
| 1719 | hdr_len = 17; |
| 1720 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1721 | hdr_val = "Proxy-Connection: close"; |
| 1722 | hdr_len = 23; |
| 1723 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1724 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1728 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1729 | hdr_val = "Connection: keep-alive"; |
| 1730 | hdr_len = 22; |
| 1731 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1732 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1733 | hdr_len = 28; |
| 1734 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1735 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1736 | } |
| 1737 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1738 | } |
| 1739 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1740 | /* 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] | 1741 | * 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] | 1742 | * so that we know we can forward between ->sol and ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1743 | * 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] | 1744 | * 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] | 1745 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1746 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1747 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1748 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1749 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1750 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1751 | const char *end = buf->data + buf->size; |
| 1752 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1753 | unsigned int chunk = 0; |
| 1754 | |
| 1755 | /* The chunk size is in the following form, though we are only |
| 1756 | * interested in the size and CRLF : |
| 1757 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1758 | */ |
| 1759 | while (1) { |
| 1760 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1761 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1762 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1763 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1764 | if (c < 0) /* not a hex digit anymore */ |
| 1765 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1766 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1767 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1768 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1769 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1770 | chunk = (chunk << 4) + c; |
| 1771 | } |
| 1772 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1773 | /* empty size not allowed */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1774 | if (ptr == ptr_old) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1775 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1776 | |
| 1777 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1778 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1779 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1780 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1781 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1782 | } |
| 1783 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1784 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1785 | * for the end of chunk size. |
| 1786 | */ |
| 1787 | while (1) { |
| 1788 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1789 | /* we now have a CR or an LF at ptr */ |
| 1790 | if (likely(*ptr == '\r')) { |
| 1791 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1792 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1793 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1794 | return 0; |
| 1795 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1796 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1797 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1798 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1799 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1800 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1801 | /* done */ |
| 1802 | break; |
| 1803 | } |
| 1804 | else if (*ptr == ';') { |
| 1805 | /* chunk extension, ends at next CRLF */ |
| 1806 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1807 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1808 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1809 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1810 | |
| 1811 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1812 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1813 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1814 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1815 | return 0; |
| 1816 | } |
| 1817 | /* we have a CRLF now, loop above */ |
| 1818 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1819 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1820 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1821 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1822 | } |
| 1823 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1824 | /* 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] | 1825 | * 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] | 1826 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1827 | */ |
Willy Tarreau | b8ffd37 | 2012-09-27 15:08:56 +0200 | [diff] [blame] | 1828 | if (ptr < ptr_old) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1829 | msg->sov += buf->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1830 | msg->sov += ptr - ptr_old; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1831 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1832 | msg->chunk_len = chunk; |
| 1833 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1834 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1835 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1836 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1837 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1838 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1839 | } |
| 1840 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1841 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1842 | * 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] | 1843 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 1844 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 1845 | * If not enough data are available, the function does not change anything |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1846 | * 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] | 1847 | * 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] | 1848 | * 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] | 1849 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 1850 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1851 | * forwarding, and that the difference between msg->sol and msg->sov exactly |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1852 | * matches the length of trailers already parsed and not forwarded. It is also |
| 1853 | * important to note that this function is designed to be able to parse wrapped |
| 1854 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1855 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1856 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1857 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1858 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1859 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1860 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1861 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1862 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1863 | const char *ptr = b_ptr(buf, msg->next); |
| 1864 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1865 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1866 | |
| 1867 | /* scan current line and stop at LF or CRLF */ |
| 1868 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1869 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1870 | return 0; |
| 1871 | |
| 1872 | if (*ptr == '\n') { |
| 1873 | if (!p1) |
| 1874 | p1 = ptr; |
| 1875 | p2 = ptr; |
| 1876 | break; |
| 1877 | } |
| 1878 | |
| 1879 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1880 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1881 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1882 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1883 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1884 | p1 = ptr; |
| 1885 | } |
| 1886 | |
| 1887 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1888 | if (ptr >= buf->data + buf->size) |
| 1889 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | /* after LF; point to beginning of next line */ |
| 1893 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1894 | if (p2 >= buf->data + buf->size) |
| 1895 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1896 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1897 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1898 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1899 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1900 | |
| 1901 | /* schedule this line for forwarding */ |
| 1902 | msg->sov += bytes; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1903 | if (msg->sov >= buf->size) |
| 1904 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1905 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1906 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1907 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1908 | * Everything was scheduled for forwarding, there's nothing |
| 1909 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 1910 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1911 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1912 | msg->msg_state = HTTP_MSG_DONE; |
| 1913 | return 1; |
| 1914 | } |
| 1915 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1916 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1917 | } |
| 1918 | } |
| 1919 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 1920 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1921 | * 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] | 1922 | * ->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] | 1923 | * 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] | 1924 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 1925 | * not enough data are available, the function does not change anything and |
| 1926 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 1927 | * does not change anything. Note: this function is designed to parse wrapped |
| 1928 | * CRLF at the end of the buffer. |
| 1929 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1930 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1931 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1932 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1933 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1934 | int bytes; |
| 1935 | |
| 1936 | /* NB: we'll check data availabilty at the end. It's not a |
| 1937 | * problem because whatever we match first will be checked |
| 1938 | * against the correct length. |
| 1939 | */ |
| 1940 | bytes = 1; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1941 | ptr = buf->p; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1942 | if (*ptr == '\r') { |
| 1943 | bytes++; |
| 1944 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1945 | if (ptr >= buf->data + buf->size) |
| 1946 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1947 | } |
| 1948 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1949 | if (bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1950 | return 0; |
| 1951 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1952 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1953 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1954 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1955 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1956 | |
| 1957 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1958 | if (ptr >= buf->data + buf->size) |
| 1959 | ptr = buf->data; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1960 | /* prepare the CRLF to be forwarded (between ->sol and ->sov) */ |
| 1961 | msg->sol = 0; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1962 | msg->sov = msg->next = bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1963 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 1964 | return 1; |
| 1965 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1966 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1967 | |
| 1968 | /* |
| 1969 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 1970 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1971 | int select_compression_request_header(struct session *s, struct buffer *req) |
| 1972 | { |
| 1973 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 1974 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1975 | struct hdr_ctx ctx; |
| 1976 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 1977 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1978 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 1979 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 1980 | * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later). |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 1981 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 1982 | */ |
| 1983 | ctx.idx = 0; |
| 1984 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 1985 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 1986 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 1987 | (ctx.vlen < 31 || |
| 1988 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 1989 | ctx.line[ctx.val + 30] < '6' || |
| 1990 | (ctx.line[ctx.val + 30] == '6' && |
| 1991 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 1992 | s->comp_algo = NULL; |
| 1993 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 1994 | } |
| 1995 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1996 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 1997 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1998 | ctx.idx = 0; |
| 1999 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2000 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2001 | if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) { |
| 2002 | s->comp_algo = comp_algo; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2003 | |
| 2004 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2005 | |
| 2006 | if ((s->be->comp && s->be->comp->offload) || |
| 2007 | (s->fe->comp && s->fe->comp->offload)) { |
| 2008 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2009 | ctx.idx = 0; |
| 2010 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2011 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2012 | } |
| 2013 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2014 | return 1; |
| 2015 | } |
| 2016 | } |
| 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | /* identity is implicit does not require headers */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2021 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
| 2022 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2023 | if (comp_algo->add_data == identity_add_data) { |
| 2024 | s->comp_algo = comp_algo; |
| 2025 | return 1; |
| 2026 | } |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2031 | return 0; |
| 2032 | } |
| 2033 | |
| 2034 | /* |
| 2035 | * Selects a comression algorithm depending of the server response. |
| 2036 | */ |
| 2037 | int select_compression_response_header(struct session *s, struct buffer *res) |
| 2038 | { |
| 2039 | struct http_txn *txn = &s->txn; |
| 2040 | struct http_msg *msg = &txn->rsp; |
| 2041 | struct hdr_ctx ctx; |
| 2042 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2043 | |
| 2044 | /* no common compression algorithm was found in request header */ |
| 2045 | if (s->comp_algo == NULL) |
| 2046 | goto fail; |
| 2047 | |
| 2048 | /* HTTP < 1.1 should not be compressed */ |
| 2049 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2050 | goto fail; |
| 2051 | |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2052 | /* 200 only */ |
| 2053 | if (txn->status != 200) |
| 2054 | goto fail; |
| 2055 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2056 | /* Content-Length is null */ |
| 2057 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2058 | goto fail; |
| 2059 | |
| 2060 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2061 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2062 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2063 | goto fail; |
| 2064 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2065 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2066 | ctx.idx = 0; |
| 2067 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2068 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2069 | goto fail; |
| 2070 | } |
| 2071 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2072 | comp_type = NULL; |
| 2073 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2074 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2075 | * not listed in the "compression type" directive if any. If no content-type was |
| 2076 | * found but configuration requires one, we don't compress either. Backend has |
| 2077 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2078 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2079 | ctx.idx = 0; |
| 2080 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2081 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2082 | goto fail; |
| 2083 | |
| 2084 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 2085 | (s->fe->comp && (comp_type = s->fe->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2086 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2087 | if (ctx.vlen >= comp_type->name_len && |
| 2088 | strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2089 | /* this Content-Type should be compressed */ |
| 2090 | break; |
| 2091 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2092 | /* this Content-Type should not be compressed */ |
| 2093 | if (comp_type == NULL) |
| 2094 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2095 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2096 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2097 | else { /* no content-type header */ |
| 2098 | if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types)) |
| 2099 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2100 | } |
| 2101 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2102 | /* limit compression rate */ |
| 2103 | if (global.comp_rate_lim > 0) |
| 2104 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2105 | goto fail; |
| 2106 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2107 | /* limit cpu usage */ |
| 2108 | if (idle_pct < compress_min_idle) |
| 2109 | goto fail; |
| 2110 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2111 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2112 | if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0) |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2113 | goto fail; |
| 2114 | |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2115 | s->flags |= SN_COMP_READY; |
| 2116 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2117 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2118 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2119 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2120 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2121 | |
| 2122 | /* add Transfer-Encoding header */ |
| 2123 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2124 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2125 | |
| 2126 | /* |
| 2127 | * Add Content-Encoding header when it's not identity encoding. |
| 2128 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2129 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2130 | * header. |
| 2131 | */ |
| 2132 | if (s->comp_algo->add_data != identity_add_data) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2133 | trash.len = 18; |
| 2134 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
| 2135 | memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len); |
| 2136 | trash.len += s->comp_algo->name_len; |
| 2137 | trash.str[trash.len] = '\0'; |
| 2138 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2139 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2140 | return 1; |
| 2141 | |
| 2142 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2143 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2144 | return 0; |
| 2145 | } |
| 2146 | |
| 2147 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2148 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2149 | * processing can continue on next analysers, or zero if it either needs more |
| 2150 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2151 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2152 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2153 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2154 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2155 | 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] | 2156 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2157 | /* |
| 2158 | * We will parse the partial (or complete) lines. |
| 2159 | * We will check the request syntax, and also join multi-line |
| 2160 | * headers. An index of all the lines will be elaborated while |
| 2161 | * parsing. |
| 2162 | * |
| 2163 | * For the parsing, we use a 28 states FSM. |
| 2164 | * |
| 2165 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2166 | * req->buf->p = beginning of request |
| 2167 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2168 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2169 | * msg->eol = end of current header or line (LF or CRLF) |
| 2170 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2171 | * |
| 2172 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2173 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2174 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2175 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2176 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2177 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2178 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2179 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2180 | struct http_txn *txn = &s->txn; |
| 2181 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2182 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2183 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2184 | 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] | 2185 | now_ms, __FUNCTION__, |
| 2186 | s, |
| 2187 | req, |
| 2188 | req->rex, req->wex, |
| 2189 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2190 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2191 | req->analysers); |
| 2192 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2193 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2194 | s->srv_error = http_return_srv_error; |
| 2195 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2196 | /* There's a protected area at the end of the buffer for rewriting |
| 2197 | * purposes. We don't want to start to parse the request if the |
| 2198 | * protected area is affected, because we may have to move processed |
| 2199 | * data later, which is much more complicated. |
| 2200 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2201 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2202 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 2203 | unlikely(channel_full(req) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2204 | bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2205 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) { |
| 2206 | if (req->buf->o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2207 | 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] | 2208 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2209 | /* 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] | 2210 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2211 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2212 | return 0; |
| 2213 | } |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2214 | if (bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2215 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite) |
| 2216 | buffer_slow_realign(msg->chn->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2217 | } |
| 2218 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2219 | /* Note that we have the same problem with the response ; we |
| 2220 | * may want to send a redirect, error or anything which requires |
| 2221 | * some spare space. So we'll ensure that we have at least |
| 2222 | * maxrewrite bytes available in the response buffer before |
| 2223 | * processing that one. This will only affect pipelined |
| 2224 | * keep-alive requests. |
| 2225 | */ |
| 2226 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 2227 | unlikely(channel_full(s->rep) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2228 | bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) || |
| 2229 | bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) { |
| 2230 | if (s->rep->buf->o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2231 | 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] | 2232 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2233 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2234 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2235 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2236 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2237 | return 0; |
| 2238 | } |
| 2239 | } |
| 2240 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2241 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2242 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2243 | } |
| 2244 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2245 | /* 1: we might have to print this header in debug mode */ |
| 2246 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2247 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2248 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2249 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2250 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2251 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2252 | /* this is a bit complex : in case of error on the request line, |
| 2253 | * we know that rq.l is still zero, so we display only the part |
| 2254 | * up to the end of the line (truncated by debug_hdr). |
| 2255 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2256 | 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] | 2257 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2258 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2259 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2260 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2261 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2262 | while (cur_idx) { |
| 2263 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2264 | debug_hdr("clihdr", s, sol, eol); |
| 2265 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2266 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2267 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2268 | } |
| 2269 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2270 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2271 | /* |
| 2272 | * Now we quickly check if we have found a full valid request. |
| 2273 | * If not so, we check the FD and buffer states before leaving. |
| 2274 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2275 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2276 | * requests are checked first. When waiting for a second request |
| 2277 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2278 | * we note the error in the session flags but don't set any state. |
| 2279 | * Since the error will be noted there, it will not be counted by |
| 2280 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2281 | * Last, we may increase some tracked counters' http request errors on |
| 2282 | * the cases that are deliberately the client's fault. For instance, |
| 2283 | * a timeout or connection reset is not counted as an error. However |
| 2284 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2285 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2286 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2287 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2288 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2289 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2290 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2291 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2292 | session_inc_http_req_ctr(s); |
| 2293 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2294 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2295 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2296 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2297 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2298 | /* 1: Since we are in header mode, if there's no space |
| 2299 | * left for headers, we won't be able to free more |
| 2300 | * later, so the session will never terminate. We |
| 2301 | * must terminate it now. |
| 2302 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2303 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2304 | /* FIXME: check if URI is set and return Status |
| 2305 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2306 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2307 | session_inc_http_req_ctr(s); |
| 2308 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2309 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2310 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2311 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2312 | goto return_bad_req; |
| 2313 | } |
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 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2316 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2317 | if (!(s->flags & SN_ERR_MASK)) |
| 2318 | s->flags |= SN_ERR_CLICL; |
| 2319 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2320 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2321 | goto failed_keep_alive; |
| 2322 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2323 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2324 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2325 | 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] | 2326 | session_inc_http_err_ctr(s); |
| 2327 | } |
| 2328 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2329 | txn->status = 400; |
| 2330 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2331 | msg->msg_state = HTTP_MSG_ERROR; |
| 2332 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2333 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2334 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2335 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2336 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2337 | if (s->listener->counters) |
| 2338 | s->listener->counters->failed_req++; |
| 2339 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2340 | if (!(s->flags & SN_FINST_MASK)) |
| 2341 | s->flags |= SN_FINST_R; |
| 2342 | return 0; |
| 2343 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2344 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2345 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2346 | 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] | 2347 | if (!(s->flags & SN_ERR_MASK)) |
| 2348 | s->flags |= SN_ERR_CLITO; |
| 2349 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2350 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2351 | goto failed_keep_alive; |
| 2352 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2353 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2354 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2355 | 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] | 2356 | session_inc_http_err_ctr(s); |
| 2357 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2358 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2359 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2360 | msg->msg_state = HTTP_MSG_ERROR; |
| 2361 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2362 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2363 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2364 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2365 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2366 | if (s->listener->counters) |
| 2367 | s->listener->counters->failed_req++; |
| 2368 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2369 | if (!(s->flags & SN_FINST_MASK)) |
| 2370 | s->flags |= SN_FINST_R; |
| 2371 | return 0; |
| 2372 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2373 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2374 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2375 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2376 | if (!(s->flags & SN_ERR_MASK)) |
| 2377 | s->flags |= SN_ERR_CLICL; |
| 2378 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2379 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2380 | goto failed_keep_alive; |
| 2381 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2382 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2383 | 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] | 2384 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2385 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2386 | msg->msg_state = HTTP_MSG_ERROR; |
| 2387 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2388 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2389 | session_inc_http_err_ctr(s); |
| 2390 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2391 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2392 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2393 | if (s->listener->counters) |
| 2394 | s->listener->counters->failed_req++; |
| 2395 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2396 | if (!(s->flags & SN_FINST_MASK)) |
| 2397 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2398 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2399 | } |
| 2400 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2401 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2402 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2403 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2404 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2405 | if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2406 | /* We need more data, we have to re-enable quick-ack in case we |
| 2407 | * previously disabled it, otherwise we might cause the client |
| 2408 | * to delay next data. |
| 2409 | */ |
Willy Tarreau | 7f7ad91 | 2012-11-11 19:27:15 +0100 | [diff] [blame] | 2410 | setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2411 | } |
| 2412 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2413 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2414 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2415 | /* If the client starts to talk, let's fall back to |
| 2416 | * request timeout processing. |
| 2417 | */ |
| 2418 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2419 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2420 | } |
| 2421 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2422 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2423 | if (!tick_isset(req->analyse_exp)) { |
| 2424 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2425 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2426 | tick_isset(s->be->timeout.httpka)) |
| 2427 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2428 | else |
| 2429 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2430 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2431 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2432 | /* we're not ready yet */ |
| 2433 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2434 | |
| 2435 | failed_keep_alive: |
| 2436 | /* Here we process low-level errors for keep-alive requests. In |
| 2437 | * short, if the request is not the first one and it experiences |
| 2438 | * a timeout, read error or shutdown, we just silently close so |
| 2439 | * that the client can try again. |
| 2440 | */ |
| 2441 | txn->status = 0; |
| 2442 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2443 | req->analysers = 0; |
| 2444 | s->logs.logwait = 0; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2445 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2446 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2447 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2448 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2449 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2450 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2451 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2452 | * 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] | 2453 | * 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] | 2454 | * 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] | 2455 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2456 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2457 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2458 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2459 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2460 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2461 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2462 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2463 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2464 | /* kill the pending keep-alive timeout */ |
| 2465 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2466 | req->analyse_exp = TICK_ETERNITY; |
| 2467 | } |
| 2468 | |
| 2469 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2470 | /* Maybe we found in invalid header name while we were configured not |
| 2471 | * to block on that, so we have to capture it now. |
| 2472 | */ |
| 2473 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2474 | 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] | 2475 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2476 | /* |
| 2477 | * 1: identify the method |
| 2478 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2479 | 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] | 2480 | |
| 2481 | /* we can make use of server redirect on GET and HEAD */ |
| 2482 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2483 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2484 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2485 | /* |
| 2486 | * 2: check if the URI matches the monitor_uri. |
| 2487 | * We have to do this for every request which gets in, because |
| 2488 | * the monitor-uri is defined by the frontend. |
| 2489 | */ |
| 2490 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2491 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2492 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2493 | s->fe->monitor_uri, |
| 2494 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2495 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2496 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2497 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2498 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2499 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2500 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2501 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2502 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2503 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2504 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2505 | 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] | 2506 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2507 | ret = acl_pass(ret); |
| 2508 | if (cond->pol == ACL_COND_UNLESS) |
| 2509 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2510 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2511 | if (ret) { |
| 2512 | /* we fail this request, let's return 503 service unavail */ |
| 2513 | txn->status = 503; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2514 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2515 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2516 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2517 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2518 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2519 | /* nothing to fail, let's reply normaly */ |
| 2520 | txn->status = 200; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2521 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2522 | goto return_prx_cond; |
| 2523 | } |
| 2524 | |
| 2525 | /* |
| 2526 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2527 | * Note: we cannot log anymore if the request has been |
| 2528 | * classified as invalid. |
| 2529 | */ |
| 2530 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2531 | /* we have a complete HTTP request that we must log */ |
| 2532 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2533 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2534 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2535 | if (urilen >= REQURI_LEN) |
| 2536 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2537 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2538 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2539 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2540 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2541 | s->do_log(s); |
| 2542 | } else { |
| 2543 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2544 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2545 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2546 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2547 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) |
| 2548 | s->unique_id = pool_alloc2(pool2_uniqueid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2549 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2550 | /* 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] | 2551 | 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] | 2552 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2553 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2554 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2555 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2556 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2557 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2558 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2559 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2560 | |
| 2561 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2562 | txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2563 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2564 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2565 | * we have what looks like a proxied connection instead of a connection, |
| 2566 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2567 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2568 | * happen to do that despite being non-standard :-( |
| 2569 | * We consider that a request not beginning with either '/' or '*' is |
| 2570 | * a proxied connection, which covers both "scheme://location" and |
| 2571 | * CONNECT ip:port. |
| 2572 | */ |
| 2573 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2574 | 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] | 2575 | txn->flags |= TX_USE_PX_CONN; |
| 2576 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2577 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2578 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2579 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2580 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2581 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2582 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2583 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2584 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2585 | /* 6: determine the transfer-length. |
| 2586 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2587 | * the presence of a message-body in a REQUEST and its transfer length |
| 2588 | * must be determined that way (in order of precedence) : |
| 2589 | * 1. The presence of a message-body in a request is signaled by the |
| 2590 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2591 | * in the request's header fields. When a request message contains |
| 2592 | * both a message-body of non-zero length and a method that does |
| 2593 | * not define any semantics for that request message-body, then an |
| 2594 | * origin server SHOULD either ignore the message-body or respond |
| 2595 | * with an appropriate error message (e.g., 413). A proxy or |
| 2596 | * gateway, when presented the same request, SHOULD either forward |
| 2597 | * the request inbound with the message- body or ignore the |
| 2598 | * message-body when determining a response. |
| 2599 | * |
| 2600 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2601 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2602 | * transfer-length is defined by the use of this transfer-coding. |
| 2603 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2604 | * transfer-coding is not present, the transfer-length is defined |
| 2605 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2606 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2607 | * 3. If a Content-Length header field is present, its decimal value in |
| 2608 | * OCTETs represents both the entity-length and the transfer-length. |
| 2609 | * If a message is received with both a Transfer-Encoding header |
| 2610 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2611 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2612 | * 4. By the server closing the connection. (Closing the connection |
| 2613 | * cannot be used to indicate the end of a request body, since that |
| 2614 | * would leave no possibility for the server to send back a response.) |
| 2615 | * |
| 2616 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2617 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2618 | * it is terminated by closing the connection. When the "chunked" |
| 2619 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2620 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2621 | */ |
| 2622 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2623 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2624 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2625 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2626 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2627 | 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] | 2628 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2629 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2630 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2631 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2632 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2633 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2634 | break; |
| 2635 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2636 | } |
| 2637 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2638 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2639 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2640 | 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] | 2641 | signed long long cl; |
| 2642 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2643 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2644 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2645 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2646 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2647 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2648 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2649 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2650 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2651 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2652 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2653 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2654 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2655 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2656 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2657 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2658 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2659 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2660 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2661 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2662 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2663 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2664 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2665 | } |
| 2666 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2667 | /* bodyless requests have a known length */ |
| 2668 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2669 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2670 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2671 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2672 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2673 | req->analyse_exp = TICK_ETERNITY; |
| 2674 | return 1; |
| 2675 | |
| 2676 | return_bad_req: |
| 2677 | /* We centralize bad requests processing here */ |
| 2678 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2679 | /* we detected a parsing error. We want to archive this request |
| 2680 | * in the dedicated proxy area for later troubleshooting. |
| 2681 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2682 | 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] | 2683 | } |
| 2684 | |
| 2685 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2686 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2687 | 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] | 2688 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2689 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2690 | if (s->listener->counters) |
| 2691 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2692 | |
| 2693 | return_prx_cond: |
| 2694 | if (!(s->flags & SN_ERR_MASK)) |
| 2695 | s->flags |= SN_ERR_PRXCOND; |
| 2696 | if (!(s->flags & SN_FINST_MASK)) |
| 2697 | s->flags |= SN_FINST_R; |
| 2698 | |
| 2699 | req->analysers = 0; |
| 2700 | req->analyse_exp = TICK_ETERNITY; |
| 2701 | return 0; |
| 2702 | } |
| 2703 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2704 | /* We reached the stats page through a POST request. |
| 2705 | * Parse the posted data and enable/disable servers if necessary. |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2706 | * 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] | 2707 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2708 | 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] | 2709 | { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2710 | struct proxy *px = NULL; |
| 2711 | struct server *sv = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2712 | |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2713 | char key[LINESIZE]; |
| 2714 | int action = ST_ADM_ACTION_NONE; |
| 2715 | int reprocess = 0; |
| 2716 | |
| 2717 | int total_servers = 0; |
| 2718 | int altered_servers = 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2719 | |
| 2720 | char *first_param, *cur_param, *next_param, *end_params; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 2721 | char *st_cur_param = NULL; |
| 2722 | char *st_next_param = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2723 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2724 | first_param = req->buf->p + txn->req.eoh + 2; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2725 | end_params = first_param + txn->req.body_len; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2726 | |
| 2727 | cur_param = next_param = end_params; |
| 2728 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2729 | if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2730 | /* Prevent buffer overflow */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2731 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2732 | return 1; |
| 2733 | } |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2734 | else if (end_params > req->buf->p + req->buf->i) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2735 | /* we need more data */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2736 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2737 | return 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2738 | } |
| 2739 | |
| 2740 | *end_params = '\0'; |
| 2741 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2742 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2743 | |
| 2744 | /* |
| 2745 | * Parse the parameters in reverse order to only store the last value. |
| 2746 | * From the html form, the backend and the action are at the end. |
| 2747 | */ |
| 2748 | while (cur_param > first_param) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2749 | char *value; |
| 2750 | int poffset, plen; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2751 | |
| 2752 | cur_param--; |
| 2753 | if ((*cur_param == '&') || (cur_param == first_param)) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2754 | reprocess_servers: |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2755 | /* Parse the key */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2756 | poffset = (cur_param != first_param ? 1 : 0); |
| 2757 | plen = next_param - cur_param + (cur_param == first_param ? 1 : 0); |
| 2758 | if ((plen > 0) && (plen <= sizeof(key))) { |
| 2759 | strncpy(key, cur_param + poffset, plen); |
| 2760 | key[plen - 1] = '\0'; |
| 2761 | } else { |
| 2762 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
| 2763 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | /* Parse the value */ |
| 2767 | value = key; |
| 2768 | while (*value != '\0' && *value != '=') { |
| 2769 | value++; |
| 2770 | } |
| 2771 | if (*value == '=') { |
| 2772 | /* Ok, a value is found, we can mark the end of the key */ |
| 2773 | *value++ = '\0'; |
| 2774 | } |
| 2775 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2776 | if (!url_decode(key) || !url_decode(value)) |
| 2777 | break; |
| 2778 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2779 | /* Now we can check the key to see what to do */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2780 | if (!px && (strcmp(key, "b") == 0)) { |
| 2781 | if ((px = findproxy(value, PR_CAP_BE)) == NULL) { |
| 2782 | /* the backend name is unknown or ambiguous (duplicate names) */ |
| 2783 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2784 | goto out; |
| 2785 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2786 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2787 | else if (!action && (strcmp(key, "action") == 0)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2788 | if (strcmp(value, "disable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2789 | action = ST_ADM_ACTION_DISABLE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2790 | } |
| 2791 | else if (strcmp(value, "enable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2792 | action = ST_ADM_ACTION_ENABLE; |
| 2793 | } |
Willy Tarreau | d728224 | 2012-06-04 00:22:44 +0200 | [diff] [blame] | 2794 | else if (strcmp(value, "stop") == 0) { |
| 2795 | action = ST_ADM_ACTION_STOP; |
| 2796 | } |
| 2797 | else if (strcmp(value, "start") == 0) { |
| 2798 | action = ST_ADM_ACTION_START; |
| 2799 | } |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2800 | else if (strcmp(value, "shutdown") == 0) { |
| 2801 | action = ST_ADM_ACTION_SHUTDOWN; |
| 2802 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2803 | else { |
| 2804 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2805 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2806 | } |
| 2807 | } |
| 2808 | else if (strcmp(key, "s") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2809 | if (!(px && action)) { |
| 2810 | /* |
| 2811 | * Indicates that we'll need to reprocess the parameters |
| 2812 | * as soon as backend and action are known |
| 2813 | */ |
| 2814 | if (!reprocess) { |
| 2815 | st_cur_param = cur_param; |
| 2816 | st_next_param = next_param; |
| 2817 | } |
| 2818 | reprocess = 1; |
| 2819 | } |
| 2820 | else if ((sv = findserver(px, value)) != NULL) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2821 | switch (action) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2822 | case ST_ADM_ACTION_DISABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2823 | if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2824 | /* Not already in maintenance, we can change the server state */ |
| 2825 | sv->state |= SRV_MAINTAIN; |
| 2826 | set_server_down(sv); |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2827 | altered_servers++; |
| 2828 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2829 | } |
| 2830 | break; |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2831 | case ST_ADM_ACTION_ENABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2832 | if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2833 | /* Already in maintenance, we can change the server state */ |
| 2834 | set_server_up(sv); |
Willy Tarreau | 7046130 | 2010-10-22 14:39:02 +0200 | [diff] [blame] | 2835 | sv->health = sv->rise; /* up, but will fall down at first failure */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2836 | altered_servers++; |
| 2837 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2838 | } |
Willy Tarreau | d728224 | 2012-06-04 00:22:44 +0200 | [diff] [blame] | 2839 | break; |
| 2840 | case ST_ADM_ACTION_STOP: |
| 2841 | case ST_ADM_ACTION_START: |
| 2842 | if (action == ST_ADM_ACTION_START) |
| 2843 | sv->uweight = sv->iweight; |
| 2844 | else |
| 2845 | sv->uweight = 0; |
| 2846 | |
| 2847 | if (px->lbprm.algo & BE_LB_PROP_DYN) { |
| 2848 | /* we must take care of not pushing the server to full throttle during slow starts */ |
| 2849 | if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN)) |
| 2850 | sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart; |
| 2851 | else |
| 2852 | sv->eweight = BE_WEIGHT_SCALE; |
| 2853 | sv->eweight *= sv->uweight; |
| 2854 | } else { |
| 2855 | sv->eweight = sv->uweight; |
| 2856 | } |
| 2857 | |
| 2858 | /* static LB algorithms are a bit harder to update */ |
| 2859 | if (px->lbprm.update_server_eweight) |
| 2860 | px->lbprm.update_server_eweight(sv); |
| 2861 | else if (sv->eweight) { |
| 2862 | if (px->lbprm.set_server_status_up) |
| 2863 | px->lbprm.set_server_status_up(sv); |
| 2864 | } |
| 2865 | else { |
| 2866 | if (px->lbprm.set_server_status_down) |
| 2867 | px->lbprm.set_server_status_down(sv); |
| 2868 | } |
| 2869 | altered_servers++; |
| 2870 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2871 | break; |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2872 | case ST_ADM_ACTION_SHUTDOWN: |
| 2873 | if (px->state != PR_STSTOPPED) { |
| 2874 | struct session *sess, *sess_bck; |
| 2875 | |
| 2876 | list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv) |
| 2877 | if (sess->srv_conn == sv) |
| 2878 | session_shutdown(sess, SN_ERR_KILLED); |
| 2879 | |
| 2880 | altered_servers++; |
| 2881 | total_servers++; |
| 2882 | } |
| 2883 | break; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2884 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2885 | } else { |
| 2886 | /* the server name is unknown or ambiguous (duplicate names) */ |
| 2887 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2888 | } |
| 2889 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2890 | if (reprocess && px && action) { |
| 2891 | /* Now, we know the backend and the action chosen by the user. |
| 2892 | * We can safely restart from the first server parameter |
| 2893 | * to reprocess them |
| 2894 | */ |
| 2895 | cur_param = st_cur_param; |
| 2896 | next_param = st_next_param; |
| 2897 | reprocess = 0; |
| 2898 | goto reprocess_servers; |
| 2899 | } |
| 2900 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2901 | next_param = cur_param; |
| 2902 | } |
| 2903 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2904 | |
| 2905 | if (total_servers == 0) { |
| 2906 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
| 2907 | } |
| 2908 | else if (altered_servers == 0) { |
| 2909 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2910 | } |
| 2911 | else if (altered_servers == total_servers) { |
| 2912 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
| 2913 | } |
| 2914 | else { |
| 2915 | si->applet.ctx.stats.st_code = STAT_STATUS_PART; |
| 2916 | } |
| 2917 | out: |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2918 | return 1; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2919 | } |
| 2920 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2921 | /* This function checks whether we need to enable a POST analyser to parse a |
| 2922 | * stats request, and also registers the stats I/O handler. It returns zero |
Willy Tarreau | cbc743e | 2012-12-28 08:36:50 +0100 | [diff] [blame] | 2923 | * if it needs to come back again, otherwise non-zero if it finishes. In the |
| 2924 | * latter case, it also clears the request analysers. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2925 | */ |
| 2926 | int http_handle_stats(struct session *s, struct channel *req) |
| 2927 | { |
| 2928 | struct stats_admin_rule *stats_admin_rule; |
| 2929 | struct stream_interface *si = s->rep->prod; |
| 2930 | struct http_txn *txn = &s->txn; |
| 2931 | struct http_msg *msg = &txn->req; |
| 2932 | struct uri_auth *uri = s->be->uri_auth; |
| 2933 | |
| 2934 | /* now check whether we have some admin rules for this request */ |
| 2935 | list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) { |
| 2936 | int ret = 1; |
| 2937 | |
| 2938 | if (stats_admin_rule->cond) { |
| 2939 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 2940 | ret = acl_pass(ret); |
| 2941 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 2942 | ret = !ret; |
| 2943 | } |
| 2944 | |
| 2945 | if (ret) { |
| 2946 | /* no rule, or the rule matches */ |
| 2947 | s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN; |
| 2948 | break; |
| 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | /* Was the status page requested with a POST ? */ |
| 2953 | if (unlikely(txn->meth == HTTP_METH_POST)) { |
| 2954 | if (si->applet.ctx.stats.flags & STAT_ADMIN) { |
| 2955 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 2956 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 2957 | * send an HTTP/1.1 100 Continue intermediate response. |
| 2958 | */ |
| 2959 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 2960 | struct hdr_ctx ctx; |
| 2961 | ctx.idx = 0; |
| 2962 | /* Expect is allowed in 1.1, look for it */ |
| 2963 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 2964 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 2965 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 2966 | } |
| 2967 | } |
| 2968 | msg->msg_state = HTTP_MSG_100_SENT; |
| 2969 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 2970 | } |
| 2971 | if (!http_process_req_stat_post(si, txn, req)) |
| 2972 | return 0; /* we need more data */ |
| 2973 | } |
| 2974 | else |
| 2975 | si->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
| 2976 | |
| 2977 | /* We don't want to land on the posted stats page because a refresh will |
| 2978 | * repost the data. We don't want this to happen on accident so we redirect |
| 2979 | * the browse to the stats page with a GET. |
| 2980 | */ |
| 2981 | chunk_printf(&trash, |
Yves Lafon | 4e8ec50 | 2013-03-11 11:06:05 -0400 | [diff] [blame^] | 2982 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2983 | "Cache-Control: no-cache\r\n" |
| 2984 | "Content-Type: text/plain\r\n" |
| 2985 | "Connection: close\r\n" |
| 2986 | "Location: %s;st=%s\r\n" |
| 2987 | "\r\n", |
| 2988 | uri->uri_prefix, |
| 2989 | ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) && |
| 2990 | (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) && |
| 2991 | stat_status_codes[si->applet.ctx.stats.st_code]) ? |
| 2992 | stat_status_codes[si->applet.ctx.stats.st_code] : |
| 2993 | stat_status_codes[STAT_STATUS_UNKN]); |
| 2994 | |
| 2995 | s->txn.status = 303; |
| 2996 | s->logs.tv_request = now; |
| 2997 | stream_int_retnclose(req->prod, &trash); |
| 2998 | s->target = &http_stats_applet.obj_type; /* just for logging the applet name */ |
| 2999 | |
| 3000 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3001 | s->fe->fe_counters.intercepted_req++; |
| 3002 | |
| 3003 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 3004 | s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy |
| 3005 | if (!(s->flags & SN_FINST_MASK)) |
| 3006 | s->flags |= SN_FINST_R; |
Willy Tarreau | cbc743e | 2012-12-28 08:36:50 +0100 | [diff] [blame] | 3007 | req->analysers = 0; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3008 | return 1; |
| 3009 | } |
| 3010 | |
| 3011 | /* OK, let's go on now */ |
| 3012 | |
| 3013 | chunk_printf(&trash, |
| 3014 | "HTTP/1.0 200 OK\r\n" |
| 3015 | "Cache-Control: no-cache\r\n" |
| 3016 | "Connection: close\r\n" |
| 3017 | "Content-Type: %s\r\n", |
Willy Tarreau | 354898b | 2012-12-23 18:15:23 +0100 | [diff] [blame] | 3018 | (si->applet.ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain"); |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3019 | |
| 3020 | if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH)) |
| 3021 | chunk_appendf(&trash, "Refresh: %d\r\n", |
| 3022 | uri->refresh); |
| 3023 | |
| 3024 | chunk_appendf(&trash, "\r\n"); |
| 3025 | |
| 3026 | s->txn.status = 200; |
| 3027 | s->logs.tv_request = now; |
| 3028 | |
| 3029 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3030 | s->fe->fe_counters.intercepted_req++; |
| 3031 | |
| 3032 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 3033 | s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy |
| 3034 | if (!(s->flags & SN_FINST_MASK)) |
| 3035 | s->flags |= SN_FINST_R; |
| 3036 | |
| 3037 | if (s->txn.meth == HTTP_METH_HEAD) { |
| 3038 | /* that's all we return in case of HEAD request, so let's immediately close. */ |
| 3039 | stream_int_retnclose(req->prod, &trash); |
| 3040 | s->target = &http_stats_applet.obj_type; /* just for logging the applet name */ |
Willy Tarreau | cbc743e | 2012-12-28 08:36:50 +0100 | [diff] [blame] | 3041 | req->analysers = 0; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3042 | return 1; |
| 3043 | } |
| 3044 | |
| 3045 | /* OK, push the response and hand over to the stats I/O handler */ |
| 3046 | bi_putchk(s->rep, &trash); |
| 3047 | |
| 3048 | s->task->nice = -32; /* small boost for HTTP statistics */ |
| 3049 | stream_int_register_handler(s->rep->prod, &http_stats_applet); |
| 3050 | s->target = s->rep->prod->conn->target; // for logging only |
| 3051 | s->rep->prod->conn->xprt_ctx = s; |
| 3052 | s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0; |
| 3053 | req->analysers = 0; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3054 | return 1; |
| 3055 | } |
| 3056 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3057 | /* Executes the http-request rules <rules> for session <s>, proxy <px> and |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3058 | * transaction <txn>. Returns the first rule that prevents further processing |
| 3059 | * of the request (auth, deny, ...) or NULL if it executed all rules or stopped |
| 3060 | * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny |
| 3061 | * rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3062 | */ |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3063 | static struct http_req_rule * |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3064 | http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3065 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3066 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3067 | struct hdr_ctx ctx; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3068 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3069 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3070 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3071 | continue; |
| 3072 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3073 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3074 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3075 | int ret; |
| 3076 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3077 | 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] | 3078 | ret = acl_pass(ret); |
| 3079 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3080 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3081 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3082 | |
| 3083 | if (!ret) /* condition not matched */ |
| 3084 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3085 | } |
| 3086 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3087 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3088 | switch (rule->action) { |
| 3089 | case HTTP_REQ_ACT_ALLOW: |
| 3090 | return NULL; /* "allow" rules are OK */ |
| 3091 | |
| 3092 | case HTTP_REQ_ACT_DENY: |
| 3093 | txn->flags |= TX_CLDENY; |
| 3094 | return rule; |
| 3095 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3096 | case HTTP_REQ_ACT_TARPIT: |
| 3097 | txn->flags |= TX_CLTARPIT; |
| 3098 | return rule; |
| 3099 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3100 | case HTTP_REQ_ACT_AUTH: |
| 3101 | return rule; |
| 3102 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3103 | case HTTP_REQ_ACT_REDIR: |
| 3104 | return rule; |
| 3105 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3106 | case HTTP_REQ_ACT_SET_HDR: |
| 3107 | ctx.idx = 0; |
| 3108 | /* remove all occurrences of the header */ |
| 3109 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3110 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3111 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3112 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3113 | /* now fall through to header addition */ |
| 3114 | |
| 3115 | case HTTP_REQ_ACT_ADD_HDR: |
| 3116 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3117 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3118 | trash.len = rule->arg.hdr_add.name_len; |
| 3119 | trash.str[trash.len++] = ':'; |
| 3120 | trash.str[trash.len++] = ' '; |
| 3121 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
| 3122 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3123 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3124 | } |
| 3125 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3126 | |
| 3127 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 418c1a0 | 2012-12-25 20:52:58 +0100 | [diff] [blame] | 3128 | return NULL; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3129 | } |
| 3130 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3131 | |
| 3132 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3133 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3134 | * as too large a request to build a valid response. |
| 3135 | */ |
| 3136 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn) |
| 3137 | { |
| 3138 | struct http_msg *msg = &txn->req; |
| 3139 | const char *msg_fmt; |
| 3140 | |
| 3141 | /* build redirect message */ |
| 3142 | switch(rule->code) { |
| 3143 | case 303: |
| 3144 | msg_fmt = HTTP_303; |
| 3145 | break; |
| 3146 | case 301: |
| 3147 | msg_fmt = HTTP_301; |
| 3148 | break; |
| 3149 | case 302: |
| 3150 | default: |
| 3151 | msg_fmt = HTTP_302; |
| 3152 | break; |
| 3153 | } |
| 3154 | |
| 3155 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3156 | return 0; |
| 3157 | |
| 3158 | switch(rule->type) { |
| 3159 | case REDIRECT_TYPE_SCHEME: { |
| 3160 | const char *path; |
| 3161 | const char *host; |
| 3162 | struct hdr_ctx ctx; |
| 3163 | int pathlen; |
| 3164 | int hostlen; |
| 3165 | |
| 3166 | host = ""; |
| 3167 | hostlen = 0; |
| 3168 | ctx.idx = 0; |
| 3169 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 3170 | host = ctx.line + ctx.val; |
| 3171 | hostlen = ctx.vlen; |
| 3172 | } |
| 3173 | |
| 3174 | path = http_get_path(txn); |
| 3175 | /* build message using path */ |
| 3176 | if (path) { |
| 3177 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3178 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3179 | int qs = 0; |
| 3180 | while (qs < pathlen) { |
| 3181 | if (path[qs] == '?') { |
| 3182 | pathlen = qs; |
| 3183 | break; |
| 3184 | } |
| 3185 | qs++; |
| 3186 | } |
| 3187 | } |
| 3188 | } else { |
| 3189 | path = "/"; |
| 3190 | pathlen = 1; |
| 3191 | } |
| 3192 | |
| 3193 | /* check if we can add scheme + "://" + host + path */ |
| 3194 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3195 | return 0; |
| 3196 | |
| 3197 | /* add scheme */ |
| 3198 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3199 | trash.len += rule->rdr_len; |
| 3200 | |
| 3201 | /* add "://" */ |
| 3202 | memcpy(trash.str + trash.len, "://", 3); |
| 3203 | trash.len += 3; |
| 3204 | |
| 3205 | /* add host */ |
| 3206 | memcpy(trash.str + trash.len, host, hostlen); |
| 3207 | trash.len += hostlen; |
| 3208 | |
| 3209 | /* add path */ |
| 3210 | memcpy(trash.str + trash.len, path, pathlen); |
| 3211 | trash.len += pathlen; |
| 3212 | |
| 3213 | /* append a slash at the end of the location is needed and missing */ |
| 3214 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3215 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3216 | if (trash.len > trash.size - 5) |
| 3217 | return 0; |
| 3218 | trash.str[trash.len] = '/'; |
| 3219 | trash.len++; |
| 3220 | } |
| 3221 | |
| 3222 | break; |
| 3223 | } |
| 3224 | case REDIRECT_TYPE_PREFIX: { |
| 3225 | const char *path; |
| 3226 | int pathlen; |
| 3227 | |
| 3228 | path = http_get_path(txn); |
| 3229 | /* build message using path */ |
| 3230 | if (path) { |
| 3231 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3232 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3233 | int qs = 0; |
| 3234 | while (qs < pathlen) { |
| 3235 | if (path[qs] == '?') { |
| 3236 | pathlen = qs; |
| 3237 | break; |
| 3238 | } |
| 3239 | qs++; |
| 3240 | } |
| 3241 | } |
| 3242 | } else { |
| 3243 | path = "/"; |
| 3244 | pathlen = 1; |
| 3245 | } |
| 3246 | |
| 3247 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 3248 | return 0; |
| 3249 | |
| 3250 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3251 | * add anything, otherwise it makes it hard for the user to |
| 3252 | * configure a self-redirection. |
| 3253 | */ |
| 3254 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 3255 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3256 | trash.len += rule->rdr_len; |
| 3257 | } |
| 3258 | |
| 3259 | /* add path */ |
| 3260 | memcpy(trash.str + trash.len, path, pathlen); |
| 3261 | trash.len += pathlen; |
| 3262 | |
| 3263 | /* append a slash at the end of the location is needed and missing */ |
| 3264 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3265 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3266 | if (trash.len > trash.size - 5) |
| 3267 | return 0; |
| 3268 | trash.str[trash.len] = '/'; |
| 3269 | trash.len++; |
| 3270 | } |
| 3271 | |
| 3272 | break; |
| 3273 | } |
| 3274 | case REDIRECT_TYPE_LOCATION: |
| 3275 | default: |
| 3276 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 3277 | return 0; |
| 3278 | |
| 3279 | /* add location */ |
| 3280 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3281 | trash.len += rule->rdr_len; |
| 3282 | break; |
| 3283 | } |
| 3284 | |
| 3285 | if (rule->cookie_len) { |
| 3286 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 3287 | trash.len += 14; |
| 3288 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 3289 | trash.len += rule->cookie_len; |
| 3290 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 3291 | trash.len += 2; |
| 3292 | } |
| 3293 | |
| 3294 | /* add end of headers and the keep-alive/close status. |
| 3295 | * We may choose to set keep-alive if the Location begins |
| 3296 | * with a slash, because the client will come back to the |
| 3297 | * same server. |
| 3298 | */ |
| 3299 | txn->status = rule->code; |
| 3300 | /* let's log the request time */ |
| 3301 | s->logs.tv_request = now; |
| 3302 | |
| 3303 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
| 3304 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3305 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
| 3306 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3307 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3308 | /* keep-alive possible */ |
| 3309 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
| 3310 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3311 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3312 | trash.len += 30; |
| 3313 | } else { |
| 3314 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 3315 | trash.len += 24; |
| 3316 | } |
| 3317 | } |
| 3318 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 3319 | trash.len += 4; |
| 3320 | bo_inject(txn->rsp.chn, trash.str, trash.len); |
| 3321 | /* "eat" the request */ |
| 3322 | bi_fast_delete(txn->req.chn->buf, msg->sov); |
| 3323 | msg->sov = 0; |
| 3324 | txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY; |
| 3325 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3326 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3327 | txn->rsp.msg_state = HTTP_MSG_DONE; |
| 3328 | } else { |
| 3329 | /* keep-alive not possible */ |
| 3330 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3331 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3332 | trash.len += 29; |
| 3333 | } else { |
| 3334 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3335 | trash.len += 23; |
| 3336 | } |
| 3337 | stream_int_retnclose(txn->req.chn->prod, &trash); |
| 3338 | txn->req.chn->analysers = 0; |
| 3339 | } |
| 3340 | |
| 3341 | if (!(s->flags & SN_ERR_MASK)) |
| 3342 | s->flags |= SN_ERR_PRXCOND; |
| 3343 | if (!(s->flags & SN_FINST_MASK)) |
| 3344 | s->flags |= SN_FINST_R; |
| 3345 | |
| 3346 | return 1; |
| 3347 | } |
| 3348 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3349 | /* This stream analyser runs all HTTP request processing which is common to |
| 3350 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3351 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3352 | * 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] | 3353 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3354 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3355 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3356 | 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] | 3357 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3358 | struct http_txn *txn = &s->txn; |
| 3359 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3360 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3361 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3362 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3363 | struct cond_wordlist *wl; |
Simon Horman | 70735c9 | 2011-06-07 11:07:50 +0900 | [diff] [blame] | 3364 | int do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3365 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3366 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3367 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3368 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3369 | return 0; |
| 3370 | } |
| 3371 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3372 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3373 | req->analyse_exp = TICK_ETERNITY; |
| 3374 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3375 | 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] | 3376 | now_ms, __FUNCTION__, |
| 3377 | s, |
| 3378 | req, |
| 3379 | req->rex, req->wex, |
| 3380 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3381 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3382 | req->analysers); |
| 3383 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3384 | /* first check whether we have some ACLs set to block this request */ |
| 3385 | list_for_each_entry(cond, &px->block_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3386 | 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] | 3387 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3388 | ret = acl_pass(ret); |
| 3389 | if (cond->pol == ACL_COND_UNLESS) |
| 3390 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 3391 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3392 | if (ret) { |
| 3393 | txn->status = 403; |
| 3394 | /* let's log the request time */ |
| 3395 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3396 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3397 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3398 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3399 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3400 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3401 | |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 3402 | /* just in case we have some per-backend tracking */ |
| 3403 | session_inc_be_http_req_ctr(s); |
| 3404 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3405 | /* evaluate http-request rules */ |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3406 | http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 3407 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3408 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3409 | if (!http_req_last_rule) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3410 | do_stats = stats_check_uri(s->rep->prod, txn, px); |
| 3411 | if (do_stats) |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3412 | http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3413 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3414 | else |
| 3415 | do_stats = 0; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3416 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3417 | /* return a 403 if either rule has blocked */ |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3418 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) { |
| 3419 | if (txn->flags & TX_CLDENY) { |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3420 | txn->status = 403; |
| 3421 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3422 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3423 | session_inc_http_err_ctr(s); |
Willy Tarreau | 6da0f6d | 2011-01-06 18:19:50 +0100 | [diff] [blame] | 3424 | s->fe->fe_counters.denied_req++; |
| 3425 | if (an_bit == AN_REQ_HTTP_PROCESS_BE) |
| 3426 | s->be->be_counters.denied_req++; |
| 3427 | if (s->listener->counters) |
| 3428 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3429 | goto return_prx_cond; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3430 | } |
| 3431 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3432 | * which may be the same as the connect timeout if unspecified. |
| 3433 | * If unset, then set it to zero because we really want it to |
| 3434 | * eventually expire. We build the tarpit as an analyser. |
| 3435 | */ |
| 3436 | if (txn->flags & TX_CLTARPIT) { |
| 3437 | channel_erase(s->req); |
| 3438 | /* wipe the request out so that we can drop the connection early |
| 3439 | * if the client closes first. |
| 3440 | */ |
| 3441 | channel_dont_connect(req); |
| 3442 | req->analysers = 0; /* remove switching rules etc... */ |
| 3443 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3444 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3445 | if (!req->analyse_exp) |
| 3446 | req->analyse_exp = tick_add(now_ms, 0); |
| 3447 | session_inc_http_err_ctr(s); |
| 3448 | s->fe->fe_counters.denied_req++; |
| 3449 | if (s->fe != s->be) |
| 3450 | s->be->be_counters.denied_req++; |
| 3451 | if (s->listener->counters) |
| 3452 | s->listener->counters->denied_req++; |
| 3453 | return 1; |
| 3454 | } |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3455 | } |
| 3456 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3457 | /* try headers filters */ |
| 3458 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3459 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3460 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3461 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3462 | /* has the request been denied ? */ |
| 3463 | if (txn->flags & TX_CLDENY) { |
| 3464 | /* no need to go further */ |
| 3465 | txn->status = 403; |
| 3466 | /* let's log the request time */ |
| 3467 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3468 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3469 | session_inc_http_err_ctr(s); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3470 | goto return_prx_cond; |
| 3471 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3472 | |
| 3473 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3474 | * which may be the same as the connect timeout if unspecified. |
| 3475 | * If unset, then set it to zero because we really want it to |
| 3476 | * eventually expire. We build the tarpit as an analyser. |
| 3477 | */ |
| 3478 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3479 | channel_erase(s->req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3480 | /* wipe the request out so that we can drop the connection early |
| 3481 | * if the client closes first. |
| 3482 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3483 | channel_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3484 | req->analysers = 0; /* remove switching rules etc... */ |
| 3485 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3486 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3487 | if (!req->analyse_exp) |
| 3488 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3489 | session_inc_http_err_ctr(s); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3490 | return 1; |
| 3491 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3492 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3493 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3494 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 3495 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3496 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 3497 | * 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] | 3498 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 3499 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 3500 | * The method consists in checking if options changed between the two calls |
| 3501 | * (implying that either one is non-null, or one of them is non-null and we |
| 3502 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 3503 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3504 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 3505 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 3506 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 3507 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 3508 | (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] | 3509 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3510 | |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3511 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE || |
| 3512 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3513 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3514 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 3515 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3516 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3517 | tmp = TX_CON_WANT_CLO; |
| 3518 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3519 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 3520 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3521 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3522 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 3523 | /* parse the Connection header and possibly clean it */ |
| 3524 | int to_del = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3525 | if ((msg->flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 3526 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 3527 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3528 | to_del |= 2; /* remove "keep-alive" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3529 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3530 | to_del |= 1; /* remove "close" */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3531 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3532 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3533 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3534 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 3535 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 3536 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 3537 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3538 | (!(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] | 3539 | ((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] | 3540 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 3541 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3542 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 3543 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3544 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3545 | /* we can be blocked here because the request needs to be authenticated, |
| 3546 | * either to pass or to access stats. |
| 3547 | */ |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3548 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 3549 | char *realm = http_req_last_rule->arg.auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3550 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3551 | if (!realm) |
| 3552 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 3553 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3554 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3555 | txn->status = 401; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3556 | stream_int_retnclose(req->prod, &trash); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3557 | /* on 401 we still count one error, because normal browsing |
| 3558 | * won't significantly increase the counter but brute force |
| 3559 | * attempts will. |
| 3560 | */ |
| 3561 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3562 | goto return_prx_cond; |
| 3563 | } |
| 3564 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3565 | /* add request headers from the rule sets in the same order */ |
| 3566 | list_for_each_entry(wl, &px->req_add, list) { |
| 3567 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3568 | 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] | 3569 | ret = acl_pass(ret); |
| 3570 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3571 | ret = !ret; |
| 3572 | if (!ret) |
| 3573 | continue; |
| 3574 | } |
| 3575 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3576 | 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] | 3577 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3578 | } |
| 3579 | |
| 3580 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) { |
| 3581 | if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn)) |
| 3582 | goto return_bad_req; |
| 3583 | req->analyse_exp = TICK_ETERNITY; |
| 3584 | return 1; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3585 | } |
| 3586 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3587 | if (unlikely(do_stats)) { |
| 3588 | /* process the stats request now */ |
| 3589 | if (!http_handle_stats(s, req)) { |
| 3590 | /* we need more data, let's come back here later */ |
| 3591 | req->analysers |= an_bit; |
| 3592 | channel_dont_connect(req); |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3593 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3594 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3595 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3596 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3597 | /* check whether we have some ACLs set to redirect this request */ |
| 3598 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3599 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3600 | int ret; |
| 3601 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3602 | 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] | 3603 | ret = acl_pass(ret); |
| 3604 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3605 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3606 | if (!ret) |
| 3607 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3608 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3609 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 3610 | goto return_bad_req; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3611 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3612 | req->analyse_exp = TICK_ETERNITY; |
| 3613 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3614 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3615 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3616 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3617 | * If this happens, then the data will not come immediately, so we must |
| 3618 | * send all what we have without waiting. Note that due to the small gain |
| 3619 | * 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] | 3620 | * 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] | 3621 | * itself once used. |
| 3622 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3623 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3624 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3625 | /* that's OK for us now, let's move on to next analysers */ |
| 3626 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3627 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3628 | return_bad_req: |
| 3629 | /* We centralize bad requests processing here */ |
| 3630 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3631 | /* we detected a parsing error. We want to archive this request |
| 3632 | * in the dedicated proxy area for later troubleshooting. |
| 3633 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3634 | 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] | 3635 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3636 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3637 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3638 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3639 | 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] | 3640 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3641 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3642 | if (s->listener->counters) |
| 3643 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3644 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3645 | return_prx_cond: |
| 3646 | if (!(s->flags & SN_ERR_MASK)) |
| 3647 | s->flags |= SN_ERR_PRXCOND; |
| 3648 | if (!(s->flags & SN_FINST_MASK)) |
| 3649 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3650 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3651 | req->analysers = 0; |
| 3652 | req->analyse_exp = TICK_ETERNITY; |
| 3653 | return 0; |
| 3654 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3655 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3656 | /* This function performs all the processing enabled for the current request. |
| 3657 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3658 | * needs more data, encounters an error, or wants to immediately abort the |
| 3659 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3660 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3661 | 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] | 3662 | { |
| 3663 | struct http_txn *txn = &s->txn; |
| 3664 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3665 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3666 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3667 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3668 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3669 | return 0; |
| 3670 | } |
| 3671 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3672 | 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] | 3673 | now_ms, __FUNCTION__, |
| 3674 | s, |
| 3675 | req, |
| 3676 | req->rex, req->wex, |
| 3677 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3678 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3679 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3680 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 3681 | if (s->fe->comp || s->be->comp) |
| 3682 | select_compression_request_header(s, req->buf); |
| 3683 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3684 | /* |
| 3685 | * Right now, we know that we have processed the entire headers |
| 3686 | * and that unwanted requests have been filtered out. We can do |
| 3687 | * whatever we want with the remaining request. Also, now we |
| 3688 | * may have separate values for ->fe, ->be. |
| 3689 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3690 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3691 | /* |
| 3692 | * If HTTP PROXY is set we simply get remote server address |
| 3693 | * parsing incoming request. |
| 3694 | */ |
| 3695 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3696 | 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] | 3697 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3698 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3699 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3700 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3701 | * Note that doing so might move headers in the request, but |
| 3702 | * the fields will stay coherent and the URI will not move. |
| 3703 | * This should only be performed in the backend. |
| 3704 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3705 | 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] | 3706 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3707 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3708 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3709 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3710 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3711 | * so let's do the same now. |
| 3712 | */ |
| 3713 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3714 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3715 | if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3716 | 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] | 3717 | } |
| 3718 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3719 | /* add unique-id if "header-unique-id" is specified */ |
| 3720 | |
| 3721 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) |
| 3722 | build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id); |
| 3723 | |
| 3724 | if (s->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3725 | chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 3726 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3727 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3728 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0)) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3729 | goto return_bad_req; |
| 3730 | } |
| 3731 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3732 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3733 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3734 | * asks for it. |
| 3735 | */ |
| 3736 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3737 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3738 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Cyril Bonté | a32d275 | 2012-05-29 23:27:41 +0200 | [diff] [blame] | 3739 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name, |
| 3740 | s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3741 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3742 | /* The header is set to be added only if none is present |
| 3743 | * and we found it, so don't do anything. |
| 3744 | */ |
| 3745 | } |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3746 | else if (s->req->prod->conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3747 | /* Add an X-Forwarded-For header unless the source IP is |
| 3748 | * in the 'except' network range. |
| 3749 | */ |
| 3750 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3751 | (((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] | 3752 | != s->fe->except_net.s_addr) && |
| 3753 | (!s->be->except_mask.s_addr || |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3754 | (((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] | 3755 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3756 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3757 | unsigned char *pn; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3758 | 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] | 3759 | |
| 3760 | /* Note: we rely on the backend to get the header name to be used for |
| 3761 | * x-forwarded-for, because the header is really meant for the backends. |
| 3762 | * However, if the backend did not specify any option, we have to rely |
| 3763 | * on the frontend's header name. |
| 3764 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3765 | if (s->be->fwdfor_hdr_len) { |
| 3766 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3767 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3768 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3769 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3770 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3771 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3772 | len += sprintf(trash.str + 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] | 3773 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3774 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3775 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3776 | } |
| 3777 | } |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3778 | else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3779 | /* FIXME: for the sake of completeness, we should also support |
| 3780 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3781 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3782 | int len; |
| 3783 | char pn[INET6_ADDRSTRLEN]; |
| 3784 | inet_ntop(AF_INET6, |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3785 | (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] | 3786 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3787 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3788 | /* Note: we rely on the backend to get the header name to be used for |
| 3789 | * x-forwarded-for, because the header is really meant for the backends. |
| 3790 | * However, if the backend did not specify any option, we have to rely |
| 3791 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3792 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3793 | if (s->be->fwdfor_hdr_len) { |
| 3794 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3795 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3796 | } else { |
| 3797 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3798 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3799 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3800 | len += sprintf(trash.str + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3801 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3802 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3803 | goto return_bad_req; |
| 3804 | } |
| 3805 | } |
| 3806 | |
| 3807 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3808 | * 10: add X-Original-To if either the frontend or the backend |
| 3809 | * asks for it. |
| 3810 | */ |
| 3811 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3812 | |
| 3813 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3814 | if (s->req->prod->conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3815 | /* Add an X-Original-To header unless the destination IP is |
| 3816 | * in the 'except' network range. |
| 3817 | */ |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3818 | conn_get_to_addr(s->req->prod->conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3819 | |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3820 | if (s->req->prod->conn->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3821 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3822 | (((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] | 3823 | != s->fe->except_to.s_addr) && |
| 3824 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3825 | (((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] | 3826 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3827 | int len; |
| 3828 | unsigned char *pn; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3829 | 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] | 3830 | |
| 3831 | /* Note: we rely on the backend to get the header name to be used for |
| 3832 | * x-original-to, because the header is really meant for the backends. |
| 3833 | * However, if the backend did not specify any option, we have to rely |
| 3834 | * on the frontend's header name. |
| 3835 | */ |
| 3836 | if (s->be->orgto_hdr_len) { |
| 3837 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3838 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3839 | } else { |
| 3840 | len = s->fe->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3841 | memcpy(trash.str, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3842 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3843 | len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3844 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3845 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3846 | goto return_bad_req; |
| 3847 | } |
| 3848 | } |
| 3849 | } |
| 3850 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3851 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 3852 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 3853 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 3854 | * "Upgrade" is present in the Connection header. |
| 3855 | */ |
| 3856 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 3857 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 3858 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3859 | unsigned int want_flags = 0; |
| 3860 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3861 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3862 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3863 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) && |
| 3864 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3865 | want_flags |= TX_CON_CLO_SET; |
| 3866 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3867 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 3868 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) || |
| 3869 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3870 | want_flags |= TX_CON_KAL_SET; |
| 3871 | } |
| 3872 | |
| 3873 | 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] | 3874 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3875 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3876 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3877 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3878 | /* If we have no server assigned yet and we're balancing on url_param |
| 3879 | * with a POST request, we may be interested in checking the body for |
| 3880 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3881 | */ |
| 3882 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3883 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3884 | s->be->url_param_post_limit != 0 && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3885 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3886 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3887 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3888 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3889 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3890 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3891 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3892 | #ifdef TCP_QUICKACK |
| 3893 | /* We expect some data from the client. Unless we know for sure |
| 3894 | * we already have a full request, we have to re-enable quick-ack |
| 3895 | * in case we previously disabled it, otherwise we might cause |
| 3896 | * the client to delay further data. |
| 3897 | */ |
| 3898 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3899 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3900 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | 7f7ad91 | 2012-11-11 19:27:15 +0100 | [diff] [blame] | 3901 | setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3902 | #endif |
| 3903 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3904 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3905 | /************************************************************* |
| 3906 | * OK, that's finished for the headers. We have done what we * |
| 3907 | * could. Let's switch to the DATA state. * |
| 3908 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3909 | req->analyse_exp = TICK_ETERNITY; |
| 3910 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3911 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 3912 | /* if the server closes the connection, we want to immediately react |
| 3913 | * and close the socket to save packets and syscalls. |
| 3914 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 3915 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
| 3916 | req->cons->flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 3917 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3918 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3919 | /* OK let's go on with the BODY now */ |
| 3920 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3921 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3922 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3923 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3924 | /* we detected a parsing error. We want to archive this request |
| 3925 | * in the dedicated proxy area for later troubleshooting. |
| 3926 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3927 | 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] | 3928 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3929 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3930 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3931 | txn->status = 400; |
| 3932 | req->analysers = 0; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3933 | 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] | 3934 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3935 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3936 | if (s->listener->counters) |
| 3937 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3938 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3939 | if (!(s->flags & SN_ERR_MASK)) |
| 3940 | s->flags |= SN_ERR_PRXCOND; |
| 3941 | if (!(s->flags & SN_FINST_MASK)) |
| 3942 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3943 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3944 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3945 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3946 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3947 | * returns zero, at the beginning because it prevents any other processing |
| 3948 | * from occurring, and at the end because it terminates the request. |
| 3949 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3950 | 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] | 3951 | { |
| 3952 | struct http_txn *txn = &s->txn; |
| 3953 | |
| 3954 | /* This connection is being tarpitted. The CLIENT side has |
| 3955 | * already set the connect expiration date to the right |
| 3956 | * timeout. We just have to check that the client is still |
| 3957 | * there and that the timeout has not expired. |
| 3958 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3959 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3960 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3961 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3962 | return 0; |
| 3963 | |
| 3964 | /* We will set the queue timer to the time spent, just for |
| 3965 | * logging purposes. We fake a 500 server error, so that the |
| 3966 | * attacker will not suspect his connection has been tarpitted. |
| 3967 | * It will not cause trouble to the logs because we can exclude |
| 3968 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3969 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3970 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3971 | |
| 3972 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3973 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3974 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3975 | |
| 3976 | req->analysers = 0; |
| 3977 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3978 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3979 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3980 | if (s->listener->counters) |
| 3981 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3982 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3983 | if (!(s->flags & SN_ERR_MASK)) |
| 3984 | s->flags |= SN_ERR_PRXCOND; |
| 3985 | if (!(s->flags & SN_FINST_MASK)) |
| 3986 | s->flags |= SN_FINST_T; |
| 3987 | return 0; |
| 3988 | } |
| 3989 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3990 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3991 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3992 | * must only be called after the standard HTTP request processing has occurred, |
| 3993 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3994 | * read more data, or 1 once it has completed its analysis. |
| 3995 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3996 | 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] | 3997 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3998 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3999 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4000 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4001 | |
| 4002 | /* We have to parse the HTTP request body to find any required data. |
| 4003 | * "balance url_param check_post" should have been the only way to get |
| 4004 | * into this. We were brought here after HTTP header analysis, so all |
| 4005 | * related structures are ready. |
| 4006 | */ |
| 4007 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4008 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4009 | goto missing_data; |
| 4010 | |
| 4011 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4012 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4013 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4014 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4015 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4016 | struct hdr_ctx ctx; |
| 4017 | ctx.idx = 0; |
| 4018 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4019 | 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] | 4020 | 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] | 4021 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4022 | } |
| 4023 | } |
| 4024 | msg->msg_state = HTTP_MSG_100_SENT; |
| 4025 | } |
| 4026 | |
| 4027 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4028 | /* we have msg->sov which points to the first byte of message body. |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4029 | * 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] | 4030 | * is still null. We must save the body in msg->next because it |
| 4031 | * survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4032 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4033 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4034 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4035 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4036 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4037 | else |
| 4038 | msg->msg_state = HTTP_MSG_DATA; |
| 4039 | } |
| 4040 | |
| 4041 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4042 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4043 | * 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] | 4044 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4045 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4046 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4047 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4048 | if (!ret) |
| 4049 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4050 | else if (ret < 0) { |
| 4051 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4052 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4053 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4054 | } |
| 4055 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4056 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4057 | * We have the first data byte is in msg->sov. We're waiting for at |
| 4058 | * least <url_param_post_limit> bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4059 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4060 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4061 | if (msg->body_len < limit) |
| 4062 | limit = msg->body_len; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4063 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4064 | if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4065 | goto http_end; |
| 4066 | |
| 4067 | missing_data: |
| 4068 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4069 | if (buffer_full(req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4070 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4071 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4072 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4073 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4074 | 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] | 4075 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4076 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4077 | |
| 4078 | if (!(s->flags & SN_ERR_MASK)) |
| 4079 | s->flags |= SN_ERR_CLITO; |
| 4080 | if (!(s->flags & SN_FINST_MASK)) |
| 4081 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4082 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4083 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4084 | |
| 4085 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4086 | 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] | 4087 | /* Not enough data. We'll re-use the http-request |
| 4088 | * timeout here. Ideally, we should set the timeout |
| 4089 | * relative to the accept() date. We just set the |
| 4090 | * request timeout once at the beginning of the |
| 4091 | * request. |
| 4092 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4093 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4094 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4095 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4096 | return 0; |
| 4097 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4098 | |
| 4099 | http_end: |
| 4100 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4101 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4102 | req->analysers &= ~an_bit; |
| 4103 | req->analyse_exp = TICK_ETERNITY; |
| 4104 | return 1; |
| 4105 | |
| 4106 | return_bad_req: /* let's centralize all bad requests */ |
| 4107 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4108 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4109 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4110 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4111 | if (!(s->flags & SN_ERR_MASK)) |
| 4112 | s->flags |= SN_ERR_PRXCOND; |
| 4113 | if (!(s->flags & SN_FINST_MASK)) |
| 4114 | s->flags |= SN_FINST_R; |
| 4115 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4116 | return_err_msg: |
| 4117 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4118 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4119 | if (s->listener->counters) |
| 4120 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4121 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4122 | } |
| 4123 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4124 | /* send a server's name with an outgoing request over an established connection. |
| 4125 | * Note: this function is designed to be called once the request has been scheduled |
| 4126 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4127 | * proceeding. |
| 4128 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4129 | 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] | 4130 | |
| 4131 | struct hdr_ctx ctx; |
| 4132 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4133 | char *hdr_name = be->server_id_hdr_name; |
| 4134 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4135 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4136 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4137 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4138 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4139 | ctx.idx = 0; |
| 4140 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4141 | old_o = chn->buf->o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4142 | if (old_o) { |
| 4143 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4144 | b_rew(chn->buf, old_o); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4145 | } |
| 4146 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4147 | old_i = chn->buf->i; |
| 4148 | 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] | 4149 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4150 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4151 | } |
| 4152 | |
| 4153 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4154 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4155 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4156 | hdr_val += hdr_name_len; |
| 4157 | *hdr_val++ = ':'; |
| 4158 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4159 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4160 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4161 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4162 | if (old_o) { |
| 4163 | /* If this was a forwarded request, we must readjust the amount of |
| 4164 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 2fef9b1 | 2013-03-26 01:08:21 +0100 | [diff] [blame] | 4165 | * variations. Note that if the request was already scheduled for |
| 4166 | * forwarding, it had its req->sol pointing to the body, which |
| 4167 | * must then be updated too. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4168 | */ |
Willy Tarreau | 2fef9b1 | 2013-03-26 01:08:21 +0100 | [diff] [blame] | 4169 | txn->req.sol += chn->buf->i - old_i; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4170 | b_adv(chn->buf, old_o + chn->buf->i - old_i); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4171 | } |
| 4172 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4173 | return 0; |
| 4174 | } |
| 4175 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4176 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4177 | * right now but it works. |
| 4178 | */ |
| 4179 | void http_end_txn_clean_session(struct session *s) |
| 4180 | { |
| 4181 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4182 | * server's connections. We need a safer way to reinitialize buffer |
| 4183 | * flags. We also need a more accurate method for computing per-request |
| 4184 | * data. |
| 4185 | */ |
| 4186 | http_silent_debug(__LINE__, s); |
| 4187 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4188 | s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 4189 | si_shutr(s->req->cons); |
| 4190 | si_shutw(s->req->cons); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4191 | |
| 4192 | http_silent_debug(__LINE__, s); |
| 4193 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4194 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4195 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4196 | if (unlikely(s->srv_conn)) |
| 4197 | sess_change_server(s, NULL); |
| 4198 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4199 | |
| 4200 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4201 | session_process_counters(s); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 4202 | session_stop_backend_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4203 | |
| 4204 | if (s->txn.status) { |
| 4205 | int n; |
| 4206 | |
| 4207 | n = s->txn.status / 100; |
| 4208 | if (n < 1 || n > 5) |
| 4209 | n = 0; |
| 4210 | |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4211 | if (s->fe->mode == PR_MODE_HTTP) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4212 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4213 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4214 | s->fe->fe_counters.p.http.comp_rsp++; |
| 4215 | } |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 4216 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4217 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4218 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4219 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4220 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4221 | s->be->be_counters.p.http.comp_rsp++; |
| 4222 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | /* don't count other requests' data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4226 | s->logs.bytes_in -= s->req->buf->i; |
| 4227 | s->logs.bytes_out -= s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4228 | |
| 4229 | /* let's do a final log if we need it */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 4230 | if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4231 | !(s->flags & SN_MONITOR) && |
| 4232 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 4233 | s->do_log(s); |
| 4234 | } |
| 4235 | |
| 4236 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4237 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4238 | tv_zero(&s->logs.tv_request); |
| 4239 | s->logs.t_queue = -1; |
| 4240 | s->logs.t_connect = -1; |
| 4241 | s->logs.t_data = -1; |
| 4242 | s->logs.t_close = 0; |
| 4243 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4244 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4245 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4246 | s->logs.bytes_in = s->req->total = s->req->buf->i; |
| 4247 | s->logs.bytes_out = s->rep->total = s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4248 | |
| 4249 | if (s->pend_pos) |
| 4250 | pendconn_free(s->pend_pos); |
| 4251 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4252 | if (objt_server(s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4253 | if (s->flags & SN_CURR_SESS) { |
| 4254 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4255 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4256 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4257 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 4258 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4259 | } |
| 4260 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4261 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4262 | |
| 4263 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 4264 | s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */ |
| 4265 | s->req->cons->conn->flags = CO_FL_NONE; |
Willy Tarreau | 14cba4b | 2012-11-30 17:33:05 +0100 | [diff] [blame] | 4266 | s->req->cons->conn->err_code = CO_ER_NONE; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4267 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 4268 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4269 | s->req->cons->err_loc = NULL; |
| 4270 | s->req->cons->exp = TICK_ETERNITY; |
| 4271 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4272 | s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT); |
| 4273 | 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] | 4274 | 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] | 4275 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4276 | |
| 4277 | if (s->flags & SN_COMP_READY) |
| 4278 | s->comp_algo->end(&s->comp_ctx); |
| 4279 | s->comp_algo = NULL; |
| 4280 | s->flags &= ~SN_COMP_READY; |
| 4281 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4282 | s->txn.meth = 0; |
| 4283 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4284 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 4285 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4286 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 4287 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4288 | if (s->fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4289 | s->req->flags |= CF_NEVER_WAIT; |
| 4290 | s->rep->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4291 | } |
| 4292 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4293 | /* if the request buffer is not empty, it means we're |
| 4294 | * about to process another request, so send pending |
| 4295 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4296 | * Just don't do this if the buffer is close to be full, |
| 4297 | * because the request will wait for it to flush a little |
| 4298 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4299 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4300 | if (s->req->buf->i) { |
| 4301 | if (s->rep->buf->o && |
| 4302 | !buffer_full(s->rep->buf, global.tune.maxrewrite) && |
| 4303 | 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] | 4304 | s->rep->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4305 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4306 | |
| 4307 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4308 | channel_auto_read(s->req); |
| 4309 | channel_auto_close(s->req); |
| 4310 | channel_auto_read(s->rep); |
| 4311 | channel_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4312 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 4313 | s->req->analysers = s->listener->analysers; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4314 | s->rep->analysers = 0; |
| 4315 | |
| 4316 | http_silent_debug(__LINE__, s); |
| 4317 | } |
| 4318 | |
| 4319 | |
| 4320 | /* This function updates the request state machine according to the response |
| 4321 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4322 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4323 | * it is only used to find when a request/response couple is complete. Both |
| 4324 | * this function and its equivalent should loop until both return zero. It |
| 4325 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4326 | */ |
| 4327 | int http_sync_req_state(struct session *s) |
| 4328 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4329 | struct channel *chn = s->req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4330 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4331 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4332 | unsigned int old_state = txn->req.msg_state; |
| 4333 | |
| 4334 | http_silent_debug(__LINE__, s); |
| 4335 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 4336 | return 0; |
| 4337 | |
| 4338 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4339 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4340 | * We can shut the read side unless we want to abort_on_close, |
| 4341 | * or we have a POST request. The issue with POST requests is |
| 4342 | * that some browsers still send a CRLF after the request, and |
| 4343 | * this CRLF must be read so that it does not remain in the kernel |
| 4344 | * buffers, otherwise a close could cause an RST on some systems |
| 4345 | * (eg: Linux). |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4346 | */ |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4347 | 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] | 4348 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4349 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4350 | /* if the server closes the connection, we want to immediately react |
| 4351 | * and close the socket to save packets and syscalls. |
| 4352 | */ |
| 4353 | chn->cons->flags |= SI_FL_NOHALF; |
| 4354 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4355 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4356 | goto wait_other_side; |
| 4357 | |
| 4358 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4359 | /* The server has not finished to respond, so we |
| 4360 | * don't want to move in order not to upset it. |
| 4361 | */ |
| 4362 | goto wait_other_side; |
| 4363 | } |
| 4364 | |
| 4365 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4366 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4367 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4368 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4369 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4370 | goto wait_other_side; |
| 4371 | } |
| 4372 | |
| 4373 | /* When we get here, it means that both the request and the |
| 4374 | * response have finished receiving. Depending on the connection |
| 4375 | * mode, we'll have to wait for the last bytes to leave in either |
| 4376 | * direction, and sometimes for a close to be effective. |
| 4377 | */ |
| 4378 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4379 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4380 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4381 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 4382 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4383 | } |
| 4384 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4385 | /* Option forceclose is set, or either side wants to close, |
| 4386 | * let's enforce it now that we're not expecting any new |
| 4387 | * data to come. The caller knows the session is complete |
| 4388 | * once both states are CLOSED. |
| 4389 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4390 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4391 | channel_shutr_now(chn); |
| 4392 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4393 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4394 | } |
| 4395 | else { |
| 4396 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4397 | * mode does not set the body analyser, we can't reach this place |
| 4398 | * in tunnel mode, so we're left with keep-alive only. |
| 4399 | * This mode is currently not implemented, we switch to tunnel mode. |
| 4400 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4401 | channel_auto_read(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4402 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4403 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4404 | } |
| 4405 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4406 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4407 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4408 | chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4409 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4410 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4411 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4412 | goto http_msg_closing; |
| 4413 | } |
| 4414 | else { |
| 4415 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4416 | goto http_msg_closed; |
| 4417 | } |
| 4418 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4419 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4420 | } |
| 4421 | |
| 4422 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4423 | http_msg_closing: |
| 4424 | /* nothing else to forward, just waiting for the output buffer |
| 4425 | * to be empty and for the shutw_now to take effect. |
| 4426 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4427 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4428 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4429 | goto http_msg_closed; |
| 4430 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4431 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4432 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4433 | goto wait_other_side; |
| 4434 | } |
| 4435 | } |
| 4436 | |
| 4437 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4438 | http_msg_closed: |
| 4439 | goto wait_other_side; |
| 4440 | } |
| 4441 | |
| 4442 | wait_other_side: |
| 4443 | http_silent_debug(__LINE__, s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4444 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4445 | } |
| 4446 | |
| 4447 | |
| 4448 | /* This function updates the response state machine according to the request |
| 4449 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4450 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4451 | * it is only used to find when a request/response couple is complete. Both |
| 4452 | * this function and its equivalent should loop until both return zero. It |
| 4453 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4454 | */ |
| 4455 | int http_sync_res_state(struct session *s) |
| 4456 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4457 | struct channel *chn = s->rep; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4458 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4459 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4460 | unsigned int old_state = txn->rsp.msg_state; |
| 4461 | |
| 4462 | http_silent_debug(__LINE__, s); |
| 4463 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4464 | return 0; |
| 4465 | |
| 4466 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4467 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4468 | * still monitor the server connection for a possible close |
| 4469 | * while the request is being uploaded, so we don't disable |
| 4470 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4471 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4472 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4473 | |
| 4474 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4475 | goto wait_other_side; |
| 4476 | |
| 4477 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4478 | /* The client seems to still be sending data, probably |
| 4479 | * because we got an error response during an upload. |
| 4480 | * We have the choice of either breaking the connection |
| 4481 | * or letting it pass through. Let's do the later. |
| 4482 | */ |
| 4483 | goto wait_other_side; |
| 4484 | } |
| 4485 | |
| 4486 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4487 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4488 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4489 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4490 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4491 | goto wait_other_side; |
| 4492 | } |
| 4493 | |
| 4494 | /* When we get here, it means that both the request and the |
| 4495 | * response have finished receiving. Depending on the connection |
| 4496 | * mode, we'll have to wait for the last bytes to leave in either |
| 4497 | * direction, and sometimes for a close to be effective. |
| 4498 | */ |
| 4499 | |
| 4500 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4501 | /* Server-close mode : shut read and wait for the request |
| 4502 | * side to close its output buffer. The caller will detect |
| 4503 | * when we're in DONE and the other is in CLOSED and will |
| 4504 | * catch that for the final cleanup. |
| 4505 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4506 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4507 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4508 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4509 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4510 | /* Option forceclose is set, or either side wants to close, |
| 4511 | * let's enforce it now that we're not expecting any new |
| 4512 | * data to come. The caller knows the session is complete |
| 4513 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4514 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4515 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4516 | channel_shutr_now(chn); |
| 4517 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4518 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4519 | } |
| 4520 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4521 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4522 | * mode does not set the body analyser, we can't reach this place |
| 4523 | * in tunnel mode, so we're left with keep-alive only. |
| 4524 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4525 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4526 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4527 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4528 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4529 | } |
| 4530 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4531 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4532 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4533 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4534 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4535 | goto http_msg_closing; |
| 4536 | } |
| 4537 | else { |
| 4538 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4539 | goto http_msg_closed; |
| 4540 | } |
| 4541 | } |
| 4542 | goto wait_other_side; |
| 4543 | } |
| 4544 | |
| 4545 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4546 | http_msg_closing: |
| 4547 | /* nothing else to forward, just waiting for the output buffer |
| 4548 | * to be empty and for the shutw_now to take effect. |
| 4549 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4550 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4551 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4552 | goto http_msg_closed; |
| 4553 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4554 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4555 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4556 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4557 | if (objt_server(s->target)) |
| 4558 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4559 | goto wait_other_side; |
| 4560 | } |
| 4561 | } |
| 4562 | |
| 4563 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4564 | http_msg_closed: |
| 4565 | /* drop any pending data */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4566 | bi_erase(chn); |
| 4567 | channel_auto_close(chn); |
| 4568 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4569 | goto wait_other_side; |
| 4570 | } |
| 4571 | |
| 4572 | wait_other_side: |
| 4573 | http_silent_debug(__LINE__, s); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4574 | /* We force the response to leave immediately if we're waiting for the |
| 4575 | * other side, since there is no pending shutdown to push it out. |
| 4576 | */ |
| 4577 | if (!channel_is_empty(chn)) |
| 4578 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4579 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4580 | } |
| 4581 | |
| 4582 | |
| 4583 | /* Resync the request and response state machines. Return 1 if either state |
| 4584 | * changes. |
| 4585 | */ |
| 4586 | int http_resync_states(struct session *s) |
| 4587 | { |
| 4588 | struct http_txn *txn = &s->txn; |
| 4589 | int old_req_state = txn->req.msg_state; |
| 4590 | int old_res_state = txn->rsp.msg_state; |
| 4591 | |
| 4592 | http_silent_debug(__LINE__, s); |
| 4593 | http_sync_req_state(s); |
| 4594 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4595 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4596 | if (!http_sync_res_state(s)) |
| 4597 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4598 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4599 | if (!http_sync_req_state(s)) |
| 4600 | break; |
| 4601 | } |
| 4602 | http_silent_debug(__LINE__, s); |
| 4603 | /* OK, both state machines agree on a compatible state. |
| 4604 | * There are a few cases we're interested in : |
| 4605 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4606 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4607 | * directions, so let's simply disable both analysers. |
| 4608 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4609 | * request. |
| 4610 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4611 | * with server-close mode means we've completed one request and we |
| 4612 | * must re-initialize the server connection. |
| 4613 | */ |
| 4614 | |
| 4615 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4616 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4617 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4618 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4619 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4620 | channel_auto_close(s->req); |
| 4621 | channel_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4622 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4623 | channel_auto_close(s->rep); |
| 4624 | channel_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4625 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4626 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
| 4627 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4628 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4629 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4630 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4631 | channel_auto_close(s->rep); |
| 4632 | channel_auto_read(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4633 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4634 | channel_abort(s->req); |
| 4635 | channel_auto_close(s->req); |
| 4636 | channel_auto_read(s->req); |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4637 | bi_erase(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4638 | } |
| 4639 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4640 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4641 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4642 | /* server-close: terminate this server connection and |
| 4643 | * reinitialize a fresh-new transaction. |
| 4644 | */ |
| 4645 | http_end_txn_clean_session(s); |
| 4646 | } |
| 4647 | |
| 4648 | http_silent_debug(__LINE__, s); |
| 4649 | return txn->req.msg_state != old_req_state || |
| 4650 | txn->rsp.msg_state != old_res_state; |
| 4651 | } |
| 4652 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4653 | /* This function is an analyser which forwards request body (including chunk |
| 4654 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4655 | * zero byte. The only situation where it must not be called is when we're in |
| 4656 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4657 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4658 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4659 | * 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] | 4660 | * 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] | 4661 | * bytes of pending data + the headers if not already done (between sol and sov). |
| 4662 | * 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] | 4663 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4664 | 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] | 4665 | { |
| 4666 | struct http_txn *txn = &s->txn; |
| 4667 | struct http_msg *msg = &s->txn.req; |
| 4668 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4669 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4670 | return 0; |
| 4671 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4672 | if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4673 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4674 | /* Output closed while we were sending data. We must abort and |
| 4675 | * wake the other side up. |
| 4676 | */ |
| 4677 | msg->msg_state = HTTP_MSG_ERROR; |
| 4678 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4679 | return 1; |
| 4680 | } |
| 4681 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4682 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4683 | channel_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4684 | |
| 4685 | /* Note that we don't have to send 100-continue back because we don't |
| 4686 | * need the data to complete our job, and it's up to the server to |
| 4687 | * decide whether to return 100, 417 or anything else in return of |
| 4688 | * an "Expect: 100-continue" header. |
| 4689 | */ |
| 4690 | |
| 4691 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4692 | /* we have msg->sov which points to the first byte of message body. |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4693 | * 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] | 4694 | * is still null. We must save the body in msg->next because it |
| 4695 | * survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4696 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4697 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4698 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4699 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4700 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4701 | else |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4702 | msg->msg_state = HTTP_MSG_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4703 | } |
| 4704 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4705 | while (1) { |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4706 | unsigned int bytes; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4707 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4708 | http_silent_debug(__LINE__, s); |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4709 | /* we may have some data pending between sol and sov */ |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4710 | bytes = msg->sov - msg->sol; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4711 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4712 | msg->sol = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4713 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4714 | msg->chunk_len += bytes; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4715 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4716 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4717 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4718 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4719 | /* must still forward */ |
| 4720 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4721 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4722 | |
| 4723 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4724 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4725 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4726 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4727 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4728 | } |
| 4729 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4730 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4731 | * 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] | 4732 | * TRAILERS state. |
| 4733 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4734 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4735 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4736 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4737 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4738 | else if (ret < 0) { |
| 4739 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4740 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4741 | 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] | 4742 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4743 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4744 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4745 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4746 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4747 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4748 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4749 | |
| 4750 | if (ret == 0) |
| 4751 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4752 | else if (ret < 0) { |
| 4753 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4754 | if (msg->err_pos >= 0) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4755 | http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4756 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4757 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4758 | /* we're in MSG_CHUNK_SIZE now */ |
| 4759 | } |
| 4760 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4761 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4762 | |
| 4763 | if (ret == 0) |
| 4764 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4765 | else if (ret < 0) { |
| 4766 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4767 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4768 | 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] | 4769 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4770 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4771 | /* we're in HTTP_MSG_DONE now */ |
| 4772 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4773 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4774 | int old_state = msg->msg_state; |
| 4775 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4776 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4777 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4778 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4779 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4780 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4781 | if (http_resync_states(s)) { |
| 4782 | /* some state changes occurred, maybe the analyser |
| 4783 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4784 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4785 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4786 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4787 | /* request errors are most likely due to |
| 4788 | * the server aborting the transfer. |
| 4789 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4790 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4791 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4792 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4793 | 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] | 4794 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4795 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4796 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4797 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4798 | |
| 4799 | /* If "option abortonclose" is set on the backend, we |
| 4800 | * want to monitor the client's connection and forward |
| 4801 | * any shutdown notification to the server, which will |
| 4802 | * decide whether to close or to go on processing the |
| 4803 | * request. |
| 4804 | */ |
| 4805 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4806 | channel_auto_read(req); |
| 4807 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4808 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4809 | else if (s->txn.meth == HTTP_METH_POST) { |
| 4810 | /* POST requests may require to read extra CRLF |
| 4811 | * sent by broken browsers and which could cause |
| 4812 | * an RST to be sent upon close on some systems |
| 4813 | * (eg: Linux). |
| 4814 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4815 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4816 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4817 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4818 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4819 | } |
| 4820 | } |
| 4821 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4822 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4823 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4824 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4825 | if (!(s->flags & SN_ERR_MASK)) |
| 4826 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4827 | if (!(s->flags & SN_FINST_MASK)) { |
| 4828 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4829 | s->flags |= SN_FINST_H; |
| 4830 | else |
| 4831 | s->flags |= SN_FINST_D; |
| 4832 | } |
| 4833 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4834 | s->fe->fe_counters.cli_aborts++; |
| 4835 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4836 | if (objt_server(s->target)) |
| 4837 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4838 | |
| 4839 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4840 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4841 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4842 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4843 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4844 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4845 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4846 | /* 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] | 4847 | * 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] | 4848 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4849 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4850 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4851 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4852 | /* 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] | 4853 | * 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] | 4854 | * 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] | 4855 | * modes are already handled by the stream sock layer. We must not do |
| 4856 | * this in content-length mode because it could present the MSG_MORE |
| 4857 | * flag with the last block of forwarded data, which would cause an |
| 4858 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4859 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4860 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4861 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4862 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4863 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4864 | return 0; |
| 4865 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4866 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4867 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4868 | if (s->listener->counters) |
| 4869 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4870 | return_bad_req_stats_ok: |
| 4871 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4872 | if (txn->status) { |
| 4873 | /* Note: we don't send any error if some data were already sent */ |
| 4874 | stream_int_retnclose(req->prod, NULL); |
| 4875 | } else { |
| 4876 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4877 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4878 | } |
| 4879 | req->analysers = 0; |
| 4880 | 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] | 4881 | |
| 4882 | if (!(s->flags & SN_ERR_MASK)) |
| 4883 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4884 | if (!(s->flags & SN_FINST_MASK)) { |
| 4885 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4886 | s->flags |= SN_FINST_H; |
| 4887 | else |
| 4888 | s->flags |= SN_FINST_D; |
| 4889 | } |
| 4890 | return 0; |
| 4891 | |
| 4892 | aborted_xfer: |
| 4893 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4894 | if (txn->status) { |
| 4895 | /* Note: we don't send any error if some data were already sent */ |
| 4896 | stream_int_retnclose(req->prod, NULL); |
| 4897 | } else { |
| 4898 | txn->status = 502; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4899 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4900 | } |
| 4901 | req->analysers = 0; |
| 4902 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 4903 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4904 | s->fe->fe_counters.srv_aborts++; |
| 4905 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4906 | if (objt_server(s->target)) |
| 4907 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4908 | |
| 4909 | if (!(s->flags & SN_ERR_MASK)) |
| 4910 | s->flags |= SN_ERR_SRVCL; |
| 4911 | if (!(s->flags & SN_FINST_MASK)) { |
| 4912 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4913 | s->flags |= SN_FINST_H; |
| 4914 | else |
| 4915 | s->flags |= SN_FINST_D; |
| 4916 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4917 | return 0; |
| 4918 | } |
| 4919 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4920 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4921 | * processing can continue on next analysers, or zero if it either needs more |
| 4922 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4923 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4924 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4925 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4926 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4927 | 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] | 4928 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4929 | struct http_txn *txn = &s->txn; |
| 4930 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4931 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4932 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4933 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4934 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4935 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4936 | 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] | 4937 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4938 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4939 | rep, |
| 4940 | rep->rex, rep->wex, |
| 4941 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4942 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4943 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4944 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4945 | /* |
| 4946 | * Now parse the partial (or complete) lines. |
| 4947 | * We will check the response syntax, and also join multi-line |
| 4948 | * headers. An index of all the lines will be elaborated while |
| 4949 | * parsing. |
| 4950 | * |
| 4951 | * For the parsing, we use a 28 states FSM. |
| 4952 | * |
| 4953 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4954 | * rep->buf->p = beginning of response |
| 4955 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 4956 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4957 | * msg->eol = end of current header or line (LF or CRLF) |
| 4958 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4959 | */ |
| 4960 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4961 | /* There's a protected area at the end of the buffer for rewriting |
| 4962 | * purposes. We don't want to start to parse the request if the |
| 4963 | * protected area is affected, because we may have to move processed |
| 4964 | * data later, which is much more complicated. |
| 4965 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4966 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 4967 | if (unlikely(channel_full(rep) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4968 | bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 4969 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) { |
| 4970 | if (rep->buf->o) { |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4971 | /* 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] | 4972 | 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] | 4973 | goto abort_response; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4974 | channel_dont_close(rep); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4975 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4976 | return 0; |
| 4977 | } |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4978 | if (rep->buf->i <= rep->buf->size - global.tune.maxrewrite) |
| 4979 | buffer_slow_realign(msg->chn->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4980 | } |
| 4981 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4982 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 4983 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4984 | } |
| 4985 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4986 | /* 1: we might have to print this header in debug mode */ |
| 4987 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4988 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4989 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4990 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4991 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4992 | sol = rep->buf->p; |
| 4993 | 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] | 4994 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4995 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4996 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4997 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4998 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4999 | while (cur_idx) { |
| 5000 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5001 | debug_hdr("srvhdr", s, sol, eol); |
| 5002 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5003 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5004 | } |
| 5005 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5006 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5007 | /* |
| 5008 | * Now we quickly check if we have found a full valid response. |
| 5009 | * If not so, we check the FD and buffer states before leaving. |
| 5010 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5011 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5012 | * responses are checked first. |
| 5013 | * |
| 5014 | * Depending on whether the client is still there or not, we |
| 5015 | * may send an error response back or not. Note that normally |
| 5016 | * we should only check for HTTP status there, and check I/O |
| 5017 | * errors somewhere else. |
| 5018 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5019 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5020 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5021 | /* Invalid response */ |
| 5022 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5023 | /* we detected a parsing error. We want to archive this response |
| 5024 | * in the dedicated proxy area for later troubleshooting. |
| 5025 | */ |
| 5026 | hdr_response_bad: |
| 5027 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5028 | 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] | 5029 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5030 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5031 | if (objt_server(s->target)) { |
| 5032 | objt_server(s->target)->counters.failed_resp++; |
| 5033 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5034 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5035 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5036 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5037 | rep->analysers = 0; |
| 5038 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5039 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5040 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5041 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5042 | |
| 5043 | if (!(s->flags & SN_ERR_MASK)) |
| 5044 | s->flags |= SN_ERR_PRXCOND; |
| 5045 | if (!(s->flags & SN_FINST_MASK)) |
| 5046 | s->flags |= SN_FINST_H; |
| 5047 | |
| 5048 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5049 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5050 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5051 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5052 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5053 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5054 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5055 | goto hdr_response_bad; |
| 5056 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5057 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5058 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5059 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5060 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5061 | 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] | 5062 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5063 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5064 | if (objt_server(s->target)) { |
| 5065 | objt_server(s->target)->counters.failed_resp++; |
| 5066 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5067 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5068 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5069 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5070 | rep->analysers = 0; |
| 5071 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5072 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5073 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5074 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5075 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5076 | if (!(s->flags & SN_ERR_MASK)) |
| 5077 | s->flags |= SN_ERR_SRVCL; |
| 5078 | if (!(s->flags & SN_FINST_MASK)) |
| 5079 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5080 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5081 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5082 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5083 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5084 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5085 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5086 | 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] | 5087 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5088 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5089 | if (objt_server(s->target)) { |
| 5090 | objt_server(s->target)->counters.failed_resp++; |
| 5091 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5092 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5093 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5094 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5095 | rep->analysers = 0; |
| 5096 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5097 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5098 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5099 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5100 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5101 | if (!(s->flags & SN_ERR_MASK)) |
| 5102 | s->flags |= SN_ERR_SRVTO; |
| 5103 | if (!(s->flags & SN_FINST_MASK)) |
| 5104 | s->flags |= SN_FINST_H; |
| 5105 | return 0; |
| 5106 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5107 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5108 | /* client abort with an abortonclose */ |
| 5109 | else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
| 5110 | s->fe->fe_counters.cli_aborts++; |
| 5111 | s->be->be_counters.cli_aborts++; |
| 5112 | if (objt_server(s->target)) |
| 5113 | objt_server(s->target)->counters.cli_aborts++; |
| 5114 | |
| 5115 | rep->analysers = 0; |
| 5116 | channel_auto_close(rep); |
| 5117 | |
| 5118 | txn->status = 400; |
| 5119 | bi_erase(rep); |
| 5120 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400)); |
| 5121 | |
| 5122 | if (!(s->flags & SN_ERR_MASK)) |
| 5123 | s->flags |= SN_ERR_CLICL; |
| 5124 | if (!(s->flags & SN_FINST_MASK)) |
| 5125 | s->flags |= SN_FINST_H; |
| 5126 | |
| 5127 | /* process_session() will take care of the error */ |
| 5128 | return 0; |
| 5129 | } |
| 5130 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5131 | /* 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] | 5132 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5133 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5134 | 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] | 5135 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5136 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5137 | if (objt_server(s->target)) { |
| 5138 | objt_server(s->target)->counters.failed_resp++; |
| 5139 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5140 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5141 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5142 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5143 | rep->analysers = 0; |
| 5144 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5145 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5146 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5147 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5148 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5149 | if (!(s->flags & SN_ERR_MASK)) |
| 5150 | s->flags |= SN_ERR_SRVCL; |
| 5151 | if (!(s->flags & SN_FINST_MASK)) |
| 5152 | s->flags |= SN_FINST_H; |
| 5153 | return 0; |
| 5154 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5155 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5156 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5157 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5158 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5159 | 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] | 5160 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5161 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5162 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5163 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5164 | |
| 5165 | if (!(s->flags & SN_ERR_MASK)) |
| 5166 | s->flags |= SN_ERR_CLICL; |
| 5167 | if (!(s->flags & SN_FINST_MASK)) |
| 5168 | s->flags |= SN_FINST_H; |
| 5169 | |
| 5170 | /* process_session() will take care of the error */ |
| 5171 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5172 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5173 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5174 | channel_dont_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5175 | return 0; |
| 5176 | } |
| 5177 | |
| 5178 | /* More interesting part now : we know that we have a complete |
| 5179 | * response which at least looks like HTTP. We have an indicator |
| 5180 | * of each header's length, so we can parse them quickly. |
| 5181 | */ |
| 5182 | |
| 5183 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5184 | 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] | 5185 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5186 | /* |
| 5187 | * 1: get the status code |
| 5188 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5189 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5190 | if (n < 1 || n > 5) |
| 5191 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5192 | /* when the client triggers a 4xx from the server, it's most often due |
| 5193 | * to a missing object or permission. These events should be tracked |
| 5194 | * because if they happen often, it may indicate a brute force or a |
| 5195 | * vulnerability scan. |
| 5196 | */ |
| 5197 | if (n == 4) |
| 5198 | session_inc_http_err_ctr(s); |
| 5199 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5200 | if (objt_server(s->target)) |
| 5201 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5202 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5203 | /* check if the response is HTTP/1.1 or above */ |
| 5204 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5205 | ((rep->buf->p[5] > '1') || |
| 5206 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5207 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5208 | |
| 5209 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5210 | txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_HDR_CONN_UPG|TX_CON_CLO_SET|TX_CON_KAL_SET); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5211 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5212 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5213 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5214 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5215 | 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] | 5216 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5217 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5218 | * and 505 are triggered on demand by client request, so we must not |
| 5219 | * count them as server failures. |
| 5220 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5221 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5222 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5223 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5224 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5225 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5226 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5227 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5228 | /* |
| 5229 | * 2: check for cacheability. |
| 5230 | */ |
| 5231 | |
| 5232 | switch (txn->status) { |
| 5233 | case 200: |
| 5234 | case 203: |
| 5235 | case 206: |
| 5236 | case 300: |
| 5237 | case 301: |
| 5238 | case 410: |
| 5239 | /* RFC2616 @13.4: |
| 5240 | * "A response received with a status code of |
| 5241 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 5242 | * by a cache (...) unless a cache-control |
| 5243 | * directive prohibits caching." |
| 5244 | * |
| 5245 | * RFC2616 @9.5: POST method : |
| 5246 | * "Responses to this method are not cacheable, |
| 5247 | * unless the response includes appropriate |
| 5248 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5249 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5250 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5251 | ((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] | 5252 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 5253 | break; |
| 5254 | default: |
| 5255 | break; |
| 5256 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5257 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5258 | /* |
| 5259 | * 3: we may need to capture headers |
| 5260 | */ |
| 5261 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 5262 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5263 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5264 | txn->rsp.cap, s->fe->rsp_cap); |
| 5265 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5266 | /* 4: determine the transfer-length. |
| 5267 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 5268 | * the presence of a message-body in a RESPONSE and its transfer length |
| 5269 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5270 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5271 | * All responses to the HEAD request method MUST NOT include a |
| 5272 | * message-body, even though the presence of entity-header fields |
| 5273 | * might lead one to believe they do. All 1xx (informational), 204 |
| 5274 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 5275 | * message-body. All other responses do include a message-body, |
| 5276 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5277 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5278 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 5279 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 5280 | * always terminated by the first empty line after the header fields, |
| 5281 | * regardless of the entity-header fields present in the message. |
| 5282 | * |
| 5283 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 5284 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 5285 | * transfer-length is defined by the use of this transfer-coding. |
| 5286 | * If a Transfer-Encoding header field is present and the "chunked" |
| 5287 | * transfer-coding is not present, the transfer-length is defined by |
| 5288 | * the sender closing the connection. |
| 5289 | * |
| 5290 | * 3. If a Content-Length header field is present, its decimal value in |
| 5291 | * OCTETs represents both the entity-length and the transfer-length. |
| 5292 | * If a message is received with both a Transfer-Encoding header |
| 5293 | * field and a Content-Length header field, the latter MUST be ignored. |
| 5294 | * |
| 5295 | * 4. If the message uses the media type "multipart/byteranges", and |
| 5296 | * the transfer-length is not otherwise specified, then this self- |
| 5297 | * delimiting media type defines the transfer-length. This media |
| 5298 | * type MUST NOT be used unless the sender knows that the recipient |
| 5299 | * can parse it; the presence in a request of a Range header with |
| 5300 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 5301 | * client can parse multipart/byteranges responses. |
| 5302 | * |
| 5303 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5304 | */ |
| 5305 | |
| 5306 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5307 | * 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] | 5308 | * 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] | 5309 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5310 | */ |
| 5311 | if (txn->meth == HTTP_METH_HEAD || |
| 5312 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5313 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5314 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 5315 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5316 | goto skip_content_length; |
| 5317 | } |
| 5318 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5319 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5320 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5321 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5322 | 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] | 5323 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5324 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5325 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5326 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5327 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5328 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5329 | break; |
| 5330 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5331 | } |
| 5332 | |
| 5333 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 5334 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5335 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5336 | 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] | 5337 | signed long long cl; |
| 5338 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5339 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5340 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5341 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5342 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5343 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5344 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5345 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5346 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5347 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5348 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5349 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5350 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5351 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5352 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5353 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5354 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5355 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5356 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5357 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5358 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5359 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5360 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5361 | } |
| 5362 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5363 | if (s->fe->comp || s->be->comp) |
| 5364 | select_compression_response_header(s, rep->buf); |
| 5365 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5366 | /* FIXME: we should also implement the multipart/byterange method. |
| 5367 | * For now on, we resort to close mode in this case (unknown length). |
| 5368 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5369 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5370 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5371 | /* end of job, return OK */ |
| 5372 | rep->analysers &= ~an_bit; |
| 5373 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5374 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5375 | return 1; |
| 5376 | } |
| 5377 | |
| 5378 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5379 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 5380 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 5381 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5382 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5383 | 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] | 5384 | { |
| 5385 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5386 | struct http_msg *msg = &txn->rsp; |
| 5387 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 5388 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5389 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5390 | 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] | 5391 | now_ms, __FUNCTION__, |
| 5392 | t, |
| 5393 | rep, |
| 5394 | rep->rex, rep->wex, |
| 5395 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5396 | rep->buf->i, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5397 | rep->analysers); |
| 5398 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5399 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5400 | return 0; |
| 5401 | |
| 5402 | rep->analysers &= ~an_bit; |
| 5403 | rep->analyse_exp = TICK_ETERNITY; |
| 5404 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5405 | /* Now we have to check if we need to modify the Connection header. |
| 5406 | * This is more difficult on the response than it is on the request, |
| 5407 | * because we can have two different HTTP versions and we don't know |
| 5408 | * how the client will interprete a response. For instance, let's say |
| 5409 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5410 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5411 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5412 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5413 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5414 | * indicating how a request MAY be understood by the client. In case |
| 5415 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5416 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5417 | * will fall back to explicit close. Note that we won't take risks with |
| 5418 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5419 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5420 | */ |
| 5421 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5422 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5423 | txn->status == 101)) { |
| 5424 | /* Either we've established an explicit tunnel, or we're |
| 5425 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5426 | * to understand the next protocols. We have to switch to tunnel |
| 5427 | * mode, so that we transfer the request and responses then let |
| 5428 | * this protocol pass unmodified. When we later implement specific |
| 5429 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5430 | * header which contains information about that protocol for |
| 5431 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5432 | */ |
| 5433 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5434 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5435 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5436 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 5437 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5438 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5439 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5440 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5441 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5442 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5443 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5444 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5445 | /* now adjust header transformations depending on current state */ |
| 5446 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5447 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5448 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5449 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5450 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5451 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5452 | else { /* SCL / KAL */ |
| 5453 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5454 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5455 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5456 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5457 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5458 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5459 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5460 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5461 | /* Some keep-alive responses are converted to Server-close if |
| 5462 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5463 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5464 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5465 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5466 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5467 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5468 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5469 | } |
| 5470 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5471 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5472 | /* |
| 5473 | * 3: we will have to evaluate the filters. |
| 5474 | * As opposed to version 1.2, now they will be evaluated in the |
| 5475 | * filters order and not in the header order. This means that |
| 5476 | * each filter has to be validated among all headers. |
| 5477 | * |
| 5478 | * Filters are tried with ->be first, then with ->fe if it is |
| 5479 | * different from ->be. |
| 5480 | */ |
| 5481 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5482 | cur_proxy = t->be; |
| 5483 | while (1) { |
| 5484 | struct proxy *rule_set = cur_proxy; |
| 5485 | |
| 5486 | /* try headers filters */ |
| 5487 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5488 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5489 | return_bad_resp: |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5490 | if (objt_server(t->target)) { |
| 5491 | objt_server(t->target)->counters.failed_resp++; |
| 5492 | health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5493 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5494 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5495 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5496 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5497 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5498 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5499 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5500 | stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5501 | if (!(t->flags & SN_ERR_MASK)) |
| 5502 | t->flags |= SN_ERR_PRXCOND; |
| 5503 | if (!(t->flags & SN_FINST_MASK)) |
| 5504 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 5505 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5506 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5507 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5508 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5509 | /* has the response been denied ? */ |
| 5510 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5511 | if (objt_server(t->target)) |
| 5512 | objt_server(t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5513 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5514 | t->be->be_counters.denied_resp++; |
| 5515 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5516 | if (t->listener->counters) |
| 5517 | t->listener->counters->denied_resp++; |
| 5518 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5519 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 5520 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5521 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5522 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5523 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5524 | if (txn->status < 200) |
| 5525 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5526 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 5527 | 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] | 5528 | ret = acl_pass(ret); |
| 5529 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5530 | ret = !ret; |
| 5531 | if (!ret) |
| 5532 | continue; |
| 5533 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5534 | 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] | 5535 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5536 | } |
| 5537 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5538 | /* check whether we're already working on the frontend */ |
| 5539 | if (cur_proxy == t->fe) |
| 5540 | break; |
| 5541 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5542 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5543 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5544 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5545 | * We may be facing a 100-continue response, in which case this |
| 5546 | * is not the right response, and we're waiting for the next one. |
| 5547 | * 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] | 5548 | * next one. |
| 5549 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5550 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5551 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5552 | msg->next -= channel_forward(rep, msg->next); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5553 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5554 | txn->status = 0; |
| 5555 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5556 | return 1; |
| 5557 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5558 | else if (unlikely(txn->status < 200)) |
| 5559 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5560 | |
| 5561 | /* we don't have any 1xx status code now */ |
| 5562 | |
| 5563 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5564 | * 4: check for server cookie. |
| 5565 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5566 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5567 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5568 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5569 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5570 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5571 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5572 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5573 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5574 | 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] | 5575 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5576 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5577 | /* |
| 5578 | * 6: add server cookie in the response if needed |
| 5579 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5580 | if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5581 | !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5582 | (!(t->flags & SN_DIRECT) || |
| 5583 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5584 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5585 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5586 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5587 | (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5588 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5589 | /* the server is known, it's not the one the client requested, or the |
| 5590 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5591 | * insert a set-cookie here, except if we want to insert only on POST |
| 5592 | * requests and this one isn't. Note that servers which don't have cookies |
| 5593 | * (eg: some backup servers) will return a full cookie removal request. |
| 5594 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5595 | if (!objt_server(t->target)->cookie) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5596 | chunk_printf(&trash, |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5597 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5598 | t->be->cookie_name); |
| 5599 | } |
| 5600 | else { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5601 | chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5602 | |
| 5603 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5604 | /* emit last_date, which is mandatory */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5605 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 5606 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5607 | trash.len += 5; |
| 5608 | |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5609 | if (t->be->cookie_maxlife) { |
| 5610 | /* emit first_date, which is either the original one or |
| 5611 | * the current date. |
| 5612 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5613 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5614 | s30tob64(txn->cookie_first_date ? |
| 5615 | txn->cookie_first_date >> 2 : |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5616 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5617 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5618 | } |
| 5619 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5620 | chunk_appendf(&trash, "; path=/"); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5621 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5622 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5623 | if (t->be->cookie_domain) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5624 | chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5625 | |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5626 | if (t->be->ck_opts & PR_CK_HTTPONLY) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5627 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5628 | |
| 5629 | if (t->be->ck_opts & PR_CK_SECURE) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5630 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5631 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5632 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5633 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5634 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5635 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5636 | if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT)) |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5637 | /* the server did not change, only the date was updated */ |
| 5638 | txn->flags |= TX_SCK_UPDATED; |
| 5639 | else |
| 5640 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5641 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5642 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5643 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5644 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5645 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5646 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5647 | if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5648 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5649 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5650 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5651 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5652 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5653 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5654 | } |
| 5655 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5656 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5657 | /* |
| 5658 | * 7: check if result will be cacheable with a cookie. |
| 5659 | * We'll block the response if security checks have caught |
| 5660 | * nasty things such as a cacheable cookie. |
| 5661 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5662 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5663 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5664 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5665 | |
| 5666 | /* we're in presence of a cacheable response containing |
| 5667 | * a set-cookie header. We'll block it as requested by |
| 5668 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5669 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5670 | if (objt_server(t->target)) |
| 5671 | objt_server(t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5672 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5673 | t->be->be_counters.denied_resp++; |
| 5674 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5675 | if (t->listener->counters) |
| 5676 | t->listener->counters->denied_resp++; |
| 5677 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5678 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5679 | t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>"); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5680 | send_log(t->be, LOG_ALERT, |
| 5681 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5682 | t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>"); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5683 | goto return_srv_prx_502; |
| 5684 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5685 | |
| 5686 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5687 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5688 | * If an "Upgrade" token is found, the header is left untouched in order |
| 5689 | * not to have to deal with some client bugs : some of them fail an upgrade |
| 5690 | * if anything but "Upgrade" is present in the Connection header. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5691 | */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5692 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 5693 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 5694 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5695 | unsigned int want_flags = 0; |
| 5696 | |
| 5697 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5698 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5699 | /* we want a keep-alive response here. Keep-alive header |
| 5700 | * required if either side is not 1.1. |
| 5701 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5702 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5703 | want_flags |= TX_CON_KAL_SET; |
| 5704 | } |
| 5705 | else { |
| 5706 | /* we want a close response here. Close header required if |
| 5707 | * the server is 1.1, regardless of the client. |
| 5708 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5709 | if (msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5710 | want_flags |= TX_CON_CLO_SET; |
| 5711 | } |
| 5712 | |
| 5713 | 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] | 5714 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5715 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5716 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5717 | skip_header_mangling: |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5718 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5719 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5720 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5721 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5722 | /************************************************************* |
| 5723 | * OK, that's finished for the headers. We have done what we * |
| 5724 | * could. Let's switch to the DATA state. * |
| 5725 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5726 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5727 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5728 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5729 | /* if the user wants to log as soon as possible, without counting |
| 5730 | * bytes from the server, then this is the right moment. We have |
| 5731 | * to temporarily assign bytes_out to log what we currently have. |
| 5732 | */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 5733 | if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5734 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5735 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 5736 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5737 | t->logs.bytes_out = 0; |
| 5738 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5739 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5740 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 5741 | * otherwise we would not let the client side wake up. |
| 5742 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5743 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5744 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5745 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5746 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5747 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5748 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5749 | /* This function is an analyser which forwards response body (including chunk |
| 5750 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5751 | * zero byte. The only situation where it must not be called is when we're in |
| 5752 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5753 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5754 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5755 | * 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] | 5756 | * 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] | 5757 | * bytes of pending data + the headers if not already done (between sol and sov). |
| 5758 | * 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] | 5759 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5760 | 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] | 5761 | { |
| 5762 | struct http_txn *txn = &s->txn; |
| 5763 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5764 | unsigned int bytes; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5765 | static struct buffer *tmpbuf = NULL; |
| 5766 | int compressing = 0; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5767 | int consumed_data = 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5768 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5769 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5770 | return 0; |
| 5771 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5772 | if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5773 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5774 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5775 | /* Output closed while we were sending data. We must abort and |
| 5776 | * wake the other side up. |
| 5777 | */ |
| 5778 | msg->msg_state = HTTP_MSG_ERROR; |
| 5779 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5780 | return 1; |
| 5781 | } |
| 5782 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5783 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5784 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5785 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5786 | /* this is the first time we need the compression buffer */ |
| 5787 | if (s->comp_algo != NULL && tmpbuf == NULL) { |
| 5788 | if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL) |
| 5789 | goto aborted_xfer; /* no memory */ |
| 5790 | } |
| 5791 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5792 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 5793 | /* we have msg->sov which points to the first byte of message body. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5794 | * rep->buf.p still points to the beginning of the message and msg->sol |
| 5795 | * is still null. We forward the headers, we don't need them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5796 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5797 | channel_forward(res, msg->sov); |
| 5798 | msg->next = 0; |
| 5799 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5800 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5801 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5802 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5803 | else |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5804 | msg->msg_state = HTTP_MSG_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5805 | } |
| 5806 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5807 | if (s->comp_algo != NULL) { |
| 5808 | int ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */ |
| 5809 | if (ret < 0) |
| 5810 | goto missing_data; /* not enough spaces in buffers */ |
| 5811 | compressing = 1; |
| 5812 | } |
| 5813 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5814 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5815 | http_silent_debug(__LINE__, s); |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5816 | /* we may have some data pending between sol and sov */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5817 | if (s->comp_algo == NULL) { |
| 5818 | bytes = msg->sov - msg->sol; |
| 5819 | if (msg->chunk_len || bytes) { |
| 5820 | msg->sol = msg->sov; |
| 5821 | msg->next -= bytes; /* will be forwarded */ |
| 5822 | msg->chunk_len += bytes; |
| 5823 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 5824 | } |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5825 | } |
| 5826 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5827 | if (msg->msg_state == HTTP_MSG_DATA) { |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5828 | int ret; |
| 5829 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5830 | /* must still forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5831 | if (compressing) { |
| 5832 | consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
| 5833 | if (ret < 0) |
| 5834 | goto aborted_xfer; |
| 5835 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5836 | |
| 5837 | if (res->to_forward || msg->chunk_len) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5838 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5839 | |
| 5840 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5841 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5842 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5843 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5844 | msg->msg_state = HTTP_MSG_DONE; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5845 | if (compressing && consumed_data) { |
| 5846 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 5847 | compressing = 0; |
| 5848 | } |
| 5849 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5850 | } |
| 5851 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5852 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5853 | * set ->sov and ->next to point to the body and switch to DATA or |
| 5854 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5855 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5856 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5857 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5858 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5859 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5860 | else if (ret < 0) { |
| 5861 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5862 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5863 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5864 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5865 | if (compressing) { |
| 5866 | if (likely(msg->chunk_len > 0)) { |
| 5867 | /* skipping data if we are in compression mode */ |
| 5868 | b_adv(res->buf, msg->next); |
| 5869 | msg->next = 0; |
| 5870 | msg->sov = 0; |
| 5871 | msg->sol = 0; |
| 5872 | } else { |
| 5873 | if (consumed_data) { |
| 5874 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 5875 | compressing = 0; |
| 5876 | } |
| 5877 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5878 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5879 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5880 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5881 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5882 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5883 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5884 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5885 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5886 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5887 | else if (ret < 0) { |
| 5888 | if (msg->err_pos >= 0) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5889 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5890 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5891 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5892 | /* skipping data in buffer for compression */ |
| 5893 | if (compressing) { |
| 5894 | b_adv(res->buf, msg->next); |
| 5895 | msg->next = 0; |
| 5896 | msg->sov = 0; |
| 5897 | msg->sol = 0; |
| 5898 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5899 | /* we're in MSG_CHUNK_SIZE now */ |
| 5900 | } |
| 5901 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5902 | int ret = http_forward_trailers(msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5903 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5904 | if (ret == 0) |
| 5905 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5906 | else if (ret < 0) { |
| 5907 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5908 | 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] | 5909 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5910 | } |
William Lallemand | 00bf1de | 2012-11-22 17:55:14 +0100 | [diff] [blame] | 5911 | if (s->comp_algo != NULL) { |
| 5912 | /* forwarding trailers */ |
| 5913 | channel_forward(res, msg->next); |
| 5914 | msg->next = 0; |
| 5915 | } |
| 5916 | /* we're in HTTP_MSG_DONE now */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5917 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5918 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5919 | int old_state = msg->msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5920 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5921 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5922 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5923 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5924 | channel_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5925 | if (http_resync_states(s)) { |
| 5926 | http_silent_debug(__LINE__, s); |
| 5927 | /* some state changes occurred, maybe the analyser |
| 5928 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5929 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5930 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5931 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5932 | /* response errors are most likely due to |
| 5933 | * the client aborting the transfer. |
| 5934 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5935 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5936 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5937 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5938 | 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] | 5939 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5940 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5941 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5942 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5943 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5944 | } |
| 5945 | } |
| 5946 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5947 | missing_data: |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5948 | if (compressing && consumed_data) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5949 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 0); |
| 5950 | compressing = 0; |
| 5951 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5952 | |
| 5953 | if (res->flags & CF_SHUTW) |
| 5954 | goto aborted_xfer; |
| 5955 | |
| 5956 | /* stop waiting for data if the input is closed before the end. If the |
| 5957 | * client side was already closed, it means that the client has aborted, |
| 5958 | * so we don't want to count this as a server abort. Otherwise it's a |
| 5959 | * server abort. |
| 5960 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5961 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5962 | if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR)) |
| 5963 | goto aborted_xfer; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5964 | if (!(s->flags & SN_ERR_MASK)) |
| 5965 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5966 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5967 | if (objt_server(s->target)) |
| 5968 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5969 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5970 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5971 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5972 | /* 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] | 5973 | if (!s->req->analysers) |
| 5974 | goto return_bad_res; |
| 5975 | |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5976 | /* forward any data pending between sol and sov */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5977 | if (s->comp_algo == NULL) { |
| 5978 | bytes = msg->sov - msg->sol; |
| 5979 | if (msg->chunk_len || bytes) { |
| 5980 | msg->sol = msg->sov; |
| 5981 | msg->next -= bytes; /* will be forwarded */ |
| 5982 | msg->chunk_len += bytes; |
| 5983 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 5984 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5985 | } |
| 5986 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5987 | /* 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] | 5988 | * 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] | 5989 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 5990 | * close. |
| 5991 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 5992 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5993 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5994 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5995 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5996 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5997 | /* 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] | 5998 | * 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] | 5999 | * 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] | 6000 | * modes are already handled by the stream sock layer. We must not do |
| 6001 | * this in content-length mode because it could present the MSG_MORE |
| 6002 | * flag with the last block of forwarded data, which would cause an |
| 6003 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6004 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6005 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6006 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6007 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6008 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6009 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6010 | return 0; |
| 6011 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6012 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6013 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6014 | if (objt_server(s->target)) |
| 6015 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6016 | |
| 6017 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6018 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6019 | /* don't send any error message as we're in the body */ |
| 6020 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6021 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6022 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6023 | if (objt_server(s->target)) |
| 6024 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6025 | |
| 6026 | if (!(s->flags & SN_ERR_MASK)) |
| 6027 | s->flags |= SN_ERR_PRXCOND; |
| 6028 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6029 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6030 | return 0; |
| 6031 | |
| 6032 | aborted_xfer: |
| 6033 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6034 | /* don't send any error message as we're in the body */ |
| 6035 | stream_int_retnclose(res->cons, NULL); |
| 6036 | res->analysers = 0; |
| 6037 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 6038 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6039 | s->fe->fe_counters.cli_aborts++; |
| 6040 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6041 | if (objt_server(s->target)) |
| 6042 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6043 | |
| 6044 | if (!(s->flags & SN_ERR_MASK)) |
| 6045 | s->flags |= SN_ERR_CLICL; |
| 6046 | if (!(s->flags & SN_FINST_MASK)) |
| 6047 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6048 | return 0; |
| 6049 | } |
| 6050 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6051 | /* Iterate the same filter through all request headers. |
| 6052 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6053 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6054 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6055 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6056 | 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] | 6057 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6058 | char term; |
| 6059 | char *cur_ptr, *cur_end, *cur_next; |
| 6060 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6061 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6062 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6063 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6064 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6065 | last_hdr = 0; |
| 6066 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6067 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6068 | old_idx = 0; |
| 6069 | |
| 6070 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6071 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6072 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6073 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6074 | (exp->action == ACT_ALLOW || |
| 6075 | exp->action == ACT_DENY || |
| 6076 | exp->action == ACT_TARPIT)) |
| 6077 | return 0; |
| 6078 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6079 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6080 | if (!cur_idx) |
| 6081 | break; |
| 6082 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6083 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6084 | cur_ptr = cur_next; |
| 6085 | cur_end = cur_ptr + cur_hdr->len; |
| 6086 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6087 | |
| 6088 | /* Now we have one header between cur_ptr and cur_end, |
| 6089 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6090 | */ |
| 6091 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6092 | /* The annoying part is that pattern matching needs |
| 6093 | * that we modify the contents to null-terminate all |
| 6094 | * strings before testing them. |
| 6095 | */ |
| 6096 | |
| 6097 | term = *cur_end; |
| 6098 | *cur_end = '\0'; |
| 6099 | |
| 6100 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6101 | switch (exp->action) { |
| 6102 | case ACT_SETBE: |
| 6103 | /* It is not possible to jump a second time. |
| 6104 | * FIXME: should we return an HTTP/500 here so that |
| 6105 | * the admin knows there's a problem ? |
| 6106 | */ |
| 6107 | if (t->be != t->fe) |
| 6108 | break; |
| 6109 | |
| 6110 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 6111 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6112 | last_hdr = 1; |
| 6113 | break; |
| 6114 | |
| 6115 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6116 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6117 | last_hdr = 1; |
| 6118 | break; |
| 6119 | |
| 6120 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6121 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6122 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6123 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6124 | t->fe->fe_counters.denied_req++; |
| 6125 | if (t->fe != t->be) |
| 6126 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6127 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 6128 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6129 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6130 | break; |
| 6131 | |
| 6132 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6133 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6134 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6135 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6136 | t->fe->fe_counters.denied_req++; |
| 6137 | if (t->fe != t->be) |
| 6138 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6139 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 6140 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6141 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6142 | break; |
| 6143 | |
| 6144 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6145 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6146 | delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6147 | /* FIXME: if the user adds a newline in the replacement, the |
| 6148 | * index will not be recalculated for now, and the new line |
| 6149 | * will not be counted as a new header. |
| 6150 | */ |
| 6151 | |
| 6152 | cur_end += delta; |
| 6153 | cur_next += delta; |
| 6154 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6155 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6156 | break; |
| 6157 | |
| 6158 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6159 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6160 | cur_next += delta; |
| 6161 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6162 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6163 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6164 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6165 | cur_hdr->len = 0; |
| 6166 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6167 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6168 | break; |
| 6169 | |
| 6170 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6171 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6172 | if (cur_end) |
| 6173 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6174 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6175 | /* keep the link from this header to next one in case of later |
| 6176 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6177 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6178 | old_idx = cur_idx; |
| 6179 | } |
| 6180 | return 0; |
| 6181 | } |
| 6182 | |
| 6183 | |
| 6184 | /* Apply the filter to the request line. |
| 6185 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6186 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6187 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6188 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6189 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6190 | 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] | 6191 | { |
| 6192 | char term; |
| 6193 | char *cur_ptr, *cur_end; |
| 6194 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6195 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6196 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6197 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6198 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6199 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6200 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6201 | (exp->action == ACT_ALLOW || |
| 6202 | exp->action == ACT_DENY || |
| 6203 | exp->action == ACT_TARPIT)) |
| 6204 | return 0; |
| 6205 | else if (exp->action == ACT_REMOVE) |
| 6206 | return 0; |
| 6207 | |
| 6208 | done = 0; |
| 6209 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6210 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6211 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6212 | |
| 6213 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6214 | |
| 6215 | /* The annoying part is that pattern matching needs |
| 6216 | * that we modify the contents to null-terminate all |
| 6217 | * strings before testing them. |
| 6218 | */ |
| 6219 | |
| 6220 | term = *cur_end; |
| 6221 | *cur_end = '\0'; |
| 6222 | |
| 6223 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6224 | switch (exp->action) { |
| 6225 | case ACT_SETBE: |
| 6226 | /* It is not possible to jump a second time. |
| 6227 | * FIXME: should we return an HTTP/500 here so that |
| 6228 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6229 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6230 | if (t->be != t->fe) |
| 6231 | break; |
| 6232 | |
| 6233 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 6234 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6235 | done = 1; |
| 6236 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6237 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6238 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6239 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6240 | done = 1; |
| 6241 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6242 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6243 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6244 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6245 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6246 | t->fe->fe_counters.denied_req++; |
| 6247 | if (t->fe != t->be) |
| 6248 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6249 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 6250 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6251 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6252 | done = 1; |
| 6253 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6254 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6255 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6256 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6257 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6258 | t->fe->fe_counters.denied_req++; |
| 6259 | if (t->fe != t->be) |
| 6260 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6261 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 6262 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6263 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6264 | done = 1; |
| 6265 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6266 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6267 | case ACT_REPLACE: |
| 6268 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6269 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6270 | delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6271 | /* FIXME: if the user adds a newline in the replacement, the |
| 6272 | * index will not be recalculated for now, and the new line |
| 6273 | * will not be counted as a new header. |
| 6274 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6275 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6276 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6277 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6278 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6279 | HTTP_MSG_RQMETH, |
| 6280 | cur_ptr, cur_end + 1, |
| 6281 | NULL, NULL); |
| 6282 | if (unlikely(!cur_end)) |
| 6283 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6284 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6285 | /* we have a full request and we know that we have either a CR |
| 6286 | * or an LF at <ptr>. |
| 6287 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6288 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6289 | 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] | 6290 | /* there is no point trying this regex on headers */ |
| 6291 | return 1; |
| 6292 | } |
| 6293 | } |
| 6294 | *cur_end = term; /* restore the string terminator */ |
| 6295 | return done; |
| 6296 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6297 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6298 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6299 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6300 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6301 | * 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] | 6302 | * 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] | 6303 | * unparsable request. Since it can manage the switch to another backend, it |
| 6304 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6305 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6306 | 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] | 6307 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6308 | struct http_txn *txn = &s->txn; |
| 6309 | struct hdr_exp *exp; |
| 6310 | |
| 6311 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6312 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6313 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6314 | /* |
| 6315 | * The interleaving of transformations and verdicts |
| 6316 | * makes it difficult to decide to continue or stop |
| 6317 | * the evaluation. |
| 6318 | */ |
| 6319 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6320 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6321 | break; |
| 6322 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6323 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6324 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6325 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6326 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6327 | |
| 6328 | /* if this filter had a condition, evaluate it now and skip to |
| 6329 | * next filter if the condition does not match. |
| 6330 | */ |
| 6331 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 6332 | 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] | 6333 | ret = acl_pass(ret); |
| 6334 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6335 | ret = !ret; |
| 6336 | |
| 6337 | if (!ret) |
| 6338 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6339 | } |
| 6340 | |
| 6341 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6342 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6343 | if (unlikely(ret < 0)) |
| 6344 | return -1; |
| 6345 | |
| 6346 | if (likely(ret == 0)) { |
| 6347 | /* The filter did not match the request, it can be |
| 6348 | * iterated through all headers. |
| 6349 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6350 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6351 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6352 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6353 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6354 | } |
| 6355 | |
| 6356 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6357 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6358 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6359 | * Try to retrieve the server associated to the appsession. |
| 6360 | * If the server is found, it's assigned to the session. |
| 6361 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6362 | 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] | 6363 | struct http_txn *txn = &t->txn; |
| 6364 | appsess *asession = NULL; |
| 6365 | char *sessid_temp = NULL; |
| 6366 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6367 | if (len > t->be->appsession_len) { |
| 6368 | len = t->be->appsession_len; |
| 6369 | } |
| 6370 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6371 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 6372 | /* 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] | 6373 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6374 | /* 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] | 6375 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6376 | } |
| 6377 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6378 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6379 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6380 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6381 | return; |
| 6382 | } |
| 6383 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6384 | memcpy(txn->sessid, buf, len); |
| 6385 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6386 | } |
| 6387 | |
| 6388 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 6389 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6390 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6391 | return; |
| 6392 | } |
| 6393 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6394 | memcpy(sessid_temp, buf, len); |
| 6395 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6396 | |
| 6397 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 6398 | /* free previously allocated memory */ |
| 6399 | pool_free2(apools.sessid, sessid_temp); |
| 6400 | |
| 6401 | if (asession != NULL) { |
| 6402 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6403 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 6404 | asession->request_count++; |
| 6405 | |
| 6406 | if (asession->serverid != NULL) { |
| 6407 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6408 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6409 | while (srv) { |
| 6410 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 6411 | if ((srv->state & SRV_RUNNING) || |
| 6412 | (t->be->options & PR_O_PERSIST) || |
| 6413 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6414 | /* we found the server and it's usable */ |
| 6415 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 6416 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6417 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6418 | t->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 6419 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6420 | break; |
| 6421 | } else { |
| 6422 | txn->flags &= ~TX_CK_MASK; |
| 6423 | txn->flags |= TX_CK_DOWN; |
| 6424 | } |
| 6425 | } |
| 6426 | srv = srv->next; |
| 6427 | } |
| 6428 | } |
| 6429 | } |
| 6430 | } |
| 6431 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6432 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6433 | * same way as with headers above except that the semi-colon also ends a token. |
| 6434 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6435 | * return a valid result. |
| 6436 | */ |
| 6437 | char *find_cookie_value_end(char *s, const char *e) |
| 6438 | { |
| 6439 | int quoted, qdpair; |
| 6440 | |
| 6441 | quoted = qdpair = 0; |
| 6442 | for (; s < e; s++) { |
| 6443 | if (qdpair) qdpair = 0; |
| 6444 | else if (quoted) { |
| 6445 | if (*s == '\\') qdpair = 1; |
| 6446 | else if (*s == '"') quoted = 0; |
| 6447 | } |
| 6448 | else if (*s == '"') quoted = 1; |
| 6449 | else if (*s == ',' || *s == ';') return s; |
| 6450 | } |
| 6451 | return s; |
| 6452 | } |
| 6453 | |
| 6454 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6455 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6456 | * the first delimiter is updated if required. The function tries as much as |
| 6457 | * possible to respect the following principles : |
| 6458 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6459 | * colon, in which case <next> is simply removed |
| 6460 | * - set exactly one space character after the new first delimiter, unless |
| 6461 | * there are not enough characters in the block being moved to do so. |
| 6462 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6463 | * one. |
| 6464 | * |
| 6465 | * It is the caller's responsibility to ensure that : |
| 6466 | * - <from> points to a valid delimiter or the colon ; |
| 6467 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6468 | * - there are non-space chars before <from> ; |
| 6469 | * - there is a CR/LF at or after <next>. |
| 6470 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6471 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6472 | { |
| 6473 | char *prev = *from; |
| 6474 | |
| 6475 | if (*prev == ':') { |
| 6476 | /* We're removing the first value, preserve the colon and add a |
| 6477 | * space if possible. |
| 6478 | */ |
| 6479 | if (!http_is_crlf[(unsigned char)*next]) |
| 6480 | next++; |
| 6481 | prev++; |
| 6482 | if (prev < next) |
| 6483 | *prev++ = ' '; |
| 6484 | |
| 6485 | while (http_is_spht[(unsigned char)*next]) |
| 6486 | next++; |
| 6487 | } else { |
| 6488 | /* Remove useless spaces before the old delimiter. */ |
| 6489 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 6490 | prev--; |
| 6491 | *from = prev; |
| 6492 | |
| 6493 | /* copy the delimiter and if possible a space if we're |
| 6494 | * not at the end of the line. |
| 6495 | */ |
| 6496 | if (!http_is_crlf[(unsigned char)*next]) { |
| 6497 | *prev++ = *next++; |
| 6498 | if (prev + 1 < next) |
| 6499 | *prev++ = ' '; |
| 6500 | while (http_is_spht[(unsigned char)*next]) |
| 6501 | next++; |
| 6502 | } |
| 6503 | } |
| 6504 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6505 | } |
| 6506 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6507 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6508 | * 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] | 6509 | * desirable to call it only when needed. This code is quite complex because |
| 6510 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6511 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6512 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6513 | void manage_client_side_cookies(struct session *t, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6514 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6515 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6516 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6517 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6518 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6519 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6520 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6521 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6522 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6523 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6524 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6525 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6526 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6527 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6528 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6529 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6530 | hdr_beg = hdr_next; |
| 6531 | hdr_end = hdr_beg + cur_hdr->len; |
| 6532 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6533 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6534 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6535 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6536 | * "Cookie:" headers. |
| 6537 | */ |
| 6538 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6539 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6540 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6541 | old_idx = cur_idx; |
| 6542 | continue; |
| 6543 | } |
| 6544 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6545 | del_from = NULL; /* nothing to be deleted */ |
| 6546 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6547 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6548 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6549 | * attributes whose name begin with a '$', and associate them with |
| 6550 | * the right cookie, if we want to delete this cookie. |
| 6551 | * So there are 3 cases for each cookie read : |
| 6552 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6553 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6554 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6555 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6556 | * "special" cookie. |
| 6557 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6558 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6559 | * *MUST* delete it. |
| 6560 | * |
| 6561 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6562 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6563 | * the RFC explicitly allows spaces before it, and not within the |
| 6564 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6565 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6566 | * after the equal sign too, resulting in some (rare) buggy |
| 6567 | * implementations trying to do that. So let's do what servers do. |
| 6568 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6569 | * allowed quoted strings in values, with any possible character |
| 6570 | * after a backslash, including control chars and delimitors, which |
| 6571 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6572 | * within values even without quotes. |
| 6573 | * |
| 6574 | * We have to keep multiple pointers in order to support cookie |
| 6575 | * removal at the beginning, middle or end of header without |
| 6576 | * corrupting the header. All of these headers are valid : |
| 6577 | * |
| 6578 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6579 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6580 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6581 | * | | | | | | | | | |
| 6582 | * | | | | | | | | hdr_end <--+ |
| 6583 | * | | | | | | | +--> next |
| 6584 | * | | | | | | +----> val_end |
| 6585 | * | | | | | +-----------> val_beg |
| 6586 | * | | | | +--------------> equal |
| 6587 | * | | | +----------------> att_end |
| 6588 | * | | +---------------------> att_beg |
| 6589 | * | +--------------------------> prev |
| 6590 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6591 | */ |
| 6592 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6593 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6594 | /* Iterate through all cookies on this line */ |
| 6595 | |
| 6596 | /* find att_beg */ |
| 6597 | att_beg = prev + 1; |
| 6598 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6599 | att_beg++; |
| 6600 | |
| 6601 | /* find att_end : this is the first character after the last non |
| 6602 | * space before the equal. It may be equal to hdr_end. |
| 6603 | */ |
| 6604 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6605 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6606 | while (equal < hdr_end) { |
| 6607 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6608 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6609 | if (http_is_spht[(unsigned char)*equal++]) |
| 6610 | continue; |
| 6611 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6612 | } |
| 6613 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6614 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6615 | * is between <att_beg> and <equal>, both may be identical. |
| 6616 | */ |
| 6617 | |
| 6618 | /* look for end of cookie if there is an equal sign */ |
| 6619 | if (equal < hdr_end && *equal == '=') { |
| 6620 | /* look for the beginning of the value */ |
| 6621 | val_beg = equal + 1; |
| 6622 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6623 | val_beg++; |
| 6624 | |
| 6625 | /* find the end of the value, respecting quotes */ |
| 6626 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6627 | |
| 6628 | /* make val_end point to the first white space or delimitor after the value */ |
| 6629 | val_end = next; |
| 6630 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6631 | val_end--; |
| 6632 | } else { |
| 6633 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6634 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6635 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6636 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6637 | * they will automatically be removed if a header before them is removed, |
| 6638 | * since they're supposed to be linked together. |
| 6639 | */ |
| 6640 | if (*att_beg == '$') |
| 6641 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6642 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6643 | /* Ignore cookies with no equal sign */ |
| 6644 | if (equal == next) { |
| 6645 | /* This is not our cookie, so we must preserve it. But if we already |
| 6646 | * scheduled another cookie for removal, we cannot remove the |
| 6647 | * complete header, but we can remove the previous block itself. |
| 6648 | */ |
| 6649 | preserve_hdr = 1; |
| 6650 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6651 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6652 | val_end += delta; |
| 6653 | next += delta; |
| 6654 | hdr_end += delta; |
| 6655 | hdr_next += delta; |
| 6656 | cur_hdr->len += delta; |
| 6657 | http_msg_move_end(&txn->req, delta); |
| 6658 | prev = del_from; |
| 6659 | del_from = NULL; |
| 6660 | } |
| 6661 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6662 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6663 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6664 | /* if there are spaces around the equal sign, we need to |
| 6665 | * strip them otherwise we'll get trouble for cookie captures, |
| 6666 | * or even for rewrites. Since this happens extremely rarely, |
| 6667 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6668 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6669 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6670 | int stripped_before = 0; |
| 6671 | int stripped_after = 0; |
| 6672 | |
| 6673 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6674 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6675 | equal += stripped_before; |
| 6676 | val_beg += stripped_before; |
| 6677 | } |
| 6678 | |
| 6679 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6680 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6681 | val_beg += stripped_after; |
| 6682 | stripped_before += stripped_after; |
| 6683 | } |
| 6684 | |
| 6685 | val_end += stripped_before; |
| 6686 | next += stripped_before; |
| 6687 | hdr_end += stripped_before; |
| 6688 | hdr_next += stripped_before; |
| 6689 | cur_hdr->len += stripped_before; |
| 6690 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6691 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6692 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6693 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6694 | /* First, let's see if we want to capture this cookie. We check |
| 6695 | * that we don't already have a client side cookie, because we |
| 6696 | * can only capture one. Also as an optimisation, we ignore |
| 6697 | * cookies shorter than the declared name. |
| 6698 | */ |
| 6699 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6700 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6701 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6702 | int log_len = val_end - att_beg; |
| 6703 | |
| 6704 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6705 | Alert("HTTP logging : out of memory.\n"); |
| 6706 | } else { |
| 6707 | if (log_len > t->fe->capture_len) |
| 6708 | log_len = t->fe->capture_len; |
| 6709 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6710 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6711 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6712 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6713 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6714 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6715 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6716 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6717 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6718 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6719 | * For cookies in prefix mode, the form is : |
| 6720 | * |
| 6721 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6722 | */ |
| 6723 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6724 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6725 | struct server *srv = t->be->srv; |
| 6726 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6727 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6728 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6729 | * have the server ID between val_beg and delim, and the original cookie between |
| 6730 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6731 | * |
| 6732 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6733 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6734 | * | || || | |+-> next |
| 6735 | * | || || | +--> val_end |
| 6736 | * | || || +---------> delim |
| 6737 | * | || |+------------> val_beg |
| 6738 | * | || +-------------> att_end = equal |
| 6739 | * | |+-----------------> att_beg |
| 6740 | * | +------------------> prev |
| 6741 | * +-------------------------> hdr_beg |
| 6742 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6743 | |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6744 | if (t->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6745 | for (delim = val_beg; delim < val_end; delim++) |
| 6746 | if (*delim == COOKIE_DELIM) |
| 6747 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6748 | } else { |
| 6749 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6750 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6751 | /* Now check if the cookie contains a date field, which would |
| 6752 | * appear after a vertical bar ('|') just after the server name |
| 6753 | * and before the delimiter. |
| 6754 | */ |
| 6755 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6756 | if (vbar1) { |
| 6757 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6758 | * right is the last seen date. It is a base64 encoded |
| 6759 | * 30-bit value representing the UNIX date since the |
| 6760 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6761 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6762 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6763 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6764 | if (val_end - vbar1 >= 5) { |
| 6765 | val = b64tos30(vbar1); |
| 6766 | if (val > 0) |
| 6767 | txn->cookie_last_date = val << 2; |
| 6768 | } |
| 6769 | /* look for a second vertical bar */ |
| 6770 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6771 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6772 | val = b64tos30(vbar1 + 1); |
| 6773 | if (val > 0) |
| 6774 | txn->cookie_first_date = val << 2; |
| 6775 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6776 | } |
| 6777 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6778 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6779 | /* if the cookie has an expiration date and the proxy wants to check |
| 6780 | * it, then we do that now. We first check if the cookie is too old, |
| 6781 | * then only if it has expired. We detect strict overflow because the |
| 6782 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6783 | * in the future are ignored if their offset is beyond one day. This |
| 6784 | * allows an admin to fix timezone issues without expiring everyone |
| 6785 | * and at the same time avoids keeping unwanted side effects for too |
| 6786 | * long. |
| 6787 | */ |
| 6788 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6789 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 6790 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6791 | txn->flags &= ~TX_CK_MASK; |
| 6792 | txn->flags |= TX_CK_OLD; |
| 6793 | delim = val_beg; // let's pretend we have not found the cookie |
| 6794 | txn->cookie_first_date = 0; |
| 6795 | txn->cookie_last_date = 0; |
| 6796 | } |
| 6797 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6798 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 6799 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6800 | txn->flags &= ~TX_CK_MASK; |
| 6801 | txn->flags |= TX_CK_EXPIRED; |
| 6802 | delim = val_beg; // let's pretend we have not found the cookie |
| 6803 | txn->cookie_first_date = 0; |
| 6804 | txn->cookie_last_date = 0; |
| 6805 | } |
| 6806 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6807 | /* Here, we'll look for the first running server which supports the cookie. |
| 6808 | * This allows to share a same cookie between several servers, for example |
| 6809 | * to dedicate backup servers to specific servers only. |
| 6810 | * However, to prevent clients from sticking to cookie-less backup server |
| 6811 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6812 | * empty cookies and mark them as invalid. |
| 6813 | * The same behaviour is applied when persistence must be ignored. |
| 6814 | */ |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 6815 | if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6816 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6817 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6818 | while (srv) { |
| 6819 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6820 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 6821 | if ((srv->state & SRV_RUNNING) || |
| 6822 | (t->be->options & PR_O_PERSIST) || |
| 6823 | (t->flags & SN_FORCE_PRST)) { |
| 6824 | /* we found the server and we can use it */ |
| 6825 | txn->flags &= ~TX_CK_MASK; |
| 6826 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 6827 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6828 | t->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6829 | break; |
| 6830 | } else { |
| 6831 | /* we found a server, but it's down, |
| 6832 | * mark it as such and go on in case |
| 6833 | * another one is available. |
| 6834 | */ |
| 6835 | txn->flags &= ~TX_CK_MASK; |
| 6836 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6837 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6838 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6839 | srv = srv->next; |
| 6840 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6841 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6842 | 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] | 6843 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6844 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6845 | if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
| 6846 | txn->flags |= TX_CK_UNUSED; |
| 6847 | else |
| 6848 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6849 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6850 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6851 | /* depending on the cookie mode, we may have to either : |
| 6852 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6853 | * the server never sees it ; |
| 6854 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6855 | * application cookie so that it does not get accidentely removed later, |
| 6856 | * if we're in cookie prefix mode |
| 6857 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6858 | if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6859 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6860 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6861 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6862 | val_end += delta; |
| 6863 | next += delta; |
| 6864 | hdr_end += delta; |
| 6865 | hdr_next += delta; |
| 6866 | cur_hdr->len += delta; |
| 6867 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6868 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6869 | del_from = NULL; |
| 6870 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6871 | } |
| 6872 | else if (del_from == NULL && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6873 | (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] | 6874 | del_from = prev; |
| 6875 | } |
| 6876 | } else { |
| 6877 | /* This is not our cookie, so we must preserve it. But if we already |
| 6878 | * scheduled another cookie for removal, we cannot remove the |
| 6879 | * complete header, but we can remove the previous block itself. |
| 6880 | */ |
| 6881 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6882 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6883 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6884 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6885 | if (att_beg >= del_from) |
| 6886 | att_beg += delta; |
| 6887 | if (att_end >= del_from) |
| 6888 | att_end += delta; |
| 6889 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6890 | val_end += delta; |
| 6891 | next += delta; |
| 6892 | hdr_end += delta; |
| 6893 | hdr_next += delta; |
| 6894 | cur_hdr->len += delta; |
| 6895 | http_msg_move_end(&txn->req, delta); |
| 6896 | prev = del_from; |
| 6897 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6898 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6899 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6900 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6901 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 6902 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 6903 | int cmp_len, value_len; |
| 6904 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6905 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6906 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6907 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6908 | value_begin = att_beg + t->be->appsession_name_len; |
| 6909 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 6910 | } else { |
| 6911 | cmp_len = att_end - att_beg; |
| 6912 | value_begin = val_beg; |
| 6913 | value_len = val_end - val_beg; |
| 6914 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6915 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6916 | /* let's see if the cookie is our appcookie */ |
| 6917 | if (cmp_len == t->be->appsession_name_len && |
| 6918 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 6919 | manage_client_side_appsession(t, value_begin, value_len); |
| 6920 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6921 | } |
| 6922 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6923 | /* continue with next cookie on this header line */ |
| 6924 | att_beg = next; |
| 6925 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6926 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6927 | /* There are no more cookies on this line. |
| 6928 | * We may still have one (or several) marked for deletion at the |
| 6929 | * end of the line. We must do this now in two ways : |
| 6930 | * - if some cookies must be preserved, we only delete from the |
| 6931 | * mark to the end of line ; |
| 6932 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6933 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6934 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6935 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6936 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6937 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6938 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6939 | cur_hdr->len += delta; |
| 6940 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6941 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6942 | |
| 6943 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6944 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6945 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6946 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6947 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6948 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6949 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6950 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6951 | } |
| 6952 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6953 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6954 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6955 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6956 | } |
| 6957 | |
| 6958 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6959 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6960 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6961 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6962 | 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] | 6963 | { |
| 6964 | char term; |
| 6965 | char *cur_ptr, *cur_end, *cur_next; |
| 6966 | int cur_idx, old_idx, last_hdr; |
| 6967 | struct http_txn *txn = &t->txn; |
| 6968 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6969 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6970 | |
| 6971 | last_hdr = 0; |
| 6972 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6973 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6974 | old_idx = 0; |
| 6975 | |
| 6976 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6977 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6978 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6979 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6980 | (exp->action == ACT_ALLOW || |
| 6981 | exp->action == ACT_DENY)) |
| 6982 | return 0; |
| 6983 | |
| 6984 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6985 | if (!cur_idx) |
| 6986 | break; |
| 6987 | |
| 6988 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6989 | cur_ptr = cur_next; |
| 6990 | cur_end = cur_ptr + cur_hdr->len; |
| 6991 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6992 | |
| 6993 | /* Now we have one header between cur_ptr and cur_end, |
| 6994 | * and the next header starts at cur_next. |
| 6995 | */ |
| 6996 | |
| 6997 | /* The annoying part is that pattern matching needs |
| 6998 | * that we modify the contents to null-terminate all |
| 6999 | * strings before testing them. |
| 7000 | */ |
| 7001 | |
| 7002 | term = *cur_end; |
| 7003 | *cur_end = '\0'; |
| 7004 | |
| 7005 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 7006 | switch (exp->action) { |
| 7007 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7008 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7009 | last_hdr = 1; |
| 7010 | break; |
| 7011 | |
| 7012 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7013 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7014 | last_hdr = 1; |
| 7015 | break; |
| 7016 | |
| 7017 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7018 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 7019 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7020 | /* FIXME: if the user adds a newline in the replacement, the |
| 7021 | * index will not be recalculated for now, and the new line |
| 7022 | * will not be counted as a new header. |
| 7023 | */ |
| 7024 | |
| 7025 | cur_end += delta; |
| 7026 | cur_next += delta; |
| 7027 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7028 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7029 | break; |
| 7030 | |
| 7031 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7032 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7033 | cur_next += delta; |
| 7034 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7035 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7036 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7037 | txn->hdr_idx.used--; |
| 7038 | cur_hdr->len = 0; |
| 7039 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7040 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7041 | break; |
| 7042 | |
| 7043 | } |
| 7044 | } |
| 7045 | if (cur_end) |
| 7046 | *cur_end = term; /* restore the string terminator */ |
| 7047 | |
| 7048 | /* keep the link from this header to next one in case of later |
| 7049 | * removal of next header. |
| 7050 | */ |
| 7051 | old_idx = cur_idx; |
| 7052 | } |
| 7053 | return 0; |
| 7054 | } |
| 7055 | |
| 7056 | |
| 7057 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7058 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7059 | * or -1 if a replacement resulted in an invalid status line. |
| 7060 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7061 | 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] | 7062 | { |
| 7063 | char term; |
| 7064 | char *cur_ptr, *cur_end; |
| 7065 | int done; |
| 7066 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7067 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7068 | |
| 7069 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7070 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7071 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7072 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7073 | (exp->action == ACT_ALLOW || |
| 7074 | exp->action == ACT_DENY)) |
| 7075 | return 0; |
| 7076 | else if (exp->action == ACT_REMOVE) |
| 7077 | return 0; |
| 7078 | |
| 7079 | done = 0; |
| 7080 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7081 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7082 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7083 | |
| 7084 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7085 | |
| 7086 | /* The annoying part is that pattern matching needs |
| 7087 | * that we modify the contents to null-terminate all |
| 7088 | * strings before testing them. |
| 7089 | */ |
| 7090 | |
| 7091 | term = *cur_end; |
| 7092 | *cur_end = '\0'; |
| 7093 | |
| 7094 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 7095 | switch (exp->action) { |
| 7096 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7097 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7098 | done = 1; |
| 7099 | break; |
| 7100 | |
| 7101 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7102 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7103 | done = 1; |
| 7104 | break; |
| 7105 | |
| 7106 | case ACT_REPLACE: |
| 7107 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7108 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 7109 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7110 | /* FIXME: if the user adds a newline in the replacement, the |
| 7111 | * index will not be recalculated for now, and the new line |
| 7112 | * will not be counted as a new header. |
| 7113 | */ |
| 7114 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7115 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7116 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7117 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7118 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7119 | cur_ptr, cur_end + 1, |
| 7120 | NULL, NULL); |
| 7121 | if (unlikely(!cur_end)) |
| 7122 | return -1; |
| 7123 | |
| 7124 | /* we have a full respnse and we know that we have either a CR |
| 7125 | * or an LF at <ptr>. |
| 7126 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7127 | 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] | 7128 | 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] | 7129 | /* there is no point trying this regex on headers */ |
| 7130 | return 1; |
| 7131 | } |
| 7132 | } |
| 7133 | *cur_end = term; /* restore the string terminator */ |
| 7134 | return done; |
| 7135 | } |
| 7136 | |
| 7137 | |
| 7138 | |
| 7139 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7140 | * 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] | 7141 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7142 | * unparsable response. |
| 7143 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7144 | 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] | 7145 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7146 | struct http_txn *txn = &s->txn; |
| 7147 | struct hdr_exp *exp; |
| 7148 | |
| 7149 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7150 | int ret; |
| 7151 | |
| 7152 | /* |
| 7153 | * The interleaving of transformations and verdicts |
| 7154 | * makes it difficult to decide to continue or stop |
| 7155 | * the evaluation. |
| 7156 | */ |
| 7157 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7158 | if (txn->flags & TX_SVDENY) |
| 7159 | break; |
| 7160 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7161 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7162 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7163 | exp->action == ACT_PASS)) { |
| 7164 | exp = exp->next; |
| 7165 | continue; |
| 7166 | } |
| 7167 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7168 | /* if this filter had a condition, evaluate it now and skip to |
| 7169 | * next filter if the condition does not match. |
| 7170 | */ |
| 7171 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7172 | 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] | 7173 | ret = acl_pass(ret); |
| 7174 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7175 | ret = !ret; |
| 7176 | if (!ret) |
| 7177 | continue; |
| 7178 | } |
| 7179 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7180 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7181 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7182 | if (unlikely(ret < 0)) |
| 7183 | return -1; |
| 7184 | |
| 7185 | if (likely(ret == 0)) { |
| 7186 | /* The filter did not match the response, it can be |
| 7187 | * iterated through all headers. |
| 7188 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7189 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7190 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7191 | } |
| 7192 | return 0; |
| 7193 | } |
| 7194 | |
| 7195 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7196 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7197 | * 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] | 7198 | * desirable to call it only when needed. This function is also used when we |
| 7199 | * 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] | 7200 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7201 | void manage_server_side_cookies(struct session *t, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7202 | { |
| 7203 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7204 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7205 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7206 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7207 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7208 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7209 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7210 | /* Iterate through the headers. |
| 7211 | * we start with the start line. |
| 7212 | */ |
| 7213 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7214 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7215 | |
| 7216 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7217 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7218 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7219 | |
| 7220 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7221 | hdr_beg = hdr_next; |
| 7222 | hdr_end = hdr_beg + cur_hdr->len; |
| 7223 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7224 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7225 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7226 | * next header starts at hdr_next. We're only interested in |
| 7227 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7228 | */ |
| 7229 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7230 | is_cookie2 = 0; |
| 7231 | prev = hdr_beg + 10; |
| 7232 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7233 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7234 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7235 | if (!val) { |
| 7236 | old_idx = cur_idx; |
| 7237 | continue; |
| 7238 | } |
| 7239 | is_cookie2 = 1; |
| 7240 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7241 | } |
| 7242 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7243 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7244 | * <prev> points to the colon. |
| 7245 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7246 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7247 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7248 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 7249 | * check-cache is enabled) and we are not interested in checking |
| 7250 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7251 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7252 | if (t->be->cookie_name == NULL && |
| 7253 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7254 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7255 | return; |
| 7256 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7257 | /* OK so now we know we have to process this response cookie. |
| 7258 | * The format of the Set-Cookie header is slightly different |
| 7259 | * from the format of the Cookie header in that it does not |
| 7260 | * support the comma as a cookie delimiter (thus the header |
| 7261 | * cannot be folded) because the Expires attribute described in |
| 7262 | * the original Netscape's spec may contain an unquoted date |
| 7263 | * with a comma inside. We have to live with this because |
| 7264 | * many browsers don't support Max-Age and some browsers don't |
| 7265 | * support quoted strings. However the Set-Cookie2 header is |
| 7266 | * clean. |
| 7267 | * |
| 7268 | * We have to keep multiple pointers in order to support cookie |
| 7269 | * removal at the beginning, middle or end of header without |
| 7270 | * corrupting the header (in case of set-cookie2). A special |
| 7271 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 7272 | * fields after the first semi-colon. The <next> pointer points |
| 7273 | * either to the end of line (set-cookie) or next unquoted comma |
| 7274 | * (set-cookie2). All of these headers are valid : |
| 7275 | * |
| 7276 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 7277 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7278 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7279 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 7280 | * | | | | | | | | | | |
| 7281 | * | | | | | | | | +-> next hdr_end <--+ |
| 7282 | * | | | | | | | +------------> scav |
| 7283 | * | | | | | | +--------------> val_end |
| 7284 | * | | | | | +--------------------> val_beg |
| 7285 | * | | | | +----------------------> equal |
| 7286 | * | | | +------------------------> att_end |
| 7287 | * | | +----------------------------> att_beg |
| 7288 | * | +------------------------------> prev |
| 7289 | * +-----------------------------------------> hdr_beg |
| 7290 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7291 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7292 | for (; prev < hdr_end; prev = next) { |
| 7293 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7294 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7295 | /* find att_beg */ |
| 7296 | att_beg = prev + 1; |
| 7297 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7298 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7299 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7300 | /* find att_end : this is the first character after the last non |
| 7301 | * space before the equal. It may be equal to hdr_end. |
| 7302 | */ |
| 7303 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7304 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7305 | while (equal < hdr_end) { |
| 7306 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 7307 | break; |
| 7308 | if (http_is_spht[(unsigned char)*equal++]) |
| 7309 | continue; |
| 7310 | att_end = equal; |
| 7311 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7312 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7313 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7314 | * is between <att_beg> and <equal>, both may be identical. |
| 7315 | */ |
| 7316 | |
| 7317 | /* look for end of cookie if there is an equal sign */ |
| 7318 | if (equal < hdr_end && *equal == '=') { |
| 7319 | /* look for the beginning of the value */ |
| 7320 | val_beg = equal + 1; |
| 7321 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7322 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7323 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7324 | /* find the end of the value, respecting quotes */ |
| 7325 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7326 | |
| 7327 | /* make val_end point to the first white space or delimitor after the value */ |
| 7328 | val_end = next; |
| 7329 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7330 | val_end--; |
| 7331 | } else { |
| 7332 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7333 | val_beg = val_end = next = equal; |
| 7334 | } |
| 7335 | |
| 7336 | if (next < hdr_end) { |
| 7337 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7338 | * a colon or semi-colon before the end. So skip all attr-value |
| 7339 | * pairs and look for the next comma. For Set-Cookie, since |
| 7340 | * commas are permitted in values, skip to the end. |
| 7341 | */ |
| 7342 | if (is_cookie2) |
| 7343 | next = find_hdr_value_end(next, hdr_end); |
| 7344 | else |
| 7345 | next = hdr_end; |
| 7346 | } |
| 7347 | |
| 7348 | /* Now everything is as on the diagram above */ |
| 7349 | |
| 7350 | /* Ignore cookies with no equal sign */ |
| 7351 | if (equal == val_end) |
| 7352 | continue; |
| 7353 | |
| 7354 | /* If there are spaces around the equal sign, we need to |
| 7355 | * strip them otherwise we'll get trouble for cookie captures, |
| 7356 | * or even for rewrites. Since this happens extremely rarely, |
| 7357 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7358 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7359 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7360 | int stripped_before = 0; |
| 7361 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7362 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7363 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7364 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7365 | equal += stripped_before; |
| 7366 | val_beg += stripped_before; |
| 7367 | } |
| 7368 | |
| 7369 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7370 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7371 | val_beg += stripped_after; |
| 7372 | stripped_before += stripped_after; |
| 7373 | } |
| 7374 | |
| 7375 | val_end += stripped_before; |
| 7376 | next += stripped_before; |
| 7377 | hdr_end += stripped_before; |
| 7378 | hdr_next += stripped_before; |
| 7379 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7380 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7381 | } |
| 7382 | |
| 7383 | /* First, let's see if we want to capture this cookie. We check |
| 7384 | * that we don't already have a server side cookie, because we |
| 7385 | * can only capture one. Also as an optimisation, we ignore |
| 7386 | * cookies shorter than the declared name. |
| 7387 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7388 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7389 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7390 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 7391 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 7392 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 7393 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7394 | Alert("HTTP logging : out of memory.\n"); |
| 7395 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7396 | else { |
| 7397 | if (log_len > t->fe->capture_len) |
| 7398 | log_len = t->fe->capture_len; |
| 7399 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7400 | txn->srv_cookie[log_len] = 0; |
| 7401 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7402 | } |
| 7403 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7404 | srv = objt_server(t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7405 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7406 | if (!(t->flags & SN_IGNORE_PRST) && |
| 7407 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 7408 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7409 | /* assume passive cookie by default */ |
| 7410 | txn->flags &= ~TX_SCK_MASK; |
| 7411 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7412 | |
| 7413 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7414 | * this occurrence because we'll insert another one later. |
| 7415 | * 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] | 7416 | * a direct access. |
| 7417 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7418 | if (t->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7419 | /* The "preserve" flag was set, we don't want to touch the |
| 7420 | * server's cookie. |
| 7421 | */ |
| 7422 | } |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7423 | else if ((srv && (t->be->ck_opts & PR_CK_INS)) || |
| 7424 | ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7425 | /* this cookie must be deleted */ |
| 7426 | if (*prev == ':' && next == hdr_end) { |
| 7427 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7428 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7429 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7430 | txn->hdr_idx.used--; |
| 7431 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7432 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7433 | hdr_next += delta; |
| 7434 | http_msg_move_end(&txn->rsp, delta); |
| 7435 | /* note: while both invalid now, <next> and <hdr_end> |
| 7436 | * are still equal, so the for() will stop as expected. |
| 7437 | */ |
| 7438 | } else { |
| 7439 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7440 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7441 | next = prev; |
| 7442 | hdr_end += delta; |
| 7443 | hdr_next += delta; |
| 7444 | cur_hdr->len += delta; |
| 7445 | http_msg_move_end(&txn->rsp, delta); |
| 7446 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7447 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7448 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7449 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7450 | } |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7451 | else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7452 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7453 | * with this server since we know it. |
| 7454 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7455 | 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] | 7456 | next += delta; |
| 7457 | hdr_end += delta; |
| 7458 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7459 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7460 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7461 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7462 | txn->flags &= ~TX_SCK_MASK; |
| 7463 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7464 | } |
Willy Tarreau | a059031 | 2012-06-06 16:07:00 +0200 | [diff] [blame] | 7465 | else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7466 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7467 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7468 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7469 | 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] | 7470 | next += delta; |
| 7471 | hdr_end += delta; |
| 7472 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7473 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7474 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7475 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7476 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7477 | txn->flags &= ~TX_SCK_MASK; |
| 7478 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7479 | } |
| 7480 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7481 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 7482 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7483 | int cmp_len, value_len; |
| 7484 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7485 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7486 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7487 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 7488 | value_begin = att_beg + t->be->appsession_name_len; |
| 7489 | 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] | 7490 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7491 | cmp_len = att_end - att_beg; |
| 7492 | value_begin = val_beg; |
| 7493 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7494 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7495 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7496 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7497 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7498 | /* free a possibly previously allocated memory */ |
| 7499 | pool_free2(apools.sessid, txn->sessid); |
| 7500 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7501 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7502 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7503 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7504 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7505 | return; |
| 7506 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7507 | memcpy(txn->sessid, value_begin, value_len); |
| 7508 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7509 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7510 | } |
| 7511 | /* that's done for this cookie, check the next one on the same |
| 7512 | * line when next != hdr_end (only if is_cookie2). |
| 7513 | */ |
| 7514 | } |
| 7515 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7516 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7517 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7518 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7519 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7520 | appsess *asession = NULL; |
| 7521 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7522 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7523 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 7524 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7525 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 7526 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 7527 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 7528 | return; |
| 7529 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7530 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 7531 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7532 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 7533 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7534 | 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] | 7535 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7536 | return; |
| 7537 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7538 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7539 | asession->sessid[t->be->appsession_len] = 0; |
| 7540 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7541 | server_id_len = strlen(objt_server(t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7542 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7543 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7544 | 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] | 7545 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7546 | return; |
| 7547 | } |
| 7548 | asession->serverid[0] = '\0'; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7549 | memcpy(asession->serverid, objt_server(t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7550 | |
| 7551 | asession->request_count = 0; |
| 7552 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 7553 | } |
| 7554 | |
| 7555 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 7556 | asession->request_count++; |
| 7557 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7558 | } |
| 7559 | |
| 7560 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7561 | /* |
| 7562 | * Check if response is cacheable or not. Updates t->flags. |
| 7563 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7564 | void check_response_for_cacheability(struct session *t, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7565 | { |
| 7566 | struct http_txn *txn = &t->txn; |
| 7567 | char *p1, *p2; |
| 7568 | |
| 7569 | char *cur_ptr, *cur_end, *cur_next; |
| 7570 | int cur_idx; |
| 7571 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 7572 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7573 | return; |
| 7574 | |
| 7575 | /* Iterate through the headers. |
| 7576 | * we start with the start line. |
| 7577 | */ |
| 7578 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7579 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7580 | |
| 7581 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7582 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7583 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7584 | |
| 7585 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7586 | cur_ptr = cur_next; |
| 7587 | cur_end = cur_ptr + cur_hdr->len; |
| 7588 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7589 | |
| 7590 | /* We have one full header between cur_ptr and cur_end, and the |
| 7591 | * next header starts at cur_next. We're only interested in |
| 7592 | * "Cookie:" headers. |
| 7593 | */ |
| 7594 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7595 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7596 | if (val) { |
| 7597 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7598 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7599 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7600 | return; |
| 7601 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7602 | } |
| 7603 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7604 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7605 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7606 | continue; |
| 7607 | |
| 7608 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7609 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7610 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7611 | |
| 7612 | if (p1 >= cur_end) /* no more info */ |
| 7613 | continue; |
| 7614 | |
| 7615 | /* p1 is at the beginning of the value */ |
| 7616 | p2 = p1; |
| 7617 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7618 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7619 | p2++; |
| 7620 | |
| 7621 | /* we have a complete value between p1 and p2 */ |
| 7622 | if (p2 < cur_end && *p2 == '=') { |
| 7623 | /* we have something of the form no-cache="set-cookie" */ |
| 7624 | if ((cur_end - p1 >= 21) && |
| 7625 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7626 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7627 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7628 | continue; |
| 7629 | } |
| 7630 | |
| 7631 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7632 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 7633 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7634 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7635 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7636 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7637 | return; |
| 7638 | } |
| 7639 | |
| 7640 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7641 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7642 | continue; |
| 7643 | } |
| 7644 | } |
| 7645 | } |
| 7646 | |
| 7647 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7648 | /* |
| 7649 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7650 | * If the server is found, it's assigned to the session. |
| 7651 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7652 | 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] | 7653 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7654 | char *end_params, *first_param, *cur_param, *next_param; |
| 7655 | char separator; |
| 7656 | int value_len; |
| 7657 | |
| 7658 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7659 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7660 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7661 | (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] | 7662 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7663 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7664 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7665 | first_param = NULL; |
| 7666 | switch (mode) { |
| 7667 | case PR_O2_AS_M_PP: |
| 7668 | first_param = memchr(begin, ';', len); |
| 7669 | break; |
| 7670 | case PR_O2_AS_M_QS: |
| 7671 | first_param = memchr(begin, '?', len); |
| 7672 | break; |
| 7673 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7674 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7675 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7676 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7677 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7678 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7679 | switch (mode) { |
| 7680 | case PR_O2_AS_M_PP: |
| 7681 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7682 | end_params = (char *) begin + len; |
| 7683 | } |
| 7684 | separator = ';'; |
| 7685 | break; |
| 7686 | case PR_O2_AS_M_QS: |
| 7687 | end_params = (char *) begin + len; |
| 7688 | separator = '&'; |
| 7689 | break; |
| 7690 | default: |
| 7691 | /* unknown mode, shouldn't happen */ |
| 7692 | return; |
| 7693 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7694 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7695 | cur_param = next_param = end_params; |
| 7696 | while (cur_param > first_param) { |
| 7697 | cur_param--; |
| 7698 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7699 | /* let's see if this is the appsession parameter */ |
| 7700 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7701 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7702 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7703 | /* Cool... it's the right one */ |
| 7704 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7705 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7706 | if (value_len > 0) { |
| 7707 | manage_client_side_appsession(t, cur_param, value_len); |
| 7708 | } |
| 7709 | break; |
| 7710 | } |
| 7711 | next_param = cur_param; |
| 7712 | } |
| 7713 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7714 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7715 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7716 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7717 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7718 | } |
| 7719 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7720 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7721 | * 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] | 7722 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7723 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7724 | * 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] | 7725 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7726 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7727 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7728 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7729 | 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] | 7730 | { |
| 7731 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7732 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7733 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7734 | const char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7735 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7736 | if (!uri_auth) |
| 7737 | return 0; |
| 7738 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7739 | 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] | 7740 | return 0; |
| 7741 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7742 | memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats)); |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7743 | si->applet.ctx.stats.st_code = STAT_STATUS_INIT; |
Willy Tarreau | 354898b | 2012-12-23 18:15:23 +0100 | [diff] [blame] | 7744 | si->applet.ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7745 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7746 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7747 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7748 | return 0; |
| 7749 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7750 | h = uri; |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 7751 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7752 | return 0; |
| 7753 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7754 | h += uri_auth->uri_len; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7755 | while (h <= uri + msg->sl.rq.u_l - 3) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7756 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7757 | si->applet.ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7758 | break; |
| 7759 | } |
| 7760 | h++; |
| 7761 | } |
| 7762 | |
| 7763 | if (uri_auth->refresh) { |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7764 | h = uri + uri_auth->uri_len; |
| 7765 | while (h <= uri + msg->sl.rq.u_l - 10) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7766 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7767 | si->applet.ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7768 | break; |
| 7769 | } |
| 7770 | h++; |
| 7771 | } |
| 7772 | } |
| 7773 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7774 | h = uri + uri_auth->uri_len; |
| 7775 | while (h <= uri + msg->sl.rq.u_l - 4) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7776 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 354898b | 2012-12-23 18:15:23 +0100 | [diff] [blame] | 7777 | si->applet.ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7778 | break; |
| 7779 | } |
| 7780 | h++; |
| 7781 | } |
| 7782 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7783 | h = uri + uri_auth->uri_len; |
| 7784 | while (h <= uri + msg->sl.rq.u_l - 8) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7785 | if (memcmp(h, ";st=", 4) == 0) { |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7786 | int i; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7787 | h += 4; |
Cyril Bonté | 20a804a | 2012-05-10 19:42:52 +0200 | [diff] [blame] | 7788 | si->applet.ctx.stats.st_code = STAT_STATUS_UNKN; |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7789 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 7790 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
| 7791 | si->applet.ctx.stats.st_code = i; |
| 7792 | break; |
| 7793 | } |
| 7794 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7795 | break; |
| 7796 | } |
| 7797 | h++; |
| 7798 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7799 | return 1; |
| 7800 | } |
| 7801 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7802 | /* |
| 7803 | * 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] | 7804 | * By default it tries to report the error position as msg->err_pos. However if |
| 7805 | * this one is not set, it will then report msg->next, which is the last known |
| 7806 | * 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] | 7807 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7808 | */ |
| 7809 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7810 | struct http_msg *msg, |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7811 | int state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7812 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7813 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7814 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7815 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7816 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 7817 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7818 | len1 = MIN(len1, es->len); |
| 7819 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 7820 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7821 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7822 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7823 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7824 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7825 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7826 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7827 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7828 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7829 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7830 | es->when = date; // user-visible date |
| 7831 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7832 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7833 | es->oe = other_end; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 7834 | es->src = s->req->prod->conn->addr.from; |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7835 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7836 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7837 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7838 | es->s_flags = s->flags; |
| 7839 | es->t_flags = s->txn.flags; |
| 7840 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7841 | es->b_out = chn->buf->o; |
| 7842 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7843 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7844 | es->m_clen = msg->chunk_len; |
| 7845 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7846 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7847 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7848 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7849 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7850 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7851 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7852 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7853 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7854 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 7855 | * -1. |
| 7856 | * 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] | 7857 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7858 | 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] | 7859 | struct hdr_idx *idx, int occ, |
| 7860 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7861 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7862 | struct hdr_ctx local_ctx; |
| 7863 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7864 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7865 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7866 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7867 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7868 | if (!ctx) { |
| 7869 | local_ctx.idx = 0; |
| 7870 | ctx = &local_ctx; |
| 7871 | } |
| 7872 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7873 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7874 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7875 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7876 | occ--; |
| 7877 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7878 | *vptr = ctx->line + ctx->val; |
| 7879 | *vlen = ctx->vlen; |
| 7880 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7881 | } |
| 7882 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7883 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7884 | } |
| 7885 | |
| 7886 | /* negative occurrence, we scan all the list then walk back */ |
| 7887 | if (-occ > MAX_HDR_HISTORY) |
| 7888 | return 0; |
| 7889 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7890 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7891 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7892 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7893 | len_hist[hist_ptr] = ctx->vlen; |
| 7894 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7895 | hist_ptr = 0; |
| 7896 | found++; |
| 7897 | } |
| 7898 | if (-occ > found) |
| 7899 | return 0; |
| 7900 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 7901 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 7902 | */ |
| 7903 | hist_ptr += occ; |
| 7904 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7905 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7906 | *vptr = ptr_hist[hist_ptr]; |
| 7907 | *vlen = len_hist[hist_ptr]; |
| 7908 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7909 | } |
| 7910 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7911 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7912 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 7913 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 7914 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7915 | */ |
| 7916 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 7917 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7918 | int max; |
| 7919 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
Willy Tarreau | 7f7ad91 | 2012-11-11 19:27:15 +0100 | [diff] [blame] | 7920 | dir, (unsigned short)t->req->prod->conn->t.sock.fd, |
| 7921 | (unsigned short)t->req->cons->conn->t.sock.fd); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7922 | |
| 7923 | for (max = 0; start + max < end; max++) |
| 7924 | if (start[max] == '\r' || start[max] == '\n') |
| 7925 | break; |
| 7926 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7927 | UBOUND(max, trash.size - trash.len - 3); |
| 7928 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 7929 | trash.str[trash.len++] = '\n'; |
| 7930 | if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7931 | } |
| 7932 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7933 | /* |
| 7934 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 7935 | * the required fields are properly allocated and that we only need to (re)init |
| 7936 | * them. This should be used before processing any new request. |
| 7937 | */ |
| 7938 | void http_init_txn(struct session *s) |
| 7939 | { |
| 7940 | struct http_txn *txn = &s->txn; |
| 7941 | struct proxy *fe = s->fe; |
| 7942 | |
| 7943 | txn->flags = 0; |
| 7944 | txn->status = -1; |
| 7945 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 7946 | global.req_count++; |
| 7947 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7948 | txn->cookie_first_date = 0; |
| 7949 | txn->cookie_last_date = 0; |
| 7950 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7951 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 7952 | 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] | 7953 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7954 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 7955 | 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] | 7956 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 7957 | txn->req.chunk_len = 0LL; |
| 7958 | txn->req.body_len = 0LL; |
| 7959 | txn->rsp.chunk_len = 0LL; |
| 7960 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7961 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7962 | 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] | 7963 | txn->req.chn = s->req; |
| 7964 | txn->rsp.chn = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7965 | |
| 7966 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7967 | |
| 7968 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7969 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7970 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7971 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7972 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7973 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 7974 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7975 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7976 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 7977 | |
| 7978 | if (txn->hdr_idx.v) |
| 7979 | hdr_idx_init(&txn->hdr_idx); |
| 7980 | } |
| 7981 | |
| 7982 | /* to be used at the end of a transaction */ |
| 7983 | void http_end_txn(struct session *s) |
| 7984 | { |
| 7985 | struct http_txn *txn = &s->txn; |
| 7986 | |
| 7987 | /* these ones will have been dynamically allocated */ |
| 7988 | pool_free2(pool2_requri, txn->uri); |
| 7989 | pool_free2(pool2_capture, txn->cli_cookie); |
| 7990 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7991 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7992 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7993 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7994 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7995 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7996 | txn->uri = NULL; |
| 7997 | txn->srv_cookie = NULL; |
| 7998 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7999 | |
| 8000 | if (txn->req.cap) { |
| 8001 | struct cap_hdr *h; |
| 8002 | for (h = s->fe->req_cap; h; h = h->next) |
| 8003 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 8004 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 8005 | } |
| 8006 | |
| 8007 | if (txn->rsp.cap) { |
| 8008 | struct cap_hdr *h; |
| 8009 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 8010 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 8011 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 8012 | } |
| 8013 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8014 | } |
| 8015 | |
| 8016 | /* to be used at the end of a transaction to prepare a new one */ |
| 8017 | void http_reset_txn(struct session *s) |
| 8018 | { |
| 8019 | http_end_txn(s); |
| 8020 | http_init_txn(s); |
| 8021 | |
| 8022 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8023 | s->logs.logwait = s->fe->to_log; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 8024 | session_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8025 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8026 | /* re-init store persistence */ |
| 8027 | s->store_count = 0; |
| 8028 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8029 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8030 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 8031 | s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8032 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8033 | /* We must trim any excess data from the response buffer, because we |
| 8034 | * may have blocked an invalid response from a server that we don't |
| 8035 | * want to accidentely forward once we disable the analysers, nor do |
| 8036 | * we want those data to come along with next response. A typical |
| 8037 | * example of such data would be from a buggy server responding to |
| 8038 | * a HEAD with some data, or sending more than the advertised |
| 8039 | * content-length. |
| 8040 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8041 | if (unlikely(s->rep->buf->i)) |
| 8042 | s->rep->buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8043 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8044 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8045 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8046 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8047 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8048 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8049 | |
| 8050 | s->req->rex = TICK_ETERNITY; |
| 8051 | s->req->wex = TICK_ETERNITY; |
| 8052 | s->req->analyse_exp = TICK_ETERNITY; |
| 8053 | s->rep->rex = TICK_ETERNITY; |
| 8054 | s->rep->wex = TICK_ETERNITY; |
| 8055 | s->rep->analyse_exp = TICK_ETERNITY; |
| 8056 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8057 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8058 | void free_http_req_rules(struct list *r) { |
| 8059 | struct http_req_rule *tr, *pr; |
| 8060 | |
| 8061 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8062 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8063 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8064 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8065 | |
| 8066 | free(pr); |
| 8067 | } |
| 8068 | } |
| 8069 | |
| 8070 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8071 | { |
| 8072 | struct http_req_rule *rule; |
| 8073 | int cur_arg; |
| 8074 | |
| 8075 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 8076 | if (!rule) { |
| 8077 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8078 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8079 | } |
| 8080 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8081 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8082 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 8083 | cur_arg = 1; |
| 8084 | } else if (!strcmp(args[0], "deny")) { |
| 8085 | rule->action = HTTP_REQ_ACT_DENY; |
| 8086 | cur_arg = 1; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8087 | } else if (!strcmp(args[0], "tarpit")) { |
| 8088 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 8089 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8090 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8091 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8092 | cur_arg = 1; |
| 8093 | |
| 8094 | while(*args[cur_arg]) { |
| 8095 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8096 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8097 | cur_arg+=2; |
| 8098 | continue; |
| 8099 | } else |
| 8100 | break; |
| 8101 | } |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8102 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 8103 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 8104 | cur_arg = 1; |
| 8105 | |
| 8106 | if (!*args[cur_arg] || !*args[cur_arg+1] || *args[cur_arg+2]) { |
| 8107 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8108 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8109 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8110 | } |
| 8111 | |
| 8112 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8113 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8114 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | 3bfeadb | 2013-03-24 07:33:22 +0100 | [diff] [blame] | 8115 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8116 | cur_arg += 2; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8117 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8118 | struct redirect_rule *redir; |
| 8119 | char *errmsg; |
| 8120 | |
| 8121 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg)) == NULL) { |
| 8122 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8123 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 8124 | goto out_err; |
| 8125 | } |
| 8126 | |
| 8127 | /* this redirect rule might already contain a parsed condition which |
| 8128 | * we'll pass to the http-request rule. |
| 8129 | */ |
| 8130 | rule->action = HTTP_REQ_ACT_REDIR; |
| 8131 | rule->arg.redir = redir; |
| 8132 | rule->cond = redir->cond; |
| 8133 | redir->cond = NULL; |
| 8134 | cur_arg = 2; |
| 8135 | return rule; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8136 | } else { |
Baptiste Assmann | 116eefe | 2013-01-05 16:02:07 +0100 | [diff] [blame] | 8137 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', but got '%s'%s.\n", |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8138 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8139 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8140 | } |
| 8141 | |
| 8142 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8143 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8144 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8145 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8146 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 8147 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 8148 | file, linenum, args[0], errmsg); |
| 8149 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8150 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8151 | } |
| 8152 | rule->cond = cond; |
| 8153 | } |
| 8154 | else if (*args[cur_arg]) { |
| 8155 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 8156 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8157 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8158 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8159 | } |
| 8160 | |
| 8161 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8162 | out_err: |
| 8163 | free(rule); |
| 8164 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8165 | } |
| 8166 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8167 | /* Parses a redirect rule. Returns the redirect rule on success or NULL on error, |
| 8168 | * with <err> filled with the error message. |
| 8169 | */ |
| 8170 | struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy, |
| 8171 | const char **args, char **errmsg) |
| 8172 | { |
| 8173 | struct redirect_rule *rule; |
| 8174 | int cur_arg; |
| 8175 | int type = REDIRECT_TYPE_NONE; |
| 8176 | int code = 302; |
| 8177 | const char *destination = NULL; |
| 8178 | const char *cookie = NULL; |
| 8179 | int cookie_set = 0; |
| 8180 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 8181 | struct acl_cond *cond = NULL; |
| 8182 | |
| 8183 | cur_arg = 0; |
| 8184 | while (*(args[cur_arg])) { |
| 8185 | if (strcmp(args[cur_arg], "location") == 0) { |
| 8186 | if (!*args[cur_arg + 1]) |
| 8187 | goto missing_arg; |
| 8188 | |
| 8189 | type = REDIRECT_TYPE_LOCATION; |
| 8190 | cur_arg++; |
| 8191 | destination = args[cur_arg]; |
| 8192 | } |
| 8193 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 8194 | if (!*args[cur_arg + 1]) |
| 8195 | goto missing_arg; |
| 8196 | |
| 8197 | type = REDIRECT_TYPE_PREFIX; |
| 8198 | cur_arg++; |
| 8199 | destination = args[cur_arg]; |
| 8200 | } |
| 8201 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 8202 | if (!*args[cur_arg + 1]) |
| 8203 | goto missing_arg; |
| 8204 | |
| 8205 | type = REDIRECT_TYPE_SCHEME; |
| 8206 | cur_arg++; |
| 8207 | destination = args[cur_arg]; |
| 8208 | } |
| 8209 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 8210 | if (!*args[cur_arg + 1]) |
| 8211 | goto missing_arg; |
| 8212 | |
| 8213 | cur_arg++; |
| 8214 | cookie = args[cur_arg]; |
| 8215 | cookie_set = 1; |
| 8216 | } |
| 8217 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 8218 | if (!*args[cur_arg + 1]) |
| 8219 | goto missing_arg; |
| 8220 | |
| 8221 | cur_arg++; |
| 8222 | cookie = args[cur_arg]; |
| 8223 | cookie_set = 0; |
| 8224 | } |
| 8225 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 8226 | if (!*args[cur_arg + 1]) |
| 8227 | goto missing_arg; |
| 8228 | |
| 8229 | cur_arg++; |
| 8230 | code = atol(args[cur_arg]); |
| 8231 | if (code < 301 || code > 303) { |
| 8232 | memprintf(errmsg, |
| 8233 | "'%s': unsupported HTTP code '%s' (must be a number between 301 and 303)", |
| 8234 | args[cur_arg - 1], args[cur_arg]); |
| 8235 | return NULL; |
| 8236 | } |
| 8237 | } |
| 8238 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 8239 | flags |= REDIRECT_FLAG_DROP_QS; |
| 8240 | } |
| 8241 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 8242 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 8243 | } |
| 8244 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 8245 | strcmp(args[cur_arg], "unless") == 0) { |
| 8246 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 8247 | if (!cond) { |
| 8248 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 8249 | return NULL; |
| 8250 | } |
| 8251 | break; |
| 8252 | } |
| 8253 | else { |
| 8254 | memprintf(errmsg, |
| 8255 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 8256 | args[cur_arg]); |
| 8257 | return NULL; |
| 8258 | } |
| 8259 | cur_arg++; |
| 8260 | } |
| 8261 | |
| 8262 | if (type == REDIRECT_TYPE_NONE) { |
| 8263 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 8264 | return NULL; |
| 8265 | } |
| 8266 | |
| 8267 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 8268 | rule->cond = cond; |
| 8269 | rule->rdr_str = strdup(destination); |
| 8270 | rule->rdr_len = strlen(destination); |
| 8271 | if (cookie) { |
| 8272 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 8273 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 8274 | */ |
| 8275 | rule->cookie_len = strlen(cookie); |
| 8276 | if (cookie_set) { |
| 8277 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 8278 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 8279 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 8280 | rule->cookie_len += 9; |
| 8281 | } else { |
| 8282 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 8283 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 8284 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 8285 | rule->cookie_len += 20; |
| 8286 | } |
| 8287 | } |
| 8288 | rule->type = type; |
| 8289 | rule->code = code; |
| 8290 | rule->flags = flags; |
| 8291 | LIST_INIT(&rule->list); |
| 8292 | return rule; |
| 8293 | |
| 8294 | missing_arg: |
| 8295 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 8296 | return NULL; |
| 8297 | } |
| 8298 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8299 | /************************************************************************/ |
| 8300 | /* The code below is dedicated to ACL parsing and matching */ |
| 8301 | /************************************************************************/ |
| 8302 | |
| 8303 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8304 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 8305 | * which means that a request or response is ready. If some data is missing, |
| 8306 | * 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] | 8307 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 8308 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8309 | * |
| 8310 | * The function returns : |
| 8311 | * 0 if some data is missing or if the requested data cannot be fetched |
| 8312 | * -1 if it is certain that we'll never have any HTTP message there |
| 8313 | * 1 if an HTTP message is ready |
| 8314 | */ |
| 8315 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8316 | 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] | 8317 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8318 | { |
| 8319 | struct http_txn *txn = l7; |
| 8320 | struct http_msg *msg = &txn->req; |
| 8321 | |
| 8322 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8323 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8324 | */ |
| 8325 | |
| 8326 | if (unlikely(!s || !txn)) |
| 8327 | return 0; |
| 8328 | |
| 8329 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8330 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8331 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8332 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8333 | if (unlikely(!s->req)) |
| 8334 | return 0; |
| 8335 | |
| 8336 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 8337 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8338 | buffer_full(s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 8339 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8340 | return -1; |
| 8341 | } |
| 8342 | |
| 8343 | /* Try to decode HTTP request */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8344 | if (likely(msg->next < s->req->buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8345 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 8346 | |
| 8347 | /* Still no valid request ? */ |
| 8348 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 8349 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8350 | buffer_full(s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 8351 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8352 | return -1; |
| 8353 | } |
| 8354 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8355 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8356 | return 0; |
| 8357 | } |
| 8358 | |
| 8359 | /* OK we just got a valid HTTP request. We have some minor |
| 8360 | * preparation to perform so that further checks can rely |
| 8361 | * on HTTP tests. |
| 8362 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8363 | 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] | 8364 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 8365 | s->flags |= SN_REDIRECTABLE; |
| 8366 | |
| 8367 | 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] | 8368 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8369 | return -1; |
| 8370 | } |
| 8371 | } |
| 8372 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8373 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8374 | return 0; /* data might have moved and indexes changed */ |
| 8375 | |
| 8376 | /* otherwise everything's ready for the request */ |
| 8377 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8378 | else { |
| 8379 | /* Check for a dependency on a response */ |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8380 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 8381 | return 0; |
| 8382 | } |
| 8383 | |
| 8384 | /* everything's OK */ |
| 8385 | return 1; |
| 8386 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8387 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8388 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8389 | 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] | 8390 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8391 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8392 | 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] | 8393 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8394 | |
| 8395 | /* 1. Check on METHOD |
| 8396 | * We use the pre-parsed method if it is known, and store its number as an |
| 8397 | * integer. If it is unknown, we use the pointer and the length. |
| 8398 | */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8399 | 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] | 8400 | { |
| 8401 | int len, meth; |
| 8402 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 8403 | len = strlen(*text); |
| 8404 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8405 | |
| 8406 | pattern->val.i = meth; |
| 8407 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 8408 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8409 | if (!pattern->ptr.str) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 8410 | memprintf(err, "out of memory while loading pattern"); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8411 | return 0; |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8412 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8413 | pattern->len = len; |
| 8414 | } |
| 8415 | return 1; |
| 8416 | } |
| 8417 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8418 | /* This function fetches the method of current HTTP request and stores |
| 8419 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 8420 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 8421 | * in <len> and <ptr> is NULL ; |
| 8422 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 8423 | * <len> to its length. |
| 8424 | * This is intended to be used with acl_match_meth() only. |
| 8425 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8426 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8427 | 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] | 8428 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8429 | { |
| 8430 | int meth; |
| 8431 | struct http_txn *txn = l7; |
| 8432 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8433 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8434 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8435 | meth = txn->meth; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8436 | smp->type = SMP_T_UINT; |
| 8437 | smp->data.uint = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8438 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8439 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8440 | /* ensure the indexes are not affected */ |
| 8441 | return 0; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8442 | smp->type = SMP_T_CSTR; |
| 8443 | smp->data.str.len = txn->req.sl.rq.m_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8444 | smp->data.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8445 | } |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 8446 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8447 | return 1; |
| 8448 | } |
| 8449 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8450 | /* See above how the method is stored in the global pattern */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8451 | static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8452 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 8453 | int icase; |
| 8454 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8455 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8456 | if (smp->type == SMP_T_UINT) { |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8457 | /* well-known method */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8458 | if (smp->data.uint == pattern->val.i) |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8459 | return ACL_PAT_PASS; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 8460 | return ACL_PAT_FAIL; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8461 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8462 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8463 | /* Uncommon method, only HTTP_METH_OTHER is accepted now */ |
| 8464 | if (pattern->val.i != HTTP_METH_OTHER) |
| 8465 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8466 | |
| 8467 | /* Other method, we must compare the strings */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8468 | if (pattern->len != smp->data.str.len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 8469 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 8470 | |
| 8471 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8472 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) || |
| 8473 | (!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] | 8474 | return ACL_PAT_FAIL; |
| 8475 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8476 | } |
| 8477 | |
| 8478 | /* 2. Check on Request/Status Version |
| 8479 | * We simply compare strings here. |
| 8480 | */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8481 | 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] | 8482 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 8483 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8484 | if (!pattern->ptr.str) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 8485 | memprintf(err, "out of memory while loading pattern"); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8486 | return 0; |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8487 | } |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 8488 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8489 | return 1; |
| 8490 | } |
| 8491 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8492 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8493 | 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] | 8494 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8495 | { |
| 8496 | struct http_txn *txn = l7; |
| 8497 | char *ptr; |
| 8498 | int len; |
| 8499 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8500 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8501 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8502 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8503 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8504 | |
| 8505 | while ((len-- > 0) && (*ptr++ != '/')); |
| 8506 | if (len <= 0) |
| 8507 | return 0; |
| 8508 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8509 | smp->type = SMP_T_CSTR; |
| 8510 | smp->data.str.str = ptr; |
| 8511 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8512 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 8513 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8514 | return 1; |
| 8515 | } |
| 8516 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8517 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8518 | 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] | 8519 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8520 | { |
| 8521 | struct http_txn *txn = l7; |
| 8522 | char *ptr; |
| 8523 | int len; |
| 8524 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8525 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8526 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 8527 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 8528 | return 0; |
| 8529 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8530 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8531 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8532 | |
| 8533 | while ((len-- > 0) && (*ptr++ != '/')); |
| 8534 | if (len <= 0) |
| 8535 | return 0; |
| 8536 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8537 | smp->type = SMP_T_CSTR; |
| 8538 | smp->data.str.str = ptr; |
| 8539 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8540 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 8541 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8542 | return 1; |
| 8543 | } |
| 8544 | |
| 8545 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8546 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8547 | 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] | 8548 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8549 | { |
| 8550 | struct http_txn *txn = l7; |
| 8551 | char *ptr; |
| 8552 | int len; |
| 8553 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8554 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8555 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 8556 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 8557 | return 0; |
| 8558 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8559 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8560 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8561 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8562 | smp->type = SMP_T_UINT; |
| 8563 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8564 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8565 | return 1; |
| 8566 | } |
| 8567 | |
| 8568 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8569 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 8570 | 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] | 8571 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8572 | { |
| 8573 | struct http_txn *txn = l7; |
| 8574 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8575 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8576 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8577 | smp->type = SMP_T_CSTR; |
| 8578 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8579 | 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] | 8580 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8581 | return 1; |
| 8582 | } |
| 8583 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8584 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 8585 | 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] | 8586 | const struct arg *args, struct sample *smp) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8587 | { |
| 8588 | struct http_txn *txn = l7; |
| 8589 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8590 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8591 | |
| 8592 | /* Parse HTTP request */ |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 8593 | url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to); |
| 8594 | 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] | 8595 | return 0; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8596 | smp->type = SMP_T_IPV4; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 8597 | 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] | 8598 | |
| 8599 | /* |
| 8600 | * If we are parsing url in frontend space, we prepare backend stage |
| 8601 | * to not parse again the same url ! optimization lazyness... |
| 8602 | */ |
| 8603 | if (px->options & PR_O_HTTP_PROXY) |
| 8604 | l4->flags |= SN_ADDR_SET; |
| 8605 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8606 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8607 | return 1; |
| 8608 | } |
| 8609 | |
| 8610 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 8611 | 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] | 8612 | const struct arg *args, struct sample *smp) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8613 | { |
| 8614 | struct http_txn *txn = l7; |
| 8615 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8616 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8617 | |
| 8618 | /* Same optimization as url_ip */ |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 8619 | 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] | 8620 | smp->type = SMP_T_UINT; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 8621 | 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] | 8622 | |
| 8623 | if (px->options & PR_O_HTTP_PROXY) |
| 8624 | l4->flags |= SN_ADDR_SET; |
| 8625 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 8626 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8627 | return 1; |
| 8628 | } |
| 8629 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8630 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 8631 | * Accepts an optional argument of type string containing the header field name, |
| 8632 | * and an optional argument of type signed or unsigned integer to request an |
| 8633 | * explicit occurrence of the header. Note that in the event of a missing name, |
| 8634 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8635 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8636 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8637 | 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] | 8638 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8639 | { |
| 8640 | struct http_txn *txn = l7; |
| 8641 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8642 | struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8643 | 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] | 8644 | int occ = 0; |
| 8645 | const char *name_str = NULL; |
| 8646 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8647 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8648 | if (args) { |
| 8649 | if (args[0].type != ARGT_STR) |
| 8650 | return 0; |
| 8651 | name_str = args[0].data.str.str; |
| 8652 | name_len = args[0].data.str.len; |
| 8653 | |
| 8654 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 8655 | occ = args[1].data.uint; |
| 8656 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8657 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8658 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8659 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8660 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8661 | /* search for header from the beginning */ |
| 8662 | ctx->idx = 0; |
| 8663 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8664 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 8665 | /* no explicit occurrence and single fetch => last header by default */ |
| 8666 | occ = -1; |
| 8667 | |
| 8668 | if (!occ) |
| 8669 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8670 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 8671 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8672 | smp->type = SMP_T_CSTR; |
| 8673 | smp->flags |= SMP_F_VOL_HDR; |
| 8674 | 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] | 8675 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8676 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8677 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8678 | return 0; |
| 8679 | } |
| 8680 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8681 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8682 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8683 | */ |
| 8684 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8685 | 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] | 8686 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8687 | { |
| 8688 | struct http_txn *txn = l7; |
| 8689 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8690 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8691 | 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] | 8692 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8693 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8694 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8695 | return 0; |
| 8696 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8697 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8698 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8699 | ctx.idx = 0; |
| 8700 | cnt = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8701 | 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] | 8702 | cnt++; |
| 8703 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8704 | smp->type = SMP_T_UINT; |
| 8705 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8706 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8707 | return 1; |
| 8708 | } |
| 8709 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8710 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 8711 | * takes a mandatory argument of type string and an optional one of type int |
| 8712 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 8713 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8714 | */ |
| 8715 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8716 | 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] | 8717 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8718 | { |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8719 | int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8720 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8721 | if (ret > 0) { |
| 8722 | smp->type = SMP_T_UINT; |
| 8723 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 8724 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8725 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8726 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8727 | } |
| 8728 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 8729 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 8730 | * and an optional one of type int to designate a specific occurrence. |
| 8731 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8732 | */ |
| 8733 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8734 | 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] | 8735 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8736 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8737 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8738 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8739 | while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) { |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 8740 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 8741 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8742 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 8743 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8744 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 8745 | if (smp->data.str.len < temp->size - 1) { |
| 8746 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 8747 | temp->str[smp->data.str.len] = '\0'; |
| 8748 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 8749 | smp->type = SMP_T_IPV6; |
| 8750 | break; |
| 8751 | } |
| 8752 | } |
| 8753 | } |
| 8754 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8755 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8756 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 8757 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8758 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8759 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8760 | } |
| 8761 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8762 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 8763 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 8764 | */ |
| 8765 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 8766 | 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] | 8767 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8768 | { |
| 8769 | struct http_txn *txn = l7; |
| 8770 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8771 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8772 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8773 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8774 | 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] | 8775 | ptr = http_get_path(txn); |
| 8776 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8777 | return 0; |
| 8778 | |
| 8779 | /* OK, we got the '/' ! */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8780 | smp->type = SMP_T_CSTR; |
| 8781 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8782 | |
| 8783 | while (ptr < end && *ptr != '?') |
| 8784 | ptr++; |
| 8785 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8786 | smp->data.str.len = ptr - smp->data.str.str; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8787 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8788 | return 1; |
| 8789 | } |
| 8790 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 8791 | /* This produces a concatenation of the first occurrence of the Host header |
| 8792 | * followed by the path component if it begins with a slash ('/'). This means |
| 8793 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 8794 | * If no Host header is found, then the path is returned as-is. The returned |
| 8795 | * value is stored in the trash so it does not need to be marked constant. |
| 8796 | */ |
| 8797 | static int |
| 8798 | smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8799 | const struct arg *args, struct sample *smp) |
| 8800 | { |
| 8801 | struct http_txn *txn = l7; |
| 8802 | char *ptr, *end, *beg; |
| 8803 | struct hdr_ctx ctx; |
| 8804 | |
| 8805 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8806 | |
| 8807 | ctx.idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8808 | 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] | 8809 | !ctx.vlen) |
| 8810 | return smp_fetch_path(px, l4, l7, opt, args, smp); |
| 8811 | |
| 8812 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8813 | memcpy(trash.str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 8814 | smp->type = SMP_T_STR; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8815 | smp->data.str.str = trash.str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 8816 | smp->data.str.len = ctx.vlen; |
| 8817 | |
| 8818 | /* now retrieve the path */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8819 | 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] | 8820 | beg = http_get_path(txn); |
| 8821 | if (!beg) |
| 8822 | beg = end; |
| 8823 | |
| 8824 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 8825 | |
| 8826 | if (beg < ptr && *beg == '/') { |
| 8827 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 8828 | smp->data.str.len += ptr - beg; |
| 8829 | } |
| 8830 | |
| 8831 | smp->flags = SMP_F_VOL_1ST; |
| 8832 | return 1; |
| 8833 | } |
| 8834 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 8835 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 8836 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 8837 | * This means that '*' will not be added, resulting in exactly the first Host |
| 8838 | * entry. If no Host header is found, then the path is used. The resulting value |
| 8839 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 8840 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 8841 | * high-traffic sites without having to store whole paths. |
| 8842 | */ |
| 8843 | static int |
| 8844 | smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8845 | const struct arg *args, struct sample *smp) |
| 8846 | { |
| 8847 | struct http_txn *txn = l7; |
| 8848 | struct hdr_ctx ctx; |
| 8849 | unsigned int hash = 0; |
| 8850 | char *ptr, *beg, *end; |
| 8851 | int len; |
| 8852 | |
| 8853 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8854 | |
| 8855 | ctx.idx = 0; |
| 8856 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 8857 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 8858 | ptr = ctx.line + ctx.val; |
| 8859 | len = ctx.vlen; |
| 8860 | while (len--) |
| 8861 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 8862 | } |
| 8863 | |
| 8864 | /* now retrieve the path */ |
| 8865 | end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 8866 | beg = http_get_path(txn); |
| 8867 | if (!beg) |
| 8868 | beg = end; |
| 8869 | |
| 8870 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 8871 | |
| 8872 | if (beg < ptr && *beg == '/') { |
| 8873 | while (beg < ptr) |
| 8874 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 8875 | } |
| 8876 | hash = full_hash(hash); |
| 8877 | |
| 8878 | smp->type = SMP_T_UINT; |
| 8879 | smp->data.uint = hash; |
| 8880 | smp->flags = SMP_F_VOL_1ST; |
| 8881 | return 1; |
| 8882 | } |
| 8883 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 8884 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 8885 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 8886 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 8887 | * on the source address length. The URL hash is stored before the address so |
| 8888 | * that in environments where IPv6 is insignificant, truncating the output to |
| 8889 | * 8 bytes would still work. |
| 8890 | */ |
| 8891 | static int |
| 8892 | smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8893 | const struct arg *args, struct sample *smp) |
| 8894 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8895 | struct chunk *temp; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 8896 | |
| 8897 | if (!smp_fetch_base32(px, l4, l7, opt, args, smp)) |
| 8898 | return 0; |
| 8899 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8900 | temp = get_trash_chunk(); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 8901 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 8902 | temp->len += sizeof(smp->data.uint); |
| 8903 | |
| 8904 | switch (l4->si[0].conn->addr.from.ss_family) { |
| 8905 | case AF_INET: |
| 8906 | memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4); |
| 8907 | temp->len += 4; |
| 8908 | break; |
| 8909 | case AF_INET6: |
| 8910 | memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16); |
| 8911 | temp->len += 16; |
| 8912 | break; |
| 8913 | default: |
| 8914 | return 0; |
| 8915 | } |
| 8916 | |
| 8917 | smp->data.str = *temp; |
| 8918 | smp->type = SMP_T_BIN; |
| 8919 | return 1; |
| 8920 | } |
| 8921 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8922 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8923 | 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] | 8924 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8925 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8926 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8927 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8928 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8929 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8930 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8931 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8932 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 8933 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8934 | return 1; |
| 8935 | } |
| 8936 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8937 | /* return a valid test if the current request is the first one on the connection */ |
| 8938 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8939 | 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] | 8940 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8941 | { |
| 8942 | if (!s) |
| 8943 | return 0; |
| 8944 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8945 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 8946 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8947 | return 1; |
| 8948 | } |
| 8949 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8950 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8951 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8952 | 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] | 8953 | const struct arg *args, struct sample *smp) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8954 | { |
| 8955 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8956 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8957 | return 0; |
| 8958 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8959 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8960 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8961 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8962 | return 0; |
| 8963 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8964 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8965 | 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] | 8966 | return 1; |
| 8967 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8968 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 8969 | /* Accepts exactly 1 argument of type userlist */ |
| 8970 | static int |
| 8971 | acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8972 | const struct arg *args, struct sample *smp) |
| 8973 | { |
| 8974 | |
| 8975 | if (!args || args->type != ARGT_USR) |
| 8976 | return 0; |
| 8977 | |
| 8978 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8979 | |
| 8980 | if (!get_http_auth(l4)) |
| 8981 | return 0; |
| 8982 | |
| 8983 | /* acl_match_auth() will need several information at once */ |
| 8984 | smp->ctx.a[0] = args->data.usr; /* user list */ |
| 8985 | smp->ctx.a[1] = l4->txn.auth.user; /* user name */ |
| 8986 | smp->ctx.a[2] = l4->txn.auth.pass; /* password */ |
| 8987 | |
| 8988 | /* if the user does not belong to the userlist or has a wrong password, |
| 8989 | * report that it unconditionally does not match. Otherwise we return |
| 8990 | * a non-zero integer which will be ignored anyway since all the params |
| 8991 | * that acl_match_auth() will use are in test->ctx.a[0,1,2]. |
| 8992 | */ |
| 8993 | smp->type = SMP_T_BOOL; |
| 8994 | smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass); |
| 8995 | if (smp->data.uint) |
| 8996 | smp->type = SMP_T_UINT; |
| 8997 | |
| 8998 | return 1; |
| 8999 | } |
| 9000 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9001 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 9002 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 9003 | * the cookie value is returned into *value and *value_l, and the function |
| 9004 | * returns a pointer to the next pointer to search from if the value was found. |
| 9005 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 9006 | * nor value_l are touched. The input <hdr> string should first point to the |
| 9007 | * header's value, and the <hdr_end> pointer must point to the first character |
| 9008 | * not part of the value. <list> must be non-zero if value may represent a list |
| 9009 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 9010 | * list is expected. |
| 9011 | */ |
| 9012 | static char * |
| 9013 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 9014 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 9015 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9016 | { |
| 9017 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 9018 | char *next; |
| 9019 | |
| 9020 | /* we search at least a cookie name followed by an equal, and more |
| 9021 | * generally something like this : |
| 9022 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 9023 | */ |
| 9024 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 9025 | /* Iterate through all cookies on this line */ |
| 9026 | |
| 9027 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 9028 | att_beg++; |
| 9029 | |
| 9030 | /* find att_end : this is the first character after the last non |
| 9031 | * space before the equal. It may be equal to hdr_end. |
| 9032 | */ |
| 9033 | equal = att_end = att_beg; |
| 9034 | |
| 9035 | while (equal < hdr_end) { |
| 9036 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 9037 | break; |
| 9038 | if (http_is_spht[(unsigned char)*equal++]) |
| 9039 | continue; |
| 9040 | att_end = equal; |
| 9041 | } |
| 9042 | |
| 9043 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 9044 | * is between <att_beg> and <equal>, both may be identical. |
| 9045 | */ |
| 9046 | |
| 9047 | /* look for end of cookie if there is an equal sign */ |
| 9048 | if (equal < hdr_end && *equal == '=') { |
| 9049 | /* look for the beginning of the value */ |
| 9050 | val_beg = equal + 1; |
| 9051 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 9052 | val_beg++; |
| 9053 | |
| 9054 | /* find the end of the value, respecting quotes */ |
| 9055 | next = find_cookie_value_end(val_beg, hdr_end); |
| 9056 | |
| 9057 | /* make val_end point to the first white space or delimitor after the value */ |
| 9058 | val_end = next; |
| 9059 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 9060 | val_end--; |
| 9061 | } else { |
| 9062 | val_beg = val_end = next = equal; |
| 9063 | } |
| 9064 | |
| 9065 | /* We have nothing to do with attributes beginning with '$'. However, |
| 9066 | * they will automatically be removed if a header before them is removed, |
| 9067 | * since they're supposed to be linked together. |
| 9068 | */ |
| 9069 | if (*att_beg == '$') |
| 9070 | continue; |
| 9071 | |
| 9072 | /* Ignore cookies with no equal sign */ |
| 9073 | if (equal == next) |
| 9074 | continue; |
| 9075 | |
| 9076 | /* Now we have the cookie name between att_beg and att_end, and |
| 9077 | * its value between val_beg and val_end. |
| 9078 | */ |
| 9079 | |
| 9080 | if (att_end - att_beg == cookie_name_l && |
| 9081 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 9082 | /* let's return this value and indicate where to go on from */ |
| 9083 | *value = val_beg; |
| 9084 | *value_l = val_end - val_beg; |
| 9085 | return next + 1; |
| 9086 | } |
| 9087 | |
| 9088 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 9089 | if (!list) |
| 9090 | break; |
| 9091 | } |
| 9092 | |
| 9093 | return NULL; |
| 9094 | } |
| 9095 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9096 | /* 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] | 9097 | * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the |
| 9098 | * 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] | 9099 | * 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] | 9100 | * 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] | 9101 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 9102 | * 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] | 9103 | */ |
| 9104 | static int |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9105 | smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9106 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9107 | { |
| 9108 | struct http_txn *txn = l7; |
| 9109 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9110 | struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9111 | const struct http_msg *msg; |
| 9112 | const char *hdr_name; |
| 9113 | int hdr_name_len; |
| 9114 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9115 | int occ = 0; |
| 9116 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9117 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9118 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9119 | return 0; |
| 9120 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9121 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9122 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9123 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9124 | msg = &txn->req; |
| 9125 | hdr_name = "Cookie"; |
| 9126 | hdr_name_len = 6; |
| 9127 | } else { |
| 9128 | msg = &txn->rsp; |
| 9129 | hdr_name = "Set-Cookie"; |
| 9130 | hdr_name_len = 10; |
| 9131 | } |
| 9132 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9133 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 9134 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 9135 | occ = -1; |
| 9136 | |
| 9137 | /* OK so basically here, either we want only one value and it's the |
| 9138 | * last one, or we want to iterate over all of them and we fetch the |
| 9139 | * next one. |
| 9140 | */ |
| 9141 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9142 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9143 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9144 | /* search for the header from the beginning, we must first initialize |
| 9145 | * the search parameters. |
| 9146 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9147 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9148 | ctx->idx = 0; |
| 9149 | } |
| 9150 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9151 | smp->flags |= SMP_F_VOL_HDR; |
| 9152 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9153 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9154 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 9155 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9156 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 9157 | goto out; |
| 9158 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9159 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9160 | continue; |
| 9161 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9162 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 9163 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9164 | } |
| 9165 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9166 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9167 | 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] | 9168 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9169 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9170 | &smp->data.str.str, |
| 9171 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9172 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9173 | found = 1; |
| 9174 | if (occ >= 0) { |
| 9175 | /* one value was returned into smp->data.str.{str,len} */ |
| 9176 | smp->flags |= SMP_F_NOT_LAST; |
| 9177 | return 1; |
| 9178 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9179 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9180 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9181 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9182 | /* all cookie headers and values were scanned. If we're looking for the |
| 9183 | * last occurrence, we may return it now. |
| 9184 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9185 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9186 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9187 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9188 | } |
| 9189 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9190 | /* 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] | 9191 | * 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] | 9192 | * multiple cookies may be parsed on the same line. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9193 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9194 | */ |
| 9195 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9196 | 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] | 9197 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9198 | { |
| 9199 | struct http_txn *txn = l7; |
| 9200 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9201 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9202 | const struct http_msg *msg; |
| 9203 | const char *hdr_name; |
| 9204 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9205 | int cnt; |
| 9206 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9207 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9208 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9209 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9210 | return 0; |
| 9211 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9212 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9213 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9214 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9215 | msg = &txn->req; |
| 9216 | hdr_name = "Cookie"; |
| 9217 | hdr_name_len = 6; |
| 9218 | } else { |
| 9219 | msg = &txn->rsp; |
| 9220 | hdr_name = "Set-Cookie"; |
| 9221 | hdr_name_len = 10; |
| 9222 | } |
| 9223 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9224 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 9225 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9226 | ctx.idx = 0; |
| 9227 | cnt = 0; |
| 9228 | |
| 9229 | while (1) { |
| 9230 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 9231 | if (!val_beg) { |
| 9232 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 9233 | break; |
| 9234 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9235 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9236 | continue; |
| 9237 | |
| 9238 | val_beg = ctx.line + ctx.val; |
| 9239 | val_end = val_beg + ctx.vlen; |
| 9240 | } |
| 9241 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9242 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9243 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9244 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9245 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9246 | &smp->data.str.str, |
| 9247 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9248 | cnt++; |
| 9249 | } |
| 9250 | } |
| 9251 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9252 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9253 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9254 | return 1; |
| 9255 | } |
| 9256 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9257 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 9258 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 9259 | */ |
| 9260 | static int |
| 9261 | smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9262 | const struct arg *args, struct sample *smp) |
| 9263 | { |
| 9264 | int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp); |
| 9265 | |
| 9266 | if (ret > 0) { |
| 9267 | smp->type = SMP_T_UINT; |
| 9268 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 9269 | } |
| 9270 | |
| 9271 | return ret; |
| 9272 | } |
| 9273 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9274 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 9275 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 9276 | /************************************************************************/ |
| 9277 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9278 | /* |
| 9279 | * Given a path string and its length, find the position of beginning of the |
| 9280 | * query string. Returns NULL if no query string is found in the path. |
| 9281 | * |
| 9282 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 9283 | * |
| 9284 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 9285 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9286 | 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] | 9287 | { |
| 9288 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 9289 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9290 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9291 | return p ? p + 1 : NULL; |
| 9292 | } |
| 9293 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9294 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9295 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9296 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9297 | } |
| 9298 | |
| 9299 | /* |
| 9300 | * Given a url parameter, find the starting position of the first occurence, |
| 9301 | * or NULL if the parameter is not found. |
| 9302 | * |
| 9303 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 9304 | * the function will return query_string+8. |
| 9305 | */ |
| 9306 | static char* |
| 9307 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9308 | char* url_param_name, size_t url_param_name_l, |
| 9309 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9310 | { |
| 9311 | char *pos, *last; |
| 9312 | |
| 9313 | pos = query_string; |
| 9314 | last = query_string + query_string_l - url_param_name_l - 1; |
| 9315 | |
| 9316 | while (pos <= last) { |
| 9317 | if (pos[url_param_name_l] == '=') { |
| 9318 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 9319 | return pos; |
| 9320 | pos += url_param_name_l + 1; |
| 9321 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9322 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9323 | pos++; |
| 9324 | pos++; |
| 9325 | } |
| 9326 | return NULL; |
| 9327 | } |
| 9328 | |
| 9329 | /* |
| 9330 | * Given a url parameter name, returns its value and size into *value and |
| 9331 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 9332 | * zero is returned and value/value_l are not touched. |
| 9333 | */ |
| 9334 | static int |
| 9335 | find_url_param_value(char* path, size_t path_l, |
| 9336 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9337 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9338 | { |
| 9339 | char *query_string, *qs_end; |
| 9340 | char *arg_start; |
| 9341 | char *value_start, *value_end; |
| 9342 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9343 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9344 | if (!query_string) |
| 9345 | return 0; |
| 9346 | |
| 9347 | qs_end = path + path_l; |
| 9348 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9349 | url_param_name, url_param_name_l, |
| 9350 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9351 | if (!arg_start) |
| 9352 | return 0; |
| 9353 | |
| 9354 | value_start = arg_start + url_param_name_l + 1; |
| 9355 | value_end = value_start; |
| 9356 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9357 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9358 | value_end++; |
| 9359 | |
| 9360 | *value = value_start; |
| 9361 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 9362 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9363 | } |
| 9364 | |
| 9365 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9366 | smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9367 | const struct arg *args, struct sample *smp) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9368 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9369 | char delim = '?'; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9370 | struct http_txn *txn = l7; |
| 9371 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9372 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9373 | if (!args || args[0].type != ARGT_STR || |
| 9374 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9375 | return 0; |
| 9376 | |
| 9377 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9378 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9379 | if (args[1].type) |
| 9380 | delim = *args[1].data.str.str; |
| 9381 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9382 | 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] | 9383 | args->data.str.str, args->data.str.len, |
| 9384 | &smp->data.str.str, &smp->data.str.len, |
| 9385 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9386 | return 0; |
| 9387 | |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 9388 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9389 | smp->flags = SMP_F_VOL_1ST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9390 | return 1; |
| 9391 | } |
| 9392 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 9393 | /* Return the signed integer value for the specified url parameter (see url_param |
| 9394 | * above). |
| 9395 | */ |
| 9396 | static int |
| 9397 | smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9398 | const struct arg *args, struct sample *smp) |
| 9399 | { |
| 9400 | int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp); |
| 9401 | |
| 9402 | if (ret > 0) { |
| 9403 | smp->type = SMP_T_UINT; |
| 9404 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 9405 | } |
| 9406 | |
| 9407 | return ret; |
| 9408 | } |
| 9409 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9410 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 9411 | * keyword. These keywords support an optional positive or negative occurrence |
| 9412 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 9413 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 9414 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 9415 | * error message in case of error, that the caller is responsible for freeing. |
| 9416 | * The initial location must either be freeable or NULL. |
| 9417 | */ |
| 9418 | static int val_hdr(struct arg *arg, char **err_msg) |
| 9419 | { |
| 9420 | 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] | 9421 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9422 | return 0; |
| 9423 | } |
| 9424 | return 1; |
| 9425 | } |
| 9426 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 9427 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9428 | /* All supported ACL keywords must be declared here. */ |
| 9429 | /************************************************************************/ |
| 9430 | |
| 9431 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9432 | * Please take care of keeping this list alphabetically sorted. |
| 9433 | */ |
| 9434 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9435 | { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 9436 | { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9437 | { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9438 | { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9439 | { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9440 | { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9441 | { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9442 | { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9443 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9444 | { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) }, |
| 9445 | { "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] | 9446 | { "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] | 9447 | { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9448 | { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9449 | { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9450 | { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9451 | { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9452 | { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9453 | { "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] | 9454 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9455 | { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr }, |
| 9456 | { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9457 | { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9458 | { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9459 | { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9460 | { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9461 | { "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 }, |
| 9462 | { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9463 | { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9464 | { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9465 | { "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] | 9466 | |
| 9467 | { "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] | 9468 | { "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] | 9469 | { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 }, |
| 9470 | |
| 9471 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 }, |
| 9472 | |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9473 | { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 9474 | { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9475 | { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9476 | { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9477 | { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9478 | { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9479 | { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9480 | { "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] | 9481 | |
| 9482 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 }, |
| 9483 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 9484 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 9485 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9486 | { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) }, |
| 9487 | { "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] | 9488 | { "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] | 9489 | { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9490 | { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9491 | { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9492 | { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9493 | { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9494 | { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9495 | { "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] | 9496 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9497 | { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr }, |
| 9498 | { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9499 | { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9500 | { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9501 | { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9502 | { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9503 | { "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 }, |
| 9504 | { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9505 | { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9506 | { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9507 | { "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] | 9508 | |
| 9509 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 }, |
| 9510 | |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9511 | { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 9512 | { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9513 | { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9514 | { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9515 | { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9516 | { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 9517 | { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9518 | { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9519 | { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9520 | { "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] | 9521 | |
| 9522 | { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) }, |
| 9523 | { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 9524 | { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 9525 | { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 9526 | { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 9527 | { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) }, |
| 9528 | { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 9529 | { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 9530 | { "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] | 9531 | { "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] | 9532 | |
| 9533 | { NULL, NULL, NULL, NULL }, |
| 9534 | }}; |
| 9535 | |
| 9536 | /************************************************************************/ |
| 9537 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 9538 | /************************************************************************/ |
| 9539 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 9540 | static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{ |
Willy Tarreau | 1b6c00c | 2012-10-05 22:41:26 +0200 | [diff] [blame] | 9541 | { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ }, |
| 9542 | { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ }, |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 9543 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ }, |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9544 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_CAP_L7|SMP_CAP_REQ }, |
Willy Tarreau | 1b6c00c | 2012-10-05 22:41:26 +0200 | [diff] [blame] | 9545 | { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ }, |
| 9546 | { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ }, |
| 9547 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_CAP_L7|SMP_CAP_REQ }, |
| 9548 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ }, |
| 9549 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ }, |
| 9550 | { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ|SMP_CAP_RES }, |
| 9551 | { "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] | 9552 | { NULL, NULL, 0, 0, 0 }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 9553 | }}; |
| 9554 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9555 | |
| 9556 | __attribute__((constructor)) |
| 9557 | static void __http_protocol_init(void) |
| 9558 | { |
| 9559 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 9560 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9561 | } |
| 9562 | |
| 9563 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 9564 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 9565 | * Local variables: |
| 9566 | * c-indent-level: 8 |
| 9567 | * c-basic-offset: 8 |
| 9568 | * End: |
| 9569 | */ |